美文网首页
贝壳物联上传温度

贝壳物联上传温度

作者: 招风小妖怪 | 来源:发表于2018-12-01 15:49 被阅读0次
image.png

init文件

--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)

cfg={}
cfg.ssid="dzsjjs-216"
cfg.pwd="dzsjjs216"

wifi.sta.config(cfg)


--here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("kaiguan.lua")
end
end)

kaiduan

DEVICEID = "8178"
APIKEY = "ccc2a577a"
INPUTID = "ccc2a577a"
host = host or "www.bigiot.net"
port = port or 8181
LED = 4
gpio.mode(LED,gpio.OUTPUT)
local function run()
  local cu = net.createConnection(net.TCP)
  cu:on("receive", function(cu, c) 
    print(c)
    r = sjson.decode(c)
    if r.M == "say" then
      if r.C == "play" then   
        gpio.write(LED, gpio.HIGH)  
        ok, played = pcall(sjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
        cu:send( played.."\n" )
      end
      if r.C == "stop" then   
        gpio.write(LED, gpio.LOW)
        ok, stoped = pcall(sjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
        cu:send( stoped.."\n" ) 
      end
    end
  end)
  cu:on('disconnection',function(scu)
    cu = nil
   
    tmr.stop(1)
    tmr.alarm(6, 5000, 0, run)
  end)
  cu:connect(port, host)
  ok, s = pcall(sjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})
  if ok then
    print(s)
  else
    print("failed to encode!")
  end 
  ----------------------
  cu:on('connection',function(scu) 
     
    ok, stoped = pcall(sjson.encode, {M="checkout",ID="8178",K="ccc2a577a"})
    cu:send( stoped.."\n" ) 

     cu:send(s.."\n")
     
    tmr.alarm(3, 5000, 1, function()
        status, temp, humi, temp_dec, humi_dec = dht.read11(1)
        if temp~=-999 and humi~=-999 then
            print('tem   '..temp)
            print('hum   '..humi)
            -- ok, stoped = pcall(sjson.encode, {M="update",ID="8178",V=[[{"7174":"]]..temp..[["}]]})
            cu:send( [[{"M":"update","ID":"8178","V":{"7174":"]]..temp..[["}}]]..'\n') 
            cu:send( [[{"M":"say","ID":"U5739","C":"htm=]]..temp..[["}]]..'\n') 
       end
       
      end)
  end)
  ------------------------------
  tmr.alarm(1, 60000, 1, function()
    cu:send(s.."\n")
  end)
end
run()

相关文章

网友评论

      本文标题:贝壳物联上传温度

      本文链接:https://www.haomeiwen.com/subject/iyhrcqtx.html