NodeMCU之简易init.lua控制led灯脚本

未结帖
0 639
ajian admin_jim 2017-11-12
悬赏:5飞吻
mySSID="xxxx"
myPWD="xxxxxxxxx"
wifi.setmode(wifi.STATION)
wifi.sta.config(mySSID,myPWD)
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
     if wifi.sta.getip() == nil then
         print("Connecting...")
     else
         tmr.stop(1)
         print("Connected, IP is "..wifi.sta.getip())
		 --dofile("led.lua")
         print("here")
         mqttServerIP = "123.56.160.221"
                  mqttClientID = "/nodemcu2"
                    
                  lighton=0
                  pin=4
                  gpio.mode(pin,gpio.OUTPUT)
                    
                  m = mqtt.Client(mqttClientID, 120)
                    
                  m:lwt("/lwt", "offline", 0, 0)
                    
                  m:on("connect", function(client) print ("connected") end)
                  m:on("offline", function(client) print ("offline") end)
                    
                  m:on("message",
                      function(client, topic, data)
                          if data == "1" then
                              gpio.write(pin,gpio.HIGH)
                          end
                          if data == "0" then
                              gpio.write(pin,gpio.LOW)
                          end
                      end
                  )
                    
                  m:connect(mqttServerIP, 1883, 0,
                      function(client)
                          print("mqtt connected")
                          m:subscribe("/nodemcu2/LED",0, function(client) print("subscribe success") end)
                      end,
                      function(client, reason)
                          print("mqtt connection failed reason: "..reason)
                      end
                  )
         
     end
end)

简易脚本

热忱回答0


最近热帖

近期热议

  1. javascript——prototype与__proto 9
  2. Mysql 中出现的Data truncated for column 3
  3. 在nginx中使用x-sendfile的解决方案 3
  4. 高版本jQuery面插件实现Ajax上传图片 1
  5. Thinkphp Socket.class.php 类的使用 1
  6. 使用ionic3创建第一个App 0
  7. ios-oc html5 0
  8. nginx.conf 0
  9. 基于ionic3.4.0的项目搭建 0
  10. php 缩略图 0