Skip to content

Instantly share code, notes, and snippets.

@openfirmware
Created February 4, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save openfirmware/8812152 to your computer and use it in GitHub Desktop.
Save openfirmware/8812152 to your computer and use it in GitHub Desktop.
Updated version of my NTC device and agent code, for use with the newer Electric Imp IDE and Xively.
const FEED_ID = "HERE";
const API_KEY = "YOUR KEY";
function send_xively(body) { //take in csv value
local xively_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".csv"; //setup url for csv
server.log(xively_url);
server.log(body); //pring body for testing
local req = http.put(xively_url, {"X-ApiKey":API_KEY, "Content-Type":"text/csv", "User-Agent":"Xively-Imp-Lib/1.0"}, body); //add headers
local res = req.sendsync(); //send request
if(res.statuscode != 200) {
server.log("error sending message: "+res.body);
}else device.send("status", (res.statuscode + " OK")); //sends status to uart. this can be removed if not desired
}
function iso8601date() {
local d = date();
local datestring = format("%04d-%02d-%02dT%02d:%02d:%02d", d.year, d.month+1, d.day, d.hour, d.min, d.sec); //ISO 8601
return datestring;
}
device.on("update", function(data) {
local date = iso8601date();
local feedCSV = "BatteryVoltage" + "," + date + "," + data["voltage"] + "\n";
feedCSV += "NTC" + "," + date + "," + data["temperature"] + "\n";
feedCSV += "Resistance" + "," + date + "," + data["resistance"];
send_xively(feedCSV);
});
/*
Copyright (C) 2013 electric imp, inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// April with a 10k, 1% from 3V3 to pin9 and 10k B57861S0103F040 NTC Thermistor from pin9 to pin8
// pin8 TEMP_READ_EN_L - drive low to enable temp reading (great for batteries!)
// pin9 ANALOG NTC value
// benchmarking runtime: first tick with hardware.micros
local tick = hardware.micros();
// turn on WiFi power save to reduce power consumption when awake
imp.setpowersave(true);
// Configure Pins
//
// pin 8 is driven high to turn off temp monitor (saves power) or low to read.
hardware.pin8.configure(DIGITAL_OUT);
hardware.pin8.write(1);
// pin 9 is the middle of the voltage divider formed by the NTC - read the
// analog voltage to determine temperature.
hardware.pin9.configure(ANALOG_IN);
// Constants
//
// All calculations are done in Kelvin. These are constants for this particular
// thermistor; if using a different one, check your datasheet.
const b_therm = 4540;
const t0_therm = 298.15;
// Battery Voltage
//
// To read the battery voltage reliably, we take 10 readings and average them.
local v_high = 0;
for(local i = 0; i < 10; i++){
imp.sleep(0.01);
v_high += hardware.voltage();
}
v_high = v_high / 10.0;
// Thermistor Reading
//
// Benchmarking: see how long thermistor is "on"
local ton = hardware.micros();
// Turn on the thermistor network
hardware.pin8.write(0);
// Gather several ADC readings and average them (just takes out some noise).
local val = 0;
for (local i = 0; i < 10; i++) {
imp.sleep(0.01);
val += hardware.pin9.read();
}
val = val / 10;
// Turn the thermistor network back off
hardware.pin8.write(1);
local toff = hardware.micros();
server.log(format("Thermistor Network on for %d microseconds", (toff - ton)));
// Scale the ADC reading to a voltage by dividing by the full-scale value and
// multiplying by the supply voltage
local v_therm = v_high * val / 65535.0;
// Calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (v_high / v_therm) - 1);
server.log("Current resistance is " + r_therm + " Ohms");
local ln_therm = math.log(10000.0 / r_therm);
local t_therm = (t0_therm * b_therm) / (b_therm - t0_therm * ln_therm) - 273.15;
// Format into a string for the string output port
local c_str = format("%.01f", t_therm);
server.log("Current temp is " + c_str + " C");
// Update the current battery voltage with a nicely-formatted string of the most
// recently-calculated value
local batt_str = format("%.02f V", v_high);
server.log("Battery Voltage is " + batt_str);
// Benchmarking runtime
local tock = hardware.micros();
server.log(format("Read cycle took %d microseconds", (tock - tick)));
// Send data to Agent
local agentData = {
"resistance": r_therm,
"temperature": t_therm,
"voltage": v_high
};
agent.send("update", agentData);
// Sleep for 15 minutes and 1 second, minus the time past the 0:15
// so we wake up near each 15 minute mark (prevents drifting on slow DHCP)
imp.onidle( function() {
server.sleepfor(1 + 15 * 60 - (time() % (15 * 60)));
});
// full firmware is reloaded and run from the top on each wake cycle, so no need
// to construct a loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment