Arduino – WiFi communication (ESP8266)

1. Hardware

Wire as shown below :
ESP8266_bb

ESP8266_schema

2. Software

2.1 ESP8266 library installation

An open-source library is available on github : https://github.com/itead/ITEADLIB_Arduino_WeeESP8266

1 – Download ZIP :
github-download-zip

2 – Import the new library :
arduino-import-library

2.2 Library parameter

As we use the ESP8266 module with a SoftwareSerial connection, we need to edit the ESP8266.H file.

This file is available in :
C:\Users\MyUser\Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master :

Uncomment the following line as below :

2.3 Program

2.4 Result

ESP8266-request-result

5 thoughts on “Arduino – WiFi communication (ESP8266)”

  1. hello i have esp8266 module and arduino ethernet and i need to connect from my android mobile direct via wifi to esp8266 and after connection send values from my mobile to open one led from my mobile…i have esp8266 AP mode my code working but after press sometimes one button from my android app the led is light on and light of but sometimes stuck…. can help me? my code is:
    #include

    #define DEBUG true

    SoftwareSerial esp8266(11,10); // make RX Arduino line is pin 11, make TX Arduino line is pin 10.
    // This means that you need to connect the TX line from the esp to the Arduino’s pin 11
    // and the RX line from the esp to the Arduino’s pin 10
    void setup()
    {
    Serial.begin(9600);
    esp8266.begin(115200); // your esp’s baud rate might be different

    pinMode(8,OUTPUT);
    digitalWrite(8,LOW);

    pinMode(6,OUTPUT);
    digitalWrite(6,LOW);

    pinMode(9,OUTPUT);
    digitalWrite(9,LOW);

    Serial.println(sendData(“AT+RST\r\n”,2000,DEBUG)); // reset module
    Serial.println(sendData(“AT+CWMODE=2\r\n”,1000,DEBUG)); // configure as access point AP=2, STA=1, 3=BOTH
    Serial.println(sendData(“AT+CIFSR\r\n”,1000,DEBUG)); // get ip address
    Serial.println(sendData(“AT+CIPMUX=1\r\n”,1000,DEBUG)); // configure for multiple connections
    Serial.println(sendData(“AT+CIPSERVER=1,80\r\n”,1000,DEBUG)); // turn on server on port 80
    }

    void loop()
    {
    /* String t;
    while (esp8266.available())
    {
    t+=(char)esp8266.read();//read byte,convert to char and append it to string
    }//end while

    Serial.println(t);
    delay(2000);
    if (t.length())
    { // if string is not empty
    if (t==”/?on”)
    {
    digitalWrite(9, HIGH);
    Serial.write(“LED is ON \n”);
    }//end if
    else if (t==”+IPD,0,457=GET /?off HTTP/1.1″)
    {
    digitalWrite(9, LOW);
    Serial.write(“LED is OFF \n”);
    }

    }//end if
    */

    if(esp8266.available()) // check if the esp is sending a message
    {

    if(esp8266.find(“+IPD,”))//+IPD,
    {
    //delay(1000); // wait for the serial buffer to fill up (read all the serial data)
    // get the connection id so that we can then disconnect
    int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
    // the ASCII decimal value and 0 (the first decimal number) starts at 48
    //**************************************************
    String t;

    esp8266.find(“/?”); // advance cursor to “pin=”
    while (esp8266.available())
    {
    t+=(char)esp8266.read();//read byte,convert to char and append it to string
    if (t.length()==2){break;}
    }
    // t=(char)esp8266.read();
    // t+=(char)esp8266.read();
    Serial.println(t);

    if(t==”on”)
    {
    digitalWrite(8, HIGH);
    Serial.write(“LED is ON \n”);

    }
    else if(t==”of”)
    {
    digitalWrite(8, LOW);
    Serial.write(“LED is OFF \n”);

    }

    if(t==”6o”)
    {
    digitalWrite(6, HIGH);
    Serial.write(“LED is ON \n”);

    }
    else if(t==”6f”)
    {
    digitalWrite(6, LOW);
    Serial.write(“LED is OFF \n”);

    }
    //***************************************************
    /*
    esp8266.find(“pin=”); // advance cursor to “pin=”

    int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
    pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number

    digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin
    */
    // make close command
    String closeCommand = “AT+CIPCLOSE=”;
    closeCommand+=connectionId; // append connection id
    closeCommand+=”\r\n”;

    sendData(closeCommand,60,DEBUG); // close connection.

    }
    }

    }//loop

    /*
    * Name: sendData
    * Description: Function used to send data to ESP8266.
    * Params: command – the data/command to send; timeout – the time to wait for a response; debug – print to Serial window?(true = yes, false = no)
    * Returns: The response from the esp8266 (if there is a reponse)
    */
    String sendData(String command, const int timeout, boolean debug)
    {
    String response = “”;

    esp8266.print(command); // send the read character to the esp8266

    long int time = millis();

    while( (time+timeout) > millis())
    {
    while(esp8266.available())
    {

    // The esp has data so display its output to the serial window
    char c = esp8266.read(); // read the next character.
    response+=c;

    }

    }

    if(debug)
    {
    Serial.print(response);
    }

    return response;
    }

  2. I followed your tutorial to the letter, but it doesn’t work for me. My monitor keeps repeating itself:

    16:07:30.087 -> Serial connection setup [DONE]
    16:07:30.087 -> WiFi setup [START]
    16:07:40.106 -> WiFi setup [AP ERR]
    16:08:00.128 -> WiFi setup [START]
    16:08:10.163 -> WiFi setup [AP ERR]
    16:08:30.144 -> WiFi setup [START]
    16:08:40.174 -> WiFi setup [AP ERR]

    The SSID and the password are correct (I double-double checked). What could be wrong? Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *