Presentation of the ESP8266 WiFi module created for the course Pervasive Systems 2016 of the Master Degree in Engineering in Computer Science (DIAG, University of Rome "La Sapienza")
Pervasive Systems 2016 Web Site: http://ichatz.me/index.php/Site/PervasiveSystems2016
LinkedIn Profile: https://www.linkedin.com/in/biagio-botticelli-444b87105?trk=hp-identity-name
GitHub Repository: https://github.com/biagiobotticelli/ESP8266
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
Adafruit Huzzah Esp8266 WiFi Board
1. Master Degree in Engineering in Computer Science
Dipartimento di Ingegneria Informatica Automatica e Gestionale "Antonio Ruberti"
University of Rome - “La Sapienza”
Individual Project of Pervasive Systems
Biagio Botticelli 1212666
A.Y. 2015/16
Adafruit Huzzah ESP8266
Wi-Fi Board
2. Contents
1. Introduction to ESP8266 family
2. Adafruit Huzzah ESP8266 WiFi Board
3. Programming ESP8266
4. Example of Application
ESP-01
Adafruit Huzzah ESP8266
2
3. Introduction to ESP8266
The ESP8266 is a System-on-a-Chip (SoC) with a full 2,4 GHz WiFi front-end
and with integrated TCP/IP protocol stack with DNS support.
First version ESP-01 (begin 2014): it allows micro-controllers to connect to a
Wi-Fi network and make simple TCP/IP connections using Hayes-style
commands.
After few time…
• Multiple hardware versions;
• Official SDK (October 2014);
• Custom Firmwares;
3
Intro HuzzahESP8266 Programming Example
5. ESP8266: Hardware
• 32-bit CPU running at 80 MHz
• 64 KB of boot ROM
• 64 KB of instruction RAM
• 96 KB of data RAM
• External Flash Memory: 512 KB to up 4 MB
• 2,4 GHz Wi-Fi 802.11 b/g/n
• WEP, WPA/WPA2 Authentication or Open Networks
• 16 GPIO pins
• SPI, I2C
• I2S interfaces with DMA (sharing pins with GPIO)
• UART on dedicated pins + 1 transmit-only UART on GPIO2
• 1 10-bit ADC
• 3 PWM
5
Intro HuzzahESP8266 Programming Example
6. Why Adafruit Huzzah ESP8266?
WARNING! The ESP8266 operates at 3,3 V!
Most of the first versions do not have an
onboard voltage regulator…
Not breadboard friendly…
The Adafruit Huzzah ESP8266 is one of the
last versions of the module that make
easier to work with.
• Integrated high-current capable 500 mA /
3,3 V regulator;
• Breadboard friendly.
6
Intro HuzzahESP8266 Programming Example
7. Why Adafruit Huzzah ESP8266?
Small Dimension: 25x38x5 mm
Very low cost board!
7
Intro HuzzahESP8266 Programming Example
8. Adafruit Huzzah ESP8266
Power Pins
• V+: input/output 5V voltage;
• VBat: input voltage from Battery;
• GND: ground;
• 3V: output 3,3V voltage;
Serial Pins
• TX: output from the module (3.3 V);
• RX: input into the module (5 V);
8
Intro HuzzahESP8266 Programming Example
9. Adafruit Huzzah ESP8266
GPIO Pins
There are 9 GPIO pins:
#0, #2, #4, #5, #12, #13, #14, #15, #16.
GPIO pins are 3,3 V logic level in/out
and can be used for any sort of input
or output signal.
9
Many have special functionality:
• #0: used to determine when to boot into the bootloader.
If the pin is held LOW during power-up it will start boot-loading.
• #2: also used to detect boot-mode.
• #15: also used to detect boot-mode.
• #16: used to wake up from sleep mode. To awake, connect it to the RESET pin.
Intro HuzzahESP8266 Programming Example
10. Adafruit Huzzah ESP8266
Analog Pin
• A: analog input.
Other Control Pins
• LDO: enable pin;
Easy way to cut power OFF:
when connected to GND, it will
turn off the 3,3 V regulator.
• RST: reset pin (5 V);
When connected to GND, it will
momentarily reset the ESP8266.
• EN (CH_PD): reset pin (3,3 V only).
When connected to GND it will
momentarily reset the ESP8266.
10
Intro HuzzahESP8266 Programming Example
11. Programming ESP8266
In order to make them work properly, remember to install the correct driver!
USB-to-Serial
To establish a communication with Adafruit ESP8266, we need an USB to
Serial converter which will provide the connectivity between USB port of our
PC/Laptop and Serial UART interface of the board.
USB-to-TTL Cable FTDI Cable FTDI Breakout
SparkFun
FTDI Basic 5V
11
Intro HuzzahESP8266 Programming Example
12. Programming ESP8266
Firmwares
Different options for firmware are available: each one allows to program the
chip in a different way.
1. AT+ Command Processor (default for ESP8266)
2. Custom Firmware (using Arduino IDE to upload);
3. Node MCU Lua (default for Adafruit Huzzah ESP8266);
4. esp-open-SDK (direct programming on ESP8266);
5. Firmware by Espruino community (Javascript);
6. Firmware by MicroPython community (Python);
Only one firmware at a time could be loaded in ESP8266!
12
Intro HuzzahESP8266 Programming Example
13. Firmware: Installation
To load the firmware in the chip, it’s needed to use:
• Flashing Tool: esptool, a Python-based cross-platform ESP8266 flashing tool;
• Image of Firmware: any image of firmware to install.
Requirement: the programmer must be able to use esptool.
In order to make esptool working, pySerial must be present in the system.
Supposing that esptool and pySerial are correctly installed and the firmware image is
available, it’s possible to begin the installation of the firmware on the module.
The ESP8266 must be in Bootloader Mode! Otherwise, flashing process will not work!
The command for the installation of the firmware using esptool is:
sudo esptool.py --port /dev/tty.device_name write_flash 0x00000 /path_to_firmware_image
• /dev/tty.device_name: name assigned by the OS to the ESP8266 device;
• /path_to_firmware_image: location of the image of the firmware.
13
Intro HuzzahESP8266 Programming Example
14. AT+ Commands
Maybe the quickest way to get started with the ESP8266!
It allows ESP8266 to process AT+ Commands received over Serial UART interface.
Pros:
• useful for beginners: the user does not need not be familiar with any programming language
or framework to use it; he can simply send a series of commands to achieve his goal.
Cons:
• USB-to-Serial Connection: the module must be always connected to the computer.
• Low-Level Programming: AT+ Commands operate at very low level.
There are different ways of sending serial
messages over the computer's USB.
The most used are:
• Serial Terminal Softwares:
• PuTty for Windows;
• CoolTerm for Windows/MacOS/Unix;
• Arduino IDE's Serial Monitor.
14
Intro HuzzahESP8266 Programming Example
17. NodeMCU Lua
NodeMCU Lua is the most popular alternative firmware to AT+ Commands!
NodeMCU runs an interpreter able to execute commands in the Lua scripting language.
• Memory Storage: store the
code in flash memory and
instruct the module to run
the application every time it
restarts!
ESPlorer: NodeMCU tool
17
Pro:
• Good for developers: environment that allows to run commands for controlling
both board's WiFi interface and GPIO functionalities.
Cons:
• Lua Language: not very
spread with lack of
official documentation;
Intro HuzzahESP8266 Programming Example
18. Arduino IDE
Whereas the Adafruit Huzzah Board is pre-programmed with the NodeMCU
firmware, it’s possible to replace it with a custom firmware using the Arduino IDE.
WARNING! This method erases the previous firmware from the ESP8266's flash!
(To come back to initial configuration of the board, there would be executed the steps described before)
Maybe it’s the easiest way to design a custom firmware for the ESP8266 module!
It would be the best choice for those
users which want to use the latest
versions of ESP8266 that allows to use it
as a regular micro-controller.
18
Intro HuzzahESP8266 Programming Example
19. Arduino IDE
The Arduino IDE will not initially recognize the ESP8266!
ESP8266 in the Arduino IDE
• Open Preferences window of the Arduino IDE;
• Enter in the Additional Boards Manager URLs field the URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
• Go in the Boards Manager and install the last release for esp8266 module by the
ESP8266 Web Community;
• Restart the Arduino IDE;
• After restarting the Arduino IDE, we can find a specific menu of the different
models of ESP8266 boards.
• Selecting the model of the board, the IDE will automatically set the correct
parameters in order to make feasible the USB-to-Serial communication.
If all the previous configuration steps are been made correctly, we
are now ready to write our custom firmware with the Arduino IDE.
19
Intro HuzzahESP8266 Programming Example
20. Example
Steps:
1. Build the hardware circuit on which the application is based;
2. Develop the custom firmware with the Arduino IDE, that makes the ESP8266
board the core of the project;
3. Use Blynk app for iOS/Android to implement the remote control;
Idea: Create an application for the remote
control of the temperature/humidity in the
room with the possibility to turn on/off the
light by using the smartphone.
20
Intro HuzzahESP8266 Programming Example
22. Example: Blynk Front-End
22
Blynk: IoT Control User Interface for ESP8266 module
Intro HuzzahESP8266 Programming Example
User Interface:
• Switch for the Remote Control of
the light;
• Gauge for the representation of the
Temperature of the room;
• Gauge for the representation of the
Humidity of the room;
26. Useful Links
Useful Links:
• ESP8266 Datasheet
• ESP8266 Community Forum
• AT+ Instruction Set
• NodeMCU Web Site
• NodeMCU Flasher
• SparkFun
26
Homepage of the course: Pervasive Systems 2016
These slides are available on Slideshare
Email Address: botticelli.1212666@studenti.uniroma1.it
GitHub: biagiobotticelli/ESP8266
LinkedIn: Biagio Botticelli
• Arduino/Genuino IDE
• Arduino Core for ESP8266
• esp-open-SDK
• esptool Flashing Tool
• Blynk
My presentation will follow four basic steps:
The first step is an introduction to the ESP module family;
The second one is a description of the features of the Adafruit Huzzah, that is a particular version of the module that Ioannis
give it to me to work with;
The third is an introduction on how it’s possible to program the chip ESP12 of the board with the different firmwares that are been introduced;
The last step is the description of one of the possible application that could be developed by using this module.
As example, I decided to develop an application using the software Blynk (that will be presented in one of the next lectures by my colleague Davide Meacci) that allows the remote control of a room by my iPhone checking the temperature and humidity, and giving me the possibility to turn on off the light.
The ESP8266 System-on-a-Chip produced by Espressif is an 80 MHz chip with a full WiFi front-end (both as client and access point) and with an integrated TCP/IP protocol stack with DNS support.
The first version of the module, the ESP-01 was introduced in early 2014: this small chip allows micro-controllers (such as Arduino) to connect to Wi-Fi and make simple TCP/IP connections using Hayes-style commands.
We will see in the next slides some examples of AT+ Commands…
Now, multiple versions of the module are being made available because after few time, the extremely low price and the potentialities of the chip had attracted many developers to built more usable hardware versions together with a more advanced software environment.
In late October 2014, Espressif has released a Software Development Kit (SDK).
This innovation allows the module to be programmed without the use of an additional micro-controller.
In this slide we can see a summary table of the ESP family.
As we can see, many versions are being introduced (from the initial ESP01 to the last ESP-14)…
For this project, I worked with an Adafruit Huzzah ESP8266 Development Board that has an ESP-12 version of the chip.
Let’s see some hardware features of the module…
ESP8266 employs a 32-bit CPU running at 80 MHz.
It has 64 KB boot ROM, 64 KB instruction RAM and 96 KB data RAM. External flash memory can be accessed through SPI.
It’s a system-on-a-chip with 2.4 GHz Wi-Fi (supporting WPA/WPA2).
It has 16 General-Purpose Input/Output (GPIO):
Serial Peripheral Interface(SPI), Inter-Integrated Circuit (I²C), Integrated Interchip Sound (I²S)
Analog-to-Digital Conversion (10-bit ADC), UART (on dedicated pins, plus a transmit-only UART can be enabled on GPIO2),
Pulse-Width Modulation (PWM).
The ESP8266 operates at 3,3 V and may require up to 170 mA of current.
Thus, we would need a 3,3 V regulator, since the micro-controller pins typically have 5 volt logic and most of the first versions of the ESPs don’t have an onboard regulator.
If we would use ESP with 5V… We could seriously damage it!
Thus, in order to make them work, we would need to create an additional Voltage Regulator or to buy an additional adapter.
The Adafruit Huzzah ESP8266 is one of the newer versions of the module.
For the power supply it has an integrated high-current capable 500 mA / 3,3 V regulator on the board; so we have not to buy a regulator and it’s is also breadboard friendly.
This is very useful if we want to use it in a dedicated circuit…
Then…
As you can see… It’s very small, so it does need much space to place it in a circuit!
And it’s very cheap with respect to other WiFi micro controllers such as the Arduino Wifi Shield.
Now, let me introduce how the board really works…
The board has 24 Pins in total:
7 Power Pins, 4 Serial, 9 GPIO, 1 Analog and 3 Control Pins.
For Power Pins, there are 3 inputs for the regulator: 2 V+ and 1 VBat.
The V+ pin is on the FTDI/Serial header and in the left side of the board.
The Ground pins are 3: 2 are on left and right side of the board (1 per side) and 1 is on the FTDI/Serial header.
There's also a 3.3 V output from the regulator available on the 3V pin.
There are 4 Serial Pins: 2 RX and 2 TX that are the serial control and bootloading pins.
They are the means for communicating with the ESP module.
The TX pin is the output from the module and is 3.3 V logic.
The RX pin is the input into the module and is 5V compliant.
The pins are available in 2 places: one set is on the right side of the board and the other one is on the FTDI/Serial header.
GPIO Pins
There are 9 GPIO pins:
#0, #2, #4, #5, #12, #13, #14, #15, #16.
All GPIO are 3,3 V logic level in and out, and are not 5 V compatible.
These pins can be used for any sort of input or output.
Many have special functionality:
GPIO #0: this pin is used by the ESP8266 to determine when to boot into the bootloader. If the pin is held low during power-up it will start bootloading.
It is also connected to the red LED so it could be used to blink it.
GPIO #2: it is another way to detect boot-mode. It is also connected to the blue LED.
GPIO #15: it is another way to detect boot-mode. It’s always possible to use it as an output.
GPIO #16: it can be used to wake up from sleep mode of the board. To awake, connect it to the RESET pin.
Analog Pin
There is a single analog input pin called A.
This pin has a 1,0 V maximum voltage, so if you have an analog voltage to read that is higher, it will have to be divided down to 0 - 1.0V range.
Other Control Pins
We have a few other pins for controlling the board.
LDO: This is the enable pin for the regulator.
By default its pulled high. When connected to GND it will turn off the 3,3 V regulator and is an easy way to cut power off to the whole setup.
RST: This is a 5 V compliant reset pin.
By default it’s pulled high. When connected to GND it will reset the ESP8266 system.
EN (CH_PD): This is a 3,3 V logic only reset pin.
By default it’s pulled high. When connected to GND it will reset the ESP8266 system.
To establish a communication with Adafruit ESP8266 board, we need an USB to Serial converter which will provide the connectivity between USB port of our PC and the Serial UART interface of the ESP8266 module.
There are different options of cables and adaptors that could be choose offering connectivity at 5 or 3,3 V with various connector interfaces. There are many option between USB-to-TTL Cables, FTDI Cables, FTDI modules and so on…
In my case, I used a SparkFun FTDI Basic 5V module.
In order to make them work, remember to install the correct driver!
Firmwares
Different options for firmware are available: each one allows to program the chip in a different way.
1. AT+ Command Processor, the default for first versions like ESP01;
2. Node MCU Lua, the default for Adafruit Huzzah ESP8266;
3. Custom Firmware, created using the Arduino IDE;
4. esp-open-SDK, that allows the direct programming on ESP8266;
5. Firmware by Espruino community (Javascript Interpret);
6. Firmware by MicroPython community (Python Interpret);
These last two, are very good interpreters but in early stage of development (published in December 2015) with not all functionalities made been available yet.
Remember that in the ESP8266 module, it’s possible to load one firmware at a time.
So before starting to programming the ESP, the developer has to make the choice of the firmware to use. It’s not possible to change it during development or share properties of different firmwares.
The Huzzah ESP8266 board is pre-programmed with a NodeMCU Lua interpreter.
But, if the we want to use a different firmware, it’s possible to use the tools developed by ESP8266 Community for loading a firmware image on the chip.
To load the firmware in the chip, we will need:
a Flashing Tool: I choose esptool, a Python-based cross-platform flashing tool.
an Image of Firmware: any image of firmware to install.
For the installation of the image on the module, we must be able to use esptool.
In order to make esptool working, we must install pySerial on our system.
Now, supposing that esptool and pySerial are correctly installed and the firmware image is available, it’s possible to begin the installation of the firmware on the module.
The ESP8266 must be in Bootloader Mode! Otherwise, flashing process will not work!
The command for the installation of the firmware using esptool is:
sudo esptool.py --port /dev/tty.device_name write_flash 0x00000 /path_to_firmware_image
where:
/dev/tty.device_name is the name assigned by the OS to the ESP8266 device;
/path_to_firmware_image is the location of the image of the firmware.
We can now focus on some of the most common firmwares for ESP…
Maybe the quickest way to get started with the ESP8266!
It allows ESP8266 to process AT+ Commands received over Serial UART interface.
The Advantages that it’s useful for a beginner user since he will not need not be familiar with any specific programming language or framework to use the ESP8266; he can simply send a series of commands to achieve his goal.
Disadvantages are that the module must be always connected to the computer. and that it operates at very low level. So it’s not so intuitive in a regular use.
How can we send AT commands to the module?
There are different ways of sending serial messages over the computer's USB.
The most used are:
Serial Terminal Softwares as PuTty or CoolTerm for Windows/MacOS/Unix;
Arduino IDE's Serial Monitor.
Let we see a list of the AT Commands
AT to see if the board is connected;
AT+CWLAP to have a list of the available access points;
AT+CWJAP to connect with an access point (with relative ssid and password)
AT+CWJAP to disconnect from an access point;
AT+CIFSR to get the IP Address;
AT+CWMODE to set the mode in which the module will operate
(Station, Access Point or both)
AT+CWLIF to check IPs of connected devices in access point mode;
AT+CIPSERVER to set up in server mode with a specific port;
and so on…
Then we have another option for firmware that is quite more advanced…
At the moment NodeMCU is the most popular alternative firmware to AT+ Commands!
NodeMCU runs a Lua interpreter onboard the ESP8266, which is able to execute commands written in the Lua scripting language.
The commands are sent to the via the Serial UART interface.
NodeMCU is a good starting point for developers as it provides an environment that allows running commands not only for controlling the ESP8266's wireless interface, but also its GPIO and hardware functionality.
In addition, we have access to Lua programming language for writing our applications.
Finally, we can save our applications in the ESP8266's flash memory and instruct it to run the application code every time it restarts!
To write our application we could use ESPplorer IDE tool.
Lua Language
Pros:
• Portable: builds on any platform with an ANSI C compiler.
• Lua is comparably as easy as Python in terms of learning how to write code.
• Embedded and extensible language that provides a straightforward interface to/from C/C++/Objective-C. It has a very clean C API.
• Sufficiently fast: performs well comparing to other languages and has a JIT compiler that noticeably improves performance on many tasks.
• Well documented: reference manual, book, wiki, 6-page short reference and more.
• Friendly and enthusiastic community.
• Clean and simple syntax suitable for beginners and accessible to non-programmers.
• Integrated interpreter: just run lua from the command line.
• Native support for coroutines to implement iterators and non-preemptive multi-threading.
• Incremental garbage collector that has low latency, no additional memory cost, little - implementation complexity, and support for weak tables.
• Simple yet powerful debug library.
• Free
Cons:
• Lua does not focus on 100% backward compatibility. Many newer releases of Lua break programs written in previous versions.
• Limited error handling support.
• Global scoping by default.
• No Unicode support.
• Lua supports only a few data structures, including arrays, lists and hash tables.
• Limited pattern-matching support.
• No POSIX functions built-in..
• No class/object finalizers.
• Packaging on Windows. It requires a fair amount of experience with Windows application packaging to produce an installer that provides everything your end user needs in a way that makes your use of Lua as the core language irrelevant to them.
Maybe it’s the easiest way to design a custom firmware for the ESP8266 module!
Whereas the Adafruit Huzzah Board is pre-programmed with the latest version of the NodeMCU firmware, we can easily replace it using the Arduino IDE.
This allows us to run our own firmware on the ESP8266, which executes every time the module is powered.
WARNING! This method erases the previous firmware from the ESP8266's flash!
(To come back to initial configuration of the board, there would be executed the steps described before in the slide 13)
It would be the best choice for those users which want to use the latest versions of ESP8266 that allows to use it as a regular micro-controller (as the ATmega328 in Arduino/Genuino UNO).
The Arduino IDE will not initially recognize the ESP8266 since it does not have it installed by default in the devices menu!
So we need to install it in the IDE if we want to program the module with it!
The installation is quite simple:
Open Preferences window of the Arduino IDE;
Enter in the Additional Boards Manager URLs field the URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Go in the Boards Manager and install the last release for esp8266 module by the ESP8266 Web Community;
Restart the Arduino IDE;
After restarting the Arduino IDE, we can find a specific menu of the different models of ESP8266 boards.
Selecting the model of the board, the IDE will automatically set the correct parameters in order to make feasible the USB-to-Serial communication.
If all the previous configuration steps are been made correctly, we are now ready to write our custom firmware with the Arduino IDE.
My Idea was to create an application for the remote control of the temperature/humidity in the room with the possibility to turn on/off the light by using the smartphone.
The first step was to create a dedicated hardware circuit with the DHT sensor and the Relay module that will be connected to the ESP;
Then I created the application code using the Arduino IDE that implement the function that I need;
To create the communication between my iPhone and the ESP, I used “Blynk”, an app that was presented by Ioannis during the Projects Presentation and that will be presented in one of the next lectures by my colleague Davide Meacci.
I choose it because it has very useful features and because it gives it to me an easy way to communicate with my Wifi module.
This is the scheme of the circuit that I built.
There are the ESP and 2 extra components: a light bulb controlled by a relay module and a DHT Temperature Humidity Sensor.
A Relay is a switch operated by a relatively small electric current that can turn on or off a larger electric current. It uses an electromagnet to mechanically operate the switch.
Relays are used because many electronic sensors are incredibly sensitive to current and they produce only small electric currents.
In my case, I need a relay to drive the light that use bigger current (220 against 5V).
The relay acts as a bridge making it possible for small current to activate larger one.
It could be seen as one circuit to switch on a second circuit.
When power flows through the first circuit, it activates the electromagnet that attracts a contact and activates the second circuit.
When the power is switched off, a spring pulls the contact back up to its original position, switching the second circuit off again.
It has two different behaviours: Normally open and Normally closed.
NO: the contacts in the second circuit are not connected by default and switch on only when a current flows through the magnet.
NC: the contacts are connected so a current flows through them by default and switch off only when the magnet is activated, pulling or pushing the contacts apart.
The relay module in normally open configuration is powered by the GND-V+ pins and controlled by GPIO 12 pins of ESP.
The DHT11 is a 4-pin sensor that operates from 3.5 to 5.5V.
It takes Temperature/Humidity of the room and send it to the ESP by the GPIO 14 pin.
It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of ±5%.
Let now see the code of my application.
Presentation of the main features of the ESP8266 WiFi module created for the course "Pervasive Systems"University of Rome "La Sapienza"