March 28, 2015

Debugging ESP8266 commands with Arduino Uno

I have recently received an ESP8266 that is a tiny and cheap WiFi module that can be controlled through serial interface commands. I have read several blogs and forums to understand how to make it work. I finally found my own approach of testing this little toy.

ESP8266 pins are described in this diagram.
These are few important things I have discovered:
  1. Power - The ESP8266 needs 3.3V power. The ESP can draw up to 250mA while the Arduino Uno 3.3 output pin can provide only 50mA. In serious project you need a good 3.3V power source but for playing and debugging the Arduino Uno 3.3V output pin can be enough.
  2. CH_PD pin - The CH_PD pin needs to be held high so connect it to VCC also.
  3. TX/RX -In theory you TX and RX pins accept 3.3V so you need level shifters to adapt Arduino's 5V signals. However, I found out that it just works without level shifters.
  4. Debugging - To debug the ESP's commands you need 2 serial ports. One (the default one) is used by the Arduino Serial Monitor so it can be used to debug. The ESP will be attached to the second serial port but unfortunately the Arduino Uno has just one serial port. Some approaches require additional hardware or an Arduino Mega but I have found that the Software Serial library allows to emulate a serial port so I will use this simpler approach.

Wiring



NOTE: This is not a perfect wiring scheme because it doesn't use level shifters. Use at your own risk!

Connections are:
  • Arduino 3.3v power > breadboard red power rail
  • Arduino GND > breadboard black ground rail
  • ESP8266 GND > ground rail
  • ESP8266 VCC > power rail
  • ESP8266 CH_PD > power rail
  • ESP8266 TXD > Arduino Digital Pin #6 (RX pin using software serial)
  • ESP8266 RXD > Arduino Digital Pin #7 (TX pin using software serial)

Code


#include "SoftwareSerial.h"

SoftwareSerial esp8266(6, 7); // RX, TX

void setup()
{
  Serial.begin(115200); // serial port used for debugging
  esp8266.begin(9600);  // your ESP's baud rate might be different
}
 
void loop()
{
  if(esp8266.available())  // check if the ESP is sending a message
  {
    while(esp8266.available())
    {
      char c = esp8266.read();  // read the next character.
      Serial.write(c);  // writes data to the serial monitor
    }
  }
 
  if(Serial.available())
  {
    delay(10);  // wait to let all the input command in the serial buffer

    // read the input command in a string
    String cmd = "";
    while(Serial.available())
    {
      cmd += (char)Serial.read();
    }

    // print the command and send it to the ESP
    Serial.println("---------------------");
    Serial.print(">> ");
    Serial.println(cmd);
    Serial.println("---------------------");
    esp8266.println(cmd); // send the read character to the esp8266
  }
}

Commands

Now that you have wired your ESP and uploaded the sketch to Arduino you can open the serial monitor and play with the 'AT' commands.
Remember to set the baud rate to 115200. Newer ESP modules also require to send NL & CR characters.



The main ESP8266 AT commands are listed here.
  • AT - Test
  • AT+RST - Reset
  • AT+CWLAP - List WiFi available networks
  • AT+CWJAP="SSID","PWD" - Connect to a network (change SSID and PWD to match your access point)
  • AT+CWJAP? - Check if connected successfully
For a full list of commands refer to this document.

11 comments:

  1. I have a problem. My serial port doesn't receive any answer from the esp8266 module - only the name of the command printed in the code. After sending a command (like "AT"), on my Arduino board lights the RX diode, and then the TX one (so I guess there is some communication), but nothing appears on the serial port. Do you know where can be a problem? I've tried the code with different baud rates, but nothing helped.

    ReplyDelete
    Replies
    1. I got same problem

      Delete
    2. If you have not figured it out yet this is most likely because your ESP8266 itself has a different baud rate that 9600. Try changing esp8266.begin(9600) from 9600 to 115200.

      Delete
  2. Hey i got same problem ..if you found solution ...plz share

    ReplyDelete
  3. Try lowering the delay to 'delay(10)'.
    I have updated the example.

    ReplyDelete
  4. hi, it's work but the coding got some problem, pls help.
    ---------------------
    >> AT+GMR
    ---------------------
    AZ冉J旍D$Q vY悅Z呯�
    嶲 8 2015 14;45:58)
    SDK version:1

    ReplyDelete
    Replies
    1. it works coz I am using it

      Delete
  5. I see this when using ESP baud rate is high (115200) and using a software serial with Arduino Uno.
    Software serial does not support high baud rates.
    You need an Arduino Mega or you have to lower the defauld baud rate of the ESP module.

    ReplyDelete
  6. Sir! I have a problem, when I wired-up the whole Circuit using Arduino & ESP-8266-01, after connections & coding, when I go to the Serial Monitor, & wrote AT for testing, then I got nothing on the Blank screen of Serial Monitor.

    ReplyDelete
    Replies
    1. I just solved this by trying out different baud rates. It works somehow, though still with messy unreadable text.

      Delete
  7. i have just tried to do it like the description but the blue led is always on!!!
    and i am revising a lot of letters..

    ReplyDelete