You should be seeing output with that, however your code is not doing what the description says it wants; you’re queueing 10 wakeups to fire in 0.5s all of which will immediately call measureBrightness(), so there will be 10 readings logged immediately.
What you want is probably this:
`OnCount <- 0
OffCount <- 0
const threshold = 37000 // readings above 37000 (at night) mean the lamp is illuminated.
readings <- 0
// Function to take and record a light reading, using Imp’s built-in light sensor
function measureBrightness() {
local brightness = hardware.lightlevel()
server.log("Brightness: " + brightness) // Remove after testing.
if (brightness >= threshold)
OnCount += 1
else
OffCount += 1
// If we've not done this 10 times, do it again in 0.5s
if (++readings < 10) imp.wakeup(0.5, measureBrightness);
}
// Measure light level ten times at 0.5 second intervals
measureBrightness();`
Tomcox,
Do you have other code after this? Such as a while(1) or long imp.sleep()s? It sounds like timers are never firing for you which would indicate something is blocking them.
`
// Beacon Monitor program for Imp device
//
// (Beacon cycle is about 1.9 seconds on and 0.8 second off)
//
// Initialize
onCount <- 0
offCount <- 0
readings <- 0
const threshold = 37000 // readings above 37000 (at night) mean the lamp is illuminated.
// Function to take and record a light reading, using Imp’s built-in light sensor
function measureBrightness() {
local brightness = hardware.lightlevel()
server.log("Brightness: " + brightness) // Remove after testing.
if (brightness >= threshold)
onCount += 1
else
offCount += 1
// If we've not done this less than ten times, do it again in 0.5s
if (++readings < 10) imp.wakeup(0.5, measureBrightness);
}
// Measure light level ten times at 0.5 second intervals
measureBrightness();
// Publish the totals (remove after testing)
server.log("ON: " + onCount)
server.log("OFF: " + offCount)
// Send beacon status to agent, but ONLY if time is between midnight and 2 a.m. This is
// to avoid messages that would be triggered by cycling imp power.
local now = date()
local currentHour = now.hour
if ((currentHour > 5) && (currentHour < 7)) { // 5 UTC is midnight CDT. 7 UTC is 2 am CDT.
if ((onCount > 0) && (offCount > 0)) // If flashing, we should have a mixture of on and off readings.
agent.send(“beacon”, “flashing”)
else
agent.send(“beacon”, “NOT flashing”)
}
The code is doing exactly what you’ve asked it to do.
measureBrightness() is executed the first time and the next call of it is scheduled in 0.5s
In the meantime, it prints out your onCount and Offcount and sends your beacon. Your problem starts with imp.onidle(). Even though you have cued measureBrightness to execute 10 times, the CPU will be idle in between.
The upshot of this is that your imp will go to sleep immediately after setting up your idle handler and wakeup at 1am, execute another measureBrightness again before going to sleep until the next day.
To operate the way (I think) you intend, you should grab all the code below your call to measureBrightness and stick it in an else {} block after if (++readings < 10) imp.wakeup(0.5, measureBrightness);