-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinit.lua
150 lines (137 loc) · 5.07 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- variables
wificfg={}
STATUS_REGISTER=0
STATUS_REGISTER_TEMP=0
MQTT_REGISTER=0
SSID_REGISTER=0
GOT_IP_FLAG=3
SSID_RECEIVED_FLAG=4
WIFIPASSWORD_RECEIVED_FLAG=5
MQTT_CONNECTED_FLAG=6
UART_TERMINATOR1='\r'
UART_TERMINATORS='[\r\n]'
MYMAC=wifi.sta.getmac()
-- split MAC address into two strings without colons
MYMACA=string.sub(MYMAC,0,2) .. string.sub(MYMAC,4,5) .. string.sub(MYMAC,7,8)
MYMACB=string.sub(MYMAC,10,11) .. string.sub(MYMAC,13,14) .. string.sub(MYMAC,16,17)
MQTTCLIENTID = "nodemcu" .. MYMACB
MQTTSRV = 'mqtt.dmz6.net'
MQTTPORT = 1883
MQTTUSER = nil
MQTTPASS = nil
MQTTRECONNECT=false
-- dont set MQTTKEEPALIVE too low!
MQTTKEEPALIVE=15
MQTTKEEPALIVETOPIC='dummy'
RXTOPIC = "dev/" .. MQTTCLIENTID .. "/rx"
TXTOPIC = "dev/" .. MQTTCLIENTID .. "/tx"
BCASTTOPIC = "clients"
CMDTOPIC = "clients/cmd"
-- setup ESP8266 as WIFI client
wifi.setmode(wifi.STATION)
-- update status register periodicaly
tmr.alarm(1, 250, 1, function()
STATUS_REGISTER=wifi.sta.status()
STATUS_REGISTER=bit.bor(STATUS_REGISTER, MQTT_REGISTER, SSID_REGISTER)
-- send update
if STATUS_REGISTER_TEMP ~= STATUS_REGISTER then
uart.write(0, STATUS_REGISTER, UART_TERMINATOR1)
end
STATUS_REGISTER_TEMP=STATUS_REGISTER
end)
-- try to reconnect to MQTT if not connected
tmr.alarm(2, 5000, 1, function()
if bit.isclear(MQTT_REGISTER, MQTT_CONNECTED_FLAG) and MQTTRECONNECT then
mqtt_connect()
end
end)
tmr.alarm(4, (MQTTKEEPALIVE-1)*1000, 1, function()
if bit.isset(MQTT_REGISTER, MQTT_CONNECTED_FLAG) then
m:publish(MQTTKEEPALIVETOPIC,'.', 0, 0, function(conn) end )
end
end)
-- initialise custom UART handler
-- be careful as this steals LUA interpreter
uart.on("data", UART_TERMINATOR1, function(data)
-- uart.write(0, STATUS_REGISTER, UART_TERMINATOR1)
local s = string.gsub(data, UART_TERMINATORS, "") -- remove termination characters
if s == 'uartstop' then
-- return to lua interpreter
uart.on('data')
-- close MQTT connection
if bit.isset(MQTT_REGISTER, MQTT_CONNECTED_FLAG) then
MQTTRECONNECT=false
m:close()
end
elseif string.byte(s,1) == 0xf0 then
-- dont pass to MQTT
elseif string.byte(s,1) == 0xf3 then
-- disconnect from MQTT and restart
uart.on('data')
if bit.isset(MQTT_REGISTER, MQTT_CONNECTED_FLAG) then
MQTTRECONNECT=false
m:close()
end
node.restart()
-- read and store SSID
elseif bit.isclear(SSID_REGISTER, SSID_RECEIVED_FLAG) then
wificfg.ssid=s
SSID_REGISTER=bit.set(SSID_REGISTER, SSID_RECEIVED_FLAG)
-- read and store WIFI passphrase, connect to AP, exec MQTT script
elseif bit.isclear(SSID_REGISTER, WIFIPASSWORD_RECEIVED_FLAG) then
wificfg.pwd=s
SSID_REGISTER=bit.set(SSID_REGISTER, WIFIPASSWORD_RECEIVED_FLAG)
wifi.sta.config(wificfg.ssid, wificfg.pwd)
wifi.sta.autoconnect(1)
tmr.alarm(3, 200, 1, function()
if wifi.sta.getip()== nil then
else
tmr.stop(3)
SSID_REGISTER=bit.set(SSID_REGISTER, GOT_IP_FLAG)
-- set reconnect flag and let tmr.alarm(2, ...) kick in
MQTTRECONNECT=true
end
end)
elseif bit.isset(MQTT_REGISTER, MQTT_CONNECTED_FLAG) then
m:publish(TXTOPIC, s, 0, 0, function(conn) end )
else
-- uh-oh?
uart.write(0, 'undefined error', UART_TERMINATOR1)
end
uart.write(0, STATUS_REGISTER, UART_TERMINATOR1)
end, 0)
-- initialise mqtt_connect() function, client object and events
mqtt_connect = function ()
-- if not connected then
if bit.isclear(MQTT_REGISTER, MQTT_CONNECTED_FLAG) then
m:connect(MQTTSRV, MQTTPORT, 0, function(conn)
m:subscribe(RXTOPIC, 0, function(conn) end )
m:subscribe(CMDTOPIC, 0, function(conn) end )
m:publish(BCASTTOPIC, "hi " .. MQTTCLIENTID .. " " .. wifi.sta.getip(), 0, 0, function(conn) end )
MQTT_REGISTER=bit.set(MQTT_REGISTER, MQTT_CONNECTED_FLAG)
-- uart.write(0, STATUS_REGISTER, data, UART_TERMINATOR1)
end)
else
end
end
m = mqtt.Client(MQTTCLIENTID, MQTTKEEPALIVE, MQTTUSER, MQTTPASS)
-- last will and testament
m:lwt(BCASTTOPIC, "bye ".. MQTTCLIENTID, 0, 0)
-- event handlers
m:on("connect", function(conn)
-- this event seems dead but its documented so...
MQTT_REGISTER=bit.set(MQTT_REGISTER, MQTT_CONNECTED_FLAG)
end)
m:on("offline", function(conn)
MQTT_REGISTER=bit.clear(MQTT_REGISTER, MQTT_CONNECTED_FLAG)
end)
m:on("message", function(conn, topic, data)
-- respond to 'ping' and 'heap' otherwise pass to UART after status register
if data == 'ping' and topic == CMDTOPIC then
m:publish(BCASTTOPIC, "pong " .. MQTTCLIENTID .. " " .. wifi.sta.getip(), 0, 0, function(conn) end )
elseif data == 'heap' and topic == CMDTOPIC then
m:publish(BCASTTOPIC, "heap " .. MQTTCLIENTID .. " " .. node.heap(), 0, 0, function(conn) end )
elseif data ~= nil and topic == RXTOPIC then
uart.write(0, STATUS_REGISTER, data, UART_TERMINATOR1)
end
end)