-
Notifications
You must be signed in to change notification settings - Fork 4
/
mqtt-button.lua
67 lines (49 loc) · 1.72 KB
/
mqtt-button.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
dofile("settings.lua")
myMACAddress = wifi.sta.getmac()
buttonUser = 1
buttonTopic = "buttons/" .. myMACAddress
wifi.setmode(wifi.STATION)
wifi.sta.config(wifiSSID, wifiPassword)
wifi.sta.connect()
-- Debounce code based on
-- https://gist.github.com/marcelstoer/59563e791effa4acb65f
function debounce (func)
local last = 0
local delay = 200 * 1000 -- 200ms * 1000 as tmr.now() has μs resolution
return function (...)
local now = tmr.now()
local delta = now - last
if delta < 0 then delta = delta + 2147483647 end; -- proposed because of delta rolling over, https://github.com/hackhitchin/esp8266-co-uk/issues/2
if delta < delay then return end;
last = now
return func(...)
end
end
local function buttonUserCallback(level)
mqttClient:publish(buttonTopic, 1 - level, 0, 0)
end
local function wifiConnected()
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
ip, netmask, gateway = wifi.sta.getip()
print("Connected to " .. ssid .. " with ip address " .. ip)
mqttClient = mqtt.Client("myclient", 120, mqttUser, mqttPassword)
mqttClient:on("connect", function(client)
print ("MQTT connected")
print("Publishing button presses at '" .. buttonTopic .. "'")
gpio.mode(buttonUser, gpio.INT, gpio.PULLUP)
gpio.trig(buttonUser, "both", debounce(buttonUserCallback))
end)
mqttClient:on("offline", function(client)
print ("MQTT offline")
end)
mqttClient:connect(mqttServerAddress, mqttPort, 0, 1)
end
local function checkConnection()
if (1 == wifi.sta.status()) then
print("Not connected")
else
tmr.stop(1)
wifiConnected()
end
end
tmr.alarm(1,1000, 1, checkConnection)