Blynk Values doesnt always update?

Hi,

Was hoping someone can help me…I’m using an ultrasonic sensor to take readings of a water tank, and then send those values to blynk at specific times. My problem is that the values seem to print fine in my serial monitor, but doesnt always update on my app. I can see the last connection time on the app, but the value stays the same. Sometimes it does update…very weird.

Im using a pro micro, 5V with a jsn-sr04t ultrasonic senor and a sim800l gsm module.

The output on my serial monitor looks like this

10:09:58
10:09:59
10:10:00
Initializing modem...
[1293686] 
   ___  __          __
  / _ )/ /_ _____  / /__
 / _  / / // / _ \/  '_/
/____/_/\_, /_//_/_/\_\
       /___/ v0.5.4 on Arduino

[1293690] Modem init...
[1294825] Connecting to network...
[1298223] Network: Mobile Telephone Network
[1298223] Connecting to  ...
[1304073] Connected to GPRS
[1304089] Connecting to blynk-cloud.com:80
[1310603] Connecting to blynk-cloud.com:80
[1311826] Ready (ping: 682ms).

Water depth: 154
Litres: 4094
Total Litres: 8188
Percentage: 81
Signal Qlty: 25
[1315557] Disconnected
10:10:31
10:10:31

and below is my sketch

#include <DS3232RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <NewPing.h>
#define SIM800_TX_PIN 8
#define SIM800_RX_PIN 9
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
int cntFailConst = 0;
#define uS_TO_m_FACTOR 60000000   // Conversion factor for micro seconds to minutes
char auth[] = "a892d375fb4b47b4b66670d5c086d6b1";
char apn[]  = "";
char user[] = "";
char pass[] = "";
const int MAX_DISTANCE = 220;                       //max distance to measure
const int Diameter1 = 185;                                //internal Diameter of tank 1 in cm
const int Depth1 = 210;                                 //total depth of tank 1 in cm , from sensor to base inside
const unsigned int Period = 60000;    //period between pings, in milliseconds. i.e  1 munute = 60,000. max 65535. Want longer? use unsigned long
const int PingPin1 = 18;                          //     GPIO5,  D1
const int EchoPin1 = 19;                        //     GPIO4,  D2
const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));          //area of base of tank 1
int Litres1, Litres2, Distance1, WaterDepth1, Decimal, Persentage, battLevel, csq;
String gsmTime;
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(SIM800_TX_PIN, SIM800_RX_PIN);
TinyGsm modem(SerialAT);
BlynkTimer timer;
NewPing sonar = NewPing(PingPin1, EchoPin1, MAX_DISTANCE);
tmElements_t tm;
//-----------------------------------------------------------------------------------------------------------------------//
void setup()
{
 
 Serial.begin(9600);
 timer.setInterval(1000L, TimeDateDisplay);
}

//-----------------------------------------------------------------------------------------------------------------------//

void loop()
{
 pinMode(EchoPin1,INPUT_PULLUP);
 powerOFF();
 timer.run();
}

//Power On and OFF Functions---------------------------------------------------------------------------------------------------------//

void powerON()
{
 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);
}
void powerOFF()
{
 pinMode(4, OUTPUT);
 digitalWrite(4, LOW);
}

//-----------------------------------------------------------------------------------------------------------------------//
void TimeDateDisplay()
{
 RTC.read(tm);
 char dateBuffer[19];
 sprintf(dateBuffer, "%02u:%02u:%02u", tm.Hour, tm.Minute, tm.Second);
 Serial.println(dateBuffer);
 if ((tm.Hour == 9) && (tm.Minute == 55) && (tm.Second == 00)) {
   myFunction();
 }
 if ((tm.Hour == 10) && (tm.Minute == 00) && (tm.Second == 00)) {
   myFunction();
 }
 if ((tm.Hour == 10) && (tm.Minute == 05) && (tm.Second == 00)) {
   myFunction();
 }
 if ((tm.Hour == 10) && (tm.Minute == 10) && (tm.Second == 00)) {
   myFunction();
 }
 if ((tm.Hour == 10) && (tm.Minute == 15) && (tm.Second == 00)) {
   myFunction();
 }
}
//-----------------------------------------------------------------------------------------------------------------------//
void myFunction()
{
 powerON();
 delay(1000);
 SerialAT.begin(57600);
 Serial.println("Initializing modem...");
 modem.restart();
 Blynk.begin(auth, modem, apn, user, pass);
 delay(3000);
 Distance1 = sonar.ping_cm();                                       //get distance to the top of the water tank 1
 if (Distance1 >= Depth1 || Distance1 == 0 )  Distance1 = Depth1;                   //check it does not go negative
 WaterDepth1 = Depth1 - Distance1;                     //calculate the depth of the water
 Litres1 = ((Depth1 * (PI * (sq(Diameter1 / 2)))) - (Distance1 * (PI * (sq(Diameter1 / 2))))) / 1000;
 Litres2 = (Litres1 * 2);
 Persentage = (Litres1 / 5000.00) * 100;
 csq = modem.getSignalQuality();
 Blynk.virtualWrite(V1, WaterDepth1);
 Blynk.virtualWrite(V2, Litres1);
 Blynk.virtualWrite(V3, Litres1);
 Blynk.virtualWrite(V4, Persentage);
 Blynk.virtualWrite(V5, Litres2);
 Blynk.virtualWrite(V6, csq);
 delay(50);
 Serial.println();
 Serial.println("Water depth: " + String(WaterDepth1));
 Serial.println("Litres: " + String(Litres1));
 Serial.println("Total Litres: " + String(Litres2));
 Serial.println("Percentage: " + String(Persentage));
 Serial.println("Signal Qlty: " + String(csq));
 Blynk.run();
 Blynk.disconnect();
 powerOFF();
}

Anyone have an idea what im doing wrong?

Pin mode is a run once void setup() command, NOT for use in the void loop() or any of your other functions.

And you are tying to run the powerOFF() function thousands of times a second with it sitting in the void loop() like that

Blynk.begin() (and many others of these commands) should also be in your void setup() not in this other function.

And Blynk.run() needs to be added into your void loop()

You do seem to have some strange code :face_with_raised_eyebrow:

Thanks for the quick reply Gunner…Ive moved pinMode() and powerOFF() out of the loop and into the void setup() and will test it soon…but the reason I run these other commands in the myFunction() is because I only want it to go online at specific times. So if its in the void setup() then my project needs to be connected all the time? (cause void sutup only runs once?)

Once that is done you can use connection management commands to disconnect and reconnect as you need.

would i still be able to use my powerOn and powerOFF functions like that? The reason I dont put it in the void setup, is because I cut the power completely to the GSM and ultrasonic sensor with a relay to save some power.

Your code is rather odd, but I guess it makes sense with what you’re trying to do.
I think the problem you described in your initial post is caused by having the Blynk.run Immediately before the Blynk.disconnect.
I’d move (or add another) Blynk.run before your delay(50) command.

Pete.