Mqtt Commissioning: Difference between revisions
Jump to navigation
Jump to search
| (7 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
[[category:ESP]] | |||
== Sequence == | |||
A first draft, needs more work: | |||
<uml> | |||
actor Actor as "IOT Friend" | |||
participant Mobile as "Mobile Device" | |||
participant ESP | |||
participant MqttBroker as "Mqtt Broker" | |||
Actor --> ESP : power up | |||
activate ESP | |||
ESP -> ESP : start access point\ndefault setup:\n ssid: AP[CHIPID]\n pass: default\n ip: 10.10.10.10 | |||
ESP -> ESP : start webserver | |||
deactivate ESP | |||
Actor --> Mobile : connect the ESP | |||
activate Mobile | |||
Mobile -> ESP : connect wifi to AP[CHIPID] | |||
activate ESP | |||
Mobile -> ESP : connect to webserver 10.10.10.10 | |||
Mobile -> ESP : set home-wifi-ssid\nset home-wifi-pass\nset mqtt-borker\n[optional] change: time-server\n[optional] change: AP-ssid\n[optional] change: AP-pass | |||
Mobile -> ESP : reboot | |||
deactivate Mobile | |||
ESP -> ESP : connect to home-wifi\nstart AP mode only if unsuccessful\n or AP-button is hold down | |||
ESP -> "ntp.org" : get internet time | |||
ESP -> MqttBroker : connect | |||
ESP -> MqttBroker : subscribe(topic='/node/config/[CHIPID]')\n(to retrieve configuration) | |||
ESP -> MqttBroker : publish(\n topic='/node/heartbeat/[CHIPID]',\n message="[TIMENOW]"\n retain=true)\n(to signal its presents) | |||
deactivate ESP | |||
Actor --> Mobile : connect to browser console | |||
activate Mobile | |||
Mobile -> MqttBroker : subscribe(topic='/node/heartbeat/+')\n receives list of connected devices and last boot time | |||
Mobile -> MqttBroker : subscribe(topic='/node/output/+')\n receives console output from each device | |||
Actor --> Mobile : commission the ESP | |||
Mobile -> MqttBroker : publish(\n topic='/node/config/[CHIPID]',\n message="[Lua Program]"\n retain=true) | |||
deactivate Mobile | |||
activate MqttBroker | |||
MqttBroker -> ESP : mqtt-forward([Lua Program]) | |||
deactivate MqttBroker | |||
activate ESP | |||
ESP -> ESP : run the Lua Program | |||
deactivate ESP | |||
</uml> | |||
== Lua Code == | == Lua Code == | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 28: | Line 75: | ||
(part 2) | (part 2) | ||
| <source lang="lua" line start="10"> | | <source lang="lua" line start="10"> | ||
tmr.softwd(30) -- restart if stall | tmr.softwd(30) -- restart if stall | ||
Latest revision as of 00:55, 6 May 2019
Sequence
A first draft, needs more work:
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) |
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)
|
|
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