Amazon Store

Monday, 23 March 2015

Robo Car Control over WiFi

This tutorial will teach you how to control a Robo car using any WiFi enabled device.

Components Required:

1.     ESP 8266 WiFi module

2.     Arduino UNO (or its equivalent)

3.     Motor driver L293D

4.     Chassis

5.     12V DC Geared motors X 2

6.     Wheels X 2

7.     Castor Wheel

8.     Wires

9.     Breadboard

10.   Small PCB

11.   Male headers

12.   Female headers

About ESP8266

ESP 8266 is the latest buzz on internet. It is a WiFi module which is just a bit larger than a 1 Re coin. Well that's not what it is famous for though. What made it very popular among electronic hobbyist is its unbelievable low cost. I got it just for $5 (~300 Rs.) An arduino along with a WiFi shield would cost 10 times of what this module costs.

It can be interfaced with any microcontroller through USART communication, meaning it just need the RX and TX pins. Another feather to its hat was added recently. The developer released the SDK files of the module and hence even the module can be re-programmed and its GPIO pins can be used independently without any additional microcontroller. But that is beyond the scope of this tutorial. We would be using the module along with Arduino UNO.

Making a Breadboard adapter for ESP8266 module:

The problem with the module's physical feature is that you cannot connect it directly to a breadboard. So I've decided to make a adapter which will be used to connect the module onto a breadboard.

These are the parts you would need to make the adapter:

Start by soldering the female header onto the PCB. Solder the corner pins first so that the header grips onto the PCB properly. Then solder the remaining 4 pins.
 

 

Next, place the male headers on the opposite side of the PCB and carefully solder it onto the PCB.

After soldering, you can remove the black plastic part attached to the male header and start soldering each male pin to its corresponding female pin. You would have something like this in the end:

Once you have this, you can snap it onto a Breadboard happily and attach the ESP 8266 module to it easily.

Here is a detailed video tutorial showing the process: http://youtu.be/GhwmgvcjRaA (Optional)

circuit Diagram:


AT Commands for ESP 8266:

Command

Description

AT+GMR

To know the version of firmware

AT+RST

To Reset module

AT+CWMODE

To set the mode of operation i.e., as Access Point or Station mode or both.

AT+CWLAP

To see the list of APs available

AT+CIPSERVER

To set as server

AT+CIPMUX

To choose between single or multiple connections

AT+CIPSEND

To set the channel number, data length and send data over WiFi

AT+CIPCLOSE

To close the connection

Server on ESP 8266:

We will be sending a web interface (an HTML page) to any device (say a WiFi enabled mobile phone) connected to the WiFi module. The code for the HTML page would be provided by the Arduino. And also, the Arduino would decode the request from the device and control the motors accordingly.

Since the WiFi module can operate as an access point also, we don't need an extra wireless router for the project.

If you have no idea about HTML pages then head to my "Controlling Appliances over WiFi and Ethernet" project and get to know the basics.

Here is the algorithm to be followed for setting up a server on ESP 8266:

Algorithm:

-         Test module using "AT" command

-         Set mode of operation using "AT+CWMODE=<mode number>" command

-         Set number of connections using command "AT+CIPMUX=<number>"

-         Set it as server using the command "AT+CIPSERVER=<mode number>,<port>"

-         If GET request received from a browser, then use the command "AT+CIPSEND=<channel number>, <data length>" followed by the html data you would like to send.

-         After completion close the connection using "AT+CIPCLOSE" command.


Coding:

#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(){
 
 
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
delay(2000);
esp.begin(9600);
esp.println("AT");
delay(1000);
esp.println("AT+CWMODE=3");
delay(1000);
esp.println("AT+CIPMUX=1");
delay(1000);
esp.println("AT+CIPSERVER=1,80");
delay(1000);
esp.println("AT+CIFSR");
delay(1000);
}
 
bool read_till_eol() {
  static int i=0;
  if(esp.available()) {
    char c=esp.read();
    buffer[i++]=c;
    if(i==BUFFER_SIZE)  i=0;
    if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {
      buffer[i]=0;
      i=0;
      return true;
    }
  }
  return false;
}
 
void loop()
{
  int ch_id, packet_len;
  char *pb;  
  if(read_till_eol()) {
    if(strncmp(buffer, "+IPD,", 5)==0) {
      // request format: +IPD,ch,len:data
      sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);
      if (packet_len > 0) {
        pb = buffer+5;
        while(*pb!=':') pb++;
        pb++;
        if (strncmp(pb, "GET / ", 6) == 0) {
         serve_homepage(ch_id,0);
          }
          else if (strncmp(pb, "GET /?fwd", 9) == 0){
               forward();
               serve_homepage(ch_id,1);
 
           }
          else if (strncmp(pb, "GET /?rev", 9) == 0){
               reverse();
               serve_homepage(ch_id,2);
 
           }
          else if (strncmp(pb, "GET /?rgt", 9) == 0){
               right();
               serve_homepage(ch_id,3);
 
           }
          else if (strncmp(pb, "GET /?lft", 9) == 0){
               left();
               serve_homepage(ch_id,4);
 
           }
          else if (strncmp(pb, "GET /?stp", 9) == 0){
               stop_bot();
               serve_homepage(ch_id,0);
 
           }
    }
  }
}
}
 
void serve_homepage(int ch_id, byte state)
{
          
          delay(1000);
          esp.print(F("AT+CIPSEND="));
          esp.print(ch_id);
          esp.print(",");
          
          esp.println("512");
          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 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 GANESH SELVARAJ for EngineersGarage.com</p><br /></center></font></BODY></HTML>");
          delay(5000);
 
          esp.print(F("AT+CIPCLOSE="));
          esp.println(ch_id);
}
 
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,HIGH);
}
void right()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}
void left()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}
void stop_bot()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}

No comments:

Post a Comment