Help with Electron and Blynk... Please!

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 blynk server timing out? I’ve extended the heartbeat timeout in blynkConfig.h from 10 to 333 but not helping.

Code works perfectly on Ubidots. So I assume the servers there don’t mind the large delays between data poles…

I also stumbled upon an article within Blynk support suggesting that looped delay in code will not work well with the Blynk servers, and to use timed events instead? so this would be with the milli(s) function? But then I read somewhere else that milli(s) function has issues also and to use "simpleTimer,h which is not in the particle library…

On the Blynk app faceplate end… I just have V coil set to “push”. also tried set to “2 seconds”.

blynk is great and really means a lot to me as part of my project - where am I going wrong? Can someone help me with a code example to get this working would be absolutely appreciated…
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.virtualWrite(3, String(Tx01));
> ubidots.add("Tx01", Tx01);
> ubidots.sendAll();
> delay(300000);//send again in 5 minutes
> }

Hi Sam, without testing that code I can think of an issue that stands out in the huge in line delay you’ve got. Yes, there are some issues with the millis() function, but for what you’re trying to do, it will work perfectly.

Without writing out all of the code again here is the gist of what you want to do to avoid those long delay times:

unsigned long lastPublish;

void loop() {
    if((millis() - lastPublish) > 300000) {
        lastPublish = millis();
        //do timed stuff here

That example I wrote is super simple, it just is constantly checking if the millis() function (always incrementing) minus the lastPublish variable is greater than your set time, if it is, then do something, and the first thing you should do is update the lastPublish variable (it doesn’t have to be called that it can be whatever you want) to ensure that timing is as accurate as possible.

The reason people suggest steering away from millis() is because of the way it works, it isn’t going to be millisecond accurate over a long period of time even though thats the units it increments in. But considering you only want it to fire off every 5 minutes or so, even a 1000% error in accuracy (1 second) isn’t going to be a big deal.


@Sam Thanks so much mate. Apologies for the delay. Your suggestion is perfect and works exactly how I need it to. As a result of your code share and advice I have a much better understanding of the millis() function and correct use etc… Cheers for your support - Core Electronics have a fantastic team of people!
FYI, I also found the SparkCorePolledTimer library a great way to make periodic actions as was later brought to my attention too. Personally, for this task I prefer to simplify with millis().

1 Like

Hi Sam,

Yep the Photon also has a bunch of hardware timers which are made even easier with some of the Particle libraries. Bear in mind that if you take a look at the Particle P1 datasheet (the SoC module that makes up the Photon) using the hardware timers is only a matter of writing to a couple of registers if you want to avoid the libraries. Glad to hear everything is coming together!