Deep sleep then doesn't compile new code

I’ve implemented this tutorial

Then I wanted to add the code for deep sleep for 5 seconds, then wake up and measure the temperature, humidity and light.

#include "Adafruit_DHT/Adafruit_DHT.h"

#define DHTPIN 0  // The signal pin of the DHT sensor is connected to D0
#define DHTTYPE DHT11

int temperature, humidity, light;
int light_pin = A0;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    dht.begin();
}

void loop() {
    temperature = (int) dht.getTempCelcius(); // returns a float
    humidity = (int) dht.getHumidity();
    float lightMeasurement = analogRead(light_pin);
    light = (int) (lightMeasurement / 4096 * 100);

    System.sleep(SLEEP_MODE_DEEP, 20);

    Particle.publish("temperature", String(temperature) + " *C");
    Particle.publish("humidity", String(humidity) + "%");
    Particle.publish("light", String(light) + "%");
}

This is my first time with sleep mode. So I didn’t know that it will flash green and reconnect the WiFi every 5 seconds without even doing the measurements. So I added a delay at the last line. Now the Photon does not accept the code and “times out”.
It’s now stuck in a wake up sleep loop.

What to do?

(I haven’t tried yet obviously, but I think I need a delay between each publish. right?)

Safe mode is always a first resort in these cases.

Also, try reading the docs on deep sleep again, that might help you understand why the current code isn’t publishing.

I got nothing. I read the documentation twice now. I can’t get the Photon to go to deep sleep, wake up, take measurements and publish then back to sleep.

To try and fix the temperature readings, try adding a 2-3 second delay after dht.begin(). The DHT11 and DHT22 sensors (and friends and cousins) require at least 2 seconds between readings.

You may also want to move the sleep code to the last line in your loop. For good measure, you can also add a short delay to make sure the publishes go through:

void loop() {
    temperature = (int) dht.getTempCelcius(); // returns a float
    humidity = (int) dht.getHumidity();
    float lightMeasurement = analogRead(light_pin);
    light = (int) (lightMeasurement / 4096 * 100);

    Particle.publish("temperature", String(temperature) + " *C");
    Particle.publish("humidity", String(humidity) + "%");
    Particle.publish("light", String(light) + "%");

    // A little delay to give the Photon time to send the publish data
    delay(1000); // This should also force a background Particle.process()

    System.sleep(SLEEP_MODE_DEEP, 20);
}

It may be safer/better/quicker to send all the data in a single publish if that’s possible for whatever is listening. Maybe something like: Particle.publish("sensors", "t:"+String(temperature)+",h:"+String(humidity)+",l:"+String(light)).

And, yes, you may get very familiar with safe mode while debugging sleep code issues. :wink:

2 Likes

“In this particular mode, the device shuts down the network subsystem and puts the microcontroller in a stand-by mode. When the device awakens from deep sleep, it will reset and run all user code from the beginning with no values being maintained in memory from before the deep sleep.
As such, it is recommended that deep sleep be called only after all user code has completed.

3 Likes

Excellent. It’s working now. I have a small issue with publishes taking longer than the delays and the sleep, but for now I can’t complain. Thanks man :relieved:

When you say the publishs are taking longer then the delay, you do realise that the Photon is rebooting, thus all the code in both setup() and loop() is being run? Plus all the ‘invisible’ support stuff that make the Photon internet connected. Because the photon has rebooted, it also needs to re-establish the wireless connection, which I presume takes a few seconds (I’m a ESP8266/Digistump Oak user, not a Photon user). So it’s probably the re-connection to the wifi and the particle cloud that is taking the time, not the loop code.

I think you may be right. For some reason, I hadn’t thought of that :blush:

The mechanics of deep sleep trip everyone up until they realise what it actually does… in the case of ESP8266s, and probably the Photon the CPU and network stack is effectively suspended for a period of time, and then ultra low power real-time clock reboots it after the programmed time has elapsed. Effectively… your code runs until the CPU goes to sleep, and then when it wakes up, it’s as if the power had just been connected, and it starts afresh.

1 Like

Maybe this project is too simple, but it seems like too much hassle for this project.
(The lights flickering all the time doesn’t help either).
I don’t even feel that it saves that much power over time (for this project at least)

If you want it to sleep for 5 seconds… then no, probably not worth it. I’d be thinking of using it if the device was sampling more like every 30 seconds or longer…

It might be worth looking at the plain sleep mode rather than deep sleep if you want a little power-saving without quite as much fiddling around…

// Turn off Wi-Fi for seconds.
// Keep application running.
// Low power usage.
System.sleep(seconds);

The particle docs indicate that as of firmware 0.5.0, [quote]
In automatic modes, the sleep() function doesn’t return until the cloud connection has been established. This means that application code can use the cloud connection as soon as sleep() returns
[/quote]

So going by that update to the reference docs, you can do a System.sleep(5); and it would be the same as doing a delay(5000); and the WiFi would take a nap during that delay. I don’t know how long it will take the WiFi to come back up again… it depends on if it is actually shut down, or put into standby mode. Worth checking though as it might give you some extra power saving for minimal pain! :wink:

1 Like

yeah 30 seconds or more seems like a good rule of thumb. I don’t know why I locked myself onto deep sleep instead of plain sleep mode, but you are right again. I think plain sleep is suitable for this project.

Thankx man for following up with this :+1:

1 Like

Please do let me know how you go… I would be interested to know if it does work that way, and if it does what you want :slight_smile:

1 Like