Hi All,
I’m a newbie to the Particle community, and I need your help.
I just got my Electron, and I’m keen to measure an analogue voltage once every ten minutes, post voltage results to Particle Cloud, and then put my Electron into deep sleep mode between measurements. That is, for my project I need to save solar + battery power, but also measure a voltage at a particular time i.e. once every ten-minutes. I’ve tried combining millis(10 * 60*1000) and System.sleep(SLEEP_MODE_DEEP,10), also polled the Particle Cloud for the time stamps too, but no success. See below for some example code. Can someone please suggest suitable code to do this? Also, does System.sleep() uses current millis? Lastly, can I use the 3G towers for accurate time stamps / event triggers? Any help would be greatly appreciated. Cheers!
// For request time synchronisation from the Particle Cloud
#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned long lastSync = millis();
// constants won't change. Used here to set a pin number :
const int ledPin = 7; // the number of the LED pin 7
// Variables will change :
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 10 * 60 * 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
if (millis() - lastSync > ONE_DAY_MILLIS) {
// Request time synchronization from the Particle Cloud
Particle.syncTime();
lastSync = millis();
}
// This is where I measure A0 voltage and post measurement on Particle Cloud
Particle.publish("Voltage = ", "123.6 V"); // post analogue voltage here
delay(5000); // Delay is to allow for FLASH OTA via 3G
System.sleep(SLEEP_MODE_DEEP,595); // Put Electron to sleep
// Check to see if it's time to blink the LED; that is if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
// And repeat!
}