"Preface
As mentioned in smart families, everyone may first think of the cattle products, or innovative products. Think that it feels very complicated, some consolidation machines, some use of the Raspberry Pie, with Arduino, no matter what, all the servers are controlled, such as the Yeelink platform, Tencent's intelligent hardware platform, etc. In fact, real realization, did not think about it, we can do only with a small single-chip microcomputer.
I. MCU implementation web server
Discuss articles with single-chip to implement web servers can also find a few articles through Baidu, but more detailed implementation methods are not found. Here is that there are two ideas to complete, one is a wired LAN module, such as W5500, this module It itself has part of the web service, which is relatively simple, but can only be accessed based on LAN. The other is to complete the network module by STM32. The simplest method I can think of is to be implemented with STM32 + ESP8266.
Based on the first method, I feel that the performance of the module is relatively large, limited to module, no development. So considered the use of the second method. In this method, some people use Arduino to complete, this is based on C. In addition, consider using micropython, which can be implemented directly with Python. Here is the details of TPyboardv202 (some treasures, 30 yuan).
II. Simulation to realize the family smart home control platform
Because I don't want to do a complicated experiment, I just want to simulate the effect. In the process of the entire module, I chose the TPyboardv202 development board to do the main control board, use a light-emitting diode to simulate a table lamp (reality, here In fact, you can use a relay to control the power of the other equipment), with a DC small motor to add mini fan blades to the analog electric fan. The entire implementation also uses a triode (S9014, NPN) to control DC motor.
3. Hardware construction and connection
1, use of the light emitting diode
The light emitting diode is relatively simple, directly watching the two "legs", the length of the length is the positive electrode, which is negative.
2, how to use DC small motor
In the DC small motor, the two terminals A and B in the red frame, no matter how the positive or negative pole can, only the direction of rotation is different. I used the B-terminal to access the positive electrode, just right clockwise.
3, method of use of triode S9014 (NPN)
This time we use the amplification and switching function of S9014, the collector is connected to the 3.3V pin of the V202, and the emitter is accessed into a certain end of the motor, and the emitter and the collector are turned on by means of the base high and low level. Thereby control the DC motor rotation or stop.
Let's introduce the wiring method.
Triar (S9014) TPyboard V202 DC Small Motor
Collecting electricity 3.3V -
Base G5 -
Emitter - B
- GND A end
TPYBOARD V202 LED
G4 positive
GND negative
My physical connection diagram
Implement web server with micropython
First, edit a main.py file. V202 boot self-start main.py file
Try: importsocket assocket except: importsocket importnetwork firemachine import_flag = pin (2, pin.out) # ESP8266 Module Small light high level: low level: bright LED = Pin (4, pin.out) # 发光Diode control pin motor = pin (5, pin.out) #DC motor control pin # initializing LED.LOW () motor.low () LED_Flag.high () DEFDO_CONNECT (SSID, PWD): STA_IF = NetWork. WLAN (NetWork.sta_if) #sta mode STA_IF.Active (false) ifNotsta_if.Ig.isconnected (): # Judging whether to connect STA_IF.Active (TRUE) Sta_if.connect (SSID, PWD) #ssid: WiFi Name PWD: WiFi Password Whilenotsta_if. Isconnected (): passifsta_if.isconnected (): returnsta_if.ifconfig () [0] Defmain (IP_, DEV_DATA, LOGIN_DATA, NAME, PWD): S = Socket.Socket () Ai = Socket.GetadDrinfo (IP_, 80) AddR = Ai [0] [- 1] s.setsockopt (socket.sol_socket, socket.so_reuseaddr, 1) s.bind (add) s.Listen (5) LED_FLAG.LOW () # s_data = login_datawhiletrue: Res = S.Accept ( ) client_s = res [0] client_addr = res [1] led_flag.high () REQ = client_s.readline () while () "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " ": Break # Print (h) REQ + = (H.Decode ('UTF-8'). Lower ()) P RINT ("Request:") Req = Req.Decode ('UTF-8'). Lower (). split ('RN') #http header resolution REQ_DATA = Req [0] .lstrip (). Rstrip () .replace ('', ') Print (REQ_DATA) IFREQ_DATA.FIND (' Favicon.ico ')> - 1: Client_s.close () Continueelse: iflen (Req_data) <= 12: # Description is the first visit, Enter login.htmls_data = login_data else: Req_data = req_data.replace ('get /?', '') .Replace ('http / 1.1', '') _name = req_data.find ('name') _PWD = Req_data.find ('PWD') if_name> - 1AND_PWD> - 1: # 是 用户 用户 登 i i i _>>>>>>>>>>>>>>:>>:::>>::::>>>::>:>>:: () f = Open ('five.html', 'r') s_data = f.read () f.close () Print ('login fail!') Else: # 是 是 控制 控制 控制 = Req_data.find ('LED = ') if_index> - 1: s_data = dev_data led_val = Req_data [_DEX + 4: _INDEX + 6] .lstrip (). Rstrip () Print (' LED: ', LED_VAL) IFLED_VAL ==' on ': Led.Value (1) ELSE: LED.VALUE (0) # Judging whether it is a control motor _INDEX = Req_data.find ('motor =') if_index> - 1: s_data = dev_data motor_val = req_data [_Index + 6: _index + 8] .lstrip (). Rstrip () Print ('motor_val:', motor_val) ifmotor_val == 'ON ': Motor.Value (1) Else: motor.value (0) Print (' ----------- ') Client_s.send (s_data) client_s.close () LED_FLAG.LOW () f = Open ('Device.html', 'R') dev_html = f.read () f.close () f = Open ('login.html', 'r') login_html = f.read () f.close () F = Open ('info.txt', 'r') info = f.read () f.close () name = info.split (',') [0] .lstrip (). rstrip () PWD = INFO .split (',') [1] .lstrip (). Rstrip () Print ('name:', name) Print ('PWD:', PWD) Myip_ = do_connect ('Essid', 'PWD') # home Network WiFi Name and Password Print (myip_) main (myip_, dev_html, login_html, name, pwd)
Login.html login page
Smart Home Network Title> h2 {margin-top: 4%; margin-bottom: 40px;} style> head> Welcome to smart home network platform h2> User Name: p >
password: p> form> Center> body> html>
Device.html Control Page
Smart Home Network Platform Title> h2 {margin-top: 4%; margin-bottom: 40px;} style> head> Welcome Smart Home Network - Control Platform h2> lighting: " "" "" value = "" OFF "" name = "" "" "" "" "" "" "" "" "" "" / Body> html>
Fail.html Login Error page (that is, makes Login.html change a little more)
Smart Home Network Title> h2 {margin-top: 4%; margin-bottom: 40px;} style> head> Welcome to smart home network platform h2> User name or password error! p> User Name: p>
password: p> form> center> body> html>
Info.txt This is the username and password (English comma separated) with files, the front is the username, and the password is behind. The username and password here are used to log in to our smart home control platform.
Admin, 123456
5. Program download test
Use the MicropythonfileUPloader tool to download the source code to the V202.
Tool Download address: http://tpyboard.com/download/tool/170.html
1. Use the USB cable to access the V202 to the computer, open the device manager, and view the loaded port. I am com44
※ If the driver installation fails, you can download the CH340 driver, manually install it.
CH340 driver download address: http://tpyboard.com/download/drive/163.html
2. Open the MicropythonfileUPloader selection port and click [Open].
3, cancel the hook of [Autorun], click on the red box's folder icon, select the source code, click [Send] Waiting to send success.
4. Download the above source file to V202. After the download is complete, click [RUN / RESET] to start executing the code.
5. After starting running, printing in the red box is the username and password we store in Info.txt, this can be customized.
6, the following pink box prints the IP address obtained from the router from the router, as long as the IP address is printed, the description will successfully access the network. My V202 acquired IP address is 192.168.1.192.
7. Here, our web server is completed.
6. Use of smart home network platform
1. In the home LAN, we can choose a PC or mobile phone, open 192.168.1.192 through the browser, open 192.168.1.192.
2. Default User Name Admin Password 123456, you can modify the info.txt file.
(1) Enter the wrong username and password will enter the error interface.
(2) Enter the correct and enter the control platform.
3, next, we can control the lights and small fans through the web page, see my experimental renderings.
Here, I just made an instance, the time limit, didn't do more in-depth development. Everyone can go deep into their own creativity. If you can set an external network IP via the router so that you can access from the external network to complete the control of the external network.
Original link: https://www.eeboard.com/news/scm-2/2/
Search for the panel network, pay attention, daily update development board, intelligent hardware, open source hardware, activity and other information can make you master. Recommended attention!
[WeChat scanning picture can be paid directly] "
Our other product: