System.sleep not working on Photon as documented-known bug?

I am trying to use the SparkFun Weather Shield in a solar powered deployment where conservation of power is critical. I don’t want to use the deep sleep mode as I want to continue to collect average rain fall and wind data between uploads. So I have tried to use System.sleep(long seconds); to turn off WiFi to conserve power. However, if I set System.sleep(5) for example (the Photon should sleep for 5 seconds) it goes to sleep with a white light on the led (not flashing green as per the documents) and it never wakes up. Is this a known issue? I have also tried to use WiFi.on and WiFi.off which seem to work in a loop between data uploads but eventually the WiFi connection fails. I have tried very long delays (e.g.30 seconds) after turning the WiFi on to allow for a connection to the SSID but it still eventually fails. When the program fails, the Photon is breathing Cyan, the serial reports “connecting” but never “connected” and the Photon is not showing as online with either the Web IDE or Particle Dev. Strange state.

It sure would be nice to be able to turn the WiFi on only when needed to conserve power.

First I must confess that I’m not completely up to date with the current status of things (have been occupied otherwise), but I remember a behind the scenes discussion about the pros and cons of System.sleep(int) vs. WiFi.off() and I’d personally rather go for the clearer WiFi.off() ... WiFi.on() ... Spark.connect() aproach.
Since your code keeps running you’ve got it all in your own hands, when to WiFi.off() and when to reconnect and the ability to keep track of the reconnection status (WiFi.ready(), Spark.connected(), …).

Granted, System.sleep(int) should work, but AFAIK it used not to reconnect to the cloud, but this is on the ToDos (or was already corrected)

You can always have a look in the GitHub issues (open and closed) if this was already addressed.

I seem to remember that there was a fix, but I might be wrong. However, maybe there might be a different way which you could even use for your advantage. I am using that in a similar project. This depends on how often you need the sensor data, though.

If you only need it - say - every minute, you could use the semi automatic system mode in conjunction with the deep sleep mode. Set the system to semi automatic and let it wake for example every minute to make a measurement. Then for example once per day you connect the WiFi, send your measurement averages and go back to deep sleep again. I found that the Photon uses 80 uA only in the deep sleep, so it should be very good with a battery or solar power.

For me it takes about 3 seconds to wakeup from deep sleep to full operation, so the approach does not work if you want to take a measurement every 3 seconds or so. Apart from that it works great and is the most energy efficient I could come up with at the moment.

There was an idea from @mdma which I use in that project as well. This might even be useful to shorten the wakeup time: Use a static’s object constructor to do the work. This is invoked way earlier than the setup/loop stuff. If you could do the measurement during that phase and immediately go to deep sleep again, it would be faster. I am not using it in that way, though. I just use it to show a status LED earlier. So going into deep sleep or making measurements from there may not work.

1 Like

Yes using WiFi.on and WiFi.off made sense and I gave it plenty of time (30 seconds) to reconnect after turning WiFi.on before attempting to upload data. This approach works for perhaps an hour or two with connections every 5 minutes but then always fails. I will investigate using WiFi.ready() and Spark.connected(). Clearly System.sleep(5) doesn’t work. I know System.sleep (deep sleep) does appear to work but then I loose all my average wind and rain data as well as cumulative rain data since all variables are cleared after waking up from deep sleep.

I wasn’t quite sure about your SYSTEM_MODE but I’d suggest using SEMI_AUTOMATIC together with WiFi.on()/off().

Good idea except deep sleep will clear average wind and rain data as well as cumulative rain data when the Photon wakes up from deep sleep. Turning off just the WiFi will decrease power usage by about 80mA so clearly the largest power consumption is the WiFi. I will try ScruffR’s suggestion to include WiFi.ready() and Spark.connected() to see if I can resolve the issue. Thanks for your suggestions.

You can store them in EEPROM, after which you can retrieve them at boot?

1 Like

You can store them in EEPROM, after which you can retrieve them at boot?

That’s exactly what I do. For now - until it is possible to store data in the RAM which is kept even during deep sleep. For me the power consumption during sleep mode is just too much.

Good idea but in my case I need to keep taking measurements of wind and rain, not just store conditions when it goes to deep sleep and then restore. Otherwise I would miss rain accumulation or wind gusts when in deep sleep.

This sounds interesting. I am in Automatic mode. I am using WIFI.on()/0ff() and testing for WiFi.ready(). That seems to work well but the Photon is still hanging at the start of a connection (sometimes–after say 8-100 successful connections) to a web site (Weather Underground), it prints out “connecting” attempts to connect but it never falls through to the else part of an if/else statement when it fails–if is “connected”, else is “connection failed”. Also, interesting when it hits this lockup state, the Photon is breathing cyan, but does not appear to be connected to the cloud as I loose connection to the device via my cloud account. So perhaps I should try SEMI_AUTOMATIC and manage Spark.connect as well.

1 Like

Ok, so I tried Semi_Automatic mode, WiFi.on()/off(), testing for WiFi.ready() and Particle.connect(). The Photon behaves appropriately for several uploads but eventually fails on “if (client.connect(SERVER, 80))” see code snippet below. The serial.prinln works and I see “connecting…” but then nothing further on the serial line, Particle Dev looses the Photon via the cloud (no breathing cyan in Particle Dev) if I try to reselect the Photon in Particle Dev it shows as not online but the Photon is physically breathing cyan. So my conclusion is that there is a bug in the firmware or chip itself. It appears the WiFi.on()/off() is NOT reliable and neither is the System.sleep(x) functionality. I hope it gets fixed.

void sendToWU()
{
  Serial.println("connecting...");
  if (client.connect(SERVER, 80))
  {
    Serial.println("Connected");

    client.print(WEBPAGE);
    client.print("ID=");
    client.print(ID);
    client.print("&PASSWORD=");
    client.print(PASSWORD);
    client.print("&dateutc=now");  //can use 'now' instead of RTC if sending in real time
    client.print("&tempf=");
    client.print(tempf);
    client.print("&dewptf=");
    client.print(dewptf);
    client.print("&humidity=");
    client.print(humidity);
    client.print("&baromin=");
    client.print(inches);
    client.print("&winddir=");
    client.print(winddir);
    client.print("&windspeedmph=");
    client.print(windspeedmph);
    client.print("&windgustmph=");
    client.print(windgustmph);
    client.print("&windgustdir=");
    client.print(windgustdir);
    client.print("&windspdmph_avg2m=");
    client.print(windspdmph_avg2m);
    client.print("&winddir_avg2m=");
    client.print(winddir_avg2m);
    client.print("&windgustmph_10m=");
    client.print(windgustmph_10m);
    client.print("&rainin=");
    client.print(rainin);
    client.print("&dailyrainin=");
    client.print(dailyrainin);
    client.print("&soiltempf=");
    client.print(soiltempf);
    client.print("&soilmoisture=");
    client.print(soilmoisture);

    //client.print("&action=updateraw");  //Standard update
    client.print("&softwaretype=Particle-Photon&action=updateraw&realtime=1&rtfreq=30");//Rapid Fire
    client.println();

    Serial.println("Upload complete");
    delay(700);                         //Without the delay it jumps to sleep too fast
  }
  else
  {
    Serial.println("Connection failed");
    return;
  }
}

(ScruffR: I edited the code formatting for you)

1 Like

Maybe this is something @mdma might have some enlightening comment on :wink:

Interesting to find I am not alone with this issue.

I have been running similar code using Deep Sleep and WiFi off on the photon all week flawlessly until yesterday afternoon, PST, sometime when all I did was change the sleep time, recompiled, and uploaded via WiFi and my code no longer functioned correctly. It behaved very erratically and unpredictably. I went back to the old code and it still didn’t work. Calling Spark Connect is leaving the Photon breathing White most of the time. When it does try to connect it gets stuck in the cloud handshake, flashing cyan rapidly. After 5 or 10 or somtimes 20 minutes of doing this it finally connects only to not go back to sleep again, it turns WiFi off and breathes white completely ignoring the deep sleep command.

I had another photon running the same code from a week ago and running fine. I also re flashed it changing nothing and it too started the same behaviour.

I am using SEMI_AUTOMATIC mode

I am using the Web IDE.

As a side note. Last night all eight of my units suddenly started going offline one after the other flashing red and then trying to reconnect to the cloud, all got stuck during the particle handshake. After about 20 minutes of doing this they all finally reconnected and apparently stayed that way all night. I mention this as this seems somehow related to what is broken either with the cloud or the web IDE or something else.

I have been using the following code for the last 6 months and it seems fairly robust. I save the failures in EEPROM, do a system reset and report where it failed when it finally reconnects. My problem units are all failing during spark connect with cyan flashing rapidly. The blue LED in my code lets me know where in the code it is…

    void Go_Online() {
    //Wifi Connection Routine
    //Connect to Wifi and the cloud. Keep track of any failures and their type and reset the system if connection fails
            
    WiFi.connect();
    int i=0;
    while (WiFi.ready()==false){
        i++;
        if (i >= 100) {
            EEPROM.write(WCFaddr, 1);
            System.reset();
        }
        digitalWrite(ledblue, HIGH);    // turn it on
        delay(300);    // wait 1 second
        digitalWrite(ledblue, LOW); // turn it off
        delay(300);    // wait another second    
    }
           
    Spark.connect();
    i=0;
    while (Spark.connected()==false){
        i++;
        if (i >= 100) {
            EEPROM.write(WCFaddr, 2);
            System.reset();
        }
        digitalWrite(ledblue, HIGH);    // turn it on
        delay(60);    // wait 1 second
        digitalWrite(ledblue, LOW); // turn it off
        delay(60);    // wait another second   
    }
            
    // End Wifi Connection routine
            
    // Wifi or Cloud connection recovery noticication
    // Once connected to the cloud, if there was a previous connection failure report it at this time
            
    if (WCF>0) {
        switch(WCF) {
            
            case 1:
                Particle.publish("FailureMode", "Reset, failed to connect to Wifi");
            break;
            
            case 2:
                Particle.publish("FailureMode", "Reset, failed to connect to Spark Cloud");
            break;
        }
        
        WCF=0;
        EEPROM.write(WCFaddr, WCF);
    }
            
    // End of recovery notification
}

Sorry for the long post, I hope it is helpful in tracking down the anomaly.

I’ll add this little tidbit here too as it seems to be related. I have a Core that has been running for 3 months continuously checking in every minute with weather and sunlight data. Today it is getting stuck during the particle handshake for several minutes before finally succeeding. Nothing has changed on this device. Seems like a particle cloud issue.

I feel a little foolish. Turns out our downstairs tenants purchased themselves a new wireless router and didn’t set it up correctly. After figuring this out and correcting I no longer am having these connection issues. Now that my photon can publish and subscribe properly it is running correctly once again and my Core is also happy.

Sorry to waste everyone’s time.

Hi @wmjenk,

I’m sorry you’re experiencing problems, but to make best use of everyone’s time it’s best we don’t cross post on topics. I didn’t see any mention of sleep in the code, so it doesn’t seem to be immediately relevant to this thread.

If you feel WiFi isn’t behaving reliably please create a new thread with a short, self-contained, compilable complete example with steps to reproduce the issue and we will be happy to investigate.

I couldn’t run the example you gave since I don’t have a corresponding server, but creating a TCP client on port 80 to a remote server works fine. Please supplement in a new thread with precise details of the issue is so we can take steps to reproduce it and investigate.

Thanks :smile:
mat.

I was simply uploading collected sensor data to Weather Underground after printing the data strings out to the serial line. With system sleep before the calling the printout and upload routines, it wasn’t waking up. I have also used Wifi.off and WiFi.on as well as WiFi ready that work several times in a row but eventually fail at the if (client.connect(SERVER,80) line, it never got to the
else
{
Serial.println(“Connection failed”);
return;.

I will write something simple using dweet.io as a an upload site and see if I can replicate the issue and then I will post something if I can replicate the issue. Thanks, Bill

1 Like