Hi guys , I’m working in a project about a Solar panel monitoring system with 3 sensors for measuring Voltage,current,temperature using ESP32 and arduino IDE . Without using blynk , I got a correct values in oled display . but when i used blynk , i didn’t get correct values in voltage and current (oLed display + App).
I think that , the wifi is the problem . because the values are not correct in oled display and App !!!
For the 3 sensors , ADC pins are : 33,34,35
thanks for the help !
Here my code
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Wire.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#include "Wire.h"
#define BLYNK_TEMPLATE_ID "Removed"
#define BLYNK_DEVICE_NAME "Removed"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#include "BlynkEdgent.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define INPUT_VOLTAGE_SENSE_PIN 34
#define INPUT_CURRENT_SENSE_PIN 35
#define TEMP_SENSE_PIN 33
#define VOLTAGE_SCALE 20.6078431372 // R1+R2 / R2 // ( + ) /
#define CURRENT_SCALE 2 // R3+R4 / R4// ( + ) /
double mVperAmp = 0.066; //Sensityvit of the sensor // use 100 for 20A Module and 66 for 30A Module
double ACSoffset = 2; // Ideally it should be ( 0.5 x Vcc )
unsigned long last_time =0;
unsigned long current_time =0;
float power =0 ; // Power in Watt
float energy =0 ; // Emergy in Watt-Hour
float tempC=0; // temperaure in Celcius
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(TEMP_SENSE_PIN);
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(115200);
delay(100);
BlynkEdgent.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
display.display();
delay(500);
}
void loop() {
// read voltage and current
float voltage = abs( return_voltage_value(INPUT_VOLTAGE_SENSE_PIN)) ;
float current = abs( return_current_value(INPUT_CURRENT_SENSE_PIN)) ;
// read temperature from DS18B20
sensors.requestTemperatures(); // get temperatures
tempC = sensors.getTempCByIndex(0);
//tempF = sensors.getTempFByIndex(0);
// Calculate power and energy
power = current * voltage ; // calculate power in Watt
last_time = current_time;
current_time = millis();
energy = energy + power *(( current_time -last_time) /3600000.0) ; // calculate power in Watt-Hour // 1 Hour = 60mins x 60 Secs x 1000 Milli Secs
// ================= Display Data on Serial Monitor ================================================
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.print("Current: ");
Serial.println(current);
Serial.print("Power: ");
Serial.println(power);
Serial.print("Energy: ");
Serial.println(energy);
Serial.print("Temp: ");
Serial.println(tempC);
Serial.println(voltage);
delay(1000);
// ================= Display Data on OLED Display ================================================
// Display Solar Panel Voltage
display.setTextSize(1);
display.clearDisplay();
display.setCursor(10, 5);
display.print(voltage,2);
display.print(" V");
// Display Solar Panel Current
display.setCursor(90, 5);
display.print(current,1);
display.print(" A");
// Display Solar Panel Power in Watt
display.setTextSize(2);
display.setCursor(25,30);
display.print(power);
display.print(" W");
// Display Energy Generated by the Solar Panel
display.setCursor(25,50);
if ( energy >= 1000 )
{
display.print(energy/1000,2);
display.print(" kWh");
}
else
{
display.print(energy,2);
display.print(" Wh");
}
// Display Temperature
display.setTextSize(1);
display.setCursor(35,18);
display.print(tempC);
display.print((char)247);
display.println("C");
display.display();
display.clearDisplay();
BlynkEdgent.run();
Blynk.virtualWrite(V0, voltage ); // virtual pin 0
Blynk.virtualWrite(V1, current ); // virtual pin 1
Blynk.virtualWrite(V2, power ); // virtual pin 2
Blynk.virtualWrite(V3, energy ); // virtual pin 3
Blynk.virtualWrite(V4, tempC ); // virtual pin 4
}
//========================= Function to Calculate Solar Panel Voltage ===================================
double return_voltage_value(int pin_no)
{
double tmp = 0;
double ADCVoltage = 0;
double inputVoltage = 0;
double avg = 0;
for (int i = 0; i < 100; i++)
{
tmp = tmp + analogRead(pin_no);
}
avg = tmp / 100;
ADCVoltage = ((avg * 3.3) / (4095)) +0.19
; // 0.184 is offset adjust by heat and try
inputVoltage = ADCVoltage * VOLTAGE_SCALE;
return inputVoltage;
}
//========================= Function to Calculate Solar Panel Current ===================================
double return_current_value(int pin_no)
{
double tmp = 0;
double avg = 0;
double ADCVoltage = 0;
double Amps = 0;
for (int z = 0; z < 150; z++)
{
tmp = tmp + analogRead(pin_no);
}
avg = tmp / 150;
ADCVoltage = ((avg*3.3) / 4095);
Amps = ((ADCVoltage * CURRENT_SCALE - ACSoffset ) / mVperAmp); // 2 is the scaling for voltage divider
return Amps;
}