top of page

ESP Part 3 : Processing Response from ESP8266

In this tutorial, we will be looking into how to process response from ESP8266 and turn it into something more meaningful.


Setup the connection as shown below :

  • Wifi VCC connect Arduino 3.3V

  • Wifi GND connect Arduino GND

  • Wifi TX connect Arduino TX Pin 2

  • Wifi RX connect Arduino RX Pin 3

Once done setting up the circuit, upload the below code into Arduino.

/**************************************************************************
   Instructions :
   The below connection setup allows Arduino Serial Monitor to communicate
   directly to Wifi ESP8266
   Wifi VCC connect Arduino 3.3V
   Wifi GND connect Arduino GND
   Wifi RX connect Arduino RX Pin 3
   Wifi TX connect Arduino TX Pin 2

   AT+RST  --> restart module
****************************************************************************/

#include <SoftwareSerial.h>

//Try varying the Software Serial pins until you get the best output
//Only use Software Serial compatible pins
SoftwareSerial ESP8266(2, 3);      //UNO Setting
//SoftwareSerial ESP8266(10, 11);  //MEGA Setting

char response;
String ESP_buffer;
int i, j;

boolean ipCheck = false;
boolean bufferComplete = false;
boolean resetESP8 = false;
boolean stationMode = false;
boolean gIPAddress = false;
boolean cipMux = false;
boolean cipServer = false;

void setup() {

  Serial.begin(115200);//Vary the values to get the best SerialMonitor response
  ESP8266.begin(115200);//Setting the baud rate of Wifi Module.Always 115200

  Serial.println("Initiating Serial Comms");
  while (!ESP8266) {
    Serial.println("Retrying ESP Serial......");
  }
  Serial.println("Serial ESP Started");
  delay(5000);

}

void loop() {
  //wifiReset();

  clearBuffer();
  while (stationMode == false) {
    wifiStationMode();
  }

  clearBuffer();
  while (gIPAddress == false) {
    getIPAddress();
  }

  clearBuffer();
  while (cipMux == false) {
    setcipMux();
  }

  clearBuffer();
  while (cipServer == false) {
    setcipServer();
  }

//  clearBuffer();
//  while (resetESP8 == false) {
//    wifiReset();
//  }

}

void wifiReset() {
  if (resetESP8 == false) {
    Serial.println("Running wifiReset");

    delay(5000);
    ESP8266.print("AT+RST\r\n");

    while (ESP8266.available()) {
      response = ESP8266.read();
      ESP_buffer += String(response);
      Serial.println(ESP_buffer);
      int inputLen = ESP_buffer.length();
      //Serial.print("Input Length");
      //Serial.println(inputLen);

      if (inputLen >= 160) {
        //achieve max length to reach the total buffer size
        //may vary from one to other
        //Serial.println("length achieved");
        bufferComplete = true;
        break;
      }
    }

    if ( bufferComplete == true) {
      if (ESP_buffer.indexOf('IP') >= 1) {
        resetESP8 = true;
        Serial.println("wifiReset Completed");
      }
    }
  }
}


void clearBuffer() {
  ESP_buffer.remove(0, ESP_buffer.length());
}

void wifiStationMode() {

  Serial.println("Running wifiStationMode");

  delay(5000);
  ESP8266.print("AT+CWMODE=3\r\n");

  while (ESP8266.available()) {
    response = ESP8266.read();
    ESP_buffer += String(response);
    //Serial.println(ESP_buffer);
    int inputLen = ESP_buffer.length();
    //Serial.print("Input Length");
    //Serial.println(inputLen);
  }

  if (ESP_buffer.indexOf('OK') >= 1) {
    stationMode = true;
    Serial.println("Wifi Station Mode Setup Completed");
  }
}

void getIPAddress() {
  Serial.println("Running getIPAddress");

  delay(5000);
  ESP8266.print("AT+CIFSR\r\n");

  while (ESP8266.available()) {
    response = ESP8266.read();
    ESP_buffer += String(response);
    //Serial.println(ESP_buffer);
    int inputLen = ESP_buffer.length();
    //Serial.print("Input Length");
    //Serial.println(inputLen);
  }

  if (ESP_buffer.indexOf('STAMAC') >= 1) {
    gIPAddress = true;
    Serial.println("getIPAddress Completed");
  }
}

void setcipMux() {
  Serial.println("Running setcipMux");

  delay(5000);
  ESP8266.print("AT+CIPMUX=1\r\n");

  while (ESP8266.available()) {
    response = ESP8266.read();
    ESP_buffer += String(response);
    //Serial.println(ESP_buffer);
    int inputLen = ESP_buffer.length();
    //Serial.print("Input Length");
    //Serial.println(inputLen);

  }

  if (ESP_buffer.indexOf('OK') >= 1) {
    cipMux = true;
    Serial.println("setcipMux Completed");
  }
}

