I can get the core to go to sleep when I call it from the Spark Core Web Interface by issuing a digitalwrite(S5) command which the code below sends the core to sleep for 5 minutes, then it wakes up and reconnects so communication is re-established. But obviously I can’t be at the keyboard 24/7 to send these commands!
void setup()
{
//Setup the Solar application here
pinMode(led2, OUTPUT);
//Register the Solar functions
Spark.function(“digitalread”, solarDigitalRead);
Spark.function(“digitalwrite”, solarDigitalWrite);
}
void loop()
{
… do various stuff
}
int solarDigitalWrite(String command)
{
bool value = 0;
//convert ascii to integer
int pinNumber = command.charAt(1) - ‘0’;
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >9) return -1;
if(command.startsWith("S"))
{
// white LED while asleep, then back to whatever
RGB.color(120, 120, 120);
RGB.brightness(64);
Spark.sleep(pinNumber * 60);
sleepDelay(pinNumber * 70);
return 123;
}
}
int sleepDelay(int seconds)
// double flashes the blue LED and delays total of 1 second
{
do
{
digitalWrite(led2, HIGH);
delay(200); // Wait for 200ms
digitalWrite(led2, LOW);
delay(200); // Wait for 200ms
digitalWrite(led2, HIGH);
delay(200); // Wait for 200ms
digitalWrite(led2, LOW);
delay(398); // Wait for 398ms
seconds = seconds - 1;
}
while (seconds > 0);
}
If, however, I use the code below on a timed basis, say once every 10 minutes, the core goes to sleep for the correct time, the re-awakes, but communication is NOT re-established - no flashing green etc, it just continues round its loop and the only way to regain contact with it from the Cloud is to press the reset button. Any ideas??? I’m guessing the function call from the Web Interface reconnects because it is supposed to return a value and the core firmware remembers that, but in the timed case (below) this is not true.
this is in the loop…
if ((Time.hour() > 14) and (Time.hour() < 19)) // GMT !
// YELLOW phase
{
RGB.color(150, 70, 0);
RGB.brightness(64);
MinNow = millis();
if (MinNow-minuteNow>600000UL)
{
minuteNow = MinNow;
Spark.sleep(12); // sleep for 12 seconds
sleepDelay(20); // delay 20 seconds
}
}