Mqtt Commissioning: Difference between revisions

From Playful Exploration
Jump to navigation Jump to search
(Created page with "== Lua Code == {| class="wikitable" |- ! file !! lua code !! Notes |- | config.lua | <source lang="lua" line>wifi.setmode(wifi.STATION) wifi.sta.config("YOUR-SSID","YOUR-PASSW...")
 
Line 74: Line 74:
  /node/heartbeat/1a:fe:34:a0:e0:4d 1456378653
  /node/heartbeat/1a:fe:34:a0:e0:4d 1456378653


Now having the MAC we can commission the device by sending lua code to the '/node/config' topic for the given MAC:
Now having the MAC we can commission the device by sending lua code to the '/node/config' topic for the given MAC.  The -r option ensures the broker will retain this message, so the client will receive it's instructions automatically when rebooting:
<source lang="bash">
<source lang="bash">
mosquitto_pub -t "/node/config/1a:fe:34:a0:e0:4d" -r -m "pin=7 gpio.mode(pin, gpio.OUTPUT) local b=0 tmr.alarm(0, 500, 1, function() gpio.write(pin, b) if b==0 then b=1 else b=0 end end)"
mosquitto_pub -t "/node/config/1a:fe:34:a0:e0:4d" -r -m "pin=7 gpio.mode(pin, gpio.OUTPUT) local b=0 tmr.alarm(0, 500, 1, function() gpio.write(pin, b) if b==0 then b=1 else b=0 end end)"
Line 89: Line 89:
end)
end)
</source>
</source>
== UART dump of the ESP  ==
NodeMCU custom build by frightanic.com
        branch: master
        commit: c8037568571edb5c568c2f8231e4f8ce0683b883
        SSL: false
        modules: adc,bmp085,dht,enduser_setup,file,gpio,i2c,mqtt,net,node,pwm,rtctime,sntp,spi,tmr,uart,wifi,ws2812
  build  built on: 2016-02-07 06:45
  powered by Lua 5.1.4 on SDK 1.4.0
Hold gpio0 low to stop boot
...booting      8.6650390625
ready  8.712890625
Try sntp.sync ...
sntp.sync      1456382295      344798  128.4.24.98
Mqtt try connect
Mqtt Connected
Mqtt subscribes
Mqtt "/node/config/1a:fe:34:a0:e0:4d":
pin=7 gpio.mode(pin, gpio.OUTPUT) local b=0 tmr.alarm(0, 500, 1, function() gpio.write(pin, b) if b==0 then b=1 else b=0 e
Mqtt heartbeat sent

Revision as of 06:43, 25 February 2016

Lua Code

file lua code Notes
config.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR-SSID","YOUR-PASSWORD")
wifi.sta.connect()
MqttBroker = 'YOUR-MQTTBORKER'
setup file, connects the esp to the router
init.lua

(part 1)

print("\n\nHold gpio0 low to stop boot")
tmr.delay(1000000)
if gpio.read(3) == 0 then print("...boot stopped") return end
print("...booting", collectgarbage('count'))
print('ready',collectgarbage('count'))

dofile('config.lc')
Startup code that allows stopping the boot by holding GPIO0 low.
init.lua

(part 2)

-- restart if stall
tmr.softwd(30)

tmr.softwd(30)    -- restart if stall

action = function()
  print("Mqtt try connect")

  if m then m:close() m=nil collectgarbage() end
  m = mqtt.Client("clientid",-1)
  --m:lwt("/node/lwt/"..wifi.ap.getmac(), ''..rtctime.get(),0,1)
  m:on("offline", function(con) print("Mqtt offline") end)
  m:on("message", function(conn, topic, data) tmr.softwd(30) print('Mqtt "' .. topic .. '":' ) if data ~= nil then print(data) node.input(data) end end)
  m:connect(MqttBroker, 1883, 0, function(conn)
    print("Mqtt Connected")
    m:subscribe("/node/config/"..wifi.ap.getmac(), 0, function(conn) tmr.softwd(30) print("Mqtt subscribes") end)
    m:publish("/node/heartbeat/"..wifi.ap.getmac(),''..rtctime.get(),0,1, function(conn) print("Mqtt heartbeat sent") end)
  end)
end

try_sntp_sync = function()
  print('Try sntp.sync ...')
  sntp.sync('www.ntp.org', function(sec,usec,server) print('sntp.sync', sec, usec, server) end, function() print('Error: sntp.sync !') end)
end

tmr.alarm(0, 2000, tmr.ALARM_SEMI, function()
  if rtctime.get()==0 then try_sntp_sync() tmr.start(0)
  else action() end
end)
  1. Retrieves the accurate time from the time server
  2. Connects to Mqtt boker
  3. Subscribes topic '/node/config/[MAC-ADDRESS]' where it retrieves the instructions to execute
  4. Publishes it's current time to topic '/node/heartbeat/[MAC-ADDRESS]' to tell the broker it's booted up

Commissioning

Commissioning is currently done from a Linux command line using mosquitto mqtt client. The -t option defines the topic to subscribe to. The '#' in the topic is a wildcard.

mosquitto_sub -t "/node/heartbeat/#" -v

The response lists nodes that were ever booted up and their boot time in in Unix Epoch Time. In this example one node with MAC 1a:fe:34:a0:e0:4d was booted at 1456378653 Epoch Time:

/node/heartbeat/1a:fe:34:a0:e0:4d 1456378653

Now having the MAC we can commission the device by sending lua code to the '/node/config' topic for the given MAC. The -r option ensures the broker will retain this message, so the client will receive it's instructions automatically when rebooting:

mosquitto_pub -t "/node/config/1a:fe:34:a0:e0:4d" -r -m "pin=7 gpio.mode(pin, gpio.OUTPUT) local b=0 tmr.alarm(0, 500, 1, function() gpio.write(pin, b) if b==0 then b=1 else b=0 end end)"

The lua code uses a timer to toggle and LED every half a second:

pin=7
gpio.mode(pin, gpio.OUTPUT)
local b=0
tmr.alarm(0, 500, 1, function()
  gpio.write(pin, b)
  if b==0 then b=1
  else b=0 end
end)

UART dump of the ESP

NodeMCU custom build by frightanic.com
        branch: master
        commit: c8037568571edb5c568c2f8231e4f8ce0683b883
        SSL: false
        modules: adc,bmp085,dht,enduser_setup,file,gpio,i2c,mqtt,net,node,pwm,rtctime,sntp,spi,tmr,uart,wifi,ws2812
 build  built on: 2016-02-07 06:45
 powered by Lua 5.1.4 on SDK 1.4.0


Hold gpio0 low to stop boot
...booting      8.6650390625
ready   8.712890625
Try sntp.sync ...
sntp.sync       1456382295      344798  128.4.24.98
Mqtt try connect
Mqtt Connected
Mqtt subscribes
Mqtt "/node/config/1a:fe:34:a0:e0:4d":
pin=7 gpio.mode(pin, gpio.OUTPUT) local b=0 tmr.alarm(0, 500, 1, function() gpio.write(pin, b) if b==0 then b=1 else b=0 e
Mqtt heartbeat sent