WiFi controlled BOT using local website

In this project I have made a WiFi controlled Bot using arduino uno and ESP8266 WiFi module. The two wheeled bot is controlled from a local website that can be opened in the browser using a predefined specific IP address of the ESP8266.


The local webpage code is stored in the arduino uno and it will be send to the browser when requested. Once the page is loaded in the browser then commands are sent back to the arduino using the Wifi module. The default IP address is 192.168.4.1

I have developed this project without  use of any library.

Components needed:

Arduino Uno -------- 1
Dc geared motor ----2
Wheel----------------- 2
Chasis ----------------1
ESP8266 -------------1
L293d------------------1
9v battery ------------1

Working Video:



Arduino Code:

/*
 * This code is written by TRISHANK SINGH MAHUR.
 * This is the code for esp8266 wifi module. In this the esp8266 is used as the local server.
 * The website is locally hosted in and passed to the arduino code.
 * Link for working project using this code: https://youtu.be/t8-VGcqGIQU
 * My youtube channel link: https://www.youtube.com/channel/UCtukbWxNCNBm8KhpFCcnx_Q
 */
#define esp Serial
#define in1 8
#define in2 9
#define in3 10
#define in4 11
#define BUFFER_SIZE 1000
char buffer[BUFFER_SIZE];

void send_at(String x)
{
  esp.println(x);
}

void setup() {
  // for motor driver
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in4,OUTPUT);
  delay(2000);

  //for initalizing connction to esp

  esp.begin(115200); //default baud rate for esp
  esp.println("AT"); //to check serial communication with esp
  delay(1000);
  esp.println("AT+CWMODE=3"); //set the esp mode as station and access point
  delay(1000); 
  esp.println("AT+CIPMUX=1"); //any device can connect to it simultaneously
  delay(1000);
  esp.println("AT+CIPSERVER=1,80"); //setting esp as a server and using port 80
  delay(1000);
  esp.println("AT+CIFSR"); //to print the ip address and mac address of the esp
  delay(1000);
   
}

void read_till_eol()
{
  static int i=0;
 if(esp.available()) //if data in the buffer available then perfom he next tasks
 {
  char c=esp.read(); // data is stored in the variable c
  buffer[i++]=c;
  if(i==BUFFER_SIZE)
   i=0;
   if(i>1 && buffer[1-2]==13 && buffer[i-1]==10)
   {
    buffer[i]=0;
    i=0;
    return true;
   }
 }
 return false;
}

void serve_homepage(int ch_id, byte state) // function to display homepageand close the tcp connection
{
  delay(1000);
  esp.print(F("AT+CIPSEND=")); //send data over tcp connection
  esp.print(ch_id);
  esp.print(",");

  esp.println("493"); // specify the no of character in the homepage
  delay(500);
  esp.print("HTTP/1.1 200 OK\r\nContent-Type:text/html\r\n\r\n<HTML><HEAD><TITLE>ROBOT CONTROLLER</TITLE></HEAD><BODY><font size=\"3\"><center>WELCOME TO THE WEB INTERFACE OF THE ROBOT CONTROLLER OVER WIFI<hr/><br/><br/>");
  esp.print("<font size=\"20\"><p><a href=\"/?fwd\"\">FORWARD</a></p><a href=\"/?lft\"\">LEFT</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/?stp\"\">STOP</a>&nbsp;&nbsp;&nbsp;&nbsp;");
  esp.print("<a href=\"/?rgt\"\">RIGHT</a><p><a href=\"/?rev\"\">REVERSE</a></p></font><br/><p>Created by trishank singh mahur</p><br/></center></BODY></HTML>");
  delay(5000);

  esp.print(F("AT+CIPCLOSE=")); // CLOSE TCP CONNECTION
  esp.println(ch_id);
}

//input for motors
void forward()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}

void reverse()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}
void right()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}
void left()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,HIGH);
}
void stop_bot()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}


void loop() {
  int ch_id,packet_len;
  char *pb;
  // compare the data that has been sent

  if(read_till_eol) // if function returns true
  {
    if(strncmp(buffer,"+IPD,",5)==0)
    {
      //request format :+IPD,ch,ln:data

      sscanf(buffer+5,"%d,%d",&ch_id,&packet_len); // stores the value of channel id and packet length
      if(packet_len>0)
      {
        pb=buffer+5;
        while(*pb!=':')pb++;
        pb++;
        if(strncmp(pb,"GET/",6)==0)// compare next 6 char after :
        serve_homepage(ch_id,0);
      }
      else if(strncmp(pb,"GET/?fwd",9)==0) // check for forward command
      {
        forward();          //forard movement  function
        serve_homepage(ch_id,1); //call the server hompage functionto show the homepage again and close the current channel
      }
      else if(strncmp(pb,"GET/?rev",9)==0) // check for reverse command
      {
        reverse();
        serve_homepage(ch_id,2);
      }
      else if(strncmp(pb,"GET/?rgt",9)==0) // check for right command
      {
        right();
        serve_homepage(ch_id,3);
      }
      else if(strncmp(pb,"GET/?lft",9)==0) // check for left command
      {
        left();
        serve_homepage(ch_id,4); 
      }
      else if(strncmp(pb,"GET/?stp",9)==0) // check for stop command
      {
        stop_bot();
        serve_homepage(ch_id,0);
      }
    }
  }
}

Comments

Popular posts from this blog

How to disable VLC Media Player Flashing Pop-Up track notification on Taskbar

Color Picker in Sublime Text 3

LiveReload webpages during editing in Sublime Text 3