Blynk app not updated at startup

Hi!
I am having some trouble with my current project. I use:
-Arduino Uno with Ethernet Shield
-Smartphone OS (iOS 14.1)
-Blynk server

I periodically send data to server via virtual pins. In my app I have four Gauges, data to three of them is pushed periodically using the virtual write function. It works without problems. The fourth gauge does not work like the other ones. I have a slider sending values 1-4 to device (V0) and the device answers on V1. This also works. But the problem I have is that when starting Blynk app on iPhone, the value of the fourth slider is not updated, the other ones are. Instead I have temporarily added some ugly periodic update with virtual write of V1. Then it works, but I suppose that when writing the value of V1 to server, that should be read by app? My feeling is that I have missed something here :slight_smile:

Best regards Björn

/*************************************************************

 Styrning av värmepump
 
 *************************************************************/
#define BLYNK_PRINT Serial 

// Blynk-items
#define BLYNK_TEMPLATE_ID           "123" (Masked)
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "ABC"     (Masked)
    
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <EEPROM.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Ethernet shield
#define W5100_CS 10
#define SDCARD_CS 4

// Uppstartsfördröjning START_DELAY * 1 s
// Ska sättas till 60 sekunder när systemet är live
// Routern verkar hitta koppling efter cirka 35 sekunder
#define START_DELAY 10

//*************************************************************/

// Används för periodisk koll
BlynkTimer periodicTimer, ledTimer;

// Räknare för antalet checkar mot cloud
// Kör varje minut
// LĂĄt dem nollas vid omstart
unsigned int connected = 0;
unsigned int not_connected = 0;

// Räknare för periodisk timer
int periodicCount = 0;

// Antal minuter - periodtid
const int period = 60; 

// EEPROM-struktur
//Address     Parameter
// 0          Level, indikator
// 1          Level        

// Virtuella pinnar
// Sätta temperaturnivå : V0
// Visa temperaturnivĂĄ  : V1
// Hall                 : V2
// Utomhus              : V3
// Vardagsrum           : V4
// Uppkoppling stat.    : V5


// Om pinne V0 (slider) ändrats, spara nivå persistent och sätt reläer
BLYNK_WRITE(V0)
{  
  int tempLevel = param.asInt();
   
  EEPROM.write(1, tempLevel);
  
  setLevel(tempLevel);

  Blynk.virtualWrite(V1, tempLevel);

}

/* Uppdatera nivå mot mobil för säkerhets skull?
BLYNK_CONNECTED()
{
  Blynk.virtualWrite(V1, EEPROM.read(1)); 
}
*/

// Kör periodiskt   
void timerHit()
{
  //---------------------------------------------------------------------------------
  
  // Kolla om en period gĂĄtt, om inte, hoppa ur
  periodicCount = periodicCount + 1;
  
  if (periodicCount < period)
  { 
    return;
  }
  //---------------------------------------------------------------------------------

  Serial.println("Kör periodisk funktion");
  
  // Eftersom en period gått, nolla räknaren
  periodicCount = 0;
  
  // Uppdatera, kanske hjälper
  //Blynk.virtualWrite(V1, EEPROM.read(1)); 

  // Börja om ifall räknarna blivit stora
  if ( (connected > 65000) || (not_connected > 65000) )
  {
    connected = 0;
    not_connected = 0;
  }
  else
  {
  // Kolla om grejerna är uppe
  if (Blynk.connected() == true)
  {
    connected = connected + 1;
  }
  else
  {
    not_connected = not_connected + 1;
  }

  // Undvik nolldivision samt uppdatera procentandel 
  if ( (connected+not_connected) != 0 )
  {
    Blynk.virtualWrite(V5, 100*connected/(connected+not_connected));
    Serial.print("Connected, not connected och andel:");
    Serial.println(connected);
    Serial.println(not_connected);
    Serial.println(100*connected/(connected+not_connected));
  }
  }

  //---------------------------------------------------------------------------------  
  
  // Temperaturer, kör tre mätningar per sensor
  double tempHall = calcTemperature(analogRead(0),analogRead(0),analogRead(0));
  
  double tempVardagsrum = calcTemperature(analogRead(1),analogRead(1),analogRead(1));
  
  double tempUte = calcTemperature(analogRead(2),analogRead(2),analogRead(2));

  double averageTempIn = (tempHall+tempVardagsrum)/2;
   
  // Uppdatera temperaturer till mobil
  Serial.println("Uppdatera temperaturer mot server");
  Serial.println(tempHall);
  Serial.println(tempVardagsrum);
  Serial.println(tempUte);
  
  Blynk.virtualWrite(V2, tempHall);

  Blynk.virtualWrite(V4, tempVardagsrum);

  Blynk.virtualWrite(V3, tempUte);

  Blynk.virtualWrite(V6, averageTempIn);

  /* Om senaste och förra mätningen är för låga -> stäng av
  if ( (lastInhouse < 10) && (averageTempIn < 10) )
  {
    EEPROM.write(1,4);
    setLevel(4);
    Blynk.virtualWrite(V1, EEPROM.read(1)); 
    lastInhouse = averageTempIn;
  }*/
     
}

