/*|-------------------------------------------------|*/
/*| RC CAR VOICE CONTROL VIA WEB INTERFACE          |*/
/*| Webkit (GOOGLE) technology for voice-to-text    |*/
/*| Author: Martin Chlebovec (martinius96)          |*/
/*| LANGUAGE English - region US --> en-US          |*/
/*|-------------------------------------------------|*/
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xAA, 0xBB, 0xCC, 0x81, 0x7B, 0x4A }; //fyzicka adresa MAC
char serverName[] = "www.arduino.php5.sk"; // webserver
IPAddress ip(192, 168, 0, 88);

EthernetClient client;
void setup() {
  if (Ethernet.begin(mac) == 0) {
    Serial.println("DHCP nepridelilo adresu, skusam so statickou...");
    Ethernet.begin(mac, ip);
  }
  Serial.begin(115200);
}
void executed_command() {
  client.stop();
  if (client.connect(serverName, 80)) {
    client.println("GET /php_car/command_executed.php HTTP/1.1");
    client.println("Host: www.arduino.php5.sk");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("Connection failed");
  }
  client.stop();
}
void loop() {
  if (client.connect(serverName, 80)) {
    client.println("GET /php_car/translation.txt HTTP/1.1");
    client.println("Host: www.arduino.php5.sk");
    client.println("Connection: close");
    client.println();
    while (client.connected()) {
      String hlavicka = client.readStringUntil('\n');
      Serial.println(hlavicka);
      if (hlavicka == "\r") {
        break;
      }
    }
    String line = client.readStringUntil('\n');
    Serial.println("Command is:");
    Serial.println(line);
    if (line == "UP") { //if command == go UP


      Serial.println("Moving forward...");
      // PUT CODE FOR MOVE FORWARD THERE
      executed_command();

    } else if (line == "DOWN") { //if command == go DOWN


      Serial.println("Moving backward...");
      // PUT CODE FOR MOVE BACKWARD THERE
      executed_command();

    } else if (line == "LEFT") { //if command == TURN LEFT


      Serial.println("Turning left...");
      // PUT CODE FOR TURN LEFT THERE
      executed_command();

    } else if (line == "RIGHT") { //if command == TURN RIGHT


      Serial.println("Turning right...");
      // PUT CODE FOR TURN RIGHT THERE
      executed_command();

    } else if (line == "Last command executed by ESP8266. Waiting for new command.") { //if command == NO_MORE_COMMAND


      Serial.println("No voice command set yet");


    } else {

      Serial.println("This command is not suported");
      executed_command();
    }
  }
  else {
    Serial.println("Connection failed");
    Serial.println();
  }
  client.stop();
  delay(5000);