With the current demand for Internet of Things (IoT), there is a need to connect microcontrollers like the Arduino to the Internet for data exchange. One convenient way of doing this is through Wi-Fi. A popular option for connecting the Arduino to the Internet is a Wi-Fi module known as the ESP8266, which comes with its own microcontroller—meaning it can be programmed like an Arduino and used in tandem with it. The instructions in this post will apply to the ESP8266-01 Wi-Fi module.
The module comes out of the box with firmware that allows it to respond to AT commands. This option only needs minimal setup but requires that the module be used with another microcontroller. The extra microcontroller will send the AT commands to the Wi-Fi module and will process its responses. The other option is to program the module directly because it has an onboard microcontroller. This can be done with nodemcu firmware which allows you to program the module in Lua and provides a Node.js-style API. This post will focus on programming the ESP8266 with the Arduino programming language using the Arduino IDE.
There are two ways to connect the Wi-Fi module to a PC in order to flush a program onto it. The simpler of the two is to connect it to your PC via an FTDI or a USB to serial board. The second option is to connect it through the Arduino.
Before making any connections, it is important to note that the ESP8266 module works with 3.3V, unlike the Arduino which uses 5V. It is therefore important to ensure that anything you connect to the module is working at 3.3V, or has its voltage stepped down with a logic level converter or a voltage divider (because connecting the module to 5V may damage it). Next, you need to identify the pins on the Wi-Fi module. The image below provides the names of the pins (pinout) on the board.
If you are using a USB to serial board, all you need to do is connect the Wi-Fi module to it as shown in the diagram below. The TX of the Wi-Fi module goes to the TX of the USB to serial board, and the RX goes to the USB to serial board’s RX. Ground and VCC should also be connected in a similar fashion. Finally, connect GPIO0 to ground and CH_PD to VCC to put the module in the right mode for flashing programs.
However, if you’re using the Arduino board in place of the FTDI, you must first ensure that you are connecting the VCC pin of the module to the 3.3V source on the Arduino, and that the GND goes to the Arduino’s ground. Next, connect the TX pin of the Wi-Fi module to the Arduino’s RX and the RX pin of the module to the Arduino’s TX. This might seem counterintuitive; however, this will ensure that the data transmitted from the Arduino will be relayed to your computer and vice versa. Your connections should look something like the image below. As mentioned previously, you should ensure that the TX and RX pins are connected through a logic-level shifter or a voltage divider. Finally, connect the Arduino’s reset pin to ground to deactivate the Arduino’s microcontroller. You can also remove the microcontroller chip (the big long chip) altogether if your board isn’t using the surface-mounted version.
Flushing a New Program
Now that you have the Wi-Fi module connected properly, connect the Arduino board or the FTDI board to your PC and open the Arduino IDE. Click on Tools in the menu and select the port under Ports, then open the serial monitor. Assuming your board is fresh out of the box, the baud rate will likely be set to 9600 or 115200, and will respond to AT commands. To test this, set the correct baud rate for your board, set the line ending to newline and carriage return, and hit Enter after typing AT. You should see OK in your serial monitor if your board is set up for AT commands and working properly.
If you don’t get any response, you may be using the wrong baud rate or your module may not be running firmware that supports AT commands. If you did not see OK after hitting Enter, try different baud rates. Check your connections to make sure they are correct or contact your supplier to find out which firmware your Wi-Fi module is running.
If everything worked as stated above, you are now ready to program the Wi-Fi module with the Arduino programming language.
To set up the Arduino IDE for the ESP8266 module, click on File -> Preferences and paste the following link [http://arduino.esp8266.com/stable/package_esp8266com_index.json] in the “additional boards manager URL” box, and click OK. Next, click on Tools -> Boards -> Boards Manager and wait for it to finish loading. ESP8266 should now be available in the Boards Manager. Click on Install to install it and select generic ESP8266 from the drop-down in Tools -> Boards and you’re ready to flush the module with a new program. You’re now set to communicate with the Internet with your Wi-Fi module. After flushing the module with a new program, connect VCC to 5V and restart the module to start running the new program.
Note: the Wi-Fi module will not respond to AT commands after flushing a new program.