Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Techatronic
Published

WiFi Controlled Robot | NodeMCU esp8266

This is a WiFi (LAN) controlled robot that can move in different directions.

IntermediateFull instructions provided2 hours1,778
WiFi Controlled Robot | NodeMCU esp8266

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
 //TECHATRONIC.COM  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #include <ESP8266WiFi.h>  
 String i;  
 WiFiServer server(80);  
 void setup()  
 {  
  i = "";  
  Serial.begin(9600);  
  pinMode(16, OUTPUT); // GPIO pin16=D0 of NodeMCU 12E or in place of '16' you can write'D0' directly || -->Connected to motordriver   
  pinMode(5, OUTPUT); // GPIO pin5= D1 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
  pinMode(4, OUTPUT); // GPIO pin4= D2 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
  pinMode(0, OUTPUT); // GPIO pin0= D3 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
   WiFi.disconnect();  
  delay(2000);  
  Serial.println("Connecting to WIFI");  
   WiFi.begin("DESKTOP","asdfghjkl"); //Enter WIFI Name & Password  
  while ((!(WiFi.status() == WL_CONNECTED))){  
   delay(300);  
   Serial.print("..");  
  }Serial.println("I am Connected");  
  Serial.println("My Local IP is : ");  
  Serial.print((WiFi.localIP()));  
  server.begin();  
 }  
 void loop()  
 {  
   WiFiClient client = server.available();  
   if (!client) { return; }  
   while(!client.available()){ delay(1); }  
   i = (client.readStringUntil('\r'));  
   i.remove(0, 5);  
   i.remove(i.length()-9,9);  
   if (i == "forward") {  
    digitalWrite(16,HIGH);  
    digitalWrite(5,LOW);  
    digitalWrite(4,HIGH);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("forward");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "reverse") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,HIGH);  
    digitalWrite(4,LOW);  
    digitalWrite(0,HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("reverse");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "right") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,LOW);  
    digitalWrite(4,HIGH);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("right");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "left") {  
    digitalWrite(16,HIGH);  
    digitalWrite(5,LOW);  
    digitalWrite(4,LOW);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("left");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "stop") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,LOW);  
    digitalWrite(4,LOW);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("stop");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
 }  

Code snippet #2

Plain text
 //TECHATRONIC.COM  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #include <ESP8266WiFi.h>  
 String i;  
 WiFiServer server(80);  
 void setup()  
 {  
  i = "";  
  Serial.begin(9600);  
  pinMode(16, OUTPUT); // GPIO pin16=D0 of NodeMCU 12E or in place of '16' you can write'D0' directly || -->Connected to motordriver   
  pinMode(5, OUTPUT); // GPIO pin5= D1 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
  pinMode(4, OUTPUT); // GPIO pin4= D2 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
  pinMode(0, OUTPUT); // GPIO pin0= D3 of NodeMCU 12E or in place of '16' you can write 'D0' directly || -->Connected to motordriver  
   WiFi.disconnect();  
  delay(2000);  
  Serial.println("Connecting to WIFI");  
   WiFi.begin("DESKTOP","asdfghjkl"); //Enter WIFI Name & Password  
  while ((!(WiFi.status() == WL_CONNECTED))){  
   delay(300);  
   Serial.print("..");  
  }Serial.println("I am Connected");  
  Serial.println("My Local IP is : ");  
  Serial.print((WiFi.localIP()));  
  server.begin();  
 }  
 void loop()  
 {  
   WiFiClient client = server.available();  
   if (!client) { return; }  
   while(!client.available()){ delay(1); }  
   i = (client.readStringUntil('\r'));  
   i.remove(0, 5);  
   i.remove(i.length()-9,9);  
   if (i == "forward") {  
    digitalWrite(16,HIGH);  
    digitalWrite(5,LOW);  
    digitalWrite(4,HIGH);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("forward");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "reverse") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,HIGH);  
    digitalWrite(4,LOW);  
    digitalWrite(0,HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("reverse");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "right") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,LOW);  
    digitalWrite(4,HIGH);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("right");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "left") {  
    digitalWrite(16,HIGH);  
    digitalWrite(5,LOW);  
    digitalWrite(4,LOW);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("left");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
   if (i == "stop") {  
    digitalWrite(16,LOW);  
    digitalWrite(5,LOW);  
    digitalWrite(4,LOW);  
    digitalWrite(0,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("stop");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
   }  
 }  

Github

https://github.com/ekstrand/ESP8266wifi

Credits

Techatronic

Techatronic

72 projects • 127 followers
Electronic engineer

Comments