一、esp8266连接wifi
wifi.setmode(wifi.STATION)
MQTTconnectFlag="Wait..."
ip="Wait..."
cfg={}
cfg.ssid="《wifi名》"
cfg.pwd="《wifi密码》"
wifi.sta.config(cfg)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("Connected, IP is "..wifi.sta.getip())
ip=wifi.sta.getip()
dofile("s51.lua")
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("wifi disconnect")
end)
二、esp8266输出pwm波控制舵机;用dht11采集温湿度上传阿里云
dhtp=1
pin=4
jdqp=2
pwm.setup(pin, 50, 130)
pwm.start(pin)
sv=net.createServer(net.TCP,30)
sv:listen(81,function(c)
c:on("receive", function(c,msg)
print(msg)
if msg=="1" then
do
pwm.setduty(pin, 70)
tmr.delay(3000000)
end
do
pwm.setduty(pin, 130)
tmr.delay(3000000)
end
end
end)
end)
gpio.mode(dhtp, gpio.INPUT)
gpio.mode(jdqp, gpio.OUTPUT)
gpio.write(jdqp, 1)
json="N/A"
wd="N/A"
sd="N/A"
js="N/A"
fs="N/A"
rw="N/A"
rw="N/A"
hw="N/A"
MQTTconnectFlag="N/A"
sj=0
ww="0"
ss="0"
flag=1
jc=1
jx=0
ProductKey ="a1ho0OSpiyR"
ClientId =wifi.sta.getmac()
DeviceName ="xiaowu"
DeviceSecret="HArCY6rc2FH4kTYtTBVPbqzLRR9Yzt29"
RegionId="cn-shanghai"
myMQTTport=1883
myMQTT=nil
myMQTThost=ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com" --host
myMQTTusername=DeviceName.."&"..ProductKey
t1="/a1ho0OSpiyR/xiaowu/user/topic0"
t2="/a1ho0OSpiyR/xiaowu/user/get"
sb="/sys/a1ho0OSpiyR/xiaowu/thing/event/property/post"
function GetNetTime()
sntp.sync({"203.107.6.88","0.nodemcu.pool.ntp.org","1.nodemcu.pool.ntp.org","2.nodemcu.pool.ntp.org","3.nodemcu.pool.ntp.org","www.beijing-time.org"},
function(sec, usec, server, info)
print('sync', sec, usec, server,info)
sj=sec-5
sj2=string.format(sec.."000")
print(sj2)
end,
function()
print("get time error")
end)
return sj
end
myMQTTtimes='6666'
hmacdata="clientId"..ClientId.."deviceName"..DeviceName.."productKey"..ProductKey.."timestamp"..myMQTTtimes
myMQTTpassword=crypto.toHex(crypto.hmac("sha1",hmacdata,DeviceSecret))
myMQTTClientId=ClientId.."|securemode=3,signmethod=hmacsha1,timestamp="..myMQTTtimes.."|"
myMQTT=mqtt.Client(myMQTTClientId, 120,myMQTTusername,myMQTTpassword)
MQTTconnectFlag=0
tmr.alarm(0,1000,1,function()
if myMQTT~=nil then
print("Attempting client connect...")
myMQTT:connect(myMQTThost, myMQTTport,0,MQTTSuccess,MQTTFailed)
end
end)
function MQTTSuccess(client)
print("MQTT connected")
client:subscribe({t1=0,t2=0}, function(conn)
print("subscribe success")
end)
myMQTT=client
MQTTconnectFlag=1
ms="CONNCET"
tmr.stop(0)
end
function MQTTFailed(client,reson)
print("Fail reson:"..reson)
MQTTconnectFlag=0
ms="FILED"
tmr.start(0)
end
myMQTT:on("offline", function(client)
print ("offline")
tmr.start(0)
end)
print(q)
myMQTT:on("message", function(client, topic, data)
print(topic ..":")
if data ~= nil then
print(data)
if data=="11" then
gpio.write(1,1)
elseif data=="0" then
client:publish(sb,q,0,0,function(client) print("updata ok!") end)
elseif string.find(data,[[{"BinarySwitch":0}]])~=nil
then
do
pwm.setduty(pin, 70)
tmr.delay(3000000)
end
do
pwm.setduty(pin, 130)
tmr.delay(3000000)
end
end
end
end)
function cj()
z,wd,sd,x,y=dht.read11(dhtp)
if wd~=-999 and sd~=-999 then
ww=wd
ss=sd
end
jdqs=gpio.read(jdqp)
if jdqs==1 then
jdq="ON"
else
jdq="OFF"
end
end
function sc()
json=string.format([[{
"id": "123",
"version": "1.0",
"params": {
"CurrentTemperature": {
"value": %d,
"time": %s
},
"CurrentHumidity": {
"value": %d,
"time": %s
},
"BinarySwitch": {
"value":%d,
"time": %s
}
},
"method": "thing.event.property.post"
}]],ww,sj2,ss,sj2,jdqs,sj2)
end
tmr.alarm(2,20000,1,function()
tmr.alarm(1, 5000, 1, function()
sj=sj+5
if MQTTconnectFlag==1 and myMQTT~=nil then
sj2=string.format(sj.."000")
cj()
sc()
myMQTT:publish(sb,json,0,0,function(client)
print("send ok" )
end)
end
tmr.alarm(3,1500,1,function()
a=gpio.read(3)
tmr.delay(1000000)
c=gpio.read(3)
if a==1 and c ==0 then
jx=jx+1
end
end)
end)
GetNetTime()
end)
注:本源码大多参考网上大神的例程,我只是稍加 修改拿来用的!