This project is based on stm32 and esp8266, and realizes functions such as mobile APP control socket, mobile APP viewing data (temperature and humidity, socket on/off), onboard OLED viewing data and other functions.
stm32 uses the HAL library, esp8266 uses the RTOS3.0 SDK, and they use serial communication for data interaction. This project provides a design prototype for more complex IoT applications, and deeper development only requires adding peripherals to the solution.
If you only implement the function of a socket, you can directly use ESP8266 to complete it. However, when the number of pins of ESP8266 is not enough and you need to use stm32 to expand the function, this project can provide a set of solutions to complete the mutual communication between STM32 and ESP8266. software and hardware design.
Choose STM32F103C8T6 as the design prototype, just choose a representative chip. If the chip function is not enough to meet subsequent development, it can be replaced with STM32F103VC, STM32F407ZE and other chips. (PS: Mainly because it’s cheap + simple + familiar)
Choose the ESP8266 chip and use the ESP-12F module launched by Essence. Personally, I feel that this chip is well-known, has many online resources, and has a low learning cost. As for EMW3080, there is less information on the Internet. Searching for these two chips on site B, it is clear at a glance that one is a full-screen tutorial and the other is a 3080 graphics card. It’s similar on csdn.
1. BOOT circuit: Both BOOTs are pulled down, and the program starts from the flash area.
2. Download circuit: Use SWD to download the program, and the interface uses X9821WRS-04-9TSN, which is fool-proof and more comfortable than using Dupont wire to plug and unplug. This is just a personal habit and can be changed to 2.54mm pin header.
3. Crystal oscillator circuit: 8MHz. I personally like the 5032 two-pin packaged crystal oscillator. It looks more comfortable than the 49S, but is a bit more expensive.
4. Reset circuit
5. Power indicator light
6. Power supply: Use SPX3819-3.3, LDO, which is more suitable for powering the main control chip.
7. Filter: four 100nF filter capacitors
One power indicator light, three custom LEDs
Two buttons for restoring factory settings, controlling OLED display, etc.
In this design, there is no USB circuit added to the stm32, and no serial port pins are reserved, so the serial port cannot output information, so adding OLED can facilitate debugging. Of course, the most important thing is that the user can see the data.
Collecting temperature and humidity can be said to be an extended function of the socket. DHT11 can be said to be a very common and cheap temperature and humidity sensor, so use it.
1. ESP_EN, GPIO0, GPIO2 pull-up
2. GPIO15 pull-down
3. Reset circuit
4. CH340 serial port one-click download circuit
ESP8266 only has one and a half UARTs, of which UART1 is used to output logs and UART0 is used to download programs, but some changes are required to achieve serial communication with STM32. Map the TX and RX of UART0 of ESP8266 to U0RTS (MTDO/GPIO15) and U0CTS (MTCK/GPIO13). After exchanging the pins, we can still use the previous TXD0 and RXD0 to download the program, but use GPIO13 and GPIO15 to communicate with stm32. This ensures that the program can be downloaded and communicated with STM32 at the same time without changing the hardware connection.
1. Development environment
MDKARM 5.28
STM32CubesMX 6.0.1
2. SDK version
STM32Cube MCU Package for STM32F1 Series 1.8.0
Keil.STM32F1xx_DFP.2.3.0.pack
3. Use freeRTOS to create "SWTask" and "UARTTask" for task scheduling
3. Create a structure, use UART DMA to synchronize the data of STM32 and ESP8266, receive variable length, and determine the frame header sof1 and sof2 to receive data.
typedef struct
{
uint8_t sof1;
uint8_t sof2;
uint16_t temperature;
uint16_t humidity;
_Bool relay;
_Bool linkkit_started;
_Bool factory_restore;
} Model_Info_t;
4. OLED driver, uses software to simulate IIC communication and display data
5. "SWTask" implements key scanning, detects long press or short press of SW1 and SW2, and performs corresponding operations.
6. "UARTTask" implements sending data, and receiving data is completed by DMA callback
1. Development environment
Visual Studio Code 1.47.3
gcc version 5.2.0 (crosstool-NG crosstool-ng-1.22.0-92-g8facf4c0)
2. SDK version
ESP8266_RTOS_SDK 3.2
3. Use esp-aliyun framework
4. Change the log output and serial port to TX1
5. Swap the pins of UART0 to communicate with STM32
6. Collect DHT11 data
7. Use hardware timers to control scheduled tasks
1. OLED bracket.
The OLED I use is a 0.96-inch IIC OLED with a size of 25mm*26mm. Considering that there are components at the bottom of the OLED module, 1mm is deliberately hollowed out in the middle so that the components can be embedded in the groove. M2*5mm self-tapping screws are used for assembly.
2. Shell. This version uses stm32, esp8266, oled, dht11 and other devices at the same time. The overall area of the board is large, so I don’t want to design a shell. In the future, a version will be designed to remove dht11 and stm32, leaving only the esp8266 and relay parts to make the size as small as possible and then design the casing.
1. Realize one-click network distribution with public APP, view temperature and humidity data, and control sockets
2. Tmall Genie voice control socket, "open socket", "close socket"
3. Press and hold SW1 to reset the ESP8266 and enter the network configuration mode. The indicator light flashes. After the network is configured, the indicator light is always on.
4. OLED displays temperature and humidity data, network connection data, etc.
1. Modified the problem in the previous version 2. Black solder mask
1. GPIO9 is used in the ESP8266 circuit to read the temperature and humidity data of DHT11, as shown in the figure below. The original intention of the design is that this pin is relatively close to the placement of DHT11, but the actual measurement found that when this pin is used, the board will Restart, and after searching for information, I found that GPIO9 and GPIO10 of ESP8266 are used to read and write FLASH. If used, it will cause errors in the reading program. So next version I will use GPIO5 to read DHT11. This version can only function by connecting an external DHT11 module, and the onboard one is no longer available.
Error picture 1
2. There is a problem with the OLED packaging. The actual size of the 0.96-inch IIC OLED I purchased is 25mm*26mm, but the size on the package is larger than the design, resulting in a very compact layout and wiring. The packaging will be updated in the next version.
Mistake picture 2
3. When designing the relay circuit, the "CH1" network was set up for the input signal wiring, but the network was not added on the microcontroller side. During DRC, since this pin has been connected to a pull-up resistor, there is no prompt that there is only one network port, so no error is detected. This reminds me that after the design is completed, every network should be checked from beginning to end. Such mistakes should not occur. This version can only remove LED4 and connect its pin to the "CH1" network through a jumper to control the relay.
Error picture 3
4. When ESP8266 and STM32 are jointly debugged, the log will be redirected to TX1 (GPIO2) output, but this version of the design does not lead out TX1. The original intention is to use it as a socket, so there is no need to add extra components. In actual use, the TX1 pin will not be used. However, since the logs of the ESP8266 cannot be seen during joint debugging, this is extremely disadvantageous for debugging the program. Considering that it is currently in the testing stage and the position of one pad on the board is more than enough, the GPIO2 pad will be added for debugging in the next version.
Improved Figure 4
5. In order to make STM32 and ESP8266 respond quickly enough, the serial port sending cycle of STM32 is set to 100ms. Sending data causes the serial port receiving task stack of ESP8266 to overflow, resulting in an error, so it is reset to 1s cycle.
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet