免费申请的汇思锐模块烧入NODEMCU测试成功(附源码)

作者:niwusong | 更新时间:2017-06-08 | 浏览量:1912

自认为比较符合逻辑通用的语言思路,做参考,原论坛中汇思锐代码是错误的,可能是故意写错的。

还有传感器的程序,见另外的源代码程序,还没有把这两个整合。

--init.lua
 if true then  --change to if true 
    ssid1="free"
    password1="??"
    ssid2="TP"
    password2="1???1"
    ssid3="freewifi"
    password3="********"
     g_mac=nil   
     print("set up wifi mode")
    wifi.setmode(wifi.STATION)
    wifi.sta.config(ssid3,password3)  
     wifi.sta.connect()
    cnt = 0
    tmr.alarm(1, 1000, tmr.ALARM_AUTO, function() 
  if (wifi.sta.getip() == nil) and (cnt < 20) then 
            print("IP1 unavaiable, Waiting...")
            cnt = cnt + 1   
     else    
        
            tmr.stop(1)
  if (cnt < 60) then print("IP:"..wifi.sta.getip())
                MAC=wifi.sta.getmac()
                mac=string.gsub(MAC,":","")
                g_mac = mac
                print("MAC:"..mac)
               
                dofile("runtime.lua") 
     else   

        
            end     
           end 
     end)
else  
    print("\n")
    print("Please edit 'init.lua' first:")
    print("Step 1: Modify wifi.sta.config() function in line 5 according settings of your wireless router.")
    print("Step 2: Change the 'if false' statement in line 1 to 'if true'.")
      node.restart()
end

--config.lua
host = host or "www.bigiot.net"
port = port or 8181
DEVICEID = "1347"
APIKEY = "09dd54b61"
LED1 = 2
LED2 = 5
LED3 = 6
LED4 = 1

--checkin.lua
if r.M == "WELCOME TO BIGIOT" then       --收到连接正常,发送checkin
    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" )
    bLive=0
    --定时心跳,防止掉线
    tmr.alarm(2, 40000, 1, function()
        --如果标记为1,表示未收到上次的心跳返回,重启
        if bLive==3 then
            node.restart()
        end
        ok, ticket = pcall(cjson.encode, {M="isOL",ID="D"..DEVICEID})
        print(ticket)
        cu:send(ticket.."\n" )
        --发送后将标记置为1
        bLive=bLive+1        
    end)
end
if r.M=="checkinok" then
    bOnLine=1
end


--runtime.lua
dofile("config.lua")

bLive=0
bOnLine=0

gpio.mode(LED1,gpio.OUTPUT)
gpio.mode(LED2,gpio.OUTPUT)
gpio.mode(LED3,gpio.OUTPUT)
gpio.mode(LED4,gpio.OUTPUT)

gpio.write(LED1, gpio.HIGH)
gpio.write(LED2, gpio.LOW)
gpio.write(LED3, gpio.HIGH)
gpio.write(LED4, gpio.HIGH)


cu = net.createConnection(net.TCP)
cu:connect(port, host)

cu:on("receive", function(cu, c) 
    print(c)
    r = cjson.decode(c)

    if r.M=="isOL" then
        bLive=0 
    end
    tmr.alarm(1, 10000, 1, function()
    
        dofile("checkin.lua")
 
          print(c)
    end)
  
    dofile("sayCommand.lua")

end)

 --sayCommand.lua
gpio.mode(config.LED1,gpio.OUTPUT)
gpio.mode(config.LED2,gpio.OUTPUT)
gpio.mode(config.LED3,gpio.OUTPUT)
gpio.mode(config.LED4,gpio.OUTPUT)

if r.M == "say" then     
    local commander=r.C    
    if commander == "play" then  
        print("play:LED1 turn on!")    
        gpio.write(config.LED1,gpio.LOW) 
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="play:LED1 turn on!"})
        cu:send( played.."\n" )
    elseif commander == "stop" then 
        print("stop:LED1 turn off!")         
        gpio.write(config.LED1,gpio.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="stop:LED1 turn off!"})
        cu:send( stoped.."\n" )     
       --------1--------
   elseif commander == "up" then 
        print("up:LED2 turn on!") 
        gpio.write(config.LED2,gpio.HIGH)
        ok, uped = pcall(cjson.encode, {M="say",ID=r.ID,C="up:LED2 turn on!"})
        cu:send( uped.."\n" )  
   elseif commander == "plus" then  
        print("plus:LED2 turn off!")       
        gpio.write(config.LED2,gpio.LOW)
        ok, plused = pcall(cjson.encode, {M="say",ID=r.ID,C="plus:LED2 turn off!"})
        cu:send( plused.."\n" )  
        --------2--------
   elseif commander == "pause" then  
        print("pause:LED3 turn on!") 
        gpio.write(config.LED3,gpio.LOW)
        ok, paused = pcall(cjson.encode, {M="say",ID=r.ID,C="pause:LED3 turn on!"})
        cu:send( paused.."\n" )  
   elseif commander == "right" then 
        print("right:LED3 turn off!")        
        gpio.write(config.LED3,gpio.HIGH)
        ok, righted = pcall(cjson.encode, {M="say",ID=r.ID,C="right:LED3 turn off!"})
        cu:send( righted.."\n" )  
        --------3--------
   elseif commander == "down" then 
        print("right:LED4 turn on!")        
        gpio.write(config.LED4,gpio.LOW)
        ok, downed = pcall(cjson.encode, {M="say",ID=r.ID,C="down:LED4 turn on!"})
        cu:send( downed.."\n" ) 
   elseif commander == "forward" then 
        print("forward:LED4 turn off!")        
        gpio.write(config.LED4,gpio.HIGH)
        ok, forward = pcall(cjson.encode, {M="say",ID=r.ID,C="forward:LED4 turn off!"})
        cu:send( forward.."\n" )  
        --------4--------        
   elseif commander == "offOn" then 
        print("offOn:LED ALL off!")        
        gpio.write(config.LED1,gpio.HIGH)
        gpio.write(config.LED2,gpio.LOW)
        gpio.write(config.LED3,gpio.HIGH)
        gpio.write(config.LED4,gpio.HIGH)
        ok, offOned = pcall(cjson.encode, {M="say",ID=r.ID,C="offOn:LED ALL off!"})
        cu:send( offOned.."\n" )         
   elseif commander == "minus" then 
        print("minus:LED ALL off!")        
        gpio.write(config.LED1,gpio.LOW)
        gpio.write(config.LED2,gpio.HIGH)
        gpio.write(config.LED3,gpio.LOW)
        gpio.write(config.LED4,gpio.LOW)
        ok, minused = pcall(cjson.encode, {M="say",ID=r.ID,C="minus:LED ALL ON!"})
        cu:send( minused.."\n" ) 
        --------5--------
   elseif commander == "left" then 
        print("^~^left^~^")        
       
        ok, lefted = pcall(cjson.encode, {M="say",ID=r.ID,C="^~^left^~^"})
        cu:send( lefted.."\n" ) 
   elseif commander == "backward" then 
        print("^~^backward^~^")      
       
        ok, forwarded = pcall(cjson.encode, {M="say",ID=r.ID,C="^~^forward^~^"})
        cu:send( forwarded.."\n" )     
    end  
end

 


评论:共2条

惟独113 评论于:2017-11-07 12:36:20
666
a18222970 评论于:2017-11-18 19:24:30
写的好,不过这个能在WIFI掉线重连的情况下链接服务器么?
返回顶部