好久不发帖了,趁感冒发烧无心工作,来一发。
直接奔入主题:
下面是main.py
- from machine import Pin
- import socket
- def webserver():
- global s ,red_stat,bai_stat,green_stat
- s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
- s.bind((wlan.ifconfig()[0],80))
- s.listen(1)
- while True:
- conn,addr = s.accept()
- request = conn.recv(1024)
- request = str(request)
- ib = request.find("Val=")
- if ib >0:
- ie = request.find(" ",ib)
- val = request[ib+4:ie]
- if val =="1":
- if red.value()==0:
- red.value(1)
- else:
- red.value(0)
- print("1")
- elif val =="2":
- if bai.value()==0:
- bai.value(1)
- else:
- bai.value(0)
- print("2")
- elif val=="3":
- if green.value()==0:
- green.value(1)
- else:
- green.value(0)
- print("3")
- conn.send(val.encode())
- else:
- with open('index.html',"r") as html:
- conn.sendall(html.read())
- conn.sendall("\r\n")
- conn.close()
- def connectWifi(ssid='xxxx',passwd='xxxxx'):
- global wlan
- wlan=network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.disconnect()
- wlan.connect(ssid,passwd)
- while(wlan.ifconfig()[0]=='0.0.0.0'):
- time.sleep(1)
- return True
- try:
- red = Pin(25,Pin.OUT)
- bai = Pin(14,Pin.OUT)
- green = Pin(12,Pin.OUT)
- connectWifi()
- webserver()
- except:
- if (s):
- s.close()
- [code]
- WebCtrl
-
LED --- Web Control for Esp8266
-
-
-
-
-
-
-
复制代码
wlan.disconnect()
wlan.active(False)[/code]
下面是index.html
此内容由EEWORLD论坛网友youxinweizhi原创,如需转载或用于商业用途需征得作者同意并注明出处
本文来自论坛,点击查看完整帖子内容。