程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Micropython server web page LED switch

編輯:Python

MicroPython Server Webpage LED switch

Recently TPYBoard v202:

The following is used micropython Developed led routine , Board connection wifi And then get Ip Address , And print it on the serial port , adopt ip Address use browser to access the page , Achieve control led switch :

effect :

I made a slight change in the official routine , Removed login page , Beautify the control page :

because esp8266 The built-in memory is too small , One time open device.html Not too big , Otherwise, incomplete web page files will appear , Web pages cannot be displayed on the mobile phone , So we must pay attention to this .

device.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style> body{
width: 100%;height: 100vh;display: flex;justify-content: center;align-items: center;background-color: #efeeee; } input[type="submit"] {
 background-color: #F5F5F5;border: 20px solid rgba(0, 0, 0, 0.1);color: #666666;font-size: 20px;font-weight: bold; height: 100px; line-height: 27px; margin: 11px 6px; min-width: 100px; padding: 0 8px;text-align: center; } </style>
<title> Smart home platform </title>
</head>
<body>
<div class="container">
<center>
<h2> Smart home control platform </h2>
<form action="/" method="get" accept-charset="utf-8">
<span style='font-size:100px;'>&#128161;</span>
<p> The light :&nbsp;</p>
<input type="Submit" value="ON" name="led" />
<input type="Submit" value="OFF" name="led" />
</form>
</center>
</div>
</body>
</html>

main.py

try:
import usocket as socket
except:
import socket
import network
from machine import UART
from machine import Pin
led_flag=Pin(2, Pin.OUT)
led = Pin(4, Pin.OUT) // Relay switch control pin connection G4 Pin
led.value(0)
led_flag.value(0)
def do_connect(ssid,pwd):
sta_if = network.WLAN(network.STA_IF)#STA Pattern 
sta_if.active(False)
if not sta_if.isconnected():# Determine whether to connect 
sta_if.active(True)
sta_if.connect(ssid,pwd)#ssid:WIFI name pwd:WIFI password 
while not sta_if.isconnected():
pass
if sta_if.isconnected():
return sta_if.ifconfig()[0]
def main(ip_):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ai = socket.getaddrinfo(ip_, 80)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
s.bind(addr)
s.listen(5)
led_flag.value(0)
while True:
res = s.accept()
client_s = res[0] # Connected clients 
led_flag.value(1) # Set up 8266 led Changliang 
req = client_s.recv(1024).decode('utf-8')
print(req)
try:
req_path = req.split(" ")[1]
except:
continue
print("req_path:------",req_path)
if req_path.find('favicon.ico') > -1:
client_s.send(" ")
client_s.close()
continue
elif req_path.find('led=ON') > -1:
led.value(1)
print('led:', led.value())
led_flag.value(0)
elif req_path.find('led=OFF') > -1:
led.value(0)
print('led:', led.value())
led_flag.value(0)
elif req_path.find('ledstatus') > -1:
client_s.send(str(led.value()))
client_s.close()
continue
with open('device.html','r') as f:
client_s.send(f.read())
client_s.close()
myip=do_connect('essid','password')# Home network WIFI Name and password 
print(myip)
main(myip)

server.zip

python It is also suitable for raspberry pie with high performance , Not suitable for MCU, although MicroPython Than Arduino Stronger , But the running performance is not as good as Arduino


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved