Although the ESP8266 programs and related products have matured significantly, some beginners still don’t know how to learn. For example, how to use an ESP8266 along with an STM32 microcontroller to create a small product? What is the learning path like? How should we get started?In this regard, I suggest:1. First, study the ESP8266 module separately, understand its AT command set, and clarify this module using a serial debugging assistant and a network debugging assistant.2. Learn the MQTT protocol. Why is it necessary to learn the MQTT protocol in the second step? Because our ESP8266 needs to connect to a server, simply put, MQTT is the communication protocol between the ESP8266 and the server. This protocol is not difficult, and I have already written notes that will be published directly on this public account later.3. You need to learn Android Studio to create your own app. Since it is IoT control, there must be an app. Although Alibaba Cloud and Smart Cloud have ready-to-use apps, as an electronics enthusiast, you should not be limited to cloud intelligence.Today, I will take you through how to use the STM32 and ESP8266 to create something like a temperature and humidity display, and display it in real-time on a mobile app.
Learning methods: 1. Frequently check major forums for information and find related videos on Bilibili. 2. Learn to apply knowledge flexibly; if you can light up an LED, you should also know how to turn it off. 3. Get hands-on experience; programs are not just written but debugged and modified.
Introduction to ESP8266 01S
Before using the AT function, the module must have the AT firmware installed. Each module manufacturer may modify and reduce the official AT firmware from Espressif, but the core AT command functionalities remain the same. The ESP8266 modules we buy generally come with pre-installed firmware, so there is no need to flash firmware ourselves.The ESP-01S WiFi module is a low-power, cost-effective embedded wireless network control module. It meets the needs of IoT applications such as smart grids, building automation, security, smart homes, and remote healthcare. The core processor of this module, the ESP8266, integrates the industry-leading Tensilica L106 ultra-low-power 32-bit microcontroller in a small package, with a 16-bit RISC architecture and supports clock frequencies of 80MHz and 160MHz, supports RTOS, and has an onboard antenna. This module supports a complete TCP/IP protocol stack. Users can use this module to add networking capabilities to existing devices or to build standalone network controllers.(1) The module operates at a DC power supply voltage of 3.3V, with a current of 500mA or more; (2) The maximum output current of the Wi-Fi module’s IO is 12mA; (3) The NRST pin of the Wi-Fi module is active low; the EN enable pin is active high; (4) To enter the upgrade mode, GPIO0 should be low, then reset and power on the module; to enter normal working mode, GPIO0 should be high, then reset and power on the module; (5) The RXD of the Wi-Fi module connects to the TXD of the external MCU, and the TXD of the Wi-Fi module connects to the RXD of the external MCU;
Flashing Firmware on ESP8266
Flashing firmware can be done using Dupont wires or a firmware flashing module (which can be bought for a few yuan on Taobao), as shown in the image below:
AT Command Control for ESP8266 Module
The official AT command set from Espressif has nearly 100 commands, but only a dozen or so are commonly used, and they are quite simple to understand. Here are some common commands, and I will demonstrate how to connect to a remote server step by step using TCP to send and receive data. More AT commands can be found in the “ESP8266 AT Command Set Manual”.To summarize, the structure of an AT command starts with AT, followed by the parameters to query (read) or set (write). For example, the command to query the Wi-Fi mode is AT+CWMODE?, and to set the Wi-Fi mode is AT+CWMODE=3. Additionally, a carriage return and line feed must be appended at the end of the data to be sent, which is represented in hexadecimal as 0x0D 0x0A. Note: When using the serial debugging assistant to send AT commands, you only need to add a carriage return after the command you want to send. You can check the option “Send in Hex” to observe if 0x0D 0x0A is appended to the data.
1. Test if the Module is Normal
This command is typically used to check if the module has started normally after powering on. If it replies with OK, it indicates normal startup.
2. Enable/Disable Echo
As seen in the above image, we sent an AT command, and the module replied with AT OK, meaning the module echoed back the command it received before sending a valid response. This echo is called echoing. After disabling echo (by sending ATE0), if we send AT again, it will only reply with OK. As shown in the image below:
3. Set AP Mode and AP Parameters
Wi-Fi mode has two types: AP mode and Station mode. AP refers to what we usually call a hotspot, such as a Wi-Fi router, a mobile phone with hotspot enabled, or public hotspots, etc. These AP devices allow other devices (like mobile phones, laptops, etc.) to connect after entering the hotspot name and password (which can also be left blank). Station refers to devices that connect to AP, such as mobile phones, laptops, etc. When the ESP8266 is set to AP mode, other devices can connect to this hotspot, supporting up to 4 Station devices. AP mode is also the default mode of the ESP8266. The setup process for the ESP8266 is as follows:1. First, send AT+CWMODE? to query which mode the module is currently in. From the image below, we can see that AT+CWMODE=2 is the hotspot mode.2. Then we send AT+CWMODE=2 to set the module to hotspot mode. At the same time, our computer will display the hotspot name.3. We now customize the hotspot name and password for our ESP8266 and check if our computer can connect to the ESP8266 module we set up.
1. Set the Wi-Fi mode to AP mode2. Set AP hotspot propertiesAT+CWSAP=AT+CWSAP_DEF=”ESP8266-XSD”,”12345678″,5,3
This means: the hotspot name is ESP8266-XSD, the password is 12345678, using channel 5, and the encryption method is WPA2_PSK. The channel corresponds to different radio frequencies. If there are Wi-Fi signals with the same channel in the same space, it will cause interference, affecting the internet quality. Therefore, channels can be set to avoid such interference. Common channels are 1, 6, and 11, as these three channels do not interfere with each other. The setup effect is shown in the image below:The number of connections can limit the number of Station devices that can connect. Broadcasting or not broadcasting the SSID refers to whether to hide the hotspot name to make it more secure. Additionally, AT+CWSAP=AT+CWSAP_DEF indicates that the parameters set will be stored in flash memory. There is another similar command, AT+CWSAP=AT+CWSAP_CUR, which means the parameters set will not be saved after a reboot.
4. Set to Station Mode
This mode is the most commonly used mode because IoT devices need to connect to the home router to access the internet. In this case, the device acts as a Station connecting to the AP hotspot. The process to set the Station and connect to the AP is as follows:
1. Set the Wi-Fi mode to Station 2. Connect to the home routerAT+CWJAP=”liuyao”,”liuyao1001″
The image below shows the serial output during the connection process:At this point, we can see that our ESP8266 module is connected to the home router. If we want to disconnect, we can use AT+CWQAP to disconnect the ESP8266 from the router.Now our ESP8266 is connected to the home router. If we want to obtain the IP address assigned to the ESP8266 by the router, we can use the AT+CIPSTA? command, which returns the local IP, gateway address, and subnet mask assigned to the ESP8266 by the router.
5. Use TCP for Device Communication Within the LAN
Establish TCP Connection (AT+CIPSTART)
To implement device communication within the LAN via TCP, the ESP8266 must first connect to the home hotspot. This command can specify the protocol type for the connection, usually either TCP or UDP. We first open the network debugging assistant and set it as the TCP server, with specific settings as shown below:Next, we send the AT command to establish a TCP connection. A successful connection will prompt CONNECT. Then we use the send command: AT+CIPSEND=14, where 15 is the length of the data to be sent. After this command is completed, the receiving window will display >, and we can send “I m TCP Client” from the sending window. The TCP server will receive this information, and then the TCP server can send “I m TCP Server”, which will be printed by the serial receiving end as the Client receives the data. Below is the process of the ESP8266 connecting to the current hotspot environment, establishing a TCP connection with the server, and transferring data, where all characters and punctuation must be in English.1. Set Wi-Fi mode to Station: AT+CWMODE=12. Connect to the home router: AT+CWJAP=”liuyao”,”liuyao1001″3. TCP: AT+CIPSTART=”TCP”,”192.168.0.103″,80804. Prompt to send data length of 14: AT+CIPSEND=145. Serial debugging assistant sends: I m TCP Client6. Network debugging assistant sends: I m TCP ServerMany people may be confused at this step, not knowing why we need to do this. What is the serial debugging assistant, network debugging assistant, and what is TCP Server?Haha, I believe many beginners are also unclear at this point. Let me briefly explain: our goal with the ESP8266 is to connect it to the home network. The network debugging assistant’s TCP Server is the network we want to connect to. Some may ask, if I want to connect to my home router’s network, why connect to the network debugging assistant’s network? This is because if you connect to the home network, how would you send data to your home router? How would you know if the router received the data? How would the router send you data back? The purpose of using the network debugging assistant is to replace the router’s local network. TCP Server means the server side. As for why we use the serial debugging assistant, I don’t need to explain. Only after you debug the relevant data on the serial assistant can we use the microcontroller’s serial interface combined with the ESP8266 to replace the client, and we no longer need the debugging assistant.Therefore, we must ensure that we have debugged the relevant data on the serial debugging assistant, understood the process and principles, before we move on to the microcontroller program and build your server, rather than using the network debugging assistant as a substitute.
Enable Transparent Transmission (AT+CIPMODE=1)
Previously, when sending data via TCP, we had to specify the length of the data to be sent before each transmission, and after receiving data, there would be a prefix of +IPD,< len >, which was inconvenient for data processing. Therefore, we can use the AT+CIPMODE=1 command to enable transparent mode. Once transparent mode is enabled, we only need to use the AT+CIPSEND command the first time we send data to inform the ESP8266 to start transparent transmission. After that, we can directly send the data we want, and when receiving data, there will be no +IPD,< len > prefix.Below is the process of the ESP8266 connecting to the current hotspot environment, establishing a TCP connection with the server, and enabling transparent mode for data transmission, where all characters and punctuation must be in English.1. Set Wi-Fi mode to Station: AT+CWMODE=12. Connect to the home router: AT+CWJAP=”liuyao”,”liuyao1001″3. TCP: AT+CIPSTART=”TCP”,”192.168.0.103″,80804. Enable transparent mode: AT+CIPMODE=15. Start transparent transmission: AT+CIPSEND6. Serial debugging assistant sends: hello I m TCP Client!7. Network debugging assistant sends: hello I m TCP Server!If you want to exit transparent transmission mode, first send +++ (0x2B 0x2B 0x2B), note that there is no line break, then use the AT+CIPMODE=0 command to exit transparent mode and return to the default transmission mode. If you do not exit transparent mode, you cannot send AT commands in transparent mode; it will default to sending strings.
6. Use SmartConfig for Device Network Configuration
Previously, we used the AT+CWJAP command to actively connect to the home Wi-Fi, but in most IoT products, there is a lack of input devices for entering the Wi-Fi password, such as keyboards, and it is even more impossible to hand over the program to users to modify the home Wi-Fi hotspot name and password. In actual project development, the most commonly used method is to use a button to make the device enter a certain mode and use a mobile phone to inform the device of the current Wi-Fi password to achieve Wi-Fi network configuration. This mode is called SmartConfig. In this mode, the ESP8266 will listen for UDP broadcast packets on a specified port. If it receives a broadcast packet in the specified format, it will parse it to obtain the Wi-Fi SSID and password, and then automatically connect to the obtained Wi-Fi hotspot, thus achieving one-click Wi-Fi configuration.
Smart configuration requires three devices: the home router, the Wi-Fi module, and the mobile phone. The module should be in station mode, and the mobile phone should be connected to the home Wi-Fi. The principle of this operation is that we need to configure the Wi-Fi as station mode because the default is AP mode, and we need to ensure that the phone is connected to the home router’s hotspot.The first line on the phone shows the hotspot name that the phone is currently connected to. The first line below requires the user to manually input the password for the router’s hotspot. There will be a button labeled “Configure Network” (Connect). When we click this button, the phone will broadcast via UDP to a certain port number or IP address. The broadcast content is a data frame structure packaged by the manufacturer, and during broadcasting, there is no need to specify a specific IP address, as each module may have a different default. Just specify the account. When we press the network configuration button, the device will enter the SmartConfig mode.At this time, the Wi-Fi module will be in a listening state. For example, it listens on port 8080 for UDP data. If it receives valid data, it will extract the useful hotspot name and password from it, and then use a function similar to transparent mode to connect to the specified hotspot, thus achieving the one-click network configuration process.Now that we know how to use the ESP8266 module, we will next use the STM32 and ESP8266 to achieve data transmission and reception with the server. We will discuss the specific operations in the next article!
Disclaimer: This article is reprinted with permission from the “Guo Guo Little Master” public account. The reprint is for learning reference only and does not represent this account’s endorsement of its views. This account is not responsible for any infringement of its content, text, or images.