Learning ESP-8266 Microcontrollers
The table below lists specific items, components and or parts in relation to use or interfacing with the ESP-8266 Wi-Fi/microcontroller. The remainder of this page gives general background information about using and programming the ESP-8266 microcontroller.
Part Num. | Component | Description | Data Sheet |
---|---|---|---|
ESP-8266 (ESP-12) | Flashing Espressif AT Command Firmware | Demonstrate how to flash/update Espressif firmware to enable ESP8266 to communicate via AT commands. | ESP8266 technical reference |
ESP8266 Overview
The following sections record some general information about using and programming the ESP8266 Wi-Fi microcontroller. This is rather an 'eclectic' grouping of information, and more represents areas of difficulty in understanding I had over time, rather than an orderly approach to learning the ESP8266 microcontroller.
Just for completeness, the ESP8266 is a low-cost Wi-Fi System-on-Chip (SoC) with full TCP/IP stack and microcontroller capability produced by the manufacturer Espressif Systems. A "bare" ESP8266 is not particularly useful for the DIY/Hobbyist. However, third-party manufactures, notably Ai-Thinker, have produced modules with the ESP8266 bundled with necessary peripheral components (from the "minimum" of including an antenna and breakout pins, through to including FTDI header, micro-USB sockets etc). In the DIY/Hobby context, when discussing ESP8266 it is one of these third-party "breakout modules" that is really being referenced.
The particular module I typically use is generally referred to as the ESP-7 (which without the ceramic antenna and with a PCB etched antenna is known as the ESP-12). A list of common modules (1).
Section Index
- Utilising the ESP8266
- Setting-up/Checking ESP8266 Functioning
- ESP8266/WiFi and the TCP/IP Stack
- Miscellaneous Info
Utilising the ESP8266
As previously mentioned, the ESP8266 chip itself is a SoC that includes a microcontroller and Wi-Fi Transceiver (2.4 GHz, and implements TCP/IP and full 802.11 b/g/n WLAN MAC protocol) that integrates antenna switches, RF balun, power amplifier, low noise receive amplifier, filters and power management. Since the ESP8266 has self-contained Wi-Fi networking capabilities, the ESP8266 can be used in a standalone application or as the slave to another microcontroller.
There are two main methods of incorporating/using the ESP8266 in DIY projects (well, three, but the third involves being 'comfortable' with using the official SDK and programming "on the metal' in C).
Firstly, there is using the ESP8266 as a "Wi-Fi peripheral" to another microcontroller (see my example using PIC 18F248) via using AT commands. This has the advantage of being able to dedicate the ESP8266 to servicing Wi-Fi/Internet functions while being able to utilise the other controller for whatever ancillary functions are required by the project in question (e.g., monitoring sensors, controlling other peripherals etc). The ESP8266 only has a limited set of GPIO's available and program memory, whereas, the designer is free to utilise whatever other "main" microcontroller is necessary.
The second method is utilising the Arduino IDE and the available ESP8266 library functions. This has the advantage of being able to potentially use the ESP8266 to perform all the necessary functions of the project in question, while providing Wi-Fi/Internet functionality, giving a potentially cheap, low-parts count outcome. Obvious disadvantage is the limited GPIO's/memory available on the ESP8266. Although it is still a very capable component and for "simple" IoT applications (e.g. a single sensor reporting back to a centralised database and/or providing control of a motor via web-browser) potentially very efficient and practical.
The third method, Espressif SDK available to program the ESP8266 in C, is potentially useful if you have the skills and need to optimise the code in some manner. Similarily, there are LUA interpreters and MicroPython firmware available for those who wish to flash the ESP8266 and potentially program in languages they have more skills/familarity.
Setting-up/Checking ESP8266 Functioning
As a DIY/Hobbyist generally the most economical components are the most desirable. This means utilising ESP8266 modules from ebay, which tend to only have the minimum parts necessary (e.g., no power supply, USB-serial converter, etc). This means some "set-up" is required in order to initially communicate with the ESP8266 and check functionality of newly purchased items for example.
Generally the ESP8266 on the breakout board (ESP-7, ESP-12 etc) will be supplied without connectors on the various pins (see middle component in the following photograph). In terms of prototyping/testing on a breadboard, some method of wires/leads etc will be required. I used cutoff wire from resistor leads (used in other various projects) as "pins". Initially, I used an IC socket as a "carrier" (see right-hand component in the following photograph) both to fit standard breadboard and to give mechanical strength to the pins that would be inserted/removed from the breadboard. However, the resistor leads proved sufficiently robust to be used directly (left-hand component in the following photograph)- although this arrangement takes up more room on a breadboard.
After arranging for the ESP-7 module to be able to be physically connected to other components, the most important point is to keep in mind that the ESP8266 operates at 3.3V (especially the RX pin when connected to an external microcontroller, which typically operate at 5V). Powering ESP-7 or similar with 5V most likely will damage the unit.
In addition, there are a number of pins that are required to be pulled up/down to Vcc/ground in order to enable ESP8266 operation and or set the boot mode for either normal operation or flashing. The following schematic diagram outlines the requirements.
-
ESP8266 Initial Setup Schematic
ESP8266 Initial Setup Schematic
Three pins (GPIO0, GPIO2 and GPIO15) control boot-up mode while the pin CH_PD is chip enable/disable. The pin CH_PD must be pulled-high with a 10Kohm resistor to Vcc.
While the three pins controlling the boot mode give eight possible modes, the following table gives the two boot modes generally used.
GPIO15 | GPIO0 | GPIO2 | Boot Mode |
---|---|---|---|
L | L | H | Flash program to device (boot from UART) |
L | H | H | normal start-up (boot from flash memory) |
Connecting GPIO0 with a switch to Vcc via a 10Kohm resistor and 100ohm resistor to ground provides a convenient method of starting the ESP8266 either in normal mode or ready for flash programming.
To prevent the potential of "random" resets, connect the RST (reset) pin to Vcc through a 10Kohm resistor. Alternatively, the RST pin can be connected via a momentary switch to either Vcc or ground, thus giving a reset.
The final 'ancillary' circuitry required in order to use a ESP8266 module is the power supply. A USB-TTL converter is typically used to connect the ESP8266 to PC for serial communications, and the usual USB-TTL converters provide both 3.3V and 5V output sourced from the PC USB port. This is not recommended as power supply for the ESP8266 (remember, 3.3V only). The ESP8266 has relatively high current requirement on startup (and Wi-Fi comms) which can overload the PC USB port, potentially causing damage.
Recommend using power supply from suitable lab supply, battery, linear voltage regulator, SMPS style regulator etc.
Connect ESP8266 to PC
With the ESP8266 physically connected as per previous section/schematic, now only require connecting the RX and TX lines of the ESP8266 to the TX and RX lines respectively of the USB-TTL converter (check that the USB-TTL converter being used outputs 3.3V on the TX line). If connecting via another microcontroller or USB-TTL converter that outputs 5V on the TX line, use a suitable voltage divider or converter (the RX pin on the ESP8266 is not 5V tolerant).
Using a suitable serial terminal programme (RealTerm, PuTTY, Terminal, Arduino IDE Serial Monitor, etc), connect via the correct COM port using a baud rate of 115200 (typically, may need to experiment with baud rate), 8 bits, no parity and 1 stop bit (see the following diagram). Now AT commands can be sent to the ESP8266 from the serial terminal, and the ESP8266 will respond.
The Espressif AT instruction set is available as a pdf (2). A good initial command to test if the ESP8266 is connected and operating correctly is simply "AT" (the capital letters A followed by T). All AT commands need to sent with both a carriage return and line feed as terminal characters (see the red highlighted region in the following diagram). If the ESP8266 is connected and operating correctly, in response the string "OK" will be received.
Another useful AT command for initial testing is "AT+GMR" which will cause the ESP8266 to respond with firmware version.
-
ESP8266 Serial Terminal Connection
ESP8266 Serial Terminal Connection
ESP8266/WiFi and the TCP/IP Stack
While the ESP8266 is a relatively powerful microcontroller in its own right, the major advantage of the ESP8266 chip is that of being a SoC that includes not only the microcontroller but a Wi-Fi Transceiver (2.4 GHz, and implements TCP/IP and full 802.11 b/g/n WLAN MAC protocol) that integrates all that is necessary to connect to the "internet" via Wi-Fi and act as a wireless access point, wireless station or both.
So this means you are not required to known the details of how to form the actual "physical" Wi-Fi connection between the ESP8266 and an wireless access point (which is generally part of your modem/router, which in turn connects to the Internet by telephone/cable to your ISP) or the details of the protocols used to transfer data from a web browser or other device on the local network to and or from the ESP8266.
However, you are required to know which AT commands are necessary to send to the ESP8266 in order to get a list of devices on the local network, how to get the ESP8266 to connect to your modem/router, what information is necessary to send/receive in order to communicate with web browsers (and hence either control the ESP8266 or enable the ESP8266 to act as a web server) etc. With this in mind, a little background about what Wi-Fi, TCP/IP and "protocols" involve is helpful.
The TCP/IP Stack
There is a myriad of information on the internet explaining TCP/IP, which tends to be overwhelming and is difficult to assimilate in regards to what is required to be known in terms of the ESP8266 and utilising the chip to connect DIY projects to the internet/browsers.
As a starting point, take a step back and consider what is required to enable two devices, potentially literally at the opposite sides of the world, to communicate information. Further, this communication needs to be not limited to particular makes/models of equipment, physical methods of sending signals (wires, radio waves etc) and be robust in terms of noisy signals, loss of connections etc, together with being secure from tampering and or interception of data.
TCP/IP stands for Transmission Control Protocol and Internet Protocol. The dictionary definition of the word 'protocol' is "a system or set of rules and acceptable behaviour". So in view of the previous paragraph, possibly at the most abstract level, the TCP/IP stack is the set of "rules" that all manufacturers, software developers, etc follow in order to provide the functionality of the Internet (i.e., two devices, potentially literally at the opposite sides of the world, can communicate information reliably and efficiently).
The word "stack" is included when referring to TCP/IP as the set of "rules" required needs to cover such diverse areas as from the actual electrical signal characteristics within physical components through to algorithms used for error correction/detection and or encryption. This multitude is categorised into four layers (i.e. the "stack") - see the following diagram based on (3) . Each layer of the stack handles the appropriate specialised function, handling the data to be transferred, and the processing passes up and down the stack to effect communications through the internet between two devices.
The ESP8266 integrating the TCP/IP stack with Wi-Fi means you do not need to worry about user datagram headers, building TCP packets etc. However, you will need to know how to instruct the ESP8266 to get IP and MAC addresses, how HTTP GET and POST requests are formed etc. These specifics are detailed in the projects listed in the table at the head of this page.
-
TCP/IP Stack
TCP/IP Stack
Miscellaneous Info
The ESP8266 is generally used simply as a Serial-to-WiFi bridge, however it is a very powerful microcontroller in its own right. The following is some information for the ESP8266 if being used as a stand alone microcontroller.
Just like "normal" microcontrollers, the ESP8266 has digital input/output pins (I/O or GPIO, General Purpose Input/Output pins), i.e., used as digital inputs to read a digital voltage, or as digital outputs to output either 0V (sink current) or 3.3V (source current).
As previously mentioned, the ESP8266 is a 3.3V microcontroller, so its I/O operates at 3.3V as well. The pins are not 5V tolerant, applying more than 3.6V on any pin will likely damage or destroy the chip. The maximum current that can be drawn from a single GPIO pin is 12mA.
The ESP8266 has 17 GPIO pins (0-16), however, only 11 of them are utilisable, as 6 pins (GPIO 6 to 11) are used to connect the onboard flash memory chip. GPIO 1 and 3 are used as TX and RX of the hardware Serial port (UART), so generally, cannot be used as normal I/O while sending/receiving serial data.
GPIO 0-15 all have built-in pull-up resistors. GPIO16 has a built-in pull-down resistor.
The ESP8266 has a single analog input, with an input range of 0 - 1.0V (above this voltage with damage or destroy the chip). The ADC (analog to digital converter) has a resolution of 10 bits.
References
- ref001: https://www.esp8266.com/wiki/doku.php?id=esp8266-module-family
- ref002: https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
- ref003: https://www.w3.org/People/Frystyk/thesis/TcpIp.html
Only Logged-In Members can add comments