[Resolved]- Blynk and Electron - Delay between data - not arriving

Hi guys
I'm building a level monitor for a tank using electron and wish for the updates to be in 5 minute intervals. I am pushing the data to Ubidots (working fine) and blynk (not working). My understanding coding is not that flash so please excuse me but I'm really hoping someone can help!
If the delay is between 1 and 10 seconds, updates come through to blynk OK. but I want the data to only be pushed after 5 minute intervals, as this is electron and I wish to conserve data. So I am finding that data is arriving perfectly on Ubidots server but just not updating on blynk. Is the server timing out? I've extended the heartbeat timeout in blynkConfig.h from 10 to 333 but not helping.

blynk is great and really means a lot to me as part of my project - where am I going wrong?
Thanks in advance guys.
here is my code:

int TnkLvlPV_RAW = A0; //raw signal data from field
float Tx01 = 0.0; //transmission tag
char resultstr[64];

void setup () {
pinMode(TnkLvlPV_RAW,INPUT);

Blynk.begin(auth);
Serial.begin(115200);
}

void loop() {
// Read alalogue value assigned to TAG "TankLevelPV_RAW"
Tx01 = analogRead(TnkLvlPV_RAW);
Tx01 = map(Tx01, 0, 4095, 0, 100); //scale the value to 0-100X
sprintf(resultstr, "{"value":%.4f}",Tx01);

delay(100);
Spark.publish("Tx01", String(Tx01) + " %");
Particle.publish("PostToUbidots", Tx01);
Blynk.run();
Blynk.virtualWrite(3, String(Tx01));
ubidots.add("Tx01", Tx01);
ubidots.sendAll();
delay(300000);//send again in 5 minutes

}

I’ve only ever tested Blynk to know how it works, so I might be wrong, but changing the heartbeat interval on your side is only one side of the equation. If the remote server wants to hear more often from you then that’s what counts.
But a stab in the dark might be to add another Blynk.begin() inside loop().
On the other hand if conserving power is your goal, then System.sleep(SLEEP_MODE_DEEP, 300, SLEEP_NETWORK_STANDBY); would be better than delay(300000); (data consuption is still low).

But deep sleep will ensure that setup(), and hence Blynk.begin() will be called on each wake.

And I see you haven’t adopted my code suggestions from your other topic in that code.

I also think that is wrong

Spark.publish("Tx01", String(Tx01) + " %"); // this should be Particle
Particle.publish("PostToUbidots", Tx01); // <-- you can't directly publish a float
// and to conserve data publish only ONE event, both seem to carry the same information
1 Like

@BIBZY84, the problem is with how you run loop(). Blynk.run() needs to run as often as possible (ie on every loop). If you want to publish every 5 minutes, you can use a non-blocking delay like this:

unsigned long sendTimer = millis();

void loop() {

  Blynk.run();

  if (millis() - sendTimer > 30000) {  // Check if 5mins have passed since last run

    // Read alalogue value assigned to TAG "TankLevelPV_RAW"
    Tx01 = analogRead(TnkLvlPV_RAW);
    Tx01 = map(Tx01, 0, 4095, 0, 100); //scale the value to 0-100X
    sprintf(resultstr, "{\"value\":%.4f}",Tx01);

    Particle.publish("Tx01", String(Tx01) + " %");
    Blynk.virtualWrite(3, String(Tx01));
    ubidots.add("Tx01", Tx01);
    ubidots.sendAll();
  }
}

Now Blynk.run() and loop() will run without interference and the sampling and posting code only runs every 5 minute. I removed the second Particle.publish() you had to avoid any problems with the 1-per-second publish limit. I am not sure why you have the sprintf(resultstr, "{\"value\":%.4f}",Tx01); line. :smile:

2 Likes

@ScruffR and @peekay123 guys, thanks again for coming to the rescue. Happy with the advice and now understand loads more about the delay issues and methodology. I’ve also had success using the SparkCorePolledTimer library
to make periodic actions.

@scruffr code suggestions appreciated. The code I posted here was scrapbook and not realtime; more so to focuss on the timing issue at hand. However, as you rightly pointed out the integer resultant from the map function won’t allow any decimal place anyway, so current state of play is round number but with decimal and then six zeros, which I will come back to and look at your suggestion again in due course.

Cheers guys

2 Likes

Thanks for the help on this @ScruffR @peekay123!