luciorocha
Published © GPL3+

ESP8266 + L298N Motor Drive + Smartphone

Control an Arduino robot with a smartphone using ESP8266 (Last update: 08/06/2022).

BeginnerProtip42,325
ESP8266 + L298N Motor Drive + Smartphone

Things used in this project

Story

Read more

Schematics

ESP8266 wiring to firmware update with FLASH Download Tools

ESP8266 witing to deploy HTTP Web Server with Arduino Board

Arduino + L298N motor drive wiring

Code

Code1: esp8266_L298N.ino

Arduino
Create a WiFi access point and provide a web server on it to move robot.
/* Create a WiFi access point and provide a web server on it to move robot. 
 *  
 *  Important: Use without ATMega328 chip, or wire RST in GND
 *  
 *  Author: Lucio A. Rocha
 *  Last Update: 25 Jan 2017
 *
 */

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

/* Set these to your desired credentials. */
const char *ssid = "utfprbot";
const char *password = "654321";

ESP8266WebServer server(80);

const int led = 2;  //GPIO2

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {

  int size=1000;
  char temp[size];
  
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\    
    <title>UTFPRBOT-1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>You are connected on UTFPRBOT #1!</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\    
    <p>Status: Light ON</h1></p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );
  server.send ( 200, "text/html", temp );  
  
}

void setup() {
  delay(1000);
  Serial.begin(9600);
  Serial.println();
  Serial.print("Configuring access point...");

  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  pinMode(led, OUTPUT);
  digitalWrite ( led, HIGH );

  //URLs available to query
  server.on("/", handleRoot);  
  server.on ( "/up", moveUp );        //1
  server.on ( "/lu", moveLeftUp );    //2
  server.on ( "/ru", moveRightUp );   //3
  server.on ( "/ld", moveLeftDown );  //4
  server.on ( "/rd", moveRightDown ); //5
  server.on ( "/do", moveDown );      //6
  server.on ( "/st", moveStop );      //7
  server.on ( "/on", turnOn );        //8
  server.on ( "/of", turnOff );       //9  
  
  server.begin();
  Serial.println("HTTP server started");
  
}

void moveUp(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move UP</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM1");
  
}

void moveLeftUp(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move LEFT-UP</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM2");
  
}

void moveLeftDown(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move LEFT-DOWN</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM4");
  
}

void moveRightUp(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move RIGHT-UP</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM3");
  
}

void moveRightDown(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move RIGHT-DOWN</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM5");
  
}

void moveDown(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Move DOWN</h3>\    
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM6");
  
}


void moveStop(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: STOP</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM7");
  
}

void turnOn(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\    
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Light ON</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p>Status: Light ON</p>\        
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM8");
}

void turnOff(){

  digitalWrite ( led, LOW );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"<html>\
  <head>\    
    <title>UTFPRBOT - 1.0</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h3>Request: Light OFF</h3>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p>Status: Light OFF</p>\        
    <p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/of\"><h1>Turn OFF</h1></a></p>\           
    <p></p>\
    <p><a href=\"http://192.168.4.1/st\"><h1 align=\"center\">STOP</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/up\"><h1 align=\"center\">/\\/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/lu\"><h1 align=\"center\">/\\_____</h1></a><a href=\"http://192.168.4.1/ru\"><h1 align=\"center\">_____/\\</h1></a></p>\
    <p></p>\
    <p><a href=\"http://192.168.4.1/ld\"><h1 align=\"center\">\\/_____</h1></a><a href=\"http://192.168.4.1/rd\"><h1 align=\"center\">_____\\/</h1></a></p>\
    <p><a href=\"http://192.168.4.1/do\"><h1 align=\"center\">\\/\\/</h1></a></p>\
    <p></p>\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);
  Serial.println("SETM9");
}

void loop() {
  //IPAddress myIP = WiFi.softAPIP();
  //Serial.print("AP IP address: ");
  //Serial.println(myIP);
  server.handleClient();
}

