Getting the ESP8266 wifi module to work with Arduino

A lot has been written about this super cheap ESP8266 wifi module. I just want to share a few remarks that might help people who want to do similar things with this, e.g. using it with Arduino via Software Serial.
Seeeduino with ESP8266 wifi and DHT22 sensor
  • you need to connect 5 pins to get this working (Vcc, GND, URXD, UTXD, CH_PD)
  • but i found that also using the RST pin to reset the module before using it worked much better
  • the RX/TX/RST pins seem 5V tolerant so no need for a level shifter 
  • my main challenge was to get the module working with Arduino's Software Serial, and in order to be able to do that, the module needs to be set to a relatively slow baud rate, e.g. 9600. my first modules came with firmware that fixed the baud rate at 115200 and did not allow me to change it;
  • i had to update the firmware; my first modules came with version 16xxx (9.1?), i upgraded to the latest version 9.5 and it took me several days to find out that this was not working properly; when i went back to a lower version (18000902 - 9.2) all worked fine!
  • i ordered a second batch which came with firmware version 18000902 - 9.2 and a baud rate of 9600 so those worked 'out of the box'
  • some firmware replies 'Ready' to the AT+RST command, others 'ready'
  • when using a Seeeduino Clio board, the 3V3 seemed not always powerful enough for the ESP8266, so it worked better with an external power supply (the 3V3 from a Seeeduino and Mega provide enough power, i did not test other boards) 
You can also check out the slides i made for a workshop i gave about Arduino+ESP8266 at Dimsumlabs.



Let me elaborate on the above remarks.
I ordered 2 of them from Seeedstudio in Oct 2014, at USD 6.95 each. The second batch i got from Taobao at RMB 13.5 each. I had earlier played a bit with a CC3000 wifi shield that i got in Shenzhen for around USD 30. It works, but i found it very unstable and had to implement a software watchdog to reset the module because it would get stuck all the time. Eventually it did work nicely to send data into the cloud at regular intervals using the Adafruit library.
ESP8266 on the left (USD 7), CC3000 on the right (USD 30)

My intention is to connect this ESP8266 to Arduino based wireless sensors, similar to the RF technology that i am using in my OpenEnergyMonitor system discussed on this blog. The problem is that the ESP8266 only has a serial interface, and the standard Arduino's (hardware) serial is used for uploading sketches and communication via the Serial Monitor. The suggested solution is to use the Arduino's hardware serial for the ESP8266 and a USB-to-serial converter for communication with the IDE. You might be able to get this to work (i used an FTDI cable with a 3V3 pin), but it is quite an annoying solution. It would be much more elegant to be able to use the Arduino's hardware serial as usual, and put the ESP8266 on a software serial (using 2 digital pins). However, the software serial cannot work faster than 19200 baud, so we have to make sure our ESP8266 is set to 9600. I will go through the steps that i needed to make this work.

Pinout of the ESP8266 modules i am using
STEP 1: TEST the ESP8266
To test the module we can use an Arduino as a Serial converter to talk to the ESP8266 directly via the IDE's Serial Monitor. We will be using Arduino pins 0 (RX) and 1 (TX) for this and we need to be sure that the sketch on the Arduino does not interfere, so upload the 'blink' sketch to your board, because that does not use the serial.
Not sure if the ESP8266 pins are officially 5V tolerant, but they seem to be OK with 5V. First i used a voltage divider (10K/5K) on the URXD pin to make sure it got 3V3 instead of 5V, and the UTXD's 3V3 is high enough to be recognised by the Arduino digital input as HIGH. But it seems it also works directly on 5V without any level shifter or voltage divider.
First solder a pull-up resistor between Vcc and CH_PD (power down pin), i used 4.6kOhm, more here.
Then connect the ESP8266 to your Arduino:
  • Vcc = 3V3
  • GND = GND
  • URXD = RX/pin0
  • UTXD = TX/pin1
