Skip to content

Instantly share code, notes, and snippets.

@wytten
Forked from anonymous/april_temp_sensor
Last active January 3, 2016 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wytten/8521011 to your computer and use it in GitHub Desktop.
Save wytten/8521011 to your computer and use it in GitHub Desktop.
Updated original april_temp_sensor to remove use of deprecated methods, to remove the use of pin2 for activating the lamp, and to use CSV format for communication with xively.com. Also added agent code taken from https://xively.com/dev/tutorials/electric_imp to which I have made trivial modifications.
const FEED_ID = "YOUR_FEED_ID";
const API_KEY = "YOUR_API_KEY";
// Accept CSV from device, then format request to and accept response from xively.com
device.on("data", function(feedCSV) {
server.log(feedCSV);
local xively_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".csv";
local request = http.put(xively_url, {"X-ApiKey":API_KEY, "Content-Type":"text/csv", "User-Agent":"Xively-Imp-Lib/1.0"}, feedCSV);
local response = request.sendsync();
if(response.statuscode != 200) {
server.log("error code '" + response.statuscode + "' sending request: "+request);
}
});
hardware.pin1.configure(ANALOG_IN);
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
local reading = hardware.pin1.read();
// get the ratio
local ratio = 65535.0 / reading;
// make units milivolts and get voltage we can work with
//voltage = (hardware.voltage() * 1000) / divider;
local voltage = 3300 / ratio;
// get temperature in degrees Celsius
local temperatureC = (voltage - 500) / 10.0;
// convert to degrees Farenheit
local temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
server.log("temp: " + temperatureF);
// format our output as comma separated name value pair
local csv = "temp," + temperatureF;
agent.send("data", csv);
}
update_temp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment