Solar PV Energy Meter 2.0


#define BLYNK_TEMPLATE_NAME "Soler monitoring system"

#define BLYNK_AUTH_TOKEN "Q6Dj092nraH2Vlxxxx"

#define BLYNK_PRINT Serial

#include <Wire.h>

#include <Adafruit_SSD1306.h>

#include <Adafruit_ADS1X15.h>

#include <BlynkSimpleEsp32.h>

#include <OneWire.h>

#include <DallasTemperature.h>

// Pin definitions

const int oneWirePin = D3; // OneWire pin for DS18B20

// OLED display

Adafruit_SSD1306 display(128, 64, &Wire);

// ADS1115

Adafruit_ADS1115 ads;

// DS18B20 temperature sensor

OneWire oneWire(oneWirePin);

DallasTemperature sensors(&oneWire);

float voltage =0;

float current =0;

float power =0;

unsigned long previousMillis = 0;

float energy = 0;

float temperature =0;

float saving =0; // cost saving

const float EnergyCost = 7.5;

// Voltage divider resistor values

const float R1 = 560;  // 560k

const float R2 = 33;  // 33k

const float R3 = 1;  // 1K

const float R4 = 2; // 2K

// ACS758 zero-current output voltage ( Offset Voltage)

float OffsetVoltage = 2.5;

// ACS758 current sensor sensitivity

const float sensitivity = 40; // in mV/A

//========================= Variables for wifi server setup =============================

const char* ssid = "ruby";

const char* password = "123456789900";

const char* blynkAuth = "Q6Dj092nraH2VlPxxxxx";    

void setup()

{

  Serial.begin(115200);  

  // Initialize the Wi-Fi and Blynk connections

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(2000);

    Serial.print(".");

  }

  Serial.println("\nConnected to Wi-Fi");

  Blynk.begin(blynkAuth, ssid, password);

   

  // Initialize the OLED display

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.clearDisplay();

  display.display();

  // Initialize the ADS1115

  ads.begin();

  // Initialize the DS18B20 temperature sensor

  sensors.begin();

}

void loop()

{

 

  // Read the raw voltage values from the ADS1115

  int16_t voltageRaw = ads.readADC_SingleEnded(0);

  int16_t currentRaw = ads.readADC_SingleEnded(1);

  // Calculate the voltage values in Volts

  float voltageVolts = voltageRaw * (0.1875/1000) ; //  0.1875mV is default Gain

  float voltage = voltageVolts * ((R1 + R2) / R2); // Calculate the actual voltage by using the voltage dividers  

 

  // Calculate the current values in Amps

  float currentVolts = currentRaw * ((R3 + R4) / R4)* (0.1875/1000); //  0.1875mV is default Gain

  current =  (currentVolts - OffsetVoltage )/(sensitivity / 1000.0) ; // convert voltage into current  

   

  // Calculate the power output (P = V * I)

  power = voltage * current;

  // Calculate energy by integrating power over time

  unsigned long currentMillis = millis();

  energy += power * (currentMillis - previousMillis) / 3600000; // Energy in Wh

  saving = EnergyCost * ( energy /1000 );

  previousMillis = currentMillis;

  // Read the temperature from the DS18B20 sensor

  sensors.requestTemperatures();

  temperature = sensors.getTempCByIndex(0);  // temperature in Celsius

//temperature = sensors.getTempFByIndex(0); // temperature in Fahrenheit

 

  //Display the values on the OLED screen  

  display.clearDisplay();

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(10, 10);

  display.print(voltage,2);

  display.println(" V");

  display.setCursor(70, 10);

  display.print(current,2);

  display.println(" A");

  display.setTextSize(2);

  display.setCursor(10,25);  

  display.print(power,2);

  display.println(" W");

  display.setCursor(10,45);

  display.print(energy,2);

  display.println(" Wh");

 // display.print("T: ");

 // display.print(temperature);

 // display.println(" C");

  display.display();

  // ================= Display Data on Blynk App ================================================

   

  // Send the data to Blynk

  Blynk.run();

  Blynk.virtualWrite(V0, voltage);

  Blynk.virtualWrite(V1, current);

  Blynk.virtualWrite(V2, power);

  Blynk.virtualWrite(V3, energy/1000);

  Blynk.virtualWrite(V4, temperature);

  Blynk.virtualWrite(V5, saving);

 // Print the values on the Serial Monitor  

  Serial.print(" Voltage: ");

  Serial.print(voltage);

  Serial.print("V");  

  Serial.print(" Current: ");

  Serial.print(current);

  Serial.print("A");

  Serial.print(" Power: ");

  Serial.print(power);

  Serial.print("W");

  Serial.print(" Energy: ");

  Serial.print(energy,2);

  Serial.print("Wh");

  Serial.print(" Savings: ");

  Serial.print("Rs.");

  Serial.print(saving,2);  

  Serial.print(" Temp: ");

  Serial.print(temperature);

  Serial.println("C");

 

  delay(2000);

}  

