- Home >> News >> Blog articles >> Internet of things
Intelligent switch hardware circuit program design
Smart home is a new technology product developed by combining the integrated wiring technology, network communication technology and automatic control technology with the residence as the platform. The smart switch is a form of smart home. Early smart switches can only perform operations through their preset programs, and cannot remotely control equipment and reflect the intelligence of smart homes. The smart home solution that emerged later is to connect the device to the network through a twisted pair. Due to the problems of short transmission distance and difficult wiring in wired connection, it has been gradually eliminated in the design of smart home. With the development of wireless technology, its advantages of long distance, low power consumption and low cost are gradually reflected. Therefore, modern smart homes mostly use wireless connection technology. Wi-Fi is a technology that allows electronic devices to connect to a wireless local area network, usually using the 2.4GHz radio frequency band for data transmission. The use of the 2.4GHz frequency band has the advantages of low cost, strong versatility, and simple structure of the transmitting circuit, so it is widely used in the design of smart homes.
1. Overview of STC15 series MCU and ESP8266 chip
STC15 series single-chip microcomputer is a series of low-cost, low-power, high-speed single-chip microcomputers compatible with 8051 core produced by Hongjing Technology. Among the STC15 series microcontrollers, STC15F2K60S2 is widely used. Its built-in large-capacity Flash memory of 60K and RAM of 2K. Different from the traditional 8051 core MCU, STC15F2K60S2 comes with two full-duplex UART serial ports, and an 8-channel 10-bit analog-to-digital converter is built in the chip, which can save the external A/D conversion chip and spare the data bus It is used for single chip I/O interface. In addition, it also has three built-in 16-bit timers, a high-precision crystal oscillator, and 1K EEPROM for users to use (through IAP technology to read and write the internal Flash memory), and the built-in EEPROM can be used for power-down storage Data usage.
ESP8266 is a low-power, highly integrated IoT Wi-Fi chip produced by ESPRESSIF. It has a built-in 32-bit high-speed microprocessor with a clock frequency of 160MHz, standard digital peripheral interfaces, antenna switches, radio frequency modules, power amplifiers, low noise amplifiers, filters and power management modules, etc. Thanks to the high integration of its chip, the ESP8266 chip can work with only 7 peripheral components.
2. Design and implementation of smart switch
The whole system can be divided into control module, acquisition module and communication module according to its function.
2.1 Control Module
The control module is composed of IN4007 diode, PC817 optocoupler, S8050NPN transistor and 5V relay. Due to the small output current of the MCU I/O interface, it is impossible to directly drive the relay, so the optocoupler element is required to drive indirectly. The mutual interference effectively improves the reliability and stability of the system. Since the relay coil will generate a high counter electromotive force when it is disconnected, in order to protect the optocoupler from being damaged, a freewheeling diode needs to be connected in parallel at both ends of the relay coil. In order to ensure the protection effect, the IN4007 with a faster switching speed is selected here. The diode acts as a freewheeling diode. Because driving the relay requires a large current, the large driving current may damage the optocoupler device, so it is necessary to connect a S8050 transistor to amplify the control signal of the optocoupler at the next stage of the optocoupler output.
2.2 Communication Module
The communication between the single chip microcomputer and the upper computer is realized through the ESP8266 IoT WiFi chip in this design. The ESP8266 chip supports both TCP and UDP protocols, and supports two modes of Client and AP, which can realize LAN communication or direct communication between the host computer and the chip. The decoding and encoding of AT commands to analyze short messages is the core of software programming. The AT command set is the industry standard of the modem communication interface, and it is the command that the modem can recognize and execute. Since the chip supports AT commands based on strings, connect the RxD2 and TxD2 pins of the STC15 microcontroller to the TxD and RxD pins of the ESP8266-ESP-01 module.
When working normally, the power supply terminal Vcc of the chip module is connected to a 3.3V DC power supply, and the GND terminal is grounded. At the same time, the level of the CH_PD terminal should be pulled high when the chip is working normally. Since the initial baud rate of the serial port of the module is 115200bps, you can pass the corresponding AT command statement AT+CIOBAUD=<baud rate>, <data bit>, <stop bit>, <parity bit> before connecting with the microcontroller , <flow control bit> is set to a custom baud rate, after the setting is completed, the serial port of the microcontroller must also be set to the same baud rate. The ESP8266 chip needs to be initialized when the power is completed, and the AT+CWMODE=<working mode> command is used to determine the working mode of the chip. Mode 1 is Client mode, mode 2 is AP mode, and mode 1 is selected here. After the setting is completed, the MCU sends the relevant information of the wireless network to the ESP8266. Set the SSID and password of the wireless network through the AT command: AT+CWJAP=<SSID>, <Password>. After the setting is completed, compare the returned strings through the MCU. If the ESP8266 chip returns "WIFI CONNECTED" and "WIFI GOT IP" strings, it means that the network connection is successful, otherwise the connection fails and reconnects. The IP address of the device is automatically assigned by the DHCP server in the LAN. After successfully connecting to the wireless network, it is necessary to configure the IP address and port number of the TCP server of the host computer, through the AT command: AT+CIPSTART=<Type>, <IP Address>, <Port> can be configured, and the Type can choose to use UDP Or the TCP protocol, since UDP is a connectionless transport layer protocol, which is not as reliable as the TCP protocol in the data transmission process, the TCP protocol is selected as the transport protocol here. After the setting is completed, if the chip returns the string "CONNECT OK", it means that the MCU and the host computer have successfully established the communication based on the wireless LAN.
2.3 Acquisition module
Due to the limited load of the smart switch, if it is overloaded for a long time, it will cause the equipment to overheat, cause fire or damage the equipment, so it is necessary to protect the smart switch from overload. When the current passes through the wire, a magnetic field will be generated, and the linear Hall element can be used to indirectly measure the current passing through the wire. Here, the Hall element is used to detect the current, and the current value in the loop is converted into a voltage value analog signal and sent to the analog-to-digital converter for processing. The STC15F2K60S2 microcontroller integrates a 10-bit analog-to-digital converter, so there is no need to connect an additional A/D conversion IC The conversion is complete. The current upper threshold is transmitted from the host computer to the microcontroller. When the system is running, compare the set value with the collected value of the Hall sensor at regular intervals. If the measured value is greater than the set value, the relay is turned off to cut off the power supply. Considering that the power-on system needs to maintain the last set parameters, the parameters set each time are stored in the E2PROM that comes with the microcontroller, and the E2PROM is called during program initialization to read the parameters set before power-off.
2.4 Overall program design
Since the C language has strong functionality and structure, it can effectively shorten the development cycle of the single-chip system and is easy to debug and maintain, so the C language is selected as the development language of the intelligent switch system. After the system is powered on, the variables stored in the E2PROM before power-off are read into the memory, and then the ESP8266 is initialized. After the initialization is completed and the TCP connection with the host computer is established, a ready command is sent to the host computer. When the system receives an instruction from the host computer, it will trigger a serial port interrupt, put a string comparison function in the interrupt program, and the single-chip microcomputer can make different responses according to different instructions. It is worth noting that: unlike the traditional 12T cycle 51 single-chip microcomputer, the machine cycle of STC15FK60S2 is 1T, so the delay program running on the traditional 51-core single-chip microcomputer needs to be accelerated by 12 times before being used on the STC15 series single-chip microcomputer.
3. The design of the upper computer program of the intelligent switch
Here, Visual Basic.NET is selected as the programming language of the upper computer program. For software developers, it is relatively simple to use VB.NET development technology and get started quickly. In the above design, the communication between ESP8266 and the upper computer is based on the TCP protocol, so Socket can be used on the upper computer to establish communication with it. There is a built-in Socket class in VB.NET. When the device is connected to the network, it can establish communication with the device under the condition that the device IP address, port number and communication protocol are known. After continuous debugging, the program can communicate with the smart switch as required.
epilogue
After continuous testing and modification of the circuit and program, the smart switch based on STC15F2K60S2 runs normally after being powered on, and can communicate with the host computer through the wireless LAN. After the post-test, the control module, communication module and acquisition module of the system all work normally, and the design goal of the system is realized. No additional wiring is required for use, just connect the switch to AC power and wireless LAN to work. When multiple devices work at the same time, different devices can be distinguished only through different IP addresses, which facilitates the cluster distribution and unified control of the system.
The above is the example of smart switch development introduced by Shenzhen Zuchuang Microelectronics Co., Ltd. for you. If you have the development needs of smart switch control board solutions, you can trust us. We represent a variety of single-chip microcomputers, voice chips, dual-mode Bluetooth ICs, and wifi chips. Brands include Songhan MCU, Yingguang MCU, Jerry Bluetooth, Ankai Bluetooth, Allwinner, and Realtek. Our technical services include: PCB design, microcontroller development, Bluetooth solutions, software and hardware custom development, APP development, small program development, WeChat official account development, etc. It can also undertake the design of intelligent electronic products, the development of living appliances, the research and development of beauty equipment, the application of Internet of things platform, the smart home control system, the development of TWS earphones, Bluetooth earphone speakers, the development of children's educational toys, the design of electronic education products, etc.
Proposal recommendation
- TOP