# Event driven Temp help

**URL:** <https://community.particle.io/t/event-driven-temp-help/44072>\
**Category:** Troubleshooting\
**Created:** [September 20, 2018, 11:04am UTC](https://community.particle.io/t/event-driven-temp-help/44072 "2018-09-20T11:04:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Autoidlab](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Autoidlab](https://community.particle.io/u/Autoidlab)\
**Post date:** [September 20, 2018, 11:04am UTC](https://community.particle.io/t/event-driven-temp-help/44072/1 "2018-09-20T11:04:44Z")

</div>

Hello,

I have some simple code that reads a temp and publishes its value every 10 seconds. I’d like it to only send the data when theres a change in temp greater than 3 degrees. Im unsure of the code and also wondering if that saved value is retained when the device loses power. Any suggestions?

```cpp
// Variables
int temperature;
int humidity;

// DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    
    // Start DHT sensor
    dht.begin();
    
}

void loop() {
    
    // Humidity measurement
    temperature = dht.getTempFarenheit();
    
    // Humidity measurement
    humidity = dht.getHumidity();
    
    Particle.publish("temperature", String(temperature) + " °F", PRIVATE);
    delay(10000);
    Particle.publish("humidity", String(humidity) + "%", PRIVATE);
    delay(10000);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 20, 2018, 11:40am UTC](https://community.particle.io/t/event-driven-temp-help/44072/2 "2018-09-20T11:40:45Z")

</div>

To answer this one first

> [@Autoidlab](#):
>
> wondering if that saved value is retained when the device loses power.

If your device really loses power and you haven't got a backup power connected (e.g. coin cell on VBAT for Photons) then the last values won't be retained, unless you store them in [EEPROM](https://docs.particle.io/reference/firmware/electron/#eeprom) (or any other non-volatile medium).

And for the other part, you'd need two variables (should be `float` or `double` instead of `int`) - one to hold the new reading and one to keep the old one - and then compare the absolute value of both like this

```cpp
  float newTemp = dht.getTempFarenheit(); // although there is a spelling error in the library 
                                          // as it should be Fahrenheit
  if (fabs(newTemp - temperature) > 3) {
    temperature = newTemp;
    ...
  }

```

BTW, I'd rather only publish _one_ event that features both values like this

```cpp
  char data[32];
  snprintf(data, sizeof(data), "%.1f °F, %.1f %%", temperature, humidity);
  Particle.publish("envData", data, PRIVATE);

```

And finally instead of using `delay()` I'd go non-blocking like this

```cpp
void loop() {
  static uint32_t ms = 0;
  if (millis() - ms < 10000) return; // if the last visit was less than 10 sec ago bail out
  ms = millis(); 
  ...
}

```

---

<div class="post-metadata">

**Author:** ![Autoidlab](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Autoidlab](https://community.particle.io/u/Autoidlab)\
**Post date:** [September 21, 2018, 1:30am UTC](https://community.particle.io/t/event-driven-temp-help/44072/3 "2018-09-21T01:30:13Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Autoidlab](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Autoidlab](https://community.particle.io/u/Autoidlab)\
**Post date:** [September 28, 2018, 4:43pm UTC](https://community.particle.io/t/event-driven-temp-help/44072/4 "2018-09-28T16:43:29Z")

</div>

Hello,

I tried your suggestion. Then publishes are now combined (good idea!) but the events are publishing regardless of changes. What did I do wrong here?

```auto
// Variables
float temperature;
float humidity;
 
// DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    
    // Start DHT sensor
    dht.begin();

}

void loop() {
    
    float newTemp = dht.getTempFarenheit(); 
    float newHumidity = dht.getHumidity();

      if (abs(newTemp - temperature) > 3) {
    temperature = newTemp;
      
      if (abs(newHumidity - humidity) >3) 
    humidity = newHumidity;
      }
    
char data[32];
  snprintf(data, sizeof(data), "%.1f °F, %.1f %%", temperature, humidity);
  Particle.publish("Temp, Humidity", data, PRIVATE);
    
     static uint32_t ms = 0;
  if (millis() - ms < 10000) return; // if the last visit was less than 10 sec ago bail out
  ms = millis(); 
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 28, 2018, 7:52pm UTC](https://community.particle.io/t/event-driven-temp-help/44072/5 "2018-09-28T19:52:36Z")

</div>

What I meant was this tho’

```cpp
void loop() {
  static uint32_t ms = 0;

  if (millis() - ms < 10000) return; // if the last visit was less than 10 sec ago bail out
  ms = millis(); 

  char data[32];
  float newTemp = dht.getTempFarenheit(); 

  if (fabs(newTemp - temperature) > 3.0) {
    temperature = newTemp;
    humidity = dht.getHumidity();
    snprintf(data, sizeof(data), "%.1f °F, %.1f %%", temperature, humidity);
    Particle.publish("Temp, Humidity", data, PRIVATE);
  }
}

```

---

<div class="post-metadata">

**Author:** ![HEng](https://avatars.discourse-cdn.com/v4/letter/h/c77e96/32.png) [@HEng](https://community.particle.io/u/HEng)\
**Post date:** [September 28, 2018, 10:51pm UTC](https://community.particle.io/t/event-driven-temp-help/44072/6 "2018-09-28T22:51:54Z")

</div>

> [@ScruffR](#):
>
> If your device really loses power and you haven’t got a backup power connected (e.g. coin cell on VBAT for Photons) then the last values won’t be retained, unless you store them in EEPROM (or any other non-volatile medium).

I'm sure you know this, but lest anyone be confused, when you use battery backup you need to declare a variable as 'retained'. Variables are not automatically saved when you have VBAT.

> [@Retained Memory Tips](https://community.particle.io/t/retained-memory-tips/38137):
>
> Retained memory is a feature of the Particle Photon and Electron that allows data to be preserved across reboots. As a quick refresher: | Technique | Fast | Unlimited | Survives | Survives | | | Read/Write | Writes | Reset | No Power | | ------------ | ---------- | --------- | -------- | --------- | | Regular RAM | Yes | Yes | No | No | | EEPROM | No | No | Yes | Yes | | Retained RAM | Yes | Yes …

---

<div class="post-metadata">

**Author:** ![Autoidlab](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Autoidlab](https://community.particle.io/u/Autoidlab)\
**Post date:** [October 1, 2018, 4:45pm UTC](https://community.particle.io/t/event-driven-temp-help/44072/7 "2018-10-01T16:45:15Z")

</div>

Ok, got it! That all works. How would that work with something like GPS coordinates (ie - 12.345678) If I only want to publish changes in position? I can’t convert a char to float, correct?

```auto
TinyGPS gps;
char szInfo[64];
// Every 15 minutes 
int sleep = 1 * 60 * 1000;

void setup(){
        Serial1.begin(9600);
}
void loop(){
    bool isValidGPS = false;
    
    for (unsigned long start = millis(); millis() - start < 1000;){
        // Check GPS data is available
        while (Serial1.available()){
            char c = Serial1.read();
            
            // parse GPS data
            if (gps.encode(c))
                isValidGPS = true;
         }
    }
     // If we have a valid GPS location then publish it
    if (isValidGPS){
        float lat, lon;
        unsigned long age;
    
        gps.f_get_position(&lat, &lon, &age);
        
        sprintf(szInfo, "%.6f,%.6f", (lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lat), (lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lon));
    }
    else{
        // Not a vlid GPS location, jsut pass 0.0,0.0
        // This is not correct because 0.0,0.0 is a valid GPS location, we have to pass a invalid GPS location
        // and check it at the client side
        sprintf(szInfo, "0.0,0.0");
    }
    
     Particle.publish("gpsloc", szInfo);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 1, 2018, 4:50pm UTC](https://community.particle.io/t/event-driven-temp-help/44072/8 "2018-10-01T16:50:22Z")

</div>

You can convert `char` to `float`, but you don’t need to since the GPS library should provide you with the latitude and longitude values as floats and if you are using `TinyGPS++` library you should also get a `distanceBetween()` function that you can use to calculate the distance between your last and current position and compare that against a threshold.

---

<div class="post-metadata">

**Author:** ![Autoidlab](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Autoidlab](https://community.particle.io/u/Autoidlab)\
**Post date:** [October 3, 2018, 1:54am UTC](https://community.particle.io/t/event-driven-temp-help/44072/9 "2018-10-03T01:54:55Z")

</div>

Thats a much better library. Thank you!
