BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Save value with syncVirtual

hello, can u give me a hand, i have little problem with my project
here’s my code

BLYNK_WRITE(VPIN_TERMINAL)
{ 
  InputTerminal = param.asStr();
  intlen = InputTerminal.toInt(); 
  CountS = CountS + intlen; 
}



void setup()
{
  Serial.begin(115200);
  delay(100);
  rtc.begin(); 
  
  // Setup a function to be called every second
  setSyncInterval(500);
  startMillis = millis();  
  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
  sendtime();
  
  Blynk.virtualWrite(VPIN_Energy, CountS); 
  
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(VPIN_Energy);
}

everyting is fine but when the device loss power, value’s on virtual PIN VPIN_Energy is gone to

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

There’s no need to add it.

You shouldn’t use delay in a Blynk sketch because it is a blocking function. The blocking function prevents the program from executing any additional actions until the current task has been completed.

If you need to keep your hardware in sync with Widgets’ state even if app is offline use “Blynk.virtualWrite”.

Your title for this topic and your limited description of the issue are confusing.

Could you please explain in detail what your issue is, and what you are trying to achieve.

Pete.

im trying to using terminal to adding an integer to count up values on virtual pin “VPIN_Energy”. it was totally works, but when power is off, that value on VPIN_Energy is back to “0” even im using Blynk.syncVirtual(VPIN_Energy)

thanks, already edited

So you’re using the terminal widget to input data from the app?

Pete.

yeah, sorry about forgeting that

Well, the Terminal widget in Blynk IoT does not remember the data that is displayed or inputted when the app is closed and re-opened.
If you want to input data then the Numeric Input widget is probably the best approach.
You can then use Blynk.syncVirtual(vPin) to retrieve the current value from that widget.

Pete.

i’ve trying using slider widget for alternative upgrade, but still same.

is the slider widget does not remember the data to?

The slider widget will be fine.
You’re clearly doing something wrong, but you seem reluctant to share any proper details so it’s difficult to help.

Pete.

i’m apologize, here’s my fully code

#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
#include <TimeLib.h>      /* Program code related to Real Time Clock (RTC). */
#include <WidgetRTC.h>    /* Communication code with Blynk Real Time Clock Widget */
#include <PZEM004Tv30.h>

#define BlynkTimer timer; /* Define parameter for Blynk Timer */
 
bool isFirstConnect = true;
const unsigned long period = 1000; 
unsigned long startMillis;                              /* start counting time for display refresh*/
unsigned long currentMillis;                            /* current counting time for display refresh */


char strBMsg[]={ "" };        //Store Blynk Message
char currentTime[9];

int intlen, InputS; 
int CountS = 0; 
String displaycurrenttimepluswifi;
String InputTerminal;
String CDEnergy;

float Power, Energy, Voltage, Current, Frequency, PFactor;

/* Use software serial for the PZEM
 * Pin 16 Rx esp32(Connects to the Tx pin on the PZEM)
 * Pin 17 Tx esp32(Connects to the Rx pin on the PZEM)
*/

#if defined(ESP32)
PZEM004Tv30 pzem(Serial2, 16, 17); //   RX & TX on esp32
#else
PZEM004Tv30 pzem(Serial2);
#endif

WidgetRTC rtc; 

 
#define VPIN_TERMINAL                 V20
#define VPIN_Voltage                  V21
#define VPIN_Current                  V22
#define VPIN_Power                    V23
#define VPIN_Energy                   V24
#define VPIN_Frequency                V25
#define VPIN_PFactor                  V26
#define VPIN_CTime                    V27
#define VPIN_CDate                    V28
#define VPIN_Input                    V30

 

void sendtime()
{
  timer.run();  
  currentMillis = millis();                                                       /* Keep counting time for display refresh */
  if(currentMillis - startMillis > period)                                        /* For every 1 second, run the set of code*/
  { 
    
    String currentTime = String(hour()) + ":" + minute() + ":" + second();        /* Define "currentTime" by combining hour, minute and second */
    String currentDate = String(day()) + " " + month() + " " + year();            /* Define "currentDate" by combining day, month, and year */

    Blynk.virtualWrite(VPIN_CTime, currentTime);                                          /* Send Time parameters to Virtual Pin V1 on Blynk App */
    Blynk.virtualWrite(VPIN_CDate, currentDate);                                          /* Send Date parameters to Virtual Pin V2 on Blynk App */
                                                      
    startMillis = millis();                                                       /* Reset time for the next counting cycle */
  }
}


void setup()
{
  Serial.begin(115200);
  delay(100);
  rtc.begin(); 
  
  // Setup a function to be called every second
  setSyncInterval(500);
  startMillis = millis();  
  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
  sendtime();
  sensorpzem();
  
  
  CDEnergy = (0.118 + CountS) - Energy;

  Blynk.virtualWrite(VPIN_Voltage, Voltage); 
  Blynk.virtualWrite(VPIN_Current, Current); 
  Blynk.virtualWrite(VPIN_Power, Power); 
  Blynk.virtualWrite(VPIN_Frequency, Frequency); 
  Blynk.virtualWrite(VPIN_PFactor, PFactor); 

  Blynk.virtualWrite(VPIN_Energy, CDEnergy); 
  

  delay(800);
//  Blynk.run();
}

//BLYNK_WRITE(VPIN_TERMINAL)
//{ 
//  InputTerminal = param.asStr();
//  intlen = InputTerminal.toInt(); 
//  CountS = CountS + intlen; 
//}

BLYNK_WRITE(VPIN_Input) //slider widget
{ 
  InputS = param.asInt(); 
  CountS = CountS + InputS; 
}

BLYNK_CONNECTED() 
{
  Blynk.syncVirtual(VPIN_Energy);
}

void sensorpzem()
{
  Voltage = pzem.voltage();
  Current = pzem.current();
  Power = pzem.power();
  Energy = pzem.energy();
  Frequency = pzem.frequency();
  PFactor = pzem.pf();
     
  if (isnan(Voltage))
  {
    Voltage=0;
    Current=0;
    Power=0;
    Frequency=0;
    PFactor=0;
    Blynk.virtualWrite(VPIN_TERMINAL,"Failed to read voltage\n");
  }
  
}

It’s not really your full code we need. It’s an explanation of how you’ve set-up your widgets and datastreams, what the functionality of these widgets and datastreams is meant to be, what it is that you are trying to achieve, and exactly what isn’t working for you.

Pete.

by using PZEM-004 sensor, i want to input some numeric values by using a datastream, and use these values for modify energy meter.

all code is fine, but when device goes off, im wondering why Blynk.syncVirtual isnt working

as you suggest i chance from terminal to slider widget and yet still cant save the data

Your sketch says that the slider is attached to VPIN_Input but you’re not synchronising that datastream in your BLYNK_CONNECTED function.

You also need to move all of that stuff out of your void loop, remove the delay and use a timer to call this code instead.

Pete.

still not working.
now i’m using EEPROM for saving values, also im using terminal widget, and everything works as i want.

but im still wondering is datastream of blynk cloud cant save integer values?

Currently, the datastream supports int, double, and string data types.

Most likely due to the way you’ve implemented this, but as you’ve not shared your code then it’s difficult to say.

Hopefully that’s not overwriting the Edgent saved credentials.
EEPROM is fine for stuff that changes very infrequently, but as each EEPROM location has an average life of 100,000 write operations then it’s very easy to kill the EEPROM of your device.

SPIFS OR LittleFS is a better solution.

Pete.

The Preferences library is also an option
https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/preferences.html

yeah that’s why im still want to try with blynk cloud

here i came with new simple code

BLYNK_CONNECTED()

{

  Blynk.syncAll();

  Blynk.syncVirtual(VPIN_Energy);

}

void setup()

{

  Serial.begin(115200);

  delay(100);

 

  // Setup a function to be called every second

  setSyncInterval(500);

  startMillis = millis();  

  BlynkEdgent.begin();

}

void loop() {

  BlynkEdgent.run();

  sendtime();

  send();

}

void send()

{

  Blynk.virtualWrite(VPIN_Energy, Energy);  

}

BLYNK_WRITE(VPIN_TERMINAL)

{

  InputTerminal = param.asStr();

  intlen = InputTerminal.toInt();

  Energy = Energy + intlen;

}

but still when device is disconnet values is gone

i’ve trying using all