Still drifting. It would be nice to know when does the device sync it’s time. It bothers me that the 2 systems don’t match even after wake up.
Here’s the device code.
// Monitor driveway alarm and send status to the internet.
// How long imp should sleep for.
SLEEP <- 3600;
// How long to delay after receiving alert from remote sensor.
DELAY <- 10;
// “visitor” light on the receiver.
visitor <- hardware.pin1;
// “low bat” light on the receiver.
low_battery <- hardware.pin2;
// Transistor switch to reset unit to turn off “visitor” light.
reset_button <- hardware.pin5;
// “power” light on receiver. Can be used to check when recovered from reset.
power_led <- hardware.pin9;
low_battery.configure(DIGITAL_IN);
visitor.configure(DIGITAL_IN_WAKEUP);
power_led.configure(DIGITAL_IN);
reset_button.configure(DIGITAL_OUT, 0);
imp.onidle(function() {
// Go to sleep and wake up on the hour.
server.sleepfor(SLEEP - (time() % SLEEP));
});
server.log(“Waking up…”);
// Get the time we woke up.
local now = time();
// DEBUG date/time logging error.
local tdebug = date(now);
server.log(“Device Time: " + format(”%02u", tdebug.hour) + “:” + format("%02u", tdebug.min) + “:” + format("%02u", tdebug.sec));
// END OF DEBUG
// Check/create vars for flags in NV memory.
// These flags are used so that only 1 message is sent
// if the alerts are still triggered right after a reset.
if (!(“nv” in getroottable() && “lowBat” in nv)) {
server.log(“In NV init”);
nv <- { “lowBat” : 0, “lastAlert” : 0 };
}
// We woke up from sleep or driveway alert.
// First check if driveway alert was activated.
if (visitor.read() == 1) {
server.log(“Driveway alert triggered”);
agent.send(“visitor”, now);
nv.lastAlert = now;
// Delay for DELAY seconds so that a unit reset doesn't trigger multiple
// alarms if someone drives slowly across the sensor.
while ((time() - now) < DELAY) { imp.sleep(0.1); }
// We have to reset the receiver in order to reset the visitor LED
reset_button.write(1);
imp.sleep(0.5);
reset_button.write(0);
// Wait until the unit has reset.
while (power_led.read() != 1) { imp.sleep(0.1); }
}
// Did we get a low battery indication?
if (low_battery.read() == 1) {
if (nv.lowBat == 0) {
server.log(“The outside driveway sensor has a low battery”);
agent.send(“lowBattery”, now);
nv.lowBat = 1;
}
} else {
if (nv.lowBat == 1) { nv.lowBat = 0 };
}