ghaytweyhtoo

[Training Camp_Advanced Class] esp8266 IoT switch based on 51 microcontroller

 
Overview

QQ picture 20200826165659.jpg

This is the latest version of the WiFi switch, which is directly connected to the mains and uses the HTTP protocol.

IMG_360220200817145517.JPG

WiFi switch design ideas

Video demonstration address https://www.bilibili.com/video/BV1TT4y1L7Ji/

Preface

The principle of this WiFi switch is to use the microcontroller to send AT commands to the esp8266 WiFi module to initialize and connect to WiFi, and then the microcontroller continues to send commands to connect to the server. After that, the microcontroller keeps sending GET commands to read the data stream, and the microcontroller receives the data returned by the server through the serial port. flow, and then determine the data flow and whether an instruction to turn on the relay has been issued. The connection is to the onenet cloud platform, which uses the HTTP protocol . Only by continuously sending GET instructions through the microcontroller can the instruction to turn on the relay or turn off the relay be issued.

1. Use STC89C51 or STC12C5A60S2 microcontroller, which is based on the 51 core (other microcontrollers cannot use it). I just wanted to use the stc15f104w microcontroller, which has 8 pins. After searching, I found that there is no serial port register, so I gave up. Finally, I chose 12c5a60s2, which I just had on hand. one. Using an external crystal oscillator of 11.0592MHz, the baud rate communication error is 0.0%.

Y2JR62KTHBQMOD0.png

2. Use the esp8266 wifi module (the existing module on hand). After continuous study and research, I finally know how to download the firmware of e sp8266 (use the ch340 serial port to download, which is very convenient. Pull the GPIO0 pin low to download), and then use the serial port The software tried the AT command and it worked normally. The test can be used as an AP and can also be connected to WiFi. This circuit diagram is copied directly from the adapter board and can be used normally. It should be noted that the EN pin must be pulled up for the module to work.

3CFGWYVOHREWNQR35.png

3. Plan to use a relay as a control component, because the relay can be used for both DC and AC, and the drive is simple. It requires a triode, a freewheeling diode and a bias resistor. This circuit is designed to control incandescent light bulbs. If you simply control DC power, you can use high-power MOS or triode (voltage withstand issues need to be considered). Let’s talk about why PNP transistors are used. The reason is that the IO port outputs a high level when the microcontroller is powered on and reset. If NPN is used, such an instantaneous relay may cause malfunction, which is not very safe, so use PNP transistors. B The base is connected to a low level to conduct. It should be noted that the two pins of the relay coil must be added with freewheeling diodes to protect the triode ( here I used a PNP triode without adding a bias resistor at the base and used a 1K resistor. I wrote a simple program to test and found that the relay could not be turned off. Later, after constant testing, After checking, I added a 4.7K pull-up resistor and increased the base resistor to 50K to switch normally ). The following is why a freewheeling diode is added—————————————————————— This is because when the switch is turned off, the inductance ( the relay coil is an inductor ) will generate The very high voltage far exceeds the power supply voltage. Ordinary electromagnetic relays can be driven by general-purpose transistors such as 8550 or 8050. The maximum withstand voltage of the collector-emitter is only a few dozen volts. When the control switch is turned off, since the current in the inductor cannot mutate suddenly, a reverse electromotive force with upper negative and lower positive polarities will be generated. This electromotive force voltage is extremely high and will directly break down the collector-emitter of the transistor, causing the relay to fail to turn off normally. The relay uses 12v10A current, drives the relay through ss8550 ( PNP ), adds a freewheeling diode 1n4007, and adds a suitable fuse to the high current end of the relay to ensure that the overcurrent contacts of the relay are not burned.

_Q~Z2451_EMB9YP0F_N.png

4. The power supply plan uses a 12v external DC power supply to power the entire system ( the on-board USB power supply is not considered, because I control the light, and the relay is 12v, USB power supply is completely unusable. As for program download, use External USB to TTL module ), using the 7805 voltage regulator chip to provide a stable voltage to the microcontroller (after testing, the 7805 can work stably without adding a heat sink). A 1N5819 diode is added behind the 7805. Its function is to provide a stable voltage to the WiFi module. When testing the power supply alone, do not supply power to the superior 7805. 7805 also supplies power to AMS1117-3.3V. The purpose of this design is to reduce the voltage difference and reduce the heat generated by 1117. After all, the current of the WiFi module is also very large when working in AP mode.

RKW0TIONFNWG03NC`5.png

5. Two buttons are reserved, which are planned to be used to manually open and close the relay when the mobile phone is away or disconnected from the network. Of course, other functions may be developed later. The buttons are designed with a hardware debounce function ( thanks to the 104 capacitor ), so that there is no need to set a delay detection in the microcontroller. This is also the first time to use hardware debouncing. The actual effect will be tested later. If the effect is not good, the software debouncing function will be added.

JOK82AKVLRLAFHHTLEAA.png

6. Four LED indicators are reserved, one is used as an indicator for normal operation of the microcontroller (0.5s flashing period), one is a WiFi connection indicator (always on when connected to WiFi), and the remaining two are used for relay status indication. Or if you have any new ideas in the future, please expand it (currently only one LED is used, and LED4 lights up to indicate that the program has completed sending the initialization AT command).

XZ`0XHCUL_X`Y76GD.png

7. Use an 11.059MHz crystal oscillator so that the error is small when the baud rate is 115200. There will be no garbled characters during serial communication.

8. The microcontroller firmware uses the burning software stc-isp from the STC official website (upload and download method after the program is completed and tested)

9. There is also a DS8B20 temperature sensor left on the board. Consider sending the data to the mobile APP for temperature detection.

10. #The following is a tutorial for creating devices on the OneNET platform#

1. Open the browser and enter the URL open.iot.10086.cn. The first step after opening is to register an account. This process is omitted.

2. Find - Developer Mode - Click in

QQ picture 20200815203818.png 3. Select multi-protocol access

~S9R5TAG1AOG18KOO5.png 4. Select HTTP protocol and click Create a product

CKV0A_PNLSSDNFI8977L.png 5. After clicking, a window will pop up. Fill in the product information casually. Select the technical parameters according to the picture and click OK.

XENZVQ8NZ22UBAE52.png

6. Click OK and another window will pop up, then click Add Device Now

U0KHU0CK8`K6LLHA~N.png

7. Then click Add Device. After filling in the information, click "Add" and the device information will appear. You need to remember the device ID here.

MXX6S_OQ8VVSL415X1X.png

8. Follow the previous step to add data flow. First click "Data Flow" - "Data Flow Template Management" - "Add Data Flow Template". After that, a window will pop up. Fill in the information (the data flow name is the one in the code source). Data flow name, whatever is written here must be written in the code source ) Click Add to complete this step.

XZE38RO`6GG61WEGWHV.png

9. Let’s add a mobile APP application interface, click Application Management

5N_`K2SP90JZ3N1CDA0A.png

After filling in the information, click Add

`NJ~QC~R097UKBRZU9M7E0.png

10. Click on the newly added application, enter the click to edit application, and then let it be up to you. Select a switch, drag it out and put it in the appropriate position. Then click on the dragged switch icon and select the data stream selection on the right. The device you just created will automatically display the data flow information you just created. The following switch information can be used by default.

1`OZ_27~D555IRNPVOO.png

8~9ZW8PNUCW`81DPLB`NBC.png

Then click the save button in the upper right corner, and then you can preview it, or you can download a mobile app and log in to your account to see the application you created!

11. The next step is to return to this interface and click View apikey in the box. This also needs to be written into the code source.

~2_I`V17E`1CJBCFB.png

Next, we set up the WiFi module. Do not solder R1R3 on the circuit board first. Use the CH340TTL module to connect the lines (TTL-RXD is connected to R1 close to the WiFi module side, TTL-TXD is connected to R3 close to the WiFi module side). GND is connected to GND, and the TTL module Connect the 5v output to the 5v pin header on the board, open the software sscom v5.13.1 (the software can be searched on Baidu), select the serial port number, select 115200 for the baud rate, add a carriage return and a line feed and check the box

MPQWSLP867EU3WKN2JN.png

Plug in the USB, select the serial port number, open the serial port, enter AT at the bottom of the software, click Send, you should receive the OK character returned by the module, so the cable is connected. If there is no reply, please check whether the TXDRXD is connected reversely. Yes, the ESp8266 module uses 3.3v for power supply and is powered by the AMS1117 on the board.

CP2VA81NBZJ5DMZUUBI9.png

After sending the AT test and OK, we can enter the command.

First, change the default baud rate to 4800; enter the command AT+UART=4800,8,1,0,0. After returning ok, you need to change the baud rate option to 4800 to use it, and then send AT to test whether it returns OK AT. //Test whether the module can communicate normally. Returning "ok" means it is normal.

AT+CWMODE=1//Configure to STA mode

AT+RST//restart to take effect

AT+CIFSR//Query device IP

AT+CWJAP="777","guaishou" //Connect to the hotspot on your mobile phone. "777" is the name of the hotspot, and "guaishou" is the password ( it is best not to have a Chinese name for the WiFi hotspot, because Chinese input is not supported )

After entering WiFi, a prompt like this will appear

B9_LIK4K12GYZ3RH9DC.png

This means that the setting is successful and normal. Then remove the TTL cable and plug it into the H3 pin header to prepare for downloading the program for the microcontroller. Here you need to download keil4 first, create the project and copy the code source (the specific process will not be written) in the code source. Modify the device ID, apikey, and data stream to compile and download.

5BSK`QJOT1KJAY9V.png

Download method, open STC-ISP

S_~3SUX0VAL4_~KOM3KRF.png

Step 3: Select the compiled hex, click download, disconnect the 5v power supply to the microcontroller and power on again. After a while, it will prompt that the program download is successful. After success, weld the jumper to test whether it is normal.

6SBNJBS4JI3IIUY_1~W_H.png

After connecting in this way, remove the serial port cable and supply 12v DC to the board. When you click the switch on the mobile phone, you will generally hear the sound of the relay closing.

At present, I have only debugged one relay.

2020-8-21

The new 5v power module I bought today has arrived, but the printed board is still being processed in the factory. Maybe the new version cannot be uploaded. Please take a look at the power module.

15.jpg

There is also a blueprint for a new version of the design

~SJC8Y6OHI1IAYTCRL.png

1_4`A7DUJSETNIR1I0M.png

The latest version adopts the original double-sided design to reduce the overall size of the board and can be used directly connected to the mains power supply! It is very convenient to control electrical appliances.

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-20 15:29:04

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号