Note that we don't have to invert RX/TX because we are not going to talk to the Arduino, the ESP8266 is going to act as the Arduino so we connect RX/RX and TX/TX.
Now open the IDE's Serial Monitor and you can talk with the ESP8266 if you baud rate is set correctly. My modules came set with 115200 baud. Also important to use the correct setting at the bottom of the Serial Monitor ‘both NL&CR’ (new line and carriage return).
Now you can use the Serial Monitor to send commands to the ESP8266 and see its replies. More info on AT commands is available elsewhere.
AT+RST (reset)
AT+CWMODE=3 (set mode to AP+STA)
AT+CWLAP (list wifi networks)
AT+CWJAP=”ssid”,”pw” (connect to your wifi)
AT+CIOBAUD? (check baud rate)
AT+CIOBAUD=9600 (set new baud rate)
AT+GMR (check firmware version)
all these commands should return you a reply in the Serial Monitor

On my first batch the original firmware (version 016000 or something like that) did not allow me to change the baud rate with the above command. So i was not able to connect the ESP8266 to Arduino via a software serial. A solution is of course to use an Arduino Mega which has multiple hardware serials, but that's not really a cheap solution for wireless nodes. I did try the original firmware with a Mega, and it worked fine with the standard 115200 baud on a second hardware serial, but as mentioned, i could not change the baud rate there.
The only option is to upgrade the firmware. On my second batch the baud rate was set to 9600 so i did not change the firmware.

STEP 2: UPGRADE the FIRMWARE (if necessary)
This means flashing a new binary to the ESP8266 module. I was not familiar with this, but got it to work via Linux on my Chromebook. Download the esptool from github. Get the binary from the firmware source. I used version 180902_02 (9.2) in the 'old' folder, which works fine for me (AT+GMR shows 18000902 as the version). There are .exe files for Windows.
Now connect the ESP8266 to the Arduino as above, 3V3 and GND, but these differently:
  • GPIO0 to GND (not used above)
  • UTXD to Arduino's RX pin0 (different from above)
  • URXD to Arduino's TX pin1 (different from above)
In the Linux terminal, go into your esptool directory, put the downloaded firmware there, and type:
> sudo ./esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash 0x000000 v0.9.2.2ATFirmware.bin
(at least that did the trick for me)
Now you should see 'erasing flash, writing 0->100%'
Remove the GND connection from GPIO0, switch the RX and TX connections, and you are ready to test again as in step 1 above. The module should be set to 9600 baud now, and you can change it with the above command.
Using the current 'latest' firmware, version 9.5, i got it to work partially with a software serial on 9600 baud; connecting to the wifi was no problem, and also opening a TCP connection (AT+CIPSTART), but i was not able to send a HTTP GET request (i could send the length with AT+CIPSEND and get the '>' but sending the data did not work, no reply from server). This may be a bug as it does work with the older firmware.

STEP 3: SOFTWARE SERIAL
Now it's time to connect the ESP8266 via software serial to Arduino, using pin 10 as RX connected to UTXD and pin 11 as TX connected to URXD.
In my experience, it works more reliably when i reset the ESP8266 before starting the software serial because ‘AT+RST’ does not seem to reset it completely, sometimes giving problems to connect to the wifi. So i connect the ESP8266 RST pin to pin 9 on Arduino, and in my sketch i set pin 9 LOW for short instance to reset the unit, and then leave it HIGH during operation. 

EXAMPLES
The way i use it is having a sensor measure some data every minute or so, and then initialise the ESP8266, connect to wifi, send the HTTP request, receive confirmation from the server that it is received, and disconnect again. 2 examples below, and visualisation of the data on cloud server at emoncms.org
If anybody interested, i can post the Arduino code too.

Industruino with ESP8266 wifi module and LiPo battery


PPD42 dust sensor with Seeeduino and ESP8266 wifi module
the Seeeduino Clio board's 3V3 is not powerful enough to drive the ESP8266, need external power supply e.g. 2x AA batteries



Post a Comment

0 Comments