Last active
May 11, 2018 02:37
-
-
Save technobly/bc42ae5913d8d89187f6633a91391bf0 to your computer and use it in GitHub Desktop.
Electron Low Power Sensor Example (Read a sensor every hour, publish once per day, deep sleep in-between) Raw
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
/* | |
****************************************************************************** | |
* Copyright (c) 2016 Particle Industries, Inc. All rights reserved. | |
* | |
* This library is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU Lesser General Public | |
* License as published by the Free Software Foundation, either | |
* version 3 of the License, or (at your option) any later version. | |
* | |
* This library is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public | |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
****************************************************************************** | |
*/ | |
#include "Particle.h" | |
// Use either MANUAL or SEMI_AUTOMATIC mode to keep the cellular modem off initially on boot | |
SYSTEM_MODE(MANUAL); | |
// SYSTEM_MODE(SEMI_AUTOMATIC); | |
// Time between sensor readings in seconds | |
#define SENSOR_READING_INTERVAL 60*60 | |
// We will publish the entire array when it's full | |
#define SENSOR_ARRAY_SIZE 24 | |
// If we lose power completely, expect these to reinitialize. | |
retained float sensor_readings[SENSOR_ARRAY_SIZE] = {0.0}; | |
retained int sensor_index = 0; | |
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); | |
void read_and_save_sensor() { | |
FuelGauge().quickStart(); | |
delay(200); | |
// These readings can be whatever you like, for example we are using PMIC SoC (%) | |
sensor_readings[sensor_index++] = FuelGauge().getSoC(); | |
} | |
void publish_sensor_readings() { | |
String results = ""; | |
int i = 0; | |
for (; i < SENSOR_ARRAY_SIZE-1; i++) { | |
results += String::format("%.2f", sensor_readings[i]) + String(","); | |
} | |
results += String::format("%.2f", sensor_readings[i]); | |
Particle.publish("sensor_readings", results); | |
// Reset sensor_readings | |
memset(sensor_readings, 0, sizeof(sensor_readings)); | |
sensor_index = 0; | |
delay(5000); // should not need this after 0.6.1 is released, ensures publish goes out before we sleep | |
} | |
void is_time_to_publish() { | |
if (sensor_index >= SENSOR_ARRAY_SIZE) { | |
Particle.connect(); | |
waitFor(Particle.connected, 300000); // this won't be necessary when 0.6.1 is released | |
if (Particle.connected()) { | |
publish_sensor_readings(); | |
} | |
} | |
} | |
void setup() | |
{ | |
// Wake from deep sleep with power to cellular modem off. | |
// Read sensor, store results to a retained array. | |
read_and_save_sensor(); | |
// Is it time to publish our results? | |
// Turn on cellular modem and publish. | |
is_time_to_publish(); | |
// Go back to deep sleep for ~1 hour (sleep time varies based on how long we were | |
// awake for to keep time between readings close to 1 hour, improve with Time class). | |
// NOTE: this following command does not work in multithreaded mode yet, | |
// but we don't need multithreading for this simple example | |
if (SENSOR_READING_INTERVAL > (millis()/1000)) { | |
System.sleep(SLEEP_MODE_SOFTPOWEROFF, SENSOR_READING_INTERVAL-(millis()/1000)); | |
} else { | |
System.sleep(SLEEP_MODE_SOFTPOWEROFF, SENSOR_READING_INTERVAL); | |
} | |
} | |
void loop() | |
{ | |
Particle.process(); // only required when using MANUAL mode and if we ever use loop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment