他们逼我做卧底

[Training Camp_Advanced Class] Internet Smart Socket

 
Overview

1. Project Introduction

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.

2. Hardware schematic design (see schematic diagram for details)

1. MCU selection

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)

2. WIFI chip selection

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.

3. STM32 minimum system circuit design

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

4. LED indicator light

One power indicator light, three custom LEDs

5. Buttons

Two buttons for restoring factory settings, controlling OLED display, etc.

6. OLED

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.

7.DHT11

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.

8. ESP8266 minimum system design

1. ESP_EN, GPIO0, GPIO2 pull-up

2. GPIO15 pull-down

3. Reset circuit

4. CH340 serial port one-click download circuit

9. ESP8266 and STM32 communication circuit design

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.

3. Hardware Layout Design

1. USB uses differential lines

USB differential.png

2. Cover the crystal oscillator with no copper inside.

Crystal oscillator without copper coating.png

3. No copper is laid in the 220V area, and the line width is 2mm.

No copper is laid in the 220V area.png

4. STM32 software design

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

5. ESP8266 software design

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

6. Mechanical design

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.

oled_bracket.pngoled.pngoled physical picture.jpg

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.

7. Public APP

Public app interface.jpg

8. Function realization

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.

9. Design issues and improvements

V2.0

1. Modified the problem in the previous version 2. Black solder mask

V1.0

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.

Mistake 1.png

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 2_2.jpg


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.



Mistake 3.png

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.png

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.































参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-07-02 03:44:31

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号