Skip to content

Instantly share code, notes, and snippets.

@lart2150
Last active April 28, 2019 16:10
Show Gist options
  • Select an option

  • Save lart2150/07f5ac2ad783c941599a79c5b78fd472 to your computer and use it in GitHub Desktop.

Select an option

Save lart2150/07f5ac2ad783c941599a79c5b78fd472 to your computer and use it in GitHub Desktop.
Simple NodeMCU alarm
--[[
I have modules adc,cron,crypto,encoder,file,gpio,http,mqtt,net,node,rtctime,sjson,sntp,struct,tmr,uart,websocket,wifi,tls
uses the tz lib at https://github.com/nodemcu/nodemcu-firmware/tree/master/lua_examples/timezone
logs to multicast udp port 4000 and to the serial
--]]
local bounce = 300 -- Debounce period. 200ms time used to debounce switches
local bounceCnt = 2
local alarmCnt = {}
currentStates = {}
lastTiggerStart = {}
timezoneOffset = -18000
tz = nil
print("starting init...")
udpSocket = net.createUDPSocket()
udpSocket:listen(5000)
udpSocket:send(4000, "255.255.255.255", "starting init...")
function httpGetWithRetry(url)
http.get(url, nil, function(code, data)
logMessage("HTTP response code " .. code)
if (code < 0) then
logMessage("HTTP request failed")
end
end)
end
function logMessage(msg)
local timeSeconds = rtctime.get() + timezoneOffset
if (timeSeconds < 1) then
timeSeconds = 0
end
local tm = rtctime.epoch2cal(timeSeconds)
local time = string.format("%02d:%02d:%02d", tm["hour"], tm["min"], tm["sec"])
udpSocket:send(4000, "255.255.255.255", time .. ": " .. msg)
print(time .. ": " .. msg)
end
function runNtpSync()
sntp.sync("192.168.1.1",
function(sec, usec, server, info)
logMessage('Ntp ' .. sec .. "." .. usec)
logMessage('Uptime hours ' .. (tmr.time() / 3600))
timezoneOffset = tz.getoffset(rtctime.get())
end,
function()
print('failed to sync ntp!')
end ,
nil,
1
)
end
for i=1,7 do
print("init pin: " .. i)
gpio.mode(i, gpio.INT, gpio.PULLUP)
local pin = i
local function triggerCallback(level)
if currentStates[pin] == level then
logMessage("state didn't change on pin: " .. pin)
return -- state is the same so don't let the trigger fire
end
local timeNow = rtctime.get()
if timeNow == lastTiggerStart[pin] then
logMessage("pin changed too recently: " .. pin .. " - " .. lastTiggerStart[pin])
return
end
lastTiggerStart[pin] = timeNow
alarmCnt[pin] = 0;
logMessage("Startime timer for pin " .. pin .. " - " .. level)
local sensorTimer = tmr.create()
sensorTimer:register(bounce, tmr.ALARM_AUTO, function()
local newLevel = gpio.read(pin);
if newLevel ~= level then
logMessage ("Bounce " .. pin)
sensorTimer:unregister()
-- lastTiggerStart[pin] = 0
return
end
if alarmCnt[pin] < bounceCnt then
alarmCnt[pin] = alarmCnt[pin] + 1
-- lastTiggerStart[pin] = 0
return
end
-- lastTiggerStart[pin] = 0
currentStates[pin] = level
logMessage("State Change pin: " .. pin .. " - " .. level)
sensorTimer:unregister()
httpGetWithRetry("https://example.com/api/door-event/?doorPort=" .. pin .. "&eventType=" .. newLevel)
end)
sensorTimer:start()
end
lastTiggerStart[pin] = -100;
gpio.trig(i, "both", triggerCallback)
currentStates[pin] = gpio.read(pin);
end
print("running...")
udpSocket:send(4000, "255.255.255.255", "running...")
tz = require('tz')
tz.setzone('chicago')
runNtpSync()
local ntpTimer = tmr.create()
ntpTimer:register(
900000,
tmr.ALARM_AUTO,
function (t)
runNtpSync()
end
)
ntpTimer:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment