好久不发帖了,趁感冒发烧无心工作,来一发。
直接奔入主题:
下面是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()
- wlan.disconnect()
- wlan.active(False)
下面是index.html
- <html>
- <head>
- <title>WebCtrl</title>
- <style type="text/css" id="style">
- .b{
- width: 100px;
- height: 100px;
- }
- </style>
- <script language="javascript">
- function dimSet(evt) {
- var me, he;
- var resp, xmlhttp=new XMLHttpRequest();
- me = evt.target;
- xmlhttp.open("GET","Val=" + me.value,true); xmlhttp.send()
- }
-
- </script>
- </head>
- <body>
- <h1>LED --- Web Control for Esp8266</h1>
- <div>
- <input class="b" type="button" value="1">
- <input class="b" type="button" value="2">
- <input class="b" type="button" value="3">
- <input class="b" type="button" value="4">
- <input class="b" type="button" value="5">
-
- </div>
- </body>
- </html>
-
-
此内容由EEWORLD论坛网友youxinweizhi原创,如需转载或用于商业用途需征得作者同意并注明出处
本帖最后由 youxinweizhi 于 2018-7-20 11:19 编辑