Home / Tutorials / Arduino Tutorial / Arduino WiFi: Using ESP8266 with AT Commands
Advertisement
Arduino WiFi with ESP8266

Arduino WiFi: Using ESP8266 with AT Commands

The ESP8266 has been a blessing to makers who need to connect their Arduino to the Internet on the cheap. In fact, boards like the NodeMCU and WeMos which feature the ESP module has become overly popular.  This is unsurprising given that these boards can replace the Arduino without a steep learning curve.

So if you’re comfortable with Arduino programming and want to send or receive data via the Internet, I recommend using the NodeMCU. Actually, I have already written up various articles on how to do this:

However, if you don’t want to purchase the NodeMCU and just want to stick with the classic ESP8266 module, then this Arduino WiFi tutorial is for you.

Wiring the Arduino to the ESP8266

As you know, the Arduino UNO, Mega or Nano doesn’t have any networking capabilities. To make the Arduino connect to the Internet, it needs a modem. The ESP8266 module will act as our modem, establishing a connection to your WiFi router in order to send/receive data to/from the web.

The ESP8266 module, specifically the ESP-01 module, runs on 3.3 V. Thus, we must not connect the Arduino output pins to the ESP8266 pins directly. In the diagram above, I used a simple voltage divider to reduce the 5 V logic levels. Also note that for the ESP8266 module to run correctly, it must have its own 3.3V, 250 mA power supply. This presents another disadvantage of this setup: you must have an extra supply instead of just using your computer’s USB port.

AT Commands

Now that we’ve connected the ESP8266 module to the Arduino, it’s time to send out special commands through the Arduino’s (software) serial port. This AT or Hayes commands are a set of commands not only used by the ESP8266 but also by other modems like GSM, Bluetooth and GPRS.

The whole list of AT commands is overwhelming. For our purpose, which is to connect to the Internet, we need only a specific set of AT commands. A more thorough explanation of each command may be found on our ESP8266 AT commands list.

AT+CIPMUX = 1 Enable single (0) or multiple connection (1) to the web server. Multiple connection is a good option if you are repeatedly sending out or reading data from the Internet.
 AT+CWMODE = 3  Set WiFi mode: 1 is station mode (ESP8266 is client), 2 is AP mode (ESP8266 acts like a WiFi router where your phone or PC can connect), 3 is AP+station mode (make the ESP8266 do both)
 AT+CWJAP = “<your-ssid>”,”<your-pw>”  Connect to your WiFi. Provide your SSID name and password inside the double qoutes.
 AT+CIFSR  This returns the IP address of the module, indicating that it has successfully connected to your WiFi router.
 AT+CIPSTART=0,"TCP","www.teachmemicro.com","80”  Start TCP or UDP connection. Here, the first parameter (0) is the id of the connection, “TCP” means we’re using TCP instead of UDP, then followed by the address (or ip) of the web server then the port number.
 AT+CIPSEND=0,16  Command to tell the module data is ready to be sent. “0” here is the connection id, and 16 is the length of the data to be sent. After this command, the ESP8266 will reply with the “>” character to tell us that it will be waiting for the data to be sent. If successful, the module will reply with “SEND OK”“0” here is the connection id, and 16 is the length of the data to be sent. After this command, the ESP8266 will reply with the “>” character to tell us that it will be waiting for the data to be sent. If successful, the module will reply with “SEND OK”.
 “Data to be sent”  The string data to be sent.

Note that these commands must be sent in correct order from the Arduino to the ESP8266 module.

Arduino WiFi Sketch Example

Using the AT commands above, we can now make a simple HTTP request that will retrieve a web page from a server. Here is the full sketch:

Note that this is a very simple sketch that sends out AT commands in succession to establish a WiFi connection. We can improve this sketch further by waiting for the ESP8266 module to reply to each command, making sure that it really did receive the command.

Send Data to a Web Page

To send data to be posted on a web page, we need a PHP script that will “catch” the data sent through the ESP8266 module. There are a hundred ways to do this, but this how I implemented it:

On a server, I created a “read.php” file that contains this:

What this script does is acquire a $_POST variable value and write it on a dynamically created text file. POST is one of the ways to send data to an HTTP server. Here is an example POST request that a web browser may send:
Next, we need to make the Arduino send this POST request via the ESP8266 module. Here’s how I did it:

Sending Data via POST Request
What this sketch does is read the A0 analog port and have whatever that value is sent as a POST request. Notice that the first parts of the sketch is similar to the previous sketch I presented. Also, this sketch is not that efficient because it would take around 4 seconds to send the value.

We’re not done yet! We need another PHP file to be able to display the values sent by the Arduino:

You can name this file <whatever-you-like>.php. This page should refresh every 5 seconds, updating the value sent by the Arduino.

That's It!

Hopefully you’ve learn how to use Arduino with the ESP8266 module to connect to the Internet. Then again, I will still suggest that you use a NodeMCU if you're building a more complicated Arduino WiFi project. The ESP8266 Arduino core is very simple to use and contains a lot of example sketches that will make your work faster.

But if you still insist in using the generic ESP8266 module, there is a way to use it with the Arduino core mentioned above without using an Arduino. However, that would be a separate tutorial so hopefully you’ll come back for that.

Check Also

Arduino Keypad 4x4

Arduino Keypad: Interfacing with 4x4 Matrix

Using a keypad is an upgrade over using buttons for input on your Arduino project. …

Leave a Reply

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