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

**URL:** https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940
**Category:** Troubleshooting
**Created:** [November 1, 2016, 11:04pm UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940 "2016-11-01T23:04:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![BIBZY84](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@BIBZY84](https://community.particle.io/u/BIBZY84)
#### Post date: [November 1, 2016, 11:04pm UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940/1 "2016-11-01T23:04:39Z")

</div>

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
> 
> ```
> 
> }

---

<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: [November 2, 2016, 10:14am UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940/2 "2016-11-02T10:14:49Z")

</div>

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

```cpp
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

```

---

<div class="post-metadata">

### Author: ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)
#### Post date: [November 2, 2016, 1:07pm UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940/3 "2016-11-02T13:07:04Z")

</div>

@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:

```auto
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. 😄

---

<div class="post-metadata">

### Author: ![BIBZY84](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@BIBZY84](https://community.particle.io/u/BIBZY84)
#### Post date: [November 3, 2016, 12:41pm UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940/4 "2016-11-03T12:41:37Z")

</div>

@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

---

<div class="post-metadata">

### Author: ![KyleG](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kyleg/32/11077_2.png) [@KyleG](https://community.particle.io/u/KyleG)
#### Post date: [November 4, 2016, 3:51pm UTC](https://community.particle.io/t/resolved-blynk-and-electron-delay-between-data-not-arriving/26940/5 "2016-11-04T15:51:31Z")

</div>

Thanks for the help on this @ScruffR @peekay123!
