Arduino Home : www.sharetechnote.com
Arduino - WiFi - ESP8266
I like most part of Arduino but there were something I am not so satisfied with it. It doesn't come out with any means of internet connection. To overcome this drawback, I first thought of using Ethernet shield just because I thought it would be simpler than using WiFi shield. However, I was told that the original Ethernet shield is not available any more in the market. I bought a clone version of the eithernet shield, but failed to make it work. And then I was thinking of WiFi shield, but it was too expensive (around 100 bucks) which is over twice as expensive as Arduino board itself. So I didn't even try this. At last, I came to learn about ESP8266 WiFi module that cost less than 10 bucks. At first, I was a little reluctant to try it because I was not sure if it really works well as most of the tutorials or YouTube shows, but I decided to try it anyway since there is no alternatives for me. Luckily it worked much easier than I was concerned about. In this tutorial, I want to share you with my experience with the WiFi module based on ESP8266 chipset. Even though using the exactly same chipset, your experience would not be as smooth as mine depending on the specific modules that you get or the firmware versions of the chipset. So don't expect that you will make it work at first try :)
What to buy ?
Basic function and pin mapping for the most of ESP8266 module are same (or similar), but the detailed behavior may be a little different depending on the firmware version or manufacturer of the module. So my recommendation is to search things about the modules and pick something from the manufacturer that provide the detailed documentation. The one that I purchased is shown below (Actually this was the only one that I could get from a local electronics shop in my area).
Followings are the descriptions on the pins out of the module. I think these pin mapping would be same for all the module that looks like this.
The function of each pins are as follows. One thing to be VERY CAREFUL is that you have to put 3.3V (not 5V) in VCC. You would burn the chips on the board if you apply higher voltage.
GND : Circuit Ground
TX : UART0 Transmit
RX : UART0 Receive
GPIO2 : General Purpose I/O
GPIO0 : General Purpose I/O
CH_EN : Chip Enable, Active High
RST : Reset, Active Low
VCC : Circuit Power, +3.3V DC
The connection between the esp8266 module and Arduino board is as follows. As you may guess from this connection, the communication between Arduino board and the esp8266 module is done via UART port. It implies that ESP8266 module is a kind of a translator converting WiFi protocol to UART (Serial Port) protocol and vice versa.
I want to point out a couple of important points that you need to pay attention to.
Note 1 : VCC of the esp8266 is connected to 3.3 V of Arduino. Do not ever connect 5V output of Arduino directly to the VCC of the esp8266 module. It will burn the chip on the module. If you want to connect 5V output to VCC, you need to build a small voltage divider circuit using resistors so that only 3.3V is supplied to VCC.
Note 2 : Both CH_PD and VCC of the esp8266 module is connected to 3.3V of Arduino board. In reality, you would not have any wires as drawn here. You would need some soldering or use a small breadboard to connect wire like this (In my case, I used a breadboard to connect wire like this. I don't like / I am not good at soldering :)
Note 3 : In this tutorial, I used 3.3V output of Arduino board to simplify the connections. If you use the connection and a small breadboard, you don't need any soldering and you don't need any extra component like resistors. However the problem of 3.3V output of Arduino board is that it is not able to supply large current. It can supply just around 50 mA max. However, as you see in electrical specification (), this amount of current may not be sufficient to some of the mode of operation of esp8266. So after you get familiar with manipulating the module and get confident, I would recommend you to use external power supply to VCC or "Arduino 5 V output + voltage divider circuit".
With all the wiring done properly, now it is time to connect to the esp8266 and configure the chipset, get it connected to your WiFi Network. Procedures described here is based on the assumption that you are connecting the module for the first time, you may skip some of the steps when you try next time.
1. Load an empty sketch to Arduino Board
This is not a mandatory step, but I would recommend this at least for the initial configuration process just to make it sure that any existing program is interfering the initial configuration process.
Creat a new sketch with no code in it (only the empty setup() and loop() is there) and compile / upload it to the board.
2. Confirm Serial Communication working
Then open the COM port monitor window and do the followings :
i) Set [Both NL & CR] as shown below
ii) Set Baud rate to be 115200 as shown below (The default baud rate of this module is 115200.)
iii) Type in 'AT' and press Enter. If you get 'OK', it means the communication between ESP8266 UART pin and Arduino UART port is working.
3. Initialize (Reset) the WiFi Module
Now reset the WiFi module. You don't need to do this every time. Just doing it for the first time or when you reset the device and configure everything from scratch.
Send AT+RST. If you see 'ready' at the end of the result, it indicates the reset is complete. The detailed output before 'ready' may vary depending on the module and firmware version that you are using.
4. Check the firmware version of WiFi Module
Now check the firmware version of the module using AT+GMR command. I don't guarantee all the operation here will be successful if it is too old version. (You can update the firmware with the latest version, but it will require some additional procedure which is not explained here)
Command : AT+GMR
AT version:0.40.0.0(Aug 8 2015 14:45:58)
Ai-Thinker Technology Co.,Ltd.
Build:18.104.22.168 Sep 11 2015 11:48:04
5. Set the device mode to make it function as both 'Client and AP (Mode 3)'.
Now set the mode of the WiFi module to be 3 as shown below.
Command : AT+CWMODE=3
Response : OK
Note : There are three different modes for ESP8266 as follows.
1 - client
2 - Access Point
3 - Client and Access Point
6. Search WiFi Access Point
Now search WiFi Accesspoints available around the module. Following is an example of search result.
Command : AT+CWLAP
You can figure out many details of the each Access Point that are found by interpreting the result of AT+CWLAP as shown below.
7. Join onto the Access Point (Connect to AP)
Now you can join to any one of the APs that are found in previous step using AT+CWJAP command as shown below.
Command : AT+CWJAP="AP_IoT1","1234"
WIFI GOT IP
8. Confirm on the assigned IP
If the connection to the specified AP (SSID) is done properly, you would get the IP address assigned to the module when you send the command AT+CIFSR.
Command : AT+CIFSR
+CIFSR:STAIP,"192.168.0.22" // Make it sure that you got IP address assigned here.
If you went through all the procedure and got proper response for those steps, the module is properly configured and connected to your WiFi network at IP level.
Now let's check if the module works at higher layer (higher layer than IP, e.g TCP layer). In this tutorial, we will configure the module as a TCP client and try to get access to a web server. (You don't need to have your own web server for this test because we will try to connect to a public server).
1. Enable the multiplexing
Enable the IP multiplexing capability of the module by using AT+CIPMUX command as shown below.
Command : AT+CIPMUX=1
2. Establish TCP Connection to a Server
Get the module to connect to a web server using a command as follows :
Command : AT+CIPSTART=4,"TCP","www.google.com",80
Note : This command would take a some time (several seconds) to establish the TCP connection to the server and give the return as shown above. In some case, you may get 'DNS fail' error. I am not sure about the exact cause of this error.. my guess is that it is because the module failed to get DNS response for the url. Probably because of too low network speed. But the exact cause is not known. Just a couple of more tries and see if you get connected.
3. Get HTTP Document
Now send following command. This indicate 'give me the prompt to send 18 bytes of data'.
Command : AT+CIPSEND=4,18
> // The result of the command is this prompt. At this prompt, you can send whatever command
// that the server can understand.
Since the server that is connected in this tutorial is a web server, you can send a command as follows to retrieve a contents.
Command : GET / HTTP/1.0 // After sending this command, you should press [Enter] key again to retrive the
// contents from the server.
+IPD,4,471:HTTP/1.0 302 Found
Content-Type: text/html; charset=UTF-8
Date: Fri, 01 Jul 2016 07:04:18 GMT
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
The document has moved