void setcipServer() {
  Serial.println("Running setcipServer");

  delay(5000);
  ESP8266.print("AT+CIPSERVER=1,80\r\n");

  while (ESP8266.available()) {
    response = ESP8266.read();
    ESP_buffer += String(response);
    //Serial.println(ESP_buffer);
    int inputLen = ESP_buffer.length();
    //Serial.print("Input Length");
    //Serial.println(inputLen);
  }
  if (ESP_buffer.indexOf('OK') >= 1) {
    cipServer = true;
    Serial.println("setcipServer Completed");
  }
}

To understand the program better, let's study the codes by sections.


The void loop of the program basically contains functions that will perform a list of checks to determine if our ESP8266 ready for use such as resetting the ESP module, setting to chip to Station Mode, getting IP address, starting up multiple IP connection and etc.


This checks will be important for further utilization of our ESP8266 modules. For instance, setting up a server or sending data to a website and many more.


The simplest function that we have in the above function is to clear buffer as shown below

void clearBuffer() {
  ESP_buffer.remove(0, ESP_buffer.length());
}

The above function will clear every content in the buffer named ESP_buffer which serves to store any response received from ESP8266. We run this before the execution of our function to avoid unnecessary content being carried over from previous processing and subsequently reducing errors.


Next, let's look into function called wifiStationMode

void wifiStationMode() {

  Serial.println("Running wifiStationMode");

  delay(5000);
  ESP8266.print("AT+CWMODE=3\r\n");

  while (ESP8266.available()) {
    response = ESP8266.read();
    ESP_buffer += String(response);
    //Serial.println(ESP_buffer);
    int inputLen = ESP_buffer.length();
    //Serial.print("Input Length");
    //Serial.println(inputLen);
  }

  if (ESP_buffer.indexOf('OK') >= 1) {
    stationMode = true;
    Serial.println("Wifi Station Mode Setup Completed");
  }
}

This function sets up the ESP chip to SoftAP+Station mode when CWMODE=3. Once the command is executed, we then simply look for the string "OK" to confirm successful execution. When ESP is available, response gathered from ESP is stored in ESP_buffer. The string "OK" is then searched within this "ESP_buffer" using indexOf. If it is found, then the boolean "stationMode" will be set to TRUE. Going back to void loop, in case if there is failure, the program will be re-executed until "stationMode=true" condition is fulfilled. By doing this, we can ensure that no setup is skipped and run or repeated only when necessary.


The function "setCipMux" and "setCipServer" functions the same way where the confirmation word "OK" is sought after the AT command execution.


Whereas the function "getIPAddress" is slightly different where the confirmation word "STAMAC" is used. The simple reason is that when AT command "AT+CIFSR" is executed, the word STAMAC is obtained at the end of response from ESP8266. To understand this part better, run command "AT+CIFSR" in Software Serial Monitor using methods from ESP Part 2 Tutorial.


Finally, let's look at the function which resets the ESP module. This function is a little more complex than the rest because reset gives the longest response and takes the longest time process.

void wifiReset() {
  if (resetESP8 == false) {
    Serial.println("Running wifiReset");

    delay(5000);
    ESP8266.print("AT+RST\r\n");

    while (ESP8266.available()) {
      response = ESP8266.read();
      ESP_buffer += String(response);
      Serial.println(ESP_buffer);
      int inputLen = ESP_buffer.length();
      //Serial.print("Input Length");
      //Serial.println(inputLen);

      if (inputLen >= 160) {
        //achieve max length to reach the total buffer size
        //may vary from one to other
        //Serial.println("length achieved");
        bufferComplete = true;
        break;
      }
    }

    if ( bufferComplete == true) {
      if (ESP_buffer.indexOf('IP') >= 1) {
        resetESP8 = true;
        Serial.println("wifiReset Completed");
      }
    }
  }
}

One notable difference would the use of buffer length(inputLen). We use this length to check if enough information has been stored in the ESP_buffer. As I have said earlier, ESP reset gives the longest response and we want to ensure if enough information has been passed onto and stored in ESP_buffer. Based on my trial and error, I figured the count 160 works best for me. If the charactes in ESP_buffer are lesser than 160, it would mean that ESP8266 is still sending responses from the reset and hence the word "IP" still not yet to be found in the buffer.


Once it is more than or equal to 160 characters, the if function breaks and goes to the next validation step which is to look for the word "IP" in the buffer. If it is found, then boolean "resetESP8" becomes TRUE indicating the completion of a successful reset.


With that, we have come to the end of the tutorial. It may be confusing when you're just reading from the blog, therefore I would advice you to experiment it yourself to grasp a better understanding of my explanations. Good Luck Trying :) Feel free to post any comments or questions that you have.


For more detailed instruction, check out my YouTube video :


Comments


bottom of page