我用NODEMCU手机控制LED灯,一直连不上贝壳服务器

作者:如三月兮 | 更新时间:2017-04-17 | 浏览量:2545

这是kaiguan.lua里面的代码:

DEVICEID = "1890"
APIKEY = "a9135a64c"
INPUTID = "1866"
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,0)
  cu:on("receive", function(cu, c) 
    print(c)
    r = cjson.decode(c)
    if r.M == "say" then
      if r.C == "play" then   
        gpio.write(LED, gpio.LOW)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
        cu:send( played.."\n" )
      end
      if r.C == "stop" then   
        gpio.write(LED, gpio.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
        cu:send( stoped.."\n" ) 
      end
    end
  end)
  cu:on('disconnection',function(scu)
    cu = nil
    --停止心跳包发送定时器,5秒后重试
    tmr.stop(1)
    tmr.alarm(6, 5000, 0, run)
  end)
  cu:connect(port, host)
  ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})
  if ok then
    print(s)
  else
    print("failed to encode!")
  end
  cu:send(s.."\n")
  tmr.alarm(1, 60000, 1, function()
    cu:send(s.."\n")
  end)
end
run()

这是init.lua里面的代码:

--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config("ZXC","12345678")
--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)

运行出来结果是这个样子的:

set up wifi mode
> IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
Config done, IP is 172.29.225.3
{"M":"checkin","K":"a9135a64c","ID":"1890"}
PANIC: unprotected error in call to Lua API (kaiguan.lua:39: not connected)

而且NODEMCU会一直重启,反复执行上面动作,到底是怎么回事哇。。。。跪求各位大神。。。

 


评论:共3条

贝壳物联 评论于:2017-04-18 09:12:05
应该是nodemcu固件缺少相应模块,第39行调用的接口没有
如三月兮 回复于:2017-04-18 12:10:47
回复 @贝壳物联:cjson,crypto,file,gpio,http,mqtt,net,node,pwm,tmr,uart,wifi 这些就是我刷的固件里面的模块,好像不少什么东西吧,net这个模块也有的。。
niwusong 评论于:2017-05-18 06:48:30
程序写的很好
返回顶部