Particle.variable and Particle.function doesn’t work with Blynk on Particle Photon

I’m new to blynk and try to combine the Particle world with Blynk. Now I’m facing the problem, that my sketch either works with the pure Particle.variable / function or works with only Blynk. when both are enabled, only the Blynk part is working. Any hint for me? The system firmware is 0.7.0, Particle.pubish is fuctional

code:

#include <LiquidCrystal_I2C_Spark.h>
#include "Adafruit_DHT.h"
#include <blynk.h>
#define DHTPIN 3
#define DHTTYPE DHT22	
#define BLYNK_PRINT Serial

DHT dht(DHTPIN, DHTTYPE);
double h,t;
LiquidCrystal_I2C *lcd;
BlynkTimer timer;
char auth[] = "xxx";

void myTimerEvent()
{
  lcd->clear();
  lcd->print("Temp/Feuchte:");lcd->setCursor(0,1);
  lcd->print( String(t,2)+" : "+String(h,2));

  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V5, millis() / 1000);
}

void setup() {
  lcd = new LiquidCrystal_I2C(0x3F, 16, 2);
  lcd->init();
  lcd->backlight();
  lcd->clear();
  dht.begin();
  Time.zone(+1);
  Blynk.begin(auth,IPAddress(172,23,56,222), 8080);
  Particle.variable("Temperatur",&t,DOUBLE);
  Particle.variable("Luftfeuchte",&h,DOUBLE);
  timer.setInterval(1000L, myTimerEvent);
  h = dht.getHumidity(); 
  t = dht.getTempCelcius();
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer 
  delay(500);
  h = dht.getHumidity(); 
  t = dht.getTempCelcius();
}

You should put the Particle.variable() setup at the top of setup() and I’d also suggest to use the modern syntax

  Particle.variable("Temperatur", t);  
  Particle.variable("Luftfeuchte", h);

Also be aware that Particle doesn’t work well with the Blynk Timer. One timer may work but I found anymore and it would randomly crash.

1 Like

cool, that worked. I’ve tried the modern syntax bevore, but also no change.
Thank you very much for the fast support!

One thing you might also want to change is to get rid of that delay(500) in loop()
If you want to only do the DHT reading less often, rather go with something like this

void loop()
{
  static uint32_t ms = 0;

  Blynk.run();
  timer.run(); // Initiates BlynkTimer 

  if (millis() - ms > 500) {
    ms = millis();
    h = dht.getHumidity(); 
    t = dht.getTempCelcius();
  }
}

BTW, which tip worked? @MikeM’s or mine about moving the variable setup to the top?

yes it worked, the moving to the beginning of setup()! The mordern style was my first test, but it didn’t help. Thanks for that. And btw. it works now with both styles, the move to the beginning of setup().

This script is only to get familar with the comination of particle and Blynk. In my productive scripts I use timer or the kind of loop you suggested. I try to avoid delay…

The background to this cure is that Particle.variable(), Particle.function() and Particle.subscribe() need to be registered with the cloud within aprox. 5 seconds after the connection gets established.
After that any new registrations would only take effect after a disconnect/reconnect.

And I guess the setup of the LCD and Blynk takes longer than 5 sec which causes the variables to not be registered in time.

1 Like

good to know! But is this mentioned in the docs? or FAQs?

It’s not decumented, but there are some long standing issue reports about this