Arduino and wifi using the esp8266

I was recently thinking about the easiest way to connect a microprocessor to the internet so that I could check from anywhere in the world the functions that a particular microprocessor was outputting or reading. Searching around on the internet I came accross these bad boys on amazon.

The ESP8266 module from Amazon

They are made by easterncomputers and contain the components required to do exactlly as I wished, or so it was claimed! I ordered two as they were so cheap for general experimenting. The first thing that I noticed on arrival was the dodgy soldering. It looks like it was done by hand and not too much care was taken. It was only afterwards that I also noticed this on the picture they were using as advertising on amazon. Nothing too bad and certainly nothing that isn’t easily repairable or necessarily a problem. The second thing I noticed was that there was no silk screen markings for the pin outs. Looking for the datasheet on their website it became quickly apparent that this wasn’t going to be the easiest project I’d embarked upon. The manufacturers website was not at the time of writing up and running! A quick google and I found a very similar unit with the same layout and with pin outs explained, so hopefully they will correlate with the unit that I have.

ESP-01 pinout viewed from the component side

ESP-01 pinout viewed from the component side. Pins are protruding down away from you as you look at this picture. Whilst googling it became apparent that the board is an ESP-01 clone, searching for ESP-01 throws up many more resources relating to this board. Taking the plunge and trusting that the pinout above was correct I wired up to a 3.3v power supply and voila a power light appears on the board to show that it is powered correctly.

It breathes! Power successfully applied to the correct pins

Now to get it communicating with an arduino. First off I connected CH_PD to the same 3.3v supply that is powering the module. Connect the Rx pin to Rx on the arduino (pin 0) and connect the TX pin to Tx on the arduino (pin 1). I then powered on the module and no smoke. I assume therefore that all is good so far. At this stage I decided to check to see if it was giving out any sort of wifi signal. Sure enough when scanning for wifi with my iphone a new wifi service was being broadcast which identified itself as AI-THINKER_8F33A5.

The wifi signal being broadcast from the ESP-01 module

With the Arduino plugged in to my laptop via USB and the wiring plugged as described above. The next thing was to upload a blank sketch from the arduino IDE, the sketch called BareMinimum is ideal as it does nothing. This allows us to have a serial monitor so that we can interract with the ESP-01. In the arduino serial monitor change the baud rate to 115200 and the line ending to Both NL & CR. Then type AT into the serial monitor and press send. If all is working well then we should recieve an OK message back from the ESP-01 via the serial monitor.

The ESP8266 ESP-01 module has three operation modes:

1. Station (STA)

2. Access Point (AP)

3. Both

In AP the Wi-Fi module acts as a Wi-Fi network, or access point, allowing other devices to connect to it. It simply establishes a two way communication between the ESP8266 and the device that is connected to it via Wi-Fi. In STA mode, the ESP-01 can connect to an AP such as the Wi-Fi network from your house. This allows any device connected to that network to communicate with the module. The third mode of operation permits the module to act as both an AP and a STA.

To begin with I’m going to set the mode to STA, this is done by typing the following command into the serial monitor. AT+CWMODE=1. Note that now the ESP-01 is no longer acting as an access point, therefore you will no longer see it when searching for it on your smart phone. If you ever want to check which mode your module is in you can simply type AT+CWMODE? This will display a number corresponding with the mode.

Now the ESP-01 is operating in STA mode, we can connect to a Wi-Fi network. First we can check if we are connected to one by sending the command: AT+CIFSR This will display the station IP address of our ESP-01 module. If we don’t get an IP address after entering the previous command, use the following command to connect to your network: AT+CWJAP="Wi-FiNetwork","Password" Type the name of your Wi-Fi network and the password to connect to it. Make sure you include the double quotation marks. After a couple of seconds, you should get an "OK" response. You can check again to see if you have an IP address using the AT+CIFSR command.

Now we need to enable multiple connections before we can configure the ESP8266 ESP-01 module as a server. Type the following into the serial monitor AT+CIPMUX=1 Now type the following to start the http server on port 80. AT+CIPSERVER=1,80. The first number indicates whether we want to close server mode (0), or open server mode (1). The second number chooses the port that the client uses to connect to our server. I chose port 80 because this is the default port for HTTP protocol.

If we now open up a web server on a pc on our network and enter the ip address of our ESP-01 module. Remember you can get the ip address by typing AT+CIFSR in the serial monitor. You should now see the request sent from our browser to the ESP-01 in the serial monitor.

The browser request recieved by the ESP-01 shown in the arduino serial monitor

The browser request received by the ESP-01 shown in the serial monitor
This is the HTTP request that our browser sends to our ESP-01 server to retrieve data. It contains information such as what file you want, the name and type of your client browser and version, what operating system you are using, the preferred language and more.

Once the request has been recieved by the server, we can send some data back to be diplayed in the requesting browser.

The first command is AT+CIPSEND=0,5 where the 0 indicates the channel used to transfer data and 5 represents the number of characters we are going to send. After hitting enter, the symbol > indicates that we can type the characters we want to send to the browser. In this case “hello”. We then get the response “SEND OK.” The data has now been transmitted to the client. Nothing has appeared in the browser yet as we need to close the channel to display the characters. Essentially it is a confirmation that the transmission has completed. The following command is used to close the channel AT+CIPCLOSE=0. “0” indicates the channel we are closing. After we hit enter the message will be displayed in the browser window.