The circuit works correctly after wifi is connected but after disconnect it does not connect again only it keeps on searching serial print is mentioned below please help us with the project

11:15:28.493 -> Voltage: 4.44V Current: -50.53A Power: -224.20W Energy: -1.53Wh Savings: Rs.-0.01 Temp: -127.00C

11:15:31.460 -> Voltage: 4.37V Current: -50.58A Power: -221.01W Energy: -1.73Wh Savings: Rs.-0.01 Temp: -127.00C

11:15:34.252 -> Voltage: 4.42V Current: -50.58A Power: -223.40W Energy: -1.90Wh Savings: Rs.-0.01 Temp: -127.00C

11:15:37.105 -> [34210] Connecting to blynk.cloud:80

11:15:37.105 -> [34211] Connecting to blynk.cloud:8080

11:15:37.105 -> Voltage: 4.42V Current: -50.58A Power: -223.40W Energy: -2.08Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:39.919 -> Voltage: 4.41V Current: -50.58A Power: -223.23W Energy: -2.25Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:42.744 -> [39857] Connecting to blynk.cloud:80

11:15:42.744 -> [39857] Connecting to blynk.cloud:8080

11:15:42.744 -> Voltage: 4.42V Current: -50.57A Power: -223.37W Energy: -2.43Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:45.541 -> Voltage: 4.41V Current: -50.57A Power: -223.20W Energy: -2.60Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:48.381 -> [45504] Connecting to blynk.cloud:80

11:15:48.381 -> [45504] Connecting to blynk.cloud:8080

11:15:48.381 -> Voltage: 4.42V Current: -50.58A Power: -223.40Energy: -2.78Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:51.208 -> Voltage: 4.42V Current: -50.58A Power: -223.40W Energy: -2.95Wh Savings: Rs.-0.02 Temp: -127.00C

11:15:54.030 -> [51151] Connecting to blynk.cloud:80

11:15:54.030 -> [51151] Connecting to blynk.cloud:8080

after disconnect not connecting to Wi-Fi hotspots
Board xiao_esp32c3

Your first set of triple backticks are on the same line as your first set of code, so that line of code is invisible.
Your second set of backticks, at the end of your code, are missing entirely.

You should post your serial monitor output (as text, also with triple backticks on separate lines at the beginning and end) which shows what happens when the disconnection occurs.

You should also read this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.

You’ve attempted to edit your post, but made things far worse.

Unformatted code has been removed (three times). I’d success that you try again, although all three copies of the code you posted appear to have the same cluttered void loop, which is probably the cause of your problem.

Pete.

Your latest post seems to be connected to your earlier one, where posted several chunks of code without the necessary triple backticks, and which was subsequently deleted after 24 hours.

I’ve merged these two topics topgether.

You’ll see that I asked you to…

but this latest post from you still doesn’t have triple backticks at the beginning and ends of your serial output.

Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your serial output so that they display correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

I’d also suggest that you post your full sketch (also with triple backticks of course), and information about the type of hardware you are using, Blynk library version info, ESP32 core version etc etc.

Pete.

Please stop creating new topics about the same project. I have once again merged your new topic into this one.

When you use triple backticks they need to be on a separate line, otherwise they don’t work properly.
I fixed this for you in your latest topic, but then you re-edited the post and broke it again.

A quick hint - nobody is going to be able to help without seeing your code.

Pete.

#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"

#define BLYNK_TEMPLATE_NAME "Soler monitoring system"

#define BLYNK_AUTH_TOKEN "Q6Dj092xxxx"

#define BLYNK_PRINT Serial

#include <Wire.h>

#include <Adafruit_SSD1306.h>

#include <Adafruit_ADS1X15.h>

#include <BlynkSimpleEsp32.h>

#include <OneWire.h>

#include <DallasTemperature.h>

// Pin definitions

const int oneWirePin = D3; // OneWire pin for DS18B20

// OLED display

Adafruit_SSD1306 display(128, 64, &Wire);

// ADS1115

Adafruit_ADS1115 ads;

// DS18B20 temperature sensor

OneWire oneWire(oneWirePin);

DallasTemperature sensors(&oneWire);

float voltage =0;

float current =0;

float power =0;

unsigned long previousMillis = 0;

float energy = 0;

float temperature =0;

float saving =0; // cost saving

const float EnergyCost = 7.5;

// Voltage divider resistor values

const float R1 = 560;  // 100K

const float R2 = 31.9;  // 10K

const float R3 = 1;  // 1K

const float R4 = 2; // 2K

// ACS758 zero-current output voltage ( Offset Voltage)

float OffsetVoltage = 2.5;

// ACS758 current sensor sensitivity

const float sensitivity = 40; // in mV/A

//========================= Variables for wifi server setup =============================

const char* ssid = "ruby";

const char* password = "123xxxx";

const char* blynkAuth = "Q6Dj092nraH2xxxxxx";    

void setup()

{

  Serial.begin(115200);  

  // Initialize the Wi-Fi and Blynk connections

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("\nConnected to Wi-Fi");

 

  Blynk.begin(blynkAuth, ssid, password);

     

  // Initialize the OLED display

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.clearDisplay();

  display.display();

  // Initialize the ADS1115

  ads.begin();

  // Initialize the DS18B20 temperature sensor

  sensors.begin();

}

void loop()

{

 

  // Read the raw voltage values from the ADS1115

  int16_t voltageRaw = ads.readADC_SingleEnded(0);

  int16_t currentRaw = ads.readADC_SingleEnded(1);

  // Calculate the voltage values in Volts

  float voltageVolts = voltageRaw * (0.1875/1000) ; //  0.1875mV is default Gain

  float voltage = voltageVolts * ((R1 + R2) / R2); // Calculate the actual voltage by using the voltage dividers  

 

  // Calculate the current values in Amps

  float currentVolts = currentRaw * ((R3 + R4) / R4)* (0.1875/1000); //  0.1875mV is default Gain

  current =  (currentVolts - OffsetVoltage )/(sensitivity / 1000.0) ; // convert voltage into current  

   

  // Calculate the power output (P = V * I)

  power = voltage * current;

  // Calculate energy by integrating power over time

  unsigned long currentMillis = millis();

  energy += power * (currentMillis - previousMillis) / 3600000; // Energy in Wh

  saving = EnergyCost * ( energy /1000 );

  previousMillis = currentMillis;

  // Read the temperature from the DS18B20 sensor

  sensors.requestTemperatures();

  temperature = sensors.getTempCByIndex(0);  // temperature in Celsius

//temperature = sensors.getTempFByIndex(0); // temperature in Fahrenheit

 

  //Display the values on the OLED screen  

  display.clearDisplay();

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(10, 10);

  display.print(voltage,2);

  display.println(" V");

  display.setCursor(70, 10);

  display.print(current,2);

  display.println(" A");

  display.setTextSize(2);

  display.setCursor(10,25);  

  display.print(power,2);

  display.println(" W");

  display.setCursor(10,45);

  display.print(energy,2);

  display.println(" Wh");

 // display.print("T: ");

 // display.print(temperature);

 // display.println(" C");

  display.display();

  // ================= Display Data on Blynk App ================================================

   

  // Send the data to Blynk

  Blynk.run();

  Blynk.virtualWrite(V0, voltage);

  Blynk.virtualWrite(V1, current);

  Blynk.virtualWrite(V2, power);

  Blynk.virtualWrite(V3, energy/1000);

  Blynk.virtualWrite(V4, temperature);

  Blynk.virtualWrite(V5, saving);

 // Print the values on the Serial Monitor  

  Serial.print(" Voltage: ");

  Serial.print(voltage);

  Serial.print("V");  

  Serial.print(" Current: ");

  Serial.print(current);

  Serial.print("A");

  Serial.print(" Power: ");

  Serial.print(power);

  Serial.print("W");

  Serial.print(" Energy: ");

  Serial.print(energy,2);

  Serial.print("Wh");

  Serial.print(" Savings: ");

  Serial.print("Rs.");

  Serial.print(saving,2);  

  Serial.print(" Temp: ");

  Serial.print(temperature);

  Serial.println("C");

 

  delay(100);

}

this is a my code

I’ve fixed this for you (again) please use triple backticks properly in future.

I see that you’ve now deleted your descriptor the issue you are now experiencing, and the serial output that goes with the code you’ve just posted. I’m not sure how you expect forum members to help based on virtually no useable information.

Pete.

I’d suggest that you read this link (which I already provided, but you seem to have ignored)…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

and also understand that you can’t send more than 10 pieces of data to Blynk per second.
You are attempting to do 6 Blynk.virtualWrites every 100ms, or 60 per second - 6 times what is allowed.

Also, why are you manually establishing a WiFi connection then calling Blynk.begin() - which establishes a WiFi connection then connects to Blynk.

Pete.