Connecting ESP8266 with Arduino | AT Commands

ESP8266 with Arduino using AT cmmands

ESP8266Connecting the ESP8266 Wi-Fi module to Arduino. The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and microcontroller capability produced by a Shanghai-based Chinese manufacturer, Espressif Systems.

The chip first came to the attention of western makers in August 2014 with the ESP-01 module, made by a third-party manufacturer, AI-Thinker. This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands. However, at the time there was almost no English-language documentation on the chip and the commands it accepted.The very low price and the fact that there were very few external components on the module which suggests that it could eventually be very inexpensive in volume, attracted many hackers to explore the module, chip, and the software on it, as well as to translate the Chinese documentation.

The ESP8285 is an ESP8266 with 1 MB of built-in flash, allowing for single-chip devices capable of connecting to Wi-Fi.

Source: Wikipedia (bit.ly/2cORmj0)


—————————————————————————————————
Links :
Sparkfun AT Guide : bit.ly/2cEzSbv

Sketch : bit.ly/2g7e2wl

#include <SoftwareSerial.h>
#define WIFI_SSID        "#####" //WIFI SSID
#define WIFI_PASS        "********" // Wifi Password
#define SERVER_IP     "192.168.0.1" // IP of the target server.
#define TIMEOUT     5000 //Timeout for the serial communication
#define CONTINUE    false
#define HALT        true
#define NO_OF_PARAMETERS 1 //No of parameters sending to the server.

SoftwareSerial esp8266SerialPort(10, 11); // RX, TX

void exception(String msg){ //Exception occured. Print msg and stops.
  Serial.println(msg);
  Serial.println("HALT");
  while(true){
   readResponseData("");
   delay(60000);
  }
}
boolean readResponseData(String keyword){ //Receive data from the serial port. Returns true if keyword matches.
  String response;
  long deadline = millis() + TIMEOUT;
  while(millis() < deadline){
    if (esp8266SerialPort.available()){
      char ch = esp8266SerialPort.read(); // Read one character from serial port and append to a string.
      response+=ch;
      if(keyword!=""){
        if(response.indexOf(keyword)>0){ //Searched keyword found.
          Serial.println(response);
          return true;
        }
      }
    }
  }
  Serial.println(response);
  return false;
}

boolean sendCommand(String command, String acknowledgement, boolean stopOnError)
{
  esp8266SerialPort.println(command);
  if (!readResponseData(acknowledgement)) 
    if (stopOnError)
      exception(command+" Failed to execute.");
    else
      return false;            // Let the caller handle it.
  return true;                   // ack blank or ack found
}

boolean initializeESP8266Module(){
  esp8266SerialPort.begin(9600);  
  esp8266SerialPort.setTimeout(TIMEOUT);
  delay(2000);

  //sendCommand("AT+RST", "ready", HALT);    // Reset & test if the module is ready  
  sendCommand("AT+GMR", "OK", CONTINUE);   // Retrieves the firmware ID (version number) of the module. 
  sendCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode. 
  sendCommand("AT+CWMODE=1","OK", HALT);    // Station mode
  sendCommand("AT+CIPMUX=1","OK", HALT);    // Allow multiple connections (we'll only use the first).

  String cmd = "AT+CWJAP=\""; cmd += WIFI_SSID; cmd += "\",\""; cmd += WIFI_PASS; cmd += "\"";
  for(int counter=0;counter<5;counter++){
    if (sendCommand(cmd, "OK", CONTINUE)){ // Join Access Point
      Serial.println("Connected to WiFi.");
      break;
    }else if(counter==4)
      exception("Connection to Wi-Fi failed. Please Check");
  }
  
  delay(5000);

  sendCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
  sendCommand("AT+CIFSR","OK", HALT);         // Echo IP address. (Firmware bug - should return "OK".)
}

void setup()  
{
  Serial.begin(9600);
  Serial.println("ESP8266 Demo");
  initializeESP8266Module();
  Serial.println("Module is ready.");
}

void loop() 
{
  String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += SERVER_IP; cmd += "\",80"; //Start a TCP connection.  to server SERVER_IP on port 80
  if (!sendCommand(cmd, "OK", CONTINUE)) 
    return;
  delay(2000);

  if (!sendCommand("AT+CIPSTATUS", "OK", CONTINUE))// Check for TCP Connection status.
    return;
    
  String  tag[NO_OF_PARAMETERS]={"NAME"}; //Tags for the parameters
  String data[NO_OF_PARAMETERS]={"my Name"}; //Values for the parameters
  
  cmd = "GET /ESP8266/index.php?";//Path to the target server file.
  for(int count=0;count<NO_OF_PARAMETERS;count++){
    if(count!=0)
      cmd+="&";
    cmd+=tag[count];
    cmd+="="+data[count];
  }
  Serial.println(cmd);
  cmd+=" HTTP/1.1\r\nHost: "; cmd += SERVER_IP; cmd += ":80\r\n\r\n";
  if (!sendCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE)){
    sendCommand("AT+CIPCLOSE", "", CONTINUE);
    Serial.println("Connection timeout.");
    return;
  }

  sendCommand(cmd, "OK", CONTINUE);// Send data to server.
  
  readResponseData("");// Read response data received from server.
  exception("ONCE ONLY");
}

Buy ESP8266 : amzn.to/2cZfUv8
Getting started with ESP8266 | AT Commands :bit.ly/2dBMjEq

********************************************************************
Subscribe YouTube : bit.ly/2gsIZ2g

Guys Subscribe to my channel for latest contents into your inbox.
Support me to keep going.

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WGLabz
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Related posts