Introduction: A Cheaper ESP8266 WiFi Shield for Arduino and Other Micros
Update: 29th Oct 2020
Tested with ESP8266 board library V2.7.4 – works
Update: 23rd Sept 2016
Do Not use the Arduino ESP board library V2.3.0 for this project. V2.2.0 works
Update: 19th May 2016
Rev 14 of this project revises libraries and code to work with ESP8266.com IDE plug-in V2.2
Update: 17th December 2105
Rev 11 of this project cleans up other attempted connections if it is already connected. Also uses the timeout set by the web config. Rev 10 ignored the timeout setting.
Update: 11th November 2015
This is Rev 10 of the this project. Rev 10 uses a non-blocking WiFi library, pfodESP8266WiFi, that lows for higher through put particularly for Windows Clients. It also allows for web page configuration of the Serial baud rate.
Update: 23rd October 2015
This is Rev 8 of the this project. Rev 8 has improved ESP8266 code that is more reliable.NOTE: Each packet sent halts this code until the receiver (client) acknowledges the packet. This can take between 10mS and 200mS. During that time the incoming Serial data from the UART is not being handled. The incoming serial buffer can buffer 256 bytes. At 9600 baud it takes about 270mS to fill the buffer so as long as you keep the Serial baud rate to 9600 or less you should not loose any outgoing data while the ESP8266 is sending the previous packet. This is providing you have a good WiFi connection. If the WiFi connection is poor, a packet can be lost and have to be re-transmitted by the ESP826, then the Serial incoming buffer may fill up if you are trying to send a lot of data and some of your data my be lost.
Update: 20th September 2015
This is Rev 3 of the this project. Rev 3 adds a connection time out setting to the web page configuration. If there is not send or receive of data in that time the WiFi Shield closes the connection and waits for a new one. This ensures the WiFi Shield recovers from 'half closed' connections which happen went the client just disappears due to bad wifi connection, power loss at the router or forced shut down of the client. See Detection of Half-Open (Dropped) TCP/IP Socket Connections for more details.
This connection time out defaults to 15 sec. but can be changed as needed. Setting it to 0 means never time out. When using pfodDesigner, set a menu refresh that is less than the connection time out.
Introduction
This is Rev 11 of ESP8266-01 WiFi Shield and is an alternative to the Cheap/Simple Wifi Shield for Arduino and other micros. If you only making one Wifi Shield then Cheap/Simple Wifi Shield for Arduino and other micros is the project to use as it is the simplest to wire up. However if you already have an ESP8266-01 module, you can use these instructions to make a WiFi Shield using it.
If you have one of the other ESP8266 bare modules, provided the module has GPIO0 and GPIO2 available, then you can use these instructions. If the module makes GPIO15 accessible YOU MUST connect it to GND via a resistor with a value between 3K3 and 10K.
Rev 10 does not need any additional I/O on the Arduino board, other then TX/RX and 5V power and GND. Rev 10 uses GPIO0 and GPIO2 as the ConfigLink, as described on this page, ESP8266-01 Pin Magic. Also the code sketches used in Rev10 are now exactly the same at those used in Cheap/Simple Wifi Shield for Arduino and other micros. It also replaces the 5V to 3V power supply daughter board with 3 discrete components and uses a resistor network for the five 3K3 resistors. The first version Rev 1 is here.
These instructions are also available at www.pfod.com.au.
Features
- Uses the inexpensive and readily available ESP8266-01 module :- Other ESP8266 modules can also be used
- Simple to use :- The 5V and 3.3V compatible shield acts as UART to WiFi bridge. It sets up a server on the IP and port you configure and once connected just passes data to and from the Serial connection. No libraries are need in the connecting micro, just a Serial (UART) connection, so it can be used for any micro-processor that has a serial port. It can also be modified to be configured to make a client connection (with optional login) to a remote server.
- Simple to configure :- Shorting out a link and powering up the shield, puts it into configuration mode. In this mode it creates a secure Access Point that you can connect to via your mobile or computer. Then opening http://10.1.1.1 presents a web page where you can configure your network's name and password and the IP and port number the shield should listen on for connections. The configuration web page uses HTML5 validation to check the user's settings.
For another (simpler) method for setting your WiFi network settings see ESP-01 Timer Switch TZ/DST Updatable Without Reprogramming which automatically goes into setup mode if it cannot connect to the network and has a QR code for easy connection to the setup access point.
Step 1: Parts List
This ESP8266-01 WiFi Shield needs the following parts, or similar. The prices shown here are as at 30 August 2015 and exclude shipping costs:-
WiFi Module ESP8266-01 – ~US$2.50 online (take your chances) OR for reliable product SparkFun or
Adafruit ESP8266-01 – US$6.95- Uno Protoshield – US$1.88 (or ProtoShield Basic for Arduino from Jaycar AU$4.95)
36-pin header Element14 – US$0.95 (or 4 off Solderless Headers - 10-pin Straight from SparkFun US$1.50 or 40 Pin Header Terminal Strip from Jaycar AU$0.95)
LD1117V33 3.3V regulator Element14 – US$0.67
1 off 1N5819 Schottky Diode Element14 – US$0.16 (or Jaycar AU$0.80) (Any Schottky Diode will do)
BOURNS 4606X-101-332LF RESISTOR NETWORK, 3K3 – US$0.27 (These pull-up resistors can be any value in the range 3K3 to 10K) you can also just use 5 x discrete 3K3 resistors instead as in Rev 1 e.g. 3K3 resistors – Digikey – US$0.52 (or 3K3ohm 1/2 Watt 1% Metal Film Resistors – Pk.8 from Jaycar AU$0.55)
1 off 330R resistor Element14 US$0.05 OR Sparkfun Resistor 330 Ohm 1/6 Watt PTH - 20 pack US$0.95 (or 330ohm 1/2 Watt 1% Metal Film Resistors – Pk.8 from Jaycar AU$0.55)
1 off 0.1uF capacitor Element14 – US$0.21 OR Sparkfun US$0.25
1 off 10uF capacitor Element14 – US$0.11 OR Sparkfun US$0.45
Total Cost ~$6.80 + shipping (as of August 2015) OR ~US$11.25 using Sparkfun or Adafruit ESP8266-01 module
To program the shield with the push button configuration and UART to WiFi bridge program, you also need a USB to Serial cable. Here a SparkFun's USB to TTL Serial Cable (US$9.95) is used because it has nicely labelled ends and has driver support for wide range of OS's, but you can also use Adafruit's USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi which is the same price.
Including the programming cable, the cost for just one WiFi Shield is ~US$16.75. A quick search finds Arduino WiFi Shields costing a minimum of US$30 up to over US$70. So even including the once off cost of the programming cable this shield is cheaper than the other available shields, as well as being much easier to configure and use.
Step 2: Construction
The above schematic (ESP8266_01_WiFi_Shield_R2.pdf) shows the arrangement of the parts needed for this shield. There are only six components, in addition to the ESP8266-01 module.
The 1N5819 diode protects the ESP8266-01 RX input from the micro-processor's 5V outputs. The 330ohm (R6) resistor provides protection against shorting out the ESP8266-01 TX output, if the micro-processor's D1 is accidentally made an output. Some sort of 3V3 supply is needed. The Arduino UNO's 3V3 pin is not strong enough to supply the ESP2866 module. Here a three terminal 5V to 3.3V regulator LD1117V33 is used. The 10uF capacitor is need to stabilize the LD1117V33 regulator, so it is mounted as close as possible to the regulator output.
Here are the top and bottom views of the completed board.
The top of the board looks clean. The bottom of the board is a bit of a rats nest.
Make sure you carefully check the wiring when you are finished, particularly the wiring to the pins of the ESP8266-01 and the LD1117V33 three terminal regulator. It is easy to wire to the wrong pin when you turn over and wire from the bottom. The regulator is mounted upside down to keep the metal tab, which is electrically connected to the output pin, tab away from the board pins.
Step 3: Programming the WiFi Shield
The WiFi Shield needs to be programmed once, only, and never again, with the web page configuration and the Serial to WiFi Bridge code.
To program the shield, follow the steps given on https://github.com/esp8266/arduino under Installing With Boards Manager. When opening the Boards Manager from the Tools → Board menu and select Type Contributed and install the esp8266 platform. This project was compiled using the ESP8266 version 1.6.4-673-g8cd3697. Later versions well be better but may have their own bugs as the platform is evolving rapidly.
Close and re-open the Arduino IDE and you can now select “Generic ESP8266 Module” from Tools → Board menu.
You also need to install the latest version of pfodESP2866BufferedClient.zip This library works with ESP8266.com IDE plug-in V2.2. If you have previously installed the pfodESP2866WiFi library, delete that library directory completely.
- Download this pfodESP2866BufferedClient.zip file to your computer, move it to your desktop or some other folder you can easily find
- Then use Arduino 1.6.5 IDE menu option Sketch → Import Library → Add Library to install it. (If Arduino does not let you install it because the library already exists then find and delete the older pfodESP8266BufferedClient folder and then import this one)
- Stop and restart the Arduino IDE and under File->Examples you should now see pfodESP8266BufferedClient.
Setting the Configuration Access Point password.
After you have installed the pfodESP8266BufferedClient library, open the Arduino IDE and copy this sketch, ESP8266_WifiShield.ino, into the IDE. Before you program the shield, you need to set your own password for the configuration access point.
In configuration mode, the WiFi Shield sets up a secure Access Point called pfodWifiWebConfig with a password contained in a QR code attached to the shield. This secure connection prevents anyone listening in on your connection while you are setting your real network's ssid and password. You should generate your own password for your shields. A SecretKeyGenerator java program is available here which generates random 128bit keys and writes out QR.png files. Another alternative is to use QR Droid Private (from Google Play) to create a QR Code for your own chosen password.
In either case you need to update the #define near the top of the sketch with your own password.
// =============== start of pfodWifiWebConfig settings ============== // update this define with the password from your QR code //http://www.forward.com.au/pfod/secureChallengeResponse/keyGenerator/index.html #define pfodWifiWebConfigPASSWORD "b0Ux9akSiwKkwCtcnjTnpWp"
You can also set your own configuration Access Point name, if you wish.
Programming the Shield
To program the shield, remove it from the Arduino board, short out the FLASH_LINK (shown here with a blue shorting link in the middle of the board) and connect the USB to Serial cable as shown in the photo. Check the photo and your wiring.
The RX lead connects to D0 and the TX lead connects to D1. The VCC (+5V) connects to the 5V pin and GND connects to the GND pin on the shield. Short out the FLASH_LINK as shown above. The photo above is for the SparkFun USB to Serial cable. If you are using the Adafruit cable, it does not have the terminals marked but is colour coded, red is power, black is ground, green is TX and white is RX.
Carefully check the VCC and GND connections as it is easy to short out the USB power supply if you are one pin off.
Then plug in the USB cable to your computer to power up the ESP8266-01 in programming mode. Select its COM port in the Tools → Port menu. Leave the CPU Frequency, Flash Size and Upload Speed at their default settings
Then select File → Upload or use the Right Arrow button to compile and upload the program. Two files are uploaded. If you get an error message uploading check your cable connections are plugged in the correct pins and try again. Once the programming is completed, remove the shorting link from FLASH_LINK.
Attaching the Configuration QR code
You will need your unique configuration access point password each time you need to configure the shield, so it is convenient to attach it as a QR code to the shield (or its case). Here is the Open Office presentation file that was used to print out the QR code and connection details for this project. Replace the QR code and password text with your own unique one to complete the shield.
Step 4: Configuring the WiFi Shield
Any WiFi shield needs to be configured with the network name and password of the local network. It also needs to be given an IP and port number to listen on for connections. All other WiFi shields have the IP and port no hard coded in the sketch and either hard code the network name and password or use a proprietary method with proprietary apps to connect to the local network. This is very restrictive when you have multiple devices in an evolving environment. This WiFi Shield uses an open source web page method to configure both the network name and password, and the IP address and port No.
The ESP8266-01 has a very limited number of available outputs, just GPIO0 and GPIO2. In this design, after powering up, the code in the ESP2866-01 checks if GPIO2 is grounded and if so sets the ESP8266-01 in config mode. However the grounding of the GPIO2 input must be delayed until after theESP8266-01 has finished powering up. If GPIO2 is grounded during power-up the ESP8266-01 module does not start up normally. This delay in grounding GPIO2 is achieved by using GPIO0 as the ground. After the ESP8266-01 starts up, the setup() code makes GPIO0 an output and sets it LOW. This will then ground GPIO2 if the CONFIG_LINK has been shorted out.
The first version of this project (Rev 1), used an extra Arduino digital I/O to do this grounding, which required extra code in the Arduino sketch. Rev 2+, removes the need for any extra code in the Arduino sketch, other then a short delay at the top of setup() to ignore the ESP8266's debug output.
To test out configuring the ESP8266-01 WiFi Shield, just plug it into an Arduino board, short out the CONFIG_LINK (blue shorting link in the left of the picture) and apply power to the Arduino board.
In this config mode the ESP8266 module sets up a secure access point with the name pfodWifiWebConfig. This access point will show up on your mobile and on your computer. To connect to this access point you will need to enter the unique password for your shield. You can type the password in by hand but it is easier and more reliable to scan the QR code you previously attached to your shield, using a QR scanner app, such as QR Droid Private
Then copy and paste the password into your mobile's WiFi setting screen to connect your mobile to the configuration access point.
Then open a web browser and type in the URL http://10.1.1.1 This will return the configuration web page.
The WiFi Shield automatically fills in the Network SSID with the local network with the best signal strength. Which will usually be the one you want. If not just overwrite that entry. You must enter a Network SSID and password and portNo. The IP address field is optional. If you leave it blank, the WiFi Shield will use DHCP to get its IP address on your local network. It is often easier to specify a specific IP address so you can easily connect to this shield.
Rev 10 also allows you to configure the Serial baud rate for this shield. Default is 19200, but the examples here use 9600 so change the baud rate to 9600.
If your browser is HTML5 compliant then the web page will validate the input before sending it.
When you click the Configure button, the WiFi Shield will process the results and store them in EEPROM and then display a response page, like the one above, telling you to power cycle to connect to your network.
Step 5: Using the WiFi Shield
In a complete project, you would mount a momentary push button on the outside of your project's box connected to the CONFIG_LINK, and instruct the user to press the push button and then power up the device to get into config mode. The code you loaded into the ESP8266-01 also drives the ESP8266's GPIO0 pin LOW when the module is in config mode, so you can connect a 270ohm resistor and LED between the 3.3V rail and GPIO0 and mount the LED on the outside of the box, to indicate to the user that they are in config mode.
Rev 10 also allows you to configure the Serial baud rate for this shield. Default is 19200, but the examples here use 9600 so change the baud rate to 9600 on the config webpage, above.
As mentioned above any sketch you load into your Arduino, or other micro-processor, needs to a short delay to skip the debug output from the ESP8266 module. Other than that, to receive and send data via WiFi, from your sketch, you just read and write to your serial port (connected to D0,D1) at 9600 baud. So to ignore the ESP8266's debug output add to a short delay at top of the setup() method
void setup() { delay(1000); // wait here for a second let ESP8266 complete powering up // this also skips the WiFi Shield's debug output on power up // before starting the Serial connection. .... other setup code here
The example here uses an Arduino UNO but you can use any micro-processor, either 5V or 3.3V based that has a UART. If you use a 3.3V micro-processor, you will need to supply 5V to the WiFi Shield's power supply. This 5V will also be connected to the shield's 5V pin, so you need to check that this is acceptable for the micro you are plugging the shield into.
As a test of this shield, pfodApp was used to turn the Uno's LED on and off via WiFi. First the pfodDesigner was used to design a simple menu.
NOTE: The latest version of pfodApp sends keepAlive msgs so that the wifi shield will not time out
Then the code was generated for the Serial connection at 9600 baud and transferred the file to the PC, using wifi file transfer.
The sketch's setup() did not need to have the delay(1000) added because the pfod parser ignores any characters outside { }, but it was include because it is recommended for this WiFi board.
The complete sketch, ESP8266_UnoLedControl.ino is here. Note there is no special WiFi code, the sketch just reads and writes to the Serial output.
Remove the WiFi Shield, select Tools → Board → Uno in the Arduino IDE and program this sketch into the UNO. NOTE: you must remove the WiFi shield to program the UNO because the USB is connected to the UNO's TX/RX pins.
Plug the WiFi Shield back in, it will automatically connect to your local network and start a server on the port you configured. In pfodApp you can set up a connection for this device. See pfodAppForAndroidGettingStarted.pdf for the details.
Then connect to turn the Uno's LED on and off from your Android mobile via wifi.
That's it finished!!
Step 6: Extensions for the WiFi Shield and Conclusions
Adding Client Support
As presented here the WiFi shield can be configured to run as a server listening on a specified IP and port No. However the pfodWifiConfig also provides support for storing and retrieving Client settings as well as Server settings. So by adding these fields to the configuration web page and saving/loading the Client values, you can also use this WiFi Shield to connect to a remote server, with a client username and password, and upload data to there.
Adding external Configuration Push Button and Led
As mentioned above, in a real application you would mount a momentary push button on the outside of your project's box connected to the CONFIG_LINK, and instruct the user to press the push button and then power up the device to get into config mode. The code you loaded into the ESP8266-01 drives the GPIO0 pin LOW when the module is in config mode, so you can connect a 270ohm resistor and LED between the 3.3V rail and GPIO0 and mount the led on the outside of the box, to indicate to the user that they are in config mode.
Conclusion
This Rev 2 of the ESP8266-01 WiFi Shield uses the cheap and readily available ESP8266-01 module. Other ESP8266 modules can also be used.
Once programmed you never need to program it again to set or change the network settings. They can all be set via a web page on a secure temporary WiFi network.
It is simple to interface to any micro that has a UART and works with on both 5V or 3.3V micro-processors.
No libraries are required to connect to this shield. It runs as a simple Serial to WiFi bridge.