We start with an intro to the required hard- and software. Using an ESP8266 as a wifi shield (instead of as a stand alone board) is considered 'tricky', most likely because of issues that arise from the hardware setup given the many different versions of seemingly similar hardwares and the interaction between these diverse parts. We will therefore discuss in detail which hardware to use and how to use it correctly to get online as fast as possible.
Please note: The choice of Arduino Mega is deliberate since it is much easier to use it in combination with an ESP8266 compared to using an Arduino UNO! Using and UNO poses additional challenges that are not covered sufficiently in this tutorial!
This tutorial works with Mac as well as with Windows!
The following steps are necessary:
- Power and connect the ESP8266 correctly for loading ("flashing") the firmware onto it
- Load ("flash") the firmware to the ESP8266
- Connect the ESP8266 to the arduino
- Customize the ESP8266 wifi sketch
I assume you have the Arduino IDE installed - plenty of tutorials around. Please note we will NOT be using the ESPtool and therefore also not require Python, pySerial etc. as used by many tutorials out there are not necessary.
These three steps above is all you need - to each of these steps there are numerous ways to get it done and googling will open up more routes than the ones described in this tutorial for you and I encourage you to read around and tinker with different setups. However this tutorial is aimed at stripping it down to the essential and giving you one working way to making it work.
1. Hardware1.1. ESP8266
There are a large number of ESP development boards out there. While more sophisticated boards will provide you for a few euro more with more (up to 16) IO pins, USB ports, buttons that allow for easier flashing etc., the process for setting them up as a wifi shield remains essentially the same.
I used an ESP8266-01 (which seem to be the 'essential' ESP board). However, beware that even within the ESP8266-01s there are different versions as explained in this very recommendable tutorial.
The older version is harder to work with, has less memory and is therefore less recommendable. I have seen it usually with the "AI-Cloud inside" print. It also can be recognized by having two LEDs on the board (one for power one for data), the new version only has one (larger) LED (the LEDs are visible in the pictures below to the right of the long horizontal antenna-bar.
Beware: The older versions do not tolerate 5V on the input pins - however 5V is the IO output voltage of Arduino boards. Older versions died on me instantly and require a voltage splitter that brings down input signals to 3,3V.
Recommendation: Try to find a newer version, which is the version with only one LED - not the older two LED version. It has more flash memory and most importantly it seems to be resilient to 5V on the input pins! I have not had any problems feeding 5V inputs from the Arduino to it.
1.2. Arduino
Arduino Mega seems to be the most trouble free option given the the 3,3V pins of the Mega will supply sufficient current to the ESP.
Beware: Arduino Nano and Uno 3,3V pins do not seem to provide sufficient current for the ESP2866 to work.
Recommendation: If possible use a Mega. I actually have not been able to make the Uno work and had no problems with the Mega.
1.3. Recommended: USB to ESP8266 Wifi Module Adapter / FTDI Serial TTL-232 USB
You will need to connect your ESP to your commputer for bootloading the required firmware.
One way of powering the ESP for bootloading seems to be the use of the Arduino as described in some tutorials. However this did not work for me.
What I can recommend is the use of either the Chip CH340 or the Chip FT232RL - both worked very well for me. Both do not cost the world and they are readily available on the web. Googling for example "USB to ESP8266 Wifi Module Adapter", or "FTDI Serial TTL-232 USB" will get you there.
Alternative: Using the Arduino as USB to Serial converter
The above described cable or adapter it not absolutely necessary, as an alternative you can go the Arduino route at described here. However I have not been able to make it work for me!
1.4. Optional (purely for comfort)
If you want a truly comfortable setup buy one of these ESP8266 breadboard adapters, it will make your life easier. I resorted to building a makeshift breadboard adapter with some Arduino headers I had lying around.
Wiring diagram for bootloading:
These setups will put the ESP8266 automatically in bootload!
I built a 'permanent' setup using a breadboard and jumper wires that allow me to flash ESPs:
The picture above shows two alternative options to connect the ESP8266 to your computer (USB to ESP8266 Wifi Module Adapter and FTDI Serial TTL-232 USB). Either is fine.
3. Load ("flash") the desired firmware to the chip3.1. Download the firmware
The firmware we use is the following - download here and leave it in your download folder or move it to a desired folder.
Should the download link above not work directly go to the developer site (which is in Russian), scroll down the page and download the following file:
file will you downloaded is called AT22SDK10020150320boot12.bin
3.2. Install nodemcuflasher software
We use nodemcuflasher for flashing the firmware (I found this by far the easiest. Installing python and pyserial in order to use the ESP tool as proposed by a number of tutorials for this purpose is not required and in my opinion too complicated - especially when working in Windows) Download and install nodemcuflasher.
Using it is extremely easy. First make sure you connected the ESP for booloading mode as described above.
3.3. Open nodemcuflasher and choose the right port
In the drop down there might be several selections. If you are unsure you can compare when plugging / unplugging your ESP setup which port appears.
3.4. Choose the firmware you just downloaded
In the nodemcu config you can choose the AT22SDK10020150320boot12.bin file in the folder you saved it to. Leave the 'offset' drop down on the right at 0x00000.
3.5. Advanced settings (baud rate)
I was successful with the following settings.
3.6. Return to the "Operation" tab and press "flash":
Once the program connects successfully the IP address of the chip will appear and the progress bar will activate - the green tickmark will indicate a successful loading of the firmware.
It will take a few minutes and show a green tickmark at the bottom left once successfully done. Congrats - you just flashed a new firmware to your ESP!
When using an Arduino Mega Life is easier since the Mega provides sufficient current on the 3,3 V pin for the ESP8266.
Caution / note: The above wiring scheme without logic level shifter will only work and not destroy the ESP with newer models. Should you have an older version you need to use or build a voltage divider or include a logic level shifter to reduce the voltage between the Arduino TX and the ESPs RX PINs down to 3,3V. The RX-connection does not need this (since only the Arduino TX is sending 5V).
Note: When using an Arduino UNO (not recommended) additional challenges need to be dealt with. The ESP8266 runs on 3,3 V and can draw up to 170mA of current (see the Expressif website https://bbs.espressif.com/viewtopic.php?t=133) - however the Arduino Uno 3,3V pin is rated at 50mA (see here https://store.arduino.cc/arduino-uno-smd-rev3). It therefore is recommendable or most likely required to power the chip with an external 1A power source. If this is the desired setup a common ground needs to be established! Please note I cannot comment on how this is properly done.
5. Customizing the ESP8266 wifi sketchI won't go into installing the Arduino IDE here.
I used the setup with Blynk. The sketch we need can be found on the Blynk website (the code concerning Blynk can be omitted if you are connecting to something else):
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h> // <= delete when not using Blynk
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXX"; // <= insert 'your' token in case you are connecting with blynk. Otherwise delete
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXXX"; // <= your wifi network ID and password
char pass[] = "XXXXXXXX";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1 // <= when using Mega use the 'hardware' Serial
// or Software Serial on Uno, Nano...
/*#include <SoftwareSerial.h>
SoftwareSerial EspSerial(0, 1); // <= when using Uno: RX / TX set used PIN
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(115200);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass); // <= delete when not using Blynk
}
void loop()
{
Blynk.run(); // <= delete when not using Blynk
}
The following needs to be customized (see comments in the code above)
- Credentials of your wifi network
- Choos hardware serial (for Mega) or software serial (e.g. for Uno). In case of the latter check the correct PINs for RX and TX are specified
- If using Blynk: Insert your authentication token (see Blynk instructions)
Load the sketch onto the Arduino and open the Serial monitor. After 10-20 sec you should read: "Connected to Wifi".
The Arduino is now connected to wifi. You can now integrated the above sketch with your programs and connect your Arduino with an IOT platform (Blynk for example).
Congratulations!
Comments