wifi.setmode(wifi.STATION)
wifi.sta.config({
ssid = "OpenWrt",
pwd = "xxx"
})
wifi.setmaxtxpower(60)
wifi.sta.sleeptype(wifi.NONE_SLEEP)
-- wifi.sta.setip({ip="192.168.123.33",netmask="255.255.255.0",gateway="192.168.123.1"})
local zhuangtai = {
d1 = {
name = "deng1",
pin = 2,
val = 0
},
d2 = {
name = "deng2",
pin = 1,
val = 0
},
r1 = {
name = "dianruzi",
pin = 5,
val = 0,
dangwei = 0
}
}
local wsdpin = 6
for i, v in pairs(zhuangtai) do
gpio.mode(zhuangtai[i].pin, gpio.OUTPUT)
end
dingshimingling = ""
if file.open("dingshi", "r") then
dingshimingling = file.read()
file.close()
end
dingshibiao = {}
function shengchengdingshibiao()
dingshibiao = {}
table.insert(dingshibiao, {
shi = 0,
fen = 0,
zhixing = dingshijiaoshi
})
if dingshimingling ~= "" then
local i = 1
while true do
local a = string.find(dingshimingling, "@", i)
if a then
local s = string.sub(dingshimingling, i, a)
i = a + 1
local aa = string.find(s, ":")
if aa then
local ins = {}
ins.shi = string.sub(s, 1, aa - 1)
local aaa = string.find(s, "*", aa)
if aaa then
ins.fen = string.sub(s, aa + 1, aaa - 1)
ins.zhixing = string.sub(s, aaa + 1, -2)
table.insert(dingshibiao, ins)
end
end
else
break
end
end
end
end
shengchengdingshibiao()
function mcufunc(mcu_do)
local dh = string.sub(mcu_do, 1, 2)
local v = string.sub(mcu_do, 4, 4)
zhuangtai[dh].val = v
if string.sub(mcu_do, 1, 1) == 'd' then
gpio.write(zhuangtai[dh].pin, tonumber(v))
else
if zhuangtai[dh].val == 0 then
zhuangtai[dh].dangwei = 0
else
zhuangtai[dh].dangwei = (2 ^ (4 - zhuangtai[dh].val))
end
end
end
jiaoshichenggong = 0
dingshijiaoshishibai = 1
function jiaoshi()
sk = net.createConnection(net.TCP, 0)
sk:dns("ntp1.aliyun.com", function(conn, ip)
if ip ~= nil then
sntp.sync(ip, function(sec, usec, server, info)
print('sync', sec, usec, server)
jiaoshichenggong = 1
dingshijiaoshishibai = 0
end, function()
print('failed!')
end)
else
print("dns err")
end
end)
end
jiaoshi()
function dingshijiaoshi()
-- 如果每天半夜校时不成功怎么办(还有如果停电再来电定时应该怎么办)
dingshijiaoshishibai = dingshijiaoshishibai + 1
jiaoshi()
end
function dingshi()
if jiaoshichenggong == 0 then
jiaoshi()
return
end
local tm = rtctime.epoch2cal(rtctime.get() + 28800)
shi = tm["hour"]
fen = tm["min"]
for i, v in ipairs(dingshibiao) do
if tonumber(dingshibiao[i].shi) == shi and tonumber(dingshibiao[i].fen) == fen then
if type(dingshibiao[i].zhixing) == "string" then
mcufunc(dingshibiao[i].zhixing)
else
dingshibiao[i].zhixing()
end
end
end
end
local baduan = 8;
function dangwei()
baduan = baduan - 1
if (baduan == 0) then
baduan = 8;
gpio.write(zhuangtai.r1.pin, gpio.LOW)
dingshi()
end
if baduan == zhuangtai.r1.dangwei then
gpio.write(zhuangtai.r1.pin, gpio.HIGH)
end
end
local mytimer = tmr.create()
mytimer:alarm(5000, tmr.ALARM_AUTO, dangwei)
srv = net.createServer(net.TCP, 10)
srv:listen(80, function(conn)
print("lianjie")
conn:on("receive", function(conn, payload)
local e = payload:find("\r\n", 1, true)
if not e then
conn:close()
return
end
local line = payload:sub(1, e - 1)
local _
_, _, method, url = line:find("^([A-Z]+) (.-) HTTP/1.1$")
postparse = {url:find("mcu_do=")}
if url == "/" then
local htmlen = #htm
local n = 1
local fini = 0
conn:send("HTTP/1.1 200 OK\r\nTransfer-Encoding:chunked\nContent-Type:text/html\r\n\r\n" ..
("%X\r\n"):format(#htm))
conn:on("sent", function(conn)
if fini == 1 then
conn:close()
return
end
if htmlen - n < 256 then
conn:send(htm:sub(n) .. "\r\n" .. "0\r\n\r\n")
fini = 1
return
end
conn:send(htm:sub(n, n + 255))
n = n + 256
end)
elseif postparse[2] ~= nil then
mcu_do = string.sub(url, postparse[2] + 1)
local f = string.sub(mcu_do, 1, 1)
if f == 'd' or f == 'r' then
mcufunc(mcu_do)
elseif string.sub(mcu_do, 1, 3) == "xgd" then
dingshimingling = string.sub(mcu_do, 4)
shengchengdingshibiao()
file.open("dingshi", "w")
file.write(dingshimingling)
file.close()
elseif string.sub(mcu_do, 1, 3) == "wsd" then
local status, temp, humi, temp_dec, humi_dec = dht.read(wsdpin)
conn:on("sent", function(conn)
conn:close()
end)
local data = temp .. "@" .. humi
conn:send("HTTP/1.1 200 OK\r\nTransfer-Encoding:chunked\nContent-Type:text/html\r\n\r\n" ..
("%X\r\n"):format(#data) .. data .. "\r\n" .. "0\r\n\r\n")
return
end
local fini = 0
conn:on("sent", function(conn)
if fini == 1 then
conn:close()
return
end
local shuju = '{"d1":' .. tostring(zhuangtai.d1.val) .. ',"d2":' .. tostring(zhuangtai.d2.val) ..
',"r1":' .. zhuangtai.r1.val .. ',"sj":' .. ({rtctime.get()})[1] .. ',"js":' ..
dingshijiaoshishibai .. ',"ds":"' .. dingshimingling .. '"}'
conn:send(("%X\r\n"):format(#shuju) .. shuju .. "\r\n" .. "0\r\n\r\n")
fini = 1
end)
conn:send("HTTP/1.1 200 OK\r\nTransfer-Encoding:chunked\nContent-Type:text/html\r\n\r\n")
else
conn:on("sent", function(conn)
conn:close()
end)
conn:send("HTTP/1.1 404 Not Found\r\n\r\n")
end
end)
end)
htm = [[<!DOCTYPE html><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"/>
<style>
input[type="button"]{margin:10px;width:40px;height:40px;border-radius: 10px;}
</style>
</head>
<script >
function dianji(val)
{
var request = new XMLHttpRequest();
request.open('GET', "?mcu_do="+val,true);
request.onload = function() {
zhuangtai=JSON.parse(this.response)
deng1.style.color=zhuangtai.d1?"red":"black"
deng2.style.color=zhuangtai.d2?"red":"black"
if( zhuangtai.r1>0){dianruzi.style.color="red";
dang.innerText= ":"+zhuangtai.r1+"档"}
else {dianruzi.style.color="black"
dang.innerText= ""}
tm=new Date(zhuangtai.sj*1000)
document.getElementById("shijianxianshi").innerText=tm.toString()+(zhuangtai.js?"::::"+zhuangtai.js:"")
dingshi.value=zhuangtai.ds.replaceAll("@","\n")
}
request.send();
}
function xiugaidingshi(){
var request = new XMLHttpRequest();
request.open('GET',"?mcu_do=xgd"+(dingshi.value.replaceAll("\n","@")),true)
request.onload = function(){chenggong.innerHTML="<p>成功</p>"}
request.send();
}
function wenshi(){
var request = new XMLHttpRequest();
request.open('GET',"?mcu_do=wsd",true)
request.onload = function(){huixian.innerText=this.response}
request.send();
}
document.onload=dianji("shx")
</script>
<body style="text-align:center">
<h1 id=deng1 style="color:black">屋里电灯</h1>
<input type="button" onclick="dianji('d1_1');" value="开" /><span style="margin:40px;"></span>
<input type="button" onclick="dianji('d1_0');" value="关" />
<h1 id=deng2 style="color:black" >院里电灯</h1>
<input type="button" onclick="dianji('d2_1');" value="开" /><span style="margin:40px;"></span>
<input type="button" onclick="dianji('d2_0');" value="关" />
<h1 id=dianruzi style="color:black" >电褥子<span id="dang"></span></h1>
<input type="button" onclick="dianji('r1_1');" value="一档" />
<input type="button" onclick="dianji('r1_2');" value="二档" />
<input type="button" onclick="dianji('r1_3');" value="三档" />
<input type="button" onclick="dianji('r1_4');" value="四档" /><br><br>
<input type="button" onclick="dianji('r1_0');" value="关" /><br><br>
<div id="shijianxianshi">---</div><br>
<textarea rows="6" cols="15" style="text-align:center" id="dingshi" /></textarea><br><br>
<input type="button" style="width:80px" onclick="xiugaidingshi()" value="修改定时"/><br>
<div id="chenggong"></div><br>
<input type="button" onclick="wenshi();" value="温湿度" style="width:60px" /><br>
<p id="huixian"></p>
</body>
</html>]]
第三集:lua
最新推荐文章于 2024-08-28 21:39:09 发布