Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/4386201 to your computer and use it in GitHub Desktop.
Save anonymous/4386201 to your computer and use it in GitHub Desktop.
Code for April Electric Imp board - temp logger using a TMP36 from adafruit
hardware.pin1.configure(ANALOG_IN);
// LED pin config
hardware.pin2.configure(DIGITAL_OUT_OD_PULLUP);
// initially set to off
hardware.pin2.write(0);
/*
If you don't have a Hannah board but want to try the
Cosm.com temperature logging exercise, here it is!
temperature sensor used is tmp36 from adafruit.com for $2.00
http://learn.adafruit.com/tmp36-temperature-sensor
temp range -40°C to 150°C / -40°F to 302°F
*/
local output = OutputPort("temp","number");
local postInit = false;
local oldTemp = null;
local reading = null;
local ratio = null;
local voltage = null;
local temperatureC = null;
local temperatureF = null;
local temp = null;
function update_temp() {
// keep the imp awake
imp.wakeup(10, update_temp);
// get the raw voltage value from temp sensor btw 0-65535
// in this case that needs mapping to the range 0-3.3v
reading = hardware.pin1.read();
// get the ratio
ratio = 65535.0 / reading;
// make units milivolts and get voltage we can work with
//voltage = (hardware.voltage() * 1000) / divider;
voltage = 3300 / ratio;
// get temperature in degrees Celsius
temperatureC = (voltage - 500) / 10.0;
server.log(postInit);
// convert to degrees Farenheit
temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
server.log("temp: " + temperatureF);
// set our output to desired temperature unit
temp = temperatureF;
if (postInit) {
// check that temp is not more than +-15 degrees F diff from oldTemp
if ( temp < (oldTemp - 15) || temp > (oldTemp + 15) ) {
return;
} else {
output.set(temp);
}
} else {
// set the old temp val for reference the first time
oldTemp = temp;
}
// over 70 is hot enought to trigger the alert light
if (temp > 70) {
hardware.pin2.write(1);
} else {
hardware.pin2.write(0);
}
// after function's first run set flag to true
postInit = true;
}
server.show(temp);
// Register with the server
imp.configure("Office Temp Sensor", [], [output]);
update_temp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment