ESP8266用网页控制电灯(micropython)
11:智能电视遥控器的主页定制 #生活技巧# #数码产品使用技巧# #智能电视遥控器设置#
#此esp8266的网页又链接到另一个esp8266 里边还有个温湿度传感器
-----------------------------------------------------------------------------------------------------------------------------------------
# #####################################################################main.py
import network
import utime
SSID = "OpenWrt"
PASSWORD = "*****************" ####################################
def do_connect():
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.ifconfig(('192.168.123.254', '255.255.255.0', '192.168.123.1', '192.168.123.1'))##############################
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(SSID, PASSWORD)
start = utime.time()
while not wlan.isconnected():
utime.sleep(1)
if utime.time()-start > 5:
print("connect timeout!")
break
if wlan.isconnected():
print('network config:', wlan.ifconfig())
do_connect()
import web2
---------------------------------------------------------------------------------------------------------------------
###########################################################web2.py
import machine
import utime
import dht
d = dht.DHT11(machine.Pin(2))
from machine import Pin
p0 = Pin(0, Pin.OUT)
p0.on()
p1 = Pin(4, Pin.OUT)
color1="blue"
color2="blue"
color3="black"
color4="black"
loc="<html>"
loc2="""
<script >
location.href="http://192.168.123.254"
</script >
</html>
"""
def html():
return """
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
</head>
<script >
function dianji(val)
{
var request = new XMLHttpRequest();
request.open('GET', "?mcu_do="+val,true);
if (val=='5on'){
{ request.onload = function() { huixian.innerHTML= this.response; }
}
}
else{
request.onload = function() {
if( this.response[6]==1)deng3.style.color="black";else deng3.style.color="red"
}
}
request.send();
}
function dianji2(val){
location.href="http://192.168.123.253/?mcu_do="+val
}
</script>
<body>
<h1 id=deng1 style="color:"""+color1+"""">屋里电灯</h1>
<input type="button" οnclick="dianji2('1on');" value="开" />
<input type="button" οnclick="dianji2('1of');" value="关" />
<h1 id=deng2 style="color:"""+color2+"""" >院里电灯</h1>
<input type="button" οnclick="dianji2('2on');" value="开" />
<input type="button" οnclick="dianji2('2of');" value="关" />
<h1 id=deng3 style="color:"""+color3+"""">西屋电灯</h1>
<input type="button" οnclick="dianji('3on');" value="开" />
<input type="button" οnclick="dianji('3of');" value="关" />
<br>
<br>
<div style="width:40px; height:50px;" id="huixian"></div>
<input type="button" οnclick="dianji('5on');" value="温湿度" />
</body>
</html>
"""
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
addr2 = socket.getaddrinfo('192.168.123.253',80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(0)
print('listening on', addr)
response2 = html
while True:
try:
cl, addr = s.accept()
print('client connected from :', addr)
#cl_file = cl.makefile('rwb', 0)
cl.settimeout(3.0)
lll= b''
while True:
line = cl.readline()
lll=lll+line
#print( lll)
if not line or line == b'\r\n':
break
llll=lll.decode("utf-8")
l=llll.find("mcu_do=",0,20)
if not l==-1:
cmd=(llll[l+7:l+10])
if cmd=="5on":
try:
d.measure()
ddd=str(d.temperature())
except OSError:
cl.send( "错")
cl.close()
else:
response =ddd + " " + str(d.humidity())
cl.send(response)
cl.close()
continue
elif cmd=="3of":
print("of")
p0.on()
cl.send(loc)
cl.send(str(p0.value())+str(p1.value()))
cl.send(loc2)
cl.close()
elif cmd=="3on":
print("on")
p0.off()
cl.send(loc)
cl.send(str(p0.value())+str(p1.value()))
cl.send(loc2)
cl.close()
elif cmd=="xof":
print("3of")
s2.connect(addr2)
sss="GET /?mcu_do=1of http/1.1\r\n"
s2.send(sss.encode('utf-8'))
sss="HOST:192.168.123.253\r\n"
s2.send(sss.encode('utf-8'))
s2.close()
cl.close()
elif cmd=="xon":
print("3on")
s2 = socket.socket()
s2.connect(addr2)
sss="GET /?mcu_do=2on http/1.1\r\n"
s2.send(sss.encode('utf-8'))
sss="HOST:192.168.123.253\r\n\r\n"
s2.send(sss.encode('utf-8'))
cl.send(s2.recv(100))
s2.close()
cl.close()
elif cmd=="yof":
print("yof")
p1.off()
cl.send(loc)
cl.send(str(p0.value())+str(p1.value()))
cl.send(loc2)
cl.close()
elif cmd=="yon":
print("yon")
p1.on()
cl.send(loc)
cl.send(str(p0.value())+str(p1.value()))
cl.send(loc2)
cl.close()
elif cmd=="rst":
machine.reset()
else:
if(p1.value()):
color3="red"
else:
color3="black"
if(p0.value()):
color4="red"
else:
color4="black"
cl.write(html())
cl.close()
except :
print( "cuowu")
网址:ESP8266用网页控制电灯(micropython) https://www.yuejiaxmz.com/news/view/142002
相关内容
语音控制:基于ESP8266的DIY助手智能家居新体验:ESP8266结合阿里云生活物联网远程控制继电器
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
【阿里云生活物联网架构师专题 ②】esp8266 sdk 直连接入阿里云物联网平台,实现天猫精灵找队友零配网功能和语音控制;
【DIY】热水器升级加装远程wifi控制功能,esp8266远程红外控制热水器启动
手机语音助手控制自己开发的物联网设备方法
【阿里云生活物联网架构师专题 ⑥】ESP8266接入阿里生活飞燕平台国际版,实现亚马逊Alexa Echo音响语音控制;
基于 STM32 的语音识别智能家居控制系统的设计(LD3320语音识别芯片+ESP8266 WIFI模块+DHT11温湿度采集+MQ系列 烟雾及可燃气体+蜂鸣器+步进电机模拟窗帘+OLED液晶显示+
深度解析:STM32对接米家平台,打造WiFi智能插座(ESP8266、电流检测)
【阿里云生活物联网架构师专题 ③】esp32 sdk 直连接入天猫精灵IOT开放平台,实现天猫精灵找队友零配网功能和语音控制;