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 (

Links :
Sparkfun AT Guide :

Sketch :

#include <SoftwareSerial.h>
#define WIFI_SSID        "#####" //WIFI SSID
#define WIFI_PASS        "********" // Wifi Password
#define SERVER_IP     "" // 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.
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 =; // Read one character from serial port and append to a string.
        if(response.indexOf(keyword)>0){ //Searched keyword found.
          return true;
  return false;

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

boolean initializeESP8266Module(){

  //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.");
    }else if(counter==4)
      exception("Connection to Wi-Fi failed. Please Check");

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

void setup()  
  Serial.println("ESP8266 Demo");
  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)) 

  if (!sendCommand("AT+CIPSTATUS", "OK", CONTINUE))// Check for TCP Connection status.
  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++){
  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.");

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

Buy ESP8266 :
Getting started with ESP8266 | AT Commands

Subscribe YouTube :

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

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