Introduction: Arduino to Arduino Communication With ESP8266's
Last Summer my girlfriend and I built a pond in our back yard. After looking around at some pond monitoring systems and seeing what people were charging for them, I decided to build my own system using Arduino's for one tenth of the cost. For this project I am using Arduino Uno's with ESP8266 wifi chips to transmit the data to a central Arduino Uno which has a 2.8" TFT touch screen shield and acts as the main controller and displays information. The system will ultimately control the pumps and report what the pH, water temperature, air temperature, and water level of the pond are. Hooking up and testing the different components was pretty easy but where I ran into a lot of trouble was with the ESP8266 boards. There is a lot of information out there and after sifting through all of it for the last month and trying to get these things to work with an Arduino, I have finally cracked it and will share it with everyone here. Hopefully this will help you all out there in getting these awesome little modules up and running in no time. I know some may ask why use the Arduino since the ESP chip has some DIO pins but the problem is that most of the stuff I am monitoring will be analog signals and I also will be using solar power to run everything which will require putting stuff to sleep when its not actively being used. As such I went the route of having the Arduino be the main brains and the ESP just to route the data.
For this instructable you will need the following hardware (x2 if you want to make both the server and client):
3. 2 10k ohm resistors
4. FTDI board
5. some wires to make connections
6. logic level converter
7. voltage regulator (you will need to reduce the Arduino 5v to 3.3v as the Arduino 3.3v cannot run the ESP).
Step 1: First Up.... Firmware
The factory firmware that comes with these is most likely going to be dated and need upgrading. There are various versions out there, NodeMCU is a popular one in which you can write lua scripts and load them into the chip itself for execution. For this project though I wanted the Arduino to be able to run the show and opted to use the latest firmware from Espressif. There are two things that you need to get for this, the firmware itself, and the AT command set. If you go to http://bbs.espressif.com/ click the SDK's link, then scroll down and you will see the latest firmware. Download that and then go get yourself the ESP flasher which if you go back to http://bbs.espressif.com/ and this time scroll down to the tools area you will be able to get the latest flash tool. Since we are downloading stuff, lets ensure we have the ESP8266 library in our Arduino IDE, to get it, go to file / preferences and enter this URL in the additional boards field:
Now you can go to tools / boards / board manager and install the ESP8266 library. We will use this later to test.
The last thing to ensure you have done is to install the FTDI driver. This can be retrieved from:
The next step is to breadboard your ESP chip with the FTDI. I have attached a schematic from Google that I used, I have also attached a picture of the PCB board I made today to program these chips with ease. You do not need to get this extravagant if you don't want to, I just did because I have a lot to flash / test and this makes it easy. If you are just going to breadboard it, you can skip using the resistors and just plug GPIO0 to GND (i used a cable that was easy to move between ground and a random spot that was neither ground nor vcc on the breadboard) and RST to VCC. You can also skip the buttons and just pull/insert the USB cable into the computer. Now you should be able to plug into your computer and open up the flasher tool. To start with, do not make any changes and ensure no files are loaded and no check boxes next to the files. Hit flash. This will read your chip and tell you what options you need to select under detected info. There is a pretty good chance that you will have the same chip I do which is the 8Mbit flash size, 26Mhz crystal, 40Mhz SPI speed and SPI mode of DIO. Using the image above, setup your flash tool as mine is. The bin files are all located inside the bin folder that you downloaded. Pay attention to the memory addresses that the bin files need to be written to. Once your setup matches mine (your com port will probably be different) hit flash and you should see the command prompt window start erasing the flash and programming the new one. Once it hits complete, unplug the USB cable.
Step 2: Testing the Firmware and Prepping
Now that we have loaded the firmware, we want to test it and make some changes to it. The biggest resource for this will be the manual for the SDK which can be retrieved from the Expressif website. For this instructable though we want to ensure we have the correct version, setup the softAP, enable DHCP, and reduce the baud rate from 115200 to 9600. The reason for the reduction in the baud rate is we need to use the software serial on the Arduino in order to be able to communicate with both the Arduino and the computer. To start, ensure you unplugged the USB cable from the computer in the last step, move the GPIO0 from GND to an unused portion of the breadboard (or just remove the cable). Now plug the USB cable back in and go to the Arduino IDE. Go to tools / boards and select Generic ESP8266. Change the port to match your detected comm port of the FTDI. Now open the serial monitor and change the baud rate to 115200. You can now begin using AT commands to talk with the Arduino. The first command you want to run is AT. This should return an OK and lets you know the AT command set is working. Now type AT+GMR and hit enter. You should see the firmware version returned similar to the picture I posted. The neat thing that has been added to the AT command set is that for most items that you will store to flash, you can run a test of the command first. This is nice because if you set a value wrong and make the card non responsive, a simple reboot will put you back to where you were. Lets set our baud rate to 9600, you will need to do this anyway and if you are just using this to get your ESP chip ready to use, this should be your last step. If you look through the manual for the firmware, you will notice that for a lot of the AT+CW commands, there are 3 versions. A depreciated version, a _CUR version and a _DEF version. The _CUR version should always be run and tested before you run the _DEF version as this writes the command to flash and will be the new default on boot for the chip. To change the baud rate we will type AT+UART_CUR=9600,8,1,0,0 (NOTE: the manual example shows 9600, 8,1,0,3 don't do this as you will lose comms with the board and have to reload a really old firmware version to restore) and hit enter. You will see something, it may be legible, it may not. The reason for this is that the chip is now operating at 9600 instead of 115200 so you will now need to change the rate to 9600 in the serial monitor. Do this now without unplugging the chip or closing the window. The window will clear and you can again run the AT and AT+GMR commands. If this is successful, rerun the UART command as _DEF this time: AT+UART_DEF=9600,8,1,0,0 You should see an OK. The last command we need to run is to turn on DHCP. It's weird but this will need to done for both softAP and station. Before we can do this though we need to ensure we are in the correct mode. For simplicity, set AT+CWMODE_CUR=3 and then AT+CWDHCP_CUR=2,1 you will notice that the numbers are slightly different as the mode goes 1=station, 2=softap, and 3=both while dhcp is 0=station, 1=softap, and 2=both. The key here is that whatever you set the mode to, you need to enable the dhcp for. Once you get an OK for both entries, commit them to flash with AT+CWMODE_DEF=3 and AT+CWDHCP_DEF=2,1. You can now unplug the ESP from your breadboard.
Step 3: Making the Server and Client
So this is where the fun starts. I have included pictures of my breadboards (I know, looks like a mess). What I have done here is run the Arduino 5v to a rail on the breadboard, then used a regulator to produce 3.3v on the other rail. You will need to run a wire to connect both grounds. You will also notice that I do not use caps on my VCC - GND rails. If you have them this is advisable. Now that I have power, I need to do a few things. The ESP will only accept 3.3V. You may read that they are 5V tolerant, they are not and while it may work for a small time, they will die over time. This setup conditions every signal to the ESP as 3.3V. Another issue that you will run into is that the ESP needs to be reset after the Arduino comes up due to some issue with the software serial. The last part which has to happen is that your software serial TX will go to the ESP TX and RX to RX. Below is a text based version of the pictures:
ESP VCC -> 3.3V rail
ESP RST -> LV CH1 logic level shifter
ESP CHPD -> 3.3V rail
ESP GND -> GND
ESP RX -> LV CH2 logic level shifter
ESP TX -> LV CH3 logic level shifter
ESP GPIO0 & 2 are not connected
Logic Level Shifter LV VCC -> 3.3V rail
Logic Level Shifter LV GND -> GND Join your 3.3 and 5 rail gnds together if they are not already done so via the regulator
Logic Level Shifter HV VCC -> 5V rail
Logic Level Shifter HV GND -> GND
Logic Level Shifter HV CH1 -> Arduino DIO 9
Logic Level Shifter HV CH2 -> Arduino DIO 10
Logic Level Shifter HV CH3 -> Arduino DIO 11
Thats it. Load which ever code you want to test, in my client version I added two push buttons to turn the LED on and off of the server. You can also watch the interactions via the serial monitor at 9600 baud.
Participated in the
Arduino All The Things! Contest