-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
153 lines (144 loc) · 4.33 KB
/
main.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
151
152
153
dofile("wlancfg.lua")
print("Initialize Hardware")
gpio.mode(0, gpio.OUTPUT)
gpio.mode(5, gpio.OUTPUT)
gpio.mode(6, gpio.OUTPUT)
-- Alles aus machen
gpio.write(0, gpio.LOW)
gpio.write(5, gpio.LOW)
gpio.write(6, gpio.LOW)
mqttIPserver="10.23.42.10"
-- The Mqtt logic
m = mqtt.Client("trafficlight", 120, "user", "pass")
global_c=nil
function startTcpServer()
s=net.createServer(net.TCP, 180)
s:listen(2323,function(c)
global_c=c
function s_output(str)
if(global_c~=nil)
then global_c:send(str)
end
end
node.output(s_output, 0)
c:on("receive",function(c,l)
node.input(l)
end)
c:on("disconnection",function(c)
node.output(nil)
global_c=nil
end)
print("Welcome to the Trafficlight")
end)
end
function mqttsubscribe()
tmr.alarm(1,50,0,function()
m:subscribe("/room/trafficlight/+/command",0, function(conn)
print("subscribed")
m:publish("/room/trafficlight/ip",wifi.sta.getip(),0,0)
tmr.stop(0) -- stop startup watchdog
end)
end)
-- Send an alive ping each half minute
tmr.alarm(2,30000,1,function()
print("HEARTBEAT! " .. node.heap())
m:publish("/room/trafficlight/seconds",(tmr.now()/1000000),0,0)
end)
end
m:on("connect", mqttsubscribe)
m:on("offline", function(con)
print ("offline")
node.restart()
end)
m:on("message", function(conn, topic, data)
-- skipp emtpy messages
if (data == nil) then
return
end
print("MQTT " .. topic .. " -> " .. data)
if topic=="/room/trafficlight/green/command" then
if (data == "on") then
tmr.stop(4)
gpio.write(0, gpio.HIGH)
m:publish("/room/trafficlight/green/state","on",0,1)
elseif ( data == "off") then
tmr.stop(4)
gpio.write(0, gpio.LOW)
m:publish("/room/trafficlight/green/state","off",0,1)
else
for k, v in string.gmatch(data, "(%w+) ([0-9]+)") do
if (k == "blink") then
tmr.alarm(4,tonumber(v),1,function()
-- Toggle the Lamp
gpio.write(0, ( gpio.read(0) + 1) % 2)
end)
end
end
end
elseif topic=="/room/trafficlight/yellow/command" then
if (data == "on") then
tmr.stop(5)
gpio.write(5, gpio.HIGH)
m:publish("/room/trafficlight/yellow/state","on",0,1)
elseif ( data == "off") then
tmr.stop(5)
gpio.write(5, gpio.LOW)
m:publish("/room/trafficlight/yellow/state","off",0,1)
else
for k, v in string.gmatch(data, "(%w+) ([0-9]+)") do
if (k == "blink") then
tmr.alarm(5,tonumber(v),1,function()
-- Toggle the Lamp
gpio.write(5, ( gpio.read(5) + 1) % 2)
end)
end
end
end
elseif topic=="/room/trafficlight/red/command" then
if (data == "on") then
tmr.stop(6)
gpio.write(6, gpio.HIGH)
m:publish("/room/trafficlight/red/state","on",0,1)
elseif (data == "off") then
tmr.stop(6)
gpio.write(6, gpio.LOW)
m:publish("/room/trafficlight/red/state","off",0,1)
else
for k, v in string.gmatch(data, "(%w+) ([0-9]+)") do
if (k == "blink") then
tmr.alarm(6,tonumber(v),1,function()
-- Toggle the Lamp
gpio.write(6, ( gpio.read(6) + 1) % 2)
end)
end
end
end
end
end)
-- Wait to be connect to the WiFi access point.
startupStage = "wlan-setup"
tmr.alarm(0, 1000, 1, function()
if startupStage == "wlan-setup" then
if wifi.sta.status() ~= 5 then
print(tostring(tmr.now()/1000000) .. " Connecting to AP...")
gpio.write(5, ( gpio.read(5) + 1) % 2)
else
-- Switch of the booting lamp
gpio.write(5, gpio.LOW)
print('IP: ',wifi.sta.getip())
if m:connect(mqttIPserver,1883,0) == false then
print("MQTT connect failed!")
node.restart()
end
startTcpServer()
startupStage = "mqtt-setup"
end
end
if startupStage == "mqtt-setup" then
print(tostring(tmr.now()/1000000) .. " Connecting and subscribing to MQTT...")
end
if (tmr.now() / 1000000) > 60 then
print("Startup failed -> Rebooting")
node.restart()
end
end)