Deep Sleep Temperature Sensor
I have a temperature sensor that I want to last for a very long time running from battery so I want to use best practices with deep sleep and collecting readings while disconnected. I also want to use best practices where the agent persists settings for the device. There will also be a mobile app that can be used to set two values: Threshold temperature (degrees F) and time to take readings before logging (in minutes)
Here is what I am trying to accomplish:
- When the code first runs, it will immediately enter deep sleep mode
- When pin 1 goes high, the device will wake up and read settings from agent
- The device will then disconnect while it is taking temperature readings and only connect after a certain amount of time to send temperature log to the agent for saving.
- Once the log is written, the device will go back to deep sleep waiting for pin 1 to wake.
- The device will take a temperature reading every 15 seconds and use it to calculate an average temperature every minute. This minute average will be saved in an array to later send to the agent.
- While reading temperatures, ignore pin 1. It is only used to wake up the device from deep sleep.
- When a temperature reading first exceeds the temperature threshold, log that time.
- Time logging doesn’t need high precision.
- Battery life is of high importance.
- The device could wake up from between 0 and 10 times a day to take readings.
- Temperature readings can be gathered from 1 to 60 minutes before logging.
- I am using a thermistor on pin 5 to take temperature readings.
In summary, there will be the following states:
- Deep sleep
- Wake and read settings (WiFi connected)
- Collect readings (WiFi off)
- Send log to server (WiFi connected)
- Deep sleep (repeat)
I have the code below but it is not working as expected.
Agent Code
`
thresholdTemp <- 99.0;
logTime <- 2;
// default settings
settings <- {};
settings.thresholdTemp <- thresholdTemp;
settings.logTime <- logTime;
// get saved settings if present
local savedSettings = server.load();
if (savedSettings.len() != 0) settings = savedSettings;
local error = server.save(settings); // if error == 0 then save successful
device.on(“get.settings”, function (noData)
{
server.log(“sending settings to device”);
device.send(“set.settings”, settings);
});
device.on(“log”, function (log)
{
//persist log in some 3rd party store
server.log("Data logged:");
server.log("Start time: " + log.startTime);
server.log("End time: " + log.endTime);
if (log.thresholdTime > 0)
{
server.log("Threshold time: " + log.thresholdTime);
}
server.log("Temp reading count: " + log.temps.len());
});
// set settings from http request`
Device Code
`const MAXSLEEP = 86396; // max amount of time to sleep (1 day)
const TEMP_INTERVAL = 15; // Interval in seconds to read temperature from sensor
// these constants are particular to the thermistor we’re using
// check your datasheet for what values you should be using
const B_THERM = 4038.0;
const T0_THERM = 298.15;
// the resistor in the circuit (10KO)
const R2 = 10000.0;
// default settings
thresholdTemp <- -1;
logTime <- -1;
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30.0);
wake <- hardware.pin1;
therm <- hardware.pin5;
therm.configure(ANALOG_IN);
currentTemp <- 0;
averageTemp <- 0;
tempTotal <- 0;
tempCounter <- 1;
thresholdLogged <- false;
logData <- {};
logData.startTime <- 0;
logData.thresholdTime <- 0;
logData.endTime <- 0;
logData.temps <- [];
agent.on(“set.settings”, function (data)
{
thresholdTemp = data.thresholdTemp;
logTime = data.logTime;
});
function goToDeepSleep() {
wake.configure(DIGITAL_IN_WAKEUP);
imp.onidle(function() {server.sleepfor(MAXSLEEP)});
}
function disconnect() {
server.log(“Disconnecting”);
server.flush(30);
server.disconnect();
}
function logConnectionHandler(reason)
{
if (reason == SERVER_CONNECTED)
{
agent.send(“log”, logData);
goToDeepSleep();
}
}
function awakeConnectionHandler(reason)
{
if (reason == SERVER_CONNECTED)
{
server.log(“device awake from pin1”);
server.log(“requesting settings from agent.”)
agent.send(“get.settings”, null);
logStartTime();
disconnect();
readTemperature();
}
}
function logStartTime()
{
logData.startTime <- time();
server.log("Log start time; " + logData.startTime);
}
function getTempInF() {
local Vin = hardware.voltage();
local Vout = Vin * therm.read() / 65535.0;
local R_Therm = (R2 * Vin / Vout) - R2;
local ln_therm = math.log(10000.0 / R_Therm);
local temp_K = (T0_THERM * B_THERM) / (B_THERM - T0_THERM * ln_therm);
local temp_C = temp_K - 273.15;
local temp_F = temp_C * 9.0 / 5.0 + 32.0;
return temp_F;
}
function readTemperature() {
local currentTime = time();
currentTemp = getTempInF();
if (!thresholdLogged && currentTemp > thresholdTemp)
{
logData.thresholdTime <- currentTime;
thresholdLogged = true;
}
tempTotal += currentTemp;
averageTemp = tempTotal / tempCounter;
if (tempCounter > (60 / TEMP_INTERVAL))
{
//write to log
logData.temps.append(averageTemp);
currentTemp = 0;
averageTemp = 0;
tempTotal = 0;
tempCounter = 0;
}
if (currentTime - logData.startTime > logTime * 60)
{
logData.endTime <- currentTime;
server.connect(logConnectionHandler, 60.0);
return;
}
tempCounter += 1;
imp.wakeup(TEMP_INTERVAL, readTemperature);
}
if (hardware.wakereason() == WAKEREASON_PIN1)
{
server.connect(awakeConnectionHandler, 60.0);
}
else
{
goToDeepSleep();
}`
Logs:
2014-10-09 13:13:34 UTC-7 [Agent] sending settings to device 2014-10-09 13:13:34 UTC-7 [Status] Device Booting; 6.83% program storage used 2014-10-09 13:13:34 UTC-7 [Device] device awake from pin1 2014-10-09 13:13:34 UTC-7 [Device] requesting settings from agent. 2014-10-09 13:13:34 UTC-7 [Device] Log start time; 1412885612 2014-10-09 13:13:34 UTC-7 [Device] Disconnecting 2014-10-09 13:13:36 UTC-7 [Agent] Data logged: 2014-10-09 13:13:36 UTC-7 [Agent] Start time: 1412885612 2014-10-09 13:13:36 UTC-7 [Agent] End time: 1412885613 2014-10-09 13:13:36 UTC-7 [Agent] Threshold time: 1412885613 2014-10-09 13:13:36 UTC-7 [Agent] Temperature reading count: 0 2014-10-09 13:13:36 UTC-7 [Status] Device disconnected 2014-10-09 13:13:36 UTC-7 [Status] Device Booting; 6.83% program storage used 2014-10-09 13:13:36 UTC-7 [Device] sleeping until 1412972012000 2014-10-09 13:14:31 UTC-7 [Agent] sending settings to device 2014-10-09 13:14:31 UTC-7 [Status] Device disconnected 2014-10-09 13:14:31 UTC-7 [Status] Device Booting; 6.83% program storage used 2014-10-09 13:14:31 UTC-7 [Device] device awake from pin1 2014-10-09 13:14:31 UTC-7 [Device] requesting settings from agent. 2014-10-09 13:14:31 UTC-7 [Device] Log start time; 1412885671 2014-10-09 13:14:31 UTC-7 [Device] Disconnecting 2014-10-09 13:14:33 UTC-7 [Agent] Data logged: 2014-10-09 13:14:33 UTC-7 [Agent] Start time: 1412885671 2014-10-09 13:14:33 UTC-7 [Agent] End time: 1412885671 2014-10-09 13:14:33 UTC-7 [Agent] Threshold time: 1412885671 2014-10-09 13:14:33 UTC-7 [Agent] Temperature reading count: 0 2014-10-09 13:14:33 UTC-7 [Status] Device Booting; 6.83% program storage used 2014-10-09 13:14:33 UTC-7 [Device] sleeping until 1412972069000