Created
October 28, 2013 01:09
-
-
Save StuffAndyMakes/7189910 to your computer and use it in GitHub Desktop.
iPotti #2 Electric Imp Device Squirrel Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // iPotti Number 2 - read value on ADC (ambient light sensor) to see if potti is in use | |
| const SLEEP_DURATION = 5; | |
| // nextReport counts up to FORCE_REPORT_COUNT and reports to server regardless of sensor reading | |
| // to le the server know the device is still alive, then counter resets and starts again | |
| const FORCE_REPORT_COUNT = 13; // about every two minutes | |
| const LIGHT_CHANGE_THRESHOLD = 2000; | |
| function readLighting() { | |
| local adcValue = hardware.pin9.read(); | |
| nv.nextReport++; | |
| if( ( math.abs( nv.lastValue - adcValue ) > LIGHT_CHANGE_THRESHOLD ) || | |
| ( nv.nextReport >= FORCE_REPORT_COUNT ) ) { | |
| // server.log output only works inside this conditional because we only fire up the WeeFees as needed | |
| // or when forced to by the nextReport counter | |
| local forceIt = false; | |
| if( nv.nextReport >= FORCE_REPORT_COUNT ) { | |
| nv.nextReport = 0; // reset minute counter for forced reporting | |
| forceIt = true; | |
| } | |
| nv.counter++; | |
| server.log( "iPotti Device Awake (Counter: " + nv.counter + ") (Next Report Counter: " + nv.nextReport + ")" ); | |
| local json = { | |
| impid = hardware.getimpeeid(), | |
| voltage = hardware.voltage(), | |
| signal = imp.rssi(), | |
| light = adcValue, | |
| counter = nv.counter, | |
| forcedReport = forceIt | |
| }; | |
| agent.send( "lightChanged", json ); | |
| nv.lastValue = adcValue; | |
| } | |
| imp.deepsleepfor( SLEEP_DURATION ); | |
| } | |
| hardware.pin9.configure( ANALOG_IN ); | |
| imp.setpowersave( true ); | |
| if( !( ("nv" in getroottable()) && ("lastValue" in nv) && ("counter" in nv) && ("nextReport" in nv) ) ) { | |
| nv <- { lastValue = 0, counter = 0, nextReport = 0 }; | |
| } | |
| imp.onidle( readLighting ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment