I have run into several issues with the System.sleep() function not working properly. I apologize if this has been addressed in other posts, but I did look for quite a while before creating a new one.
For example, with the following code, which should put the wifi to sleep for 10 seconds (after being powered on for 30), the Particle enters sleep mode after 30 seconds as it should (the RGB LED starts breathing white, not “flashing green” as the firmware documentation states), and never wakes up. The loop() code continues to execute as I can still see the “setupMillis” continuing to count up over a serial connection in ParticleDev, but it stays in that state indefinitely. The wifi never wakes up either.
int setupMillis;
bool boolHasSlept=0;
void setup() {
pinMode(D7, OUTPUT);
setupMillis = millis();
Serial.begin(9600);
}
void loop() {
Serial.print("Current Time: ");
Serial.println(millis());
Serial.println(WiFi.localIP());
if (millis() - setupMillis > 30000 and boolHasSlept==0) {
Serial.println("Sleeping for 10s...");
boolHasSlept=1;
System.sleep(10);
}
digitalWrite(D7, HIGH);
delay(100);
digitalWrite(D7, LOW);
delay(2000);
}
Also, the following code should put the Particle into deep sleep for 10 seconds or until pin D2 goes high. This time though, the RGB LED turns off for an instant (more like 10 milliseconds), and then the loop() function appears to be executing (as indicated by the D7 LED continuing to flash), but there are no serial communications in Particle Dev anymore (the wifi does continue to ping though). Have tried with longer duration setpoints with no change in the amount of time the RGB LED “flashes” when the sleep command is executed.
int setupMillis;
bool boolHasSlept=0;
void setup() {
pinMode(D7, OUTPUT);
setupMillis = millis();
Serial.begin(9600);
}
void loop() {
Serial.print("Current Time: ");
Serial.println(millis());
Serial.println(WiFi.localIP());
if (millis() - setupMillis > 30000 and boolHasSlept==0) {
Serial.println("Sleeping for 10s...");
boolHasSlept=1;
System.sleep(D2,RISING,100);
}
digitalWrite(D7, HIGH);
delay(100);
digitalWrite(D7, LOW);
delay(2000);
}
The third and final example is the Deep Sleep function. When this executes, the RGB LED starts flashing blue for very brief pulses (like 10 milliseconds every second) and the Particle is totally dead to the world - no code executing and need to start it in Safe Mode to re-flash it.
int setupMillis;
bool boolHasSlept=0;
void setup() {
pinMode(D7, OUTPUT);
setupMillis = millis();
Serial.begin(9600);
}
void loop() {
Serial.print("Current Time: ");
Serial.println(millis());
Serial.println(WiFi.localIP());
if (millis() - setupMillis > 30000 and boolHasSlept==0) {
Serial.println("Deeply sleeping for 10s...");
boolHasSlept=1;
System.sleep(SLEEP_MODE_DEEP,10);
}
digitalWrite(D7, HIGH);
delay(100);
digitalWrite(D7, LOW);
delay(2000);
}
I’ve also had issues with the D0 and D1 pins not working when configured as discrete inputs, but that’s a topic for another day! Thanks in advance for any feedback!