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.

Post a Comment