Code2: SoftwareSerial

Arduino
Software to parse serial data from ESP8266 and
move motors controlled by L298N motor drive.
/*
SoftwareSerialParser
(based on SoftwareSerialExample.ino)

Description: Software to parse serial data from ESP8266 and
move motors controlled by L298N motor drive.

Author: Lucio A. Rocha
Last update: 12/03/2017

 */
#include <SoftwareSerial.h>

int IN3 = 8; //IN3 motor = pino no arduino
int IN4 = 7;
int ENB = 6; // ENB eh o pino de controle de velocidade no motor

int IN1 = 5; //IN1 motor = pino no arduino
int IN2 = 4;
int ENA = 3; // ENA eh o pino de controle de velocidade no motor

int TIME_MOVE=2000; //Tempo de cada movimento

SoftwareSerial mySerial(0, 1); // RX, TX

void setup() {

  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("SoftwareSerialParser Started!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}


void moveMotor(char c){
   
   Serial.println(c);

   switch (c){
    //Up
    case '1': 
      //Prepara a saida do motor
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      //Prepara a saida do motor
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      //Move
      analogWrite(ENA, 255);
      analogWrite(ENB, 255);
      delay(TIME_MOVE);  
      //Desliga o motor e espera
      analogWrite(ENA, 0);
      analogWrite(ENB, 0);      
      break;
    //LeftUp
    case '2':       
      //Prepara a saida do motor
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      //Move      
      analogWrite(ENB, 255);
      delay(TIME_MOVE);  
      //Desliga o motor e espera      
      analogWrite(ENB, 0);      
      break;      
    //RightUp
    case '3':       
      //Prepara a saida do motor
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);      
      //Move
      analogWrite(ENA, 255);      
      delay(TIME_MOVE);  
      //Desliga o motor e espera
      analogWrite(ENA, 0);
      break;
    //LeftDown
    case '4':       
      //Prepara a saida do motor
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      //Move      
      analogWrite(ENB, 255);
      delay(TIME_MOVE);  
      //Desliga o motor e espera      
      analogWrite(ENB, 0);      
      break;        
      //RightDown
    case '5':       
      //Prepara a saida do motor
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);      
      //Move
      analogWrite(ENA, 255);      
      delay(TIME_MOVE);  
      //Desliga o motor e espera
      analogWrite(ENA, 0);
      break;
      //Down
    case '6':                
//Prepara a saida do motor
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      //Prepara a saida do motor
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      //Move
      analogWrite(ENA, 255);
      analogWrite(ENB, 255);
      delay(TIME_MOVE);  
      //Desliga o motor e espera
      analogWrite(ENA, 0);
      analogWrite(ENB, 0);      
      break;
  //Stop
    case '7':                
      //Desliga o motor e espera
      analogWrite(ENA, 0);
      analogWrite(ENB, 0);      
      break;      
    default:
      break;
   }//end switch
 
}


void parseMove(String response){
      Serial.print("[");
      String movement="";
      int i=0;
      char c = response[i];
      while (i<4){ //Take first 5 chars from readings of ESP8266 line
        c = response[i];
        movement+=c;
        Serial.print(c);
        i=i+1;
     }
    //Takes on more char to identify the number of movement
    c = response[i];
    if (movement=="SETM"){     
      movement+=c;
      Serial.println("\n===========================================");      
      Serial.println(movement);
      Serial.println(c);

      //c is the number of movement
      moveMotor(c);

      Serial.println("-------------------------------------------");  
    }
     Serial.print("]");
}  

void loop() { // run over and over
  String response = "";
  bool achou=false;  
  while (mySerial.available()) {
    response = mySerial.readStringUntil('\n');
    Serial.println(response);

    parseMove(response);
   
  }
}

Credits

luciorocha

luciorocha

5 projects • 30 followers

Comments