What to do to store data after esp32 is turn-off

Good day, Im doing power consumption monitoring using Esp32 and blynk. Basically, there is a kilowatt-hrs (kWh) that will be displayed in the app (V11). My obvious problem is the value displayed in V11 which is kWh will reset everytime the esp32 goes off or unplug the 5v supply.

My question is, is there a code that will retrieve the value of kWh after turning the esp32 ON again?.

Blynk legacy or IOT ?

Check this out
https://docs.blynk.io/en/blynk.edgent/mainoperations/state-syncing

Im using Blynk IOT, I will try this later and update here if it works. Thankyou so much for your patience sir.

1 Like

It will retrieve the data (kWh) if I just off and on the wifi but It will resets the value of kWh if I unplug and plug again the 5v supply.

Post your updated sketch.
Don’t forget the triple backticks at the beginning and end of your sketch.
Triple backticks look like this:
```

Pete.

1 Like

this is my code sir

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLQzI3GAxo"
#define BLYNK_DEVICE_NAME "HOMEAUTION"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#include "EmonLib.h"
#include "BlynkEdgent.h"
//#include <driver/adc.h>

EnergyMonitor emon;
//EnergyMonitor emon2;

#define vCalibration 105
#define currCalibration 0.75


int relayPin1 = 27; 
int relayPin2 = 26;
int relayPin3 = 17; 
double kWh = 0;
double power;
int state_pin1;
int state_pin2;
int state_pin3;
double getCurrent = 0;
double getVoltage = 0;
BlynkTimer timer;

unsigned long lastmillis = millis ();
unsigned long seconds;

BLYNK_CONNECTED () {
Blynk.syncAll ();
}

void myCalculation () {
  
  emon.calcVI(20,2000); // calculate Vrms Irms Power
 

seconds = (millis () - lastmillis)/1000 ;
  Serial.print ("\nSeconds : ");
 Serial.print (seconds);
 
if (state_pin1 == 1 || state_pin2 == 1 || state_pin3 == 1){

   getCurrent = emon.Irms;
   getVoltage = emon.Vrms;
   power = emon.Vrms*emon.Irms;
   
    kWh = kWh + (power*(seconds)/36000.0)/1000; // calculate kWh
 Blynk.virtualWrite (V5, getVoltage);
 Blynk.virtualWrite (V6, getCurrent);
 Blynk.virtualWrite (V16, power); 
Blynk.virtualWrite (V11, kWh);

}
else {
  kWh;
  power = 0;
     getCurrent = 0;
  getVoltage = 0;
  
Blynk.virtualWrite (V5, getVoltage);
 Blynk.virtualWrite (V6, getCurrent);
 Blynk.virtualWrite (V16, power); 
Blynk.virtualWrite (V11, kWh);
}
      lastmillis = millis () ;

}

BLYNK_WRITE (V0) {

   state_pin1 = param.asInt ();

  if (state_pin1== 1) {
    digitalWrite (relayPin1, LOW) ; 
  }
  else {
    digitalWrite (relayPin1, HIGH) ;
  }
}
BLYNK_WRITE (V1) {

   state_pin2 = param.asInt ();

  if (state_pin2== 1) {
    digitalWrite (relayPin2, LOW) ; 
  }
  else {
    digitalWrite (relayPin2, HIGH) ;
  }
}

BLYNK_WRITE (V2) {

   state_pin3 = param.asInt ();

  if (state_pin3== 1) {
    digitalWrite (relayPin3, LOW) ; 
  }
  else {
    digitalWrite (relayPin3, HIGH) ;
  }
}


void setup()
{
  // adc1_config_channel_atten(ADC1_CHANNEL_7,ADC_ATTEN_DB_0);//35
//adc1_config_channel_atten(ADC1_CHANNEL_6,ADC_ATTEN_DB_0);//34
  Serial.begin(115200);
 
 emon.voltage(34, vCalibration, 1.7); // pin, calibration, phaseshift
  emon.current(35, currCalibration);

    pinMode (relayPin1, OUTPUT);
  pinMode (relayPin2, OUTPUT);
  pinMode (relayPin3, OUTPUT);

//all are off (connected to normally open in relay)
    digitalWrite (relayPin1, HIGH) ;
  digitalWrite (relayPin2, HIGH) ;
   digitalWrite (relayPin3, HIGH) ;

   BlynkEdgent.begin();
  timer.setInterval (2000L, myCalculation);
}

void loop() {
  BlynkEdgent.run();
  timer.run ();
}

Do your V0, V1 and V2 datastreams have the “Sync with latest server value every time device connects to the cloud” option enabled in the Advanced datastream setup?

If not, then Blynk.syncAll won’t achieve anything.

Pete.

1 Like

Good day pete, I tried " Sync with latest server" but still doesn’t work. If my esp32 goes off (unplug USB cable connected on laptop) and tried to on again, all my stored data resets automatically.

Exactly which virtual pin value(s) are you trying to synchronise, and how are those datastreams configured (screeenshots would help).

Adding serial print messages to show the program flow (such as a message in BLYNK_CONNECTED that says “BLYNK_CONNECTED function triggered”) and serial print messages to show the values of the various variables you want to synchronise at various locations within the sketch is your next step.

Pete.

You need to add in a function to retrieve the value from the server.

For example:

 BLYNK_WRITE (V11)
{
kWh = param.asDouble();
}
1 Like

Good day sorry for the late reply, the pictures below is part of my code (specific in retrieving kWh (V11)), and data stream.connected blynkwrite

This will appear if i run the serial monitor.

[2679] Using Dynamic IP: 192.168.43.65

[2689] Connecting to blynk.cloud:443

[5474] Certificate OK

[7315] Ready (ping: 1840ms).

My concern on this project is, even though we have the data on the cloud and can be retrieved…. There are times when there is no internet connectivity. Then there is no previous data to add the consumptions that’s going on until the internet is up. If this is just a hobby project, then it’s totally fine. If you want a perfect meter, you might want to consider adding a battery pack to your esp32 to always keep it running and an external SD card module and save the data(min 10hrs of backup)

This way the meter will be always up and ready to take the readings as soon as the power is back.

If I turn off the wifi connection and turn it on after few minutes it still can retrieve the data from V11. But it won’t retrieve if I unplug the 5v supply to my ESP32.

This is for our thesis that the kWh must be displayed (V11) for a month. But the problem is when power suddenly goes off the kWh that displayed in V11 restarts to 0.

Is it possible to retrieved if I use BLYNK_CONNECTED ()??? Or should I make a simple database like excel?

Posting snippets of code doesn’t help us (and therefore doesn’t help you), especially when those code snippets are in the form of screenshots.

You’ve ignored this piece of advice…

so I’ll take a back step until you’ve done that and posted you full updated code along with the serial output and some narrative that explains what steps you took in terms of removing and restoring power and removing and restoring WiFi to go along with the serial monitor output.

Pete.