Initial commit
commit
877bb0b0a0
|
@ -0,0 +1 @@
|
|||
config.lua
|
|
@ -0,0 +1,21 @@
|
|||
-- vim: set sw=4 ts=4 sts=4 et :
|
||||
|
||||
config = {
|
||||
wifi = {
|
||||
ssid = "homeassistant",
|
||||
pwd = "somereallylongandsecurewpapassphrasehopefully"
|
||||
},
|
||||
mqtt = {
|
||||
host = "192.168.1.10",
|
||||
clientid = "client1",
|
||||
user = "homeassistant",
|
||||
pwd = "mqttpassword",
|
||||
keepalive = 120,
|
||||
topic = "buttons/basement"
|
||||
},
|
||||
buttons = {
|
||||
[1] = "theatre_lights",
|
||||
[2] = "main_basement_lights",
|
||||
[7] = "office_lights"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
-- vim: set sw=4 ts=4 sts=4 et :
|
||||
|
||||
function main()
|
||||
dofile("config.lua")
|
||||
dofile("mqttbutton.lua")
|
||||
|
||||
mqttbutton_main()
|
||||
end
|
||||
|
||||
-- Wait 1 second before starting. If there is a bug that crashes the
|
||||
-- microcontroller, this hopefully gives enough time to delete the program.
|
||||
tmr.alarm(0, 1000, 0, main)
|
|
@ -0,0 +1,87 @@
|
|||
-- vim: set sw=4 ts=4 sts=4 et :
|
||||
|
||||
WIFI_TIMER_ID = 1
|
||||
|
||||
function gpiosetup()
|
||||
local k, v
|
||||
if (config.buttons ~= nil) then
|
||||
for pin, payload in pairs(config.buttons) do
|
||||
gpio.mode(pin, gpio.INT)
|
||||
gpio.trig(pin, "down", function(level)
|
||||
if (gpio.read(pin) == gpio.LOW) then
|
||||
on_button_press(pin, payload)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function mqttsetup()
|
||||
if (mqtt_client == nil) then
|
||||
mqtt_client = mqtt.Client(
|
||||
config.mqtt.clientid,
|
||||
config.mqtt.keepalive,
|
||||
config.mqtt.user,
|
||||
config.mqtt.pwd
|
||||
)
|
||||
mqtt_client:on("connect", on_mqtt_connected)
|
||||
mqtt_client:on("offline", on_mqtt_disconnected)
|
||||
end
|
||||
mqtt_client:connect(config.mqtt.host)
|
||||
end
|
||||
|
||||
function on_button_press(pin, payload)
|
||||
print("Button pressed: " .. pin)
|
||||
if (mqtt_client ~= nil) then
|
||||
mqtt_client:publish(config.mqtt.topic, payload, 0, 0)
|
||||
end
|
||||
end
|
||||
|
||||
function on_mqtt_connected(client)
|
||||
print("MQTT Connected")
|
||||
end
|
||||
|
||||
function on_mqtt_disconnected(client)
|
||||
print("MQTT Disconnected")
|
||||
client:close()
|
||||
on_wifi_timer()
|
||||
end
|
||||
|
||||
function on_wifi_connected()
|
||||
mqttsetup()
|
||||
end
|
||||
|
||||
function on_wifi_timer()
|
||||
local status = wifi.sta.status()
|
||||
if (status == 0) then -- Idle
|
||||
wifi.sta.connect()
|
||||
tmr.alarm(WIFI_TIMER_ID, 250, 0, on_wifi_timer)
|
||||
elseif (status == 1) then -- Connecting
|
||||
tmr.alarm(WIFI_TIMER_ID, 250, 0, on_wifi_timer)
|
||||
elseif (status == 5) then -- Connected
|
||||
local ip = wifi.sta.getip()
|
||||
if (ip ~= nil) then
|
||||
print("WiFi Connected: " .. wifi.sta.getip())
|
||||
on_wifi_connected()
|
||||
else
|
||||
tmr.alarm(WIFI_TIMER_ID, 250, 0, on_wifi_timer)
|
||||
end
|
||||
else -- Error
|
||||
print("WiFi Connection Error: " .. status)
|
||||
tmr.alarm(WIFI_TIMER_ID, 1000, 0, on_wifi_timer)
|
||||
end
|
||||
end
|
||||
|
||||
function mqttbutton_main()
|
||||
wifisetup()
|
||||
gpiosetup()
|
||||
end
|
||||
|
||||
function wifisetup()
|
||||
if (wifi.getmode() ~= wifi.STATION) then
|
||||
wifi.setmode(wifi.STATION)
|
||||
end
|
||||
|
||||
wifi.sta.config(config.wifi.ssid, config.wifi.pwd)
|
||||
tmr.alarm(WIFI_TIMER_ID, 250, 0, on_wifi_timer)
|
||||
end
|
Loading…
Reference in New Issue