//---------------------------------------------------------------------------------
void timerHitLed()
{
  digitalWrite(5, HIGH);
  delay(40);
  digitalWrite(5, LOW);

  Blynk.virtualWrite(V1, EEPROM.read(1));
  //Serial.println("Kör periodisk LED-funktion");
}

void setup()
{
 
  // Debug console
  Serial.begin(9600);

  pinMode(2, OUTPUT); // Relä för level 3
  pinMode(3, OUTPUT); // Relä för level 2
  pinMode(4, OUTPUT); // Relä för level 1
  pinMode(5, OUTPUT); // LED

  // Använd inte SD-kort       
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  // Vänta lite, öka chansen att routern vaknat efter strömavbrott
  delay(1000*START_DELAY);

  // Authentisera mot cloud
  Blynk.begin(auth);

  // Om det är första gången, sätt level
  if (EEPROM.read(0) != 173)
  {
    EEPROM.write(0,173);
    EEPROM.write(1,4);
    setLevel(4);
  }
  else
  {
    // Sätt sparat värde
    setLevel(EEPROM.read(1));
  }

  // Periodisk funktion, kör varje minut för att pusha data
  periodicTimer.setInterval(60000L, timerHit);
  ledTimer.setInterval(5000L, timerHitLed);
}

void loop()
{
  Blynk.run();
  periodicTimer.run();
  ledTimer.run();
}


// Gör om mätvärde till temperatur
double calcTemperature(int reading1, int reading2, int reading3) 
{
  // Medelvärde
  int reading = (reading1+reading2+reading3)/3;
  double Temp;
  Temp = log(10000.0 * ((1024.0 / reading - 1)));
  Temp = 1 /(0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp);
  Temp = Temp - 273.15; // convert from Kelvin to Celsius
  return Temp;
}


void setLevel(byte value)
{
  
// Sätt relä direkt
  if (value==4)
  { 
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==3)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==2)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
  }
  if (value==1)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
  }  
}  

You don’t need to do this. One BlynkTimer instance can support up to 16 timers.

Also, you’ll very quickly kill your device’s NVR if you keep doing EEPROM read/writes like this.
If you need to store values locally then you’d be better using an ESP device and a WiFi connection, as this would allow the use of SPIFFS or LittleFS.
But, I see no purpose in storing the variables locally in this case. You’d normally do local storage if your device was designed to work when not connected to Blynk, but as you’re using the blocking Blynk.begin command then the sketch won’t run if you aren’t connected to Blynk.
You’d therefore be better storing these values on the Blynk server.

As far as I can tell from your rather vague description of the problem, it sounds like you need to add a BLYNK_CONNECTED function, with a Blynk.syncVirtual(V0) command in it.

Pete.

Hi, thanks for the help. I changed to:

  //Periodisk funktion, kör varje minut för att pusha data
  periodicTimer.setInterval(60000L, timerHit);
  periodicTimer.setInterval(5000L, timerHitLed);
...
...
...
// Synka värdet från server
 BLYNK_CONNECTED()
{
   Blynk.syncVirtual(V0);
}

Removed all EEPROM stuff, hope it will work this way.

The problem I have with the app is:
My device pushes data to server via virtual pins periodically. In app I have connected V1, V2, V3 etc. to Gauge widgets. When I open app, the widgets connected to V1, V2 etc. are updated with the latest values sent to server. But widget connected to V1 is not. For that one I have to wait a very long time before it is updated. But if the device writes to V1 it shows up in app immediately.

Br Björn

What are the differences between the way that your V1 datastream is configured, and the way that your V2 & V 3 datastreams are configured?

Pete.

I checked datastreams now and had somehow failed to set V1 correctly for that gauge, unbelievable :slight_smile: I just assumed that was correct, but obviously it wasn’t. Thanks a lot for pointing mi in that direction! Works great now!

Br Björn

1 Like