28

Arduino ESP8266 Tutorial and Web Server Monitor Example

I have heard so many people talking about the cheap $4 WiFi chip know as the ESP8266.  I kind of ignored it at first while my friend went hog wild and built the ESP8266.com community and forum.  Eventually I decided to join in on the fun and ordered a couple off of eBay.  In hind sight, I am wishing I had ordered a bunch!  These little guys pack a punch especially when coupled with an external Micro Controller like an Arduino.

As a first attemp at working with the ESP8266 I decided to build a simple Green / Red status light for my OwnCloud server I host at home.  I will probably extend this project in the future to be a bit more robust and encompas more services but i thought it would make a great getting started tutorial for working with the ESP8266 and an Arduino micro controller.

The first step of the project is setting up the hardware. To do this we first need to understand the pinout of the ESP 8266.  The 1st version of the board (ESP8266 ESP01) is what I used and includes a 2 x 4 male header.  The Pinout includes VCC (+3.3v), GND, TX, RX, RESET, 2 IO pins and a special CH_PD pin.

 

ESP8266 01 Pinout

 

In my setup I connected this to an Arduino Nano v3.0 The pinouts are pretty straight forward as they are all labeled on the board.  For illustration I am including anyways just incase you don’t have one in front of you.

Arduino Nano V3

Connecting the two boards together is very straight forward.  We will simply cross over between the RX and TX lines of the two devices.  This will allow the Arduino to send Serial commands to the ESP8266 and then receive the resulting data from it.  Some people have reported that it is important to use a logic level converter or a resistor divider to drop the 5V TX from the Arduino to 3.3V going into WiFi chip however I had no issues with simply connecting them directly.  The final setup looks like the following;

Serial Connection

Once we have made the serial connections we will need to power the ESP8266.  It is important to note that the ESP8266 requires 3.3V and that 5V will likely release the magic black smoke.  While the Arduino Nano does have a 3.3V Voltage Regular on board, my particular Arduino was not capable of providing sufficient current to power the WiFi module.  As a result I used a LD33V voltage regulator.  This can be powered off of either the 5V from the Arduino (which I show below) or by using an external power source.  Drawing too much current through the Arduino may result in heat issues.

Power-Connections

 

ESP8266 Serial Commands

Understanding the Serial Commands being sent to the ESP8266 is a great place to start learning about the ESP8266

The first thing we need to know is how to restart the chip.  To do this we send the following command:

AT+RST

Next will will want to set the chip as either a wireless client or as an access point.  In our case we are looking to become a client to validate whether or not a Web Server is available.  If we wanted to act as a wireless Access Point then we would need to set the mode to 2 instead and issue some other commands.  For now let’s stick to the client settings.

AT+CWMODE=1

If we are using a serial connection to talk to the chip, at this point we would would probably want to check out what we can hear.  The ESP8266 provides the following command to list all of the available Wireless Access Points in the area which are broadcasting their SSID.

AT+CWLAP

Once we have established that our wireless network is out there, we need to connect to it.  To connect we simply issue the following command replacing SSID with the name of your wireless network and password with the WPA key used to secure your network.

AT+CWJAP=”SSID”,”PASSWORD”

You should now be connected!  If you have that serial terminal still open, you can ask the WiFi module for its current IP address by issuing the command;

AT+CWLIF

Wonderfull! Now we are ready to query a webpage!

Querry Web Page

The first thing we need to do is establish a connection to the webserver.  To do this you use the following command while obviously replacing the IP address with that of your web server.  In our case we are using standard HTTP (NOT SSL/HTTPS) therefore our port is 80.  I am not sure how this would work over SSL so if you figure it out please post in the comments below.

AT+CIPSTART=”TCP”,”192.168.88.35″, 80

Next we need to let the WiFi module know how many characters we are sending in our query.  To do this we issue a command like the following

AT+CIPSEND=50

Finally we can perform an HTTP get against the server. Note the number 50 above is the total number of characters in this command.  We count \r & \n as single characters and we do not count the last \r\n sequence.

GET /status.html HTTP/1.0\r\nHost: 192.168.88.35\r\n\r\n

This should result in a response from the webserver.  The last step left is to be proper citizens and close the connection by issuing the following command.

AT+CIPCLOSE

This is very cool stuff!  Now lets move this into something usefull and start writing some Arduino code.

Arduino Code

The following code is what I flashed to the Arduino Nano v3 to control the ESP8266.

IMPORTANT: You should be aware that while flashing the Arduino you must power down or disconnect the ESP8266 as it can cause problems while flashing your Arduino.

 

dave

28 Comments

      • yeah with serial connection. it should transmit signals wireless. anyway thank yu !!!!

        • You really cannot send the analog signal per se via digital means. You have to sample it, or get timed measurements along the application lifecicle. For example, you could (depending on your signal frequency or the requirements of your application) get instant measurements at 100ms intervals for a low frequency signal. The smaller the time interval, the better the digital representation of the analog part. Try just not to exagerate on the time interval size, too small and you will be putting too much load on the processor and too high will make you lose signal.

          • consider two arduino board with two esp8266’s in the first board using a if else statement if they are out of loop it should blink a light in second arduino board. so the esp is not working all the time it ll be working if this out of the loop

  1. i was try to execute your sample to my arduino nano and esp8266 but i always get false on Serial.find(“>”) can you help me to solve this problem

    • I was having issues with this, too. Sticking “delay(100);” on the line before the serial.find made mine work. I don’t see other people needing to do that, so I’m not sure why I needed to.

    • Is it really executing? Commands like serial.println(“AT+RST”); are not executing in my arduino ide, only just printing on serial monitor. I have used esplorer also. But there is also same problem with those commands. Help me…..

      • Yes, the commands such as Serial.println(“AT+RST”) are executing.
        If you want to see the response from the ESP8266, you need to write additional code to capture the response and as part of this additional code, you would need set up a software serial connection. The Nano and the Uno only has one hardware serial port and that is being used here to talk to the ESP8266.

        Look at the waitForResponse( ) function at http://thearduinoguy.org/using-an-esp8266-as-a-time-source/ It is waiting for OK plus carriage return plus line feed.

  2. I have been playing around with a very similar setup with very similar code. I was having some problems sending from the Arduino. I figured out that it was some problem with the number I was passing to the CIPSEND command. My hunch told me that I was miscounting the “/r/n” characters somehow. I see that you made mention of that.

    The cmd.length() would return 53 in your code(line 46), wouldn’t it? It does in mine. Since the length of cmd is only 50, the ESP8266 just sits there waiting for 3 more characters. How did you get yours to work? I don’t see you adjusting that length number anywhere.

  3. Thank’s, lets me start with a working system.
    there is however one issue I have: it runs only 10 cycles then, I have to disconnect / reconnect power.
    is it in the code or it is a buffer issue?

  4. If you connect ESP8266 to arduino nano:
    – Nano has only 1 serial. if you connect to nano Tx and Rx you must upload to nano bare minimum and conect Tx to Tx and Rx to Rx and (use serial monitor)

    – if you use softwareSerial it wil not work with 115200 (most esp are default to 115200). first you must on esp change speed and use 9600 ,upload program with softwareSerial. here you conect Tx to Rx and Rx to Tx (SoftwareSerial Serial1(2, 3); // RX | TX)
    pin 2(arduino Rx to ESP Tx) pin 3 (arduino Tx to esp Rx)
    (I lost 2 days to figure it out)

    • It will still work, but you could always use voltage divider to get voltage to 3.3V

  5. How you send 50 ? If you count \r,\n as a single, and avoiding last sequence “\r\n” it should be 48 bytes..

    “Finally we can perform an HTTP get against the server. Note the number 50 above is the total number of characters in this command. We count \r & \n as single characters and we do not count the last \r\n sequence.

    GET /status.html HTTP/1.0\r\nHost: 192.168.88.35\r\n\r\n”

    • Hi DODUTILS,

      Because 3V3 pin on Arduino can’t provide required current for ESP8266 when wifi module is connecting. Therefore you should use external LDO component that supplying from external power supply.

  6. Hi,
    I have two questions about your schematics:
    1) In theory, the Arduino Tx Pin is driven with 5V, but the ESP8266 is not 5V tolerant. Have you really tried that successfully without any level shifter?
    2) The Arduino Rx Pin is also connected to the USB-UART chip via an 1k Resistor. The connected Tx Pin of the ESP can drive up to 12mA, so I assume that the ESP can override the data coming from the USB-UART chip. Is that correct?

    • 1) Yes, some people have used shifters or some voltage dividers but I simply connected them straight to each other with no apparent issues.
      2) I am unsure as I did not try using the USB bus at the same time.

      Dave

  7. Is this code really works. I have tried this code in Arduino IDE.
    I am using arduino nano and esp01 (which labeled as Al cloud inside).
    This code only printing commands on serial monitor but not sending as a command to arduino+ esp8266 and also returns nothing. What i have to do now? This commands are printing on serial monitor without using esp module also.

  8. AT commands are executing when typing in serial monitor manually.
    But serial.println(“AT+RST”); ,
    Serial.println(“AT+CWLAP”); ………… etc.
    these commands are just printing onto serial monitor, not executing.
    Please help me.

    • I am facing the same problem…..
      Did you get any solution
      Please help me.

  9. may i know what do you mean by “power down or disconnect the ESP8266 as it can cause problems while flashing your Arduino”?do i need manually removing the power on esp8266 while performing the test?if that it is right, maybe we can put some switching transistor to disconnect the esp8266 power source at the time we need to disconnect.

    • We need to ensure the ESP does not respond (interfere) with the serial communications between the Arduino and the Computer because all three share the same serial bus.

  10. hi
    i have 3 sensor and 1 relay for control, i want 3 sensor send continous to website
    in 24hour and control ac for on and off maybe you have a solution for my problem thank

  11. Thanks! Have been trying to find examoles for two Days nome ad clear as yours.

Leave a Reply to Jon Cancel reply

Your email address will not be published. Required fields are marked *