# Particle Photon + DHT22: optimizing battery life

**URL:** <https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914>\
**Category:** Hardware\
**Created:** [March 9, 2016, 1:39pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914 "2016-03-09T13:39:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ltpitt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ltpitt/32/6710_2.png) [@ltpitt](https://community.particle.io/u/ltpitt)\
**Post date:** [March 9, 2016, 1:39pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/1 "2016-03-09T13:39:44Z")

</div>

Hi all,

I have a perfectly working setup of Particle Photon and DHT22.

I would like to make it wireless but it works for a very short amount of time on a huge 9200 mAh battery pack.  
This is 100% related to using sleep and deep sleep.

Do you have any suggestions about how to implement this correctly?

Is it possible to “permanently” publish a variable in particle cloud so I can let my application retrieve data from cloud and keep my Photon + DHT22 in deep sleep until next update?

This is my current code:

```
// Acquire sensor data from Particle Photon
//
// To read variables from Particle Api use:
// https://api.particle.io/v1/devices/YOUR_DEVICE_ID/humidity/?access_token=YOUR_ACCESS_TOKEN
//
// 

#include "PietteTech_DHT/PietteTech_DHT.h"
#include "math.h"

#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D2 // Digital pin for communications

// declaration
void dht_wrapper(); // must be declared before the lib initialization

// library instialization
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

int n; // sensor acquisition counter
double temp; // temperature  
double umid; // humidity
double t; // needed in rounding

void dht_wrapper() {
    DHT.isrCallback();
}

void setup()
{
    Serial.begin(9600);

    Serial.println("Device starting...");

    Serial.println("DHT Example program using DHT.acquireAndWait");
    Serial.print("LIB version: ");
    Serial.println(DHTLIB_VERSION);
    Serial.println("---------------");
    
    pinMode(D7, OUTPUT);

    Particle.variable("humidity", umid); //write the value on the cloud variable called "Humidity"
    Particle.variable("temperature", temp); //write the value on the cloud variable called "Temperature"

}

void loop()
{
    Serial.print("\n");
    Serial.print(n);
    Serial.print(": Retrieving information from sensor: ");
    Serial.print("Read sensor: ");
    delay(100);

    int result = DHT.acquireAndWait();

    
    // Error handling
    switch (result) {
        case DHTLIB_OK:
            Serial.println("OK");
            break;
        case DHTLIB_ERROR_CHECKSUM:
            Serial.println("Error\n\r\tChecksum error");
            break;
        case DHTLIB_ERROR_ISR_TIMEOUT:
            Serial.println("Error\n\r\tISR time out error");
            break;
        case DHTLIB_ERROR_RESPONSE_TIMEOUT:
            Serial.println("Error\n\r\tResponse time out error");
            break;
        case DHTLIB_ERROR_DATA_TIMEOUT:
            Serial.println("Error\n\r\tData time out error");
            break;
        case DHTLIB_ERROR_ACQUIRING:
            Serial.println("Error\n\r\tAcquiring");
            break;
        case DHTLIB_ERROR_DELTA:
            Serial.println("Error\n\r\tDelta time to small");
            break;
        case DHTLIB_ERROR_NOTSTARTED:
            Serial.println("Error\n\r\tNot started");
            break;
        default:
            Serial.println("Unknown error");
            break;
    }

    // humidity rounding
    umid=DHT.getHumidity();
    t=umid-floor(umid); // get only decimal part
    t=t*100; // multiply by 100 decimal part, get xx.xxxxxxxxx
    t=floor(t); // get only last two characters of decimal part, get yy
    umid=floor(umid)+(t/100);

    // temperature rounding
    temp=DHT.getCelsius();
    t=temp-floor(temp); // get only decimal part
    t=t*100; // multiply by 100 decimal part, get xx.xxxxxxxxx
    t=floor(t); // get only last two characters of decimal part, get yy
    temp=floor(temp)+(t/100);

    // print sensor data to serial
    Serial.print("Humidity (%): ");
    Serial.println(DHT.getHumidity(), 2);

    Serial.print("Temperature (oC): ");
    Serial.println(DHT.getCelsius(), 2);

    Serial.print("Temperature (oF): ");
    Serial.println(DHT.getFahrenheit(), 2);

    Serial.print("Temperature (K): ");
    Serial.println(DHT.getKelvin(), 2);

    Serial.print("Dew Point (oC): ");
    Serial.println(DHT.getDewPoint());

    Serial.print("Dew Point Slow (oC): ");
    Serial.println(DHT.getDewPointSlow());

    
    // Blink onboard led
    digitalWrite(D7, HIGH);
    delay(1000);
    digitalWrite(D7, LOW);

    // Increase acquisition counter
    n++;
    

    // Wait for 10 minutes
    delay(600000);
}
```

---

<div class="post-metadata">

**Author:** ![MORA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mora/32/8575_2.png) [@MORA](https://community.particle.io/u/MORA)\
**Post date:** [March 10, 2016, 10:25am UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/2 "2016-03-10T10:25:50Z")

</div>

delay does not save power, just halt program execution.

You need to use SLEEP\_MODE\_DEEP for the long wait, which will return your program to the start after the duration and all variables that are not persisted will be reset.  
[https://docs.particle.io/reference/firmware/photon/#sleep-sleep-](https://docs.particle.io/reference/firmware/photon/#sleep-sleep-)

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [March 10, 2016, 4:33pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/3 "2016-03-10T16:33:09Z")

</div>

> [@ltpitt](#):
>
> is it possible to "permanently" publish a variable in particle cloud

No, the Particle cloud does not store data, but there are a lot of free or inexpensive services out there that do (Ubidots, librato, ThingSpeak, etc).

---

<div class="post-metadata">

**Author:** ![CuriousTech](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/curioustech/32/3315_2.png) [@CuriousTech](https://community.particle.io/u/CuriousTech)\
**Post date:** [March 10, 2016, 8:40pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/4 "2016-03-10T20:40:38Z")

</div>

Use Particle.publish() to publish the data. Particle.variable() only serves a variable when requested (while awake).

---

<div class="post-metadata">

**Author:** ![ltpitt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ltpitt/32/6710_2.png) [@ltpitt](https://community.particle.io/u/ltpitt)\
**Post date:** [March 14, 2016, 1:03pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/5 "2016-03-14T13:03:33Z")

</div>

Are you sure about this?

It seems like this works only for events (reading docs now).

---

<div class="post-metadata">

**Author:** ![MORA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mora/32/8575_2.png) [@MORA](https://community.particle.io/u/MORA)\
**Post date:** [March 14, 2016, 2:33pm UTC](https://community.particle.io/t/particle-photon-dht22-optimizing-battery-life/20914/6 "2016-03-14T14:33:08Z")

</div>

Particle.publish will publish a value to particle cloud, where it then needs to be picked up by something (subscribe) or trigger a webhook.

Publish is an event, but it can carry data with it, so the event could be sensorReading, “25.67” for example.
