[Solved] Multiple VirtualWrites causing red SOS blink

This has been a very strange issue. I am pushing data to 2 virtual pins, temperature and humidity from a DHT22 sensor. I am using the PietteTech_DHT library. However it seems if I push the temp and humidity values from the readings it causes my Photon to crash and do a red SOS blinking. If I hard code in say some int values like 5 and 6, it works just fine. But what is strange is that doing just one virtualWrite works just fine with the actual sensor values. Blynk Debug output has nothing that I can see of value. It just crashes and restarts.

Heres my stripped down code:

//#define BLYNK_DEBUG // Optional, this enables lots of prints
//#define BLYNK_PRINT Serial

#include "blynk/blynk.h"

char auth[] = "0123456789abcd";

#include <math.h>

// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"

double temp                         = 0;
double humidity                     = 0;

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

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

PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

Timer blynk_timer(15000, blynkPublish);
Timer environment_timer(600000, environmentPublish);

void setup(){

    Particle.variable("g_temp", temp);
    Particle.variable("g_humidity", humidity);

	delay(1000);

    environmentPublish();

	delay(1000);

    blynk_timer.start();
    environment_timer.start();

    Particle.publish("End of setup");
    
    Blynk.begin(auth);

}

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

void loop(){

    Blynk.run();

}

void blynkPublish(){

    if(Blynk.connected()){

       /* This failes */
        Blynk.virtualWrite(V0, temp);  // temp shows up like 70.2 when I view the variable
        Blynk.virtualWrite(V1, humidity); // humidity shows up like 50.4 when I view the variable

       /* 
       This is fine
        Blynk.virtualWrite(V0, 5);
        Blynk.virtualWrite(V1, 6);

        */


    }

}

void environmentPublish(){

    DHT.acquireAndWait();

    delay(500);

    temp = DHT.getFahrenheit();
    humidity = DHT.getHumidity();

    temp = round(temp*10)/10.0;
    humidity = round(humidity*10)/10.0;

    Particle.publish("temp", String(temp), 60, PRIVATE);
    Particle.publish("humidity", String(humidity), 60, PRIVATE);

}
  1. Timer is not running, no timer library is included… I recommend using ‘sparkcorepolledtimer’ library
  2. Red SOS is unlikely to be caused by Blynk.

Debug:

  1. Try getting sensor data without Blynk in intervals
  2. Try bare Blynk example without modifications
  3. When you have succeded, combine both
  1. Timers are included by default. See https://docs.particle.io/reference/firmware/photon/#software-timers

  2. It only happens when I use blynk. The original sketch has been working for over a year with no issues prior to me adding blynk.

I’ve spent hours troubleshooting this and I’ve taken this down to the simplest forms I can think of. I can get data just fine from sensor. I can also send data to blynk. The breaking point has been when I do two virtual writes in this particular sketch. What is strange is that I am using blynk on a different device and I am sending 3 virturalwrites in succession with no issue. It seems to be something with the data type/formating or combination of something in this particular sketch. I’m just failing to find the needle in the haystack.

Could you try putting a small delay (20-50ms) between writes?

I submitted the same issue to the particle forums and got a response that helped me stop the crashing. I changed my datatypes for temp and humidity to String. In my environmentPublish() I did this:

void environmentPublish(){

    DHT.acquireAndWait();

    temp = String::format("%.1f",DHT.getFahrenheit());
    humidity = String::format("%.1f",DHT.getHumidity());
    
    Particle.publish("librato_garage_temp", temp, 60, PRIVATE);
    Particle.publish("librato_garage_humidity", humidity, 60, PRIVATE);
}

Now the string data pushes to Blynk with no issues. So its not really solved but it no longer crashes.

This is VERY strange. The same operations are basically performed inside Blynk library…

I would not put a Blynk operation in a timer like that. Timer functions need to execute quickly. Set a Boolean in the timer and send in the main loop.