Please help needed for editing code for new sensor

Hi,for my project iam using an acs712 20a current sensor and dht11 temperature and ambient sensor. but in the code its is acs723 and DS18B20 temperature sensor.
so can someone help me editing the code.iam using esp32 module
This is the code below:

 #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <DallasTemperature.h> 
    #include <OneWire.h>   
    #include "Wire.h"    
    #include <WiFi.h>          
    #define BLYNK_PRINT Serial
    #include <BlynkSimpleEsp32.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 4
    #define VOLTAGE_SCALE  7.911 // R1+R2 / R2 // ( 47K + 6.8K ) / 6.8K
    #define CURRENT_SCALE  1.5 // R4+R5 / R5 // ( 1K + 2K ) / 2K
    
    double mVperAmp = 200; //Sensityvit of the sensor //  use 100 for 20A Module and 66 for 30A Module
    double ACSoffset = 514; // Ideally it should be ( 0.1 x Vcc ) // measured value is 514mV
    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
    //float tempF = 0; temperature in F
    float saving=0; // cost saving  
   
    
     WiFiClient client;
    // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
    // GPIO where the DS18B20 is connected to
    const int oneWireBus = 2;    
    // Setup a oneWire instance to communicate with any OneWire devices
    OneWire oneWire(TEMP_SENSE_PIN);
    DallasTemperature sensors(&oneWire);
    
//========================= Variables for wifi server setup =============================
     
  // Your WiFi credentials.
  // Set password to "" for open networks.
  char ssid[] = "XXXX"; // WiFi Router ssid
  char pass[] = "XXXX"; // WiFi Router password
  
  // copy it from the mail received from Blynk
  char auth[] = "XXXX";  
  
//========================= Setup Function ================================================ 
    
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
  
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }  
  display.clearDisplay();  
  display.setTextColor(WHITE);
  display.display();  
  delay(500);
}

//========================= Loop Function ================================================ 
    

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

  saving = 6.5 * ( energy /1000 ); // 6.5 is cost per kWh // used just for example
  

  // ================= 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, 10);
  display.print(voltage,1);
  display.print(" V");

  // Display Solar Panel Current 
  
  display.setCursor(70, 10);

  if (current >0 && current < 1 )
  {
   display.print(current*1000,0);
   display.print(" mA");
  }
  else
  {
  display.print(current,2);  
  display.print(" A");
  }

 // Display Solar Panel Power in Watt

  display.setTextSize(2);
  display.setCursor(10,25);
  display.print(power);
  display.print(" W");

 // Display Energy Generated by the Solar Panel 
  display.setCursor(10,45);
  
  if ( energy >= 1000 )
  {
   display.print(energy/1000,3);
   display.print(" kWh");
  }
  else
  {
  display.print(energy,1);  
  display.print(" Wh");
  }
  display.display();
  display.clearDisplay();
  
  
// ================= Display Data on Blynk App ================================================
   Blynk.run();  
   Blynk.virtualWrite(0, voltage ); // virtual pin 0
   Blynk.virtualWrite(1, current ); // virtual pin 1 
   Blynk.virtualWrite(2, power);    // virtual pin 2
   Blynk.virtualWrite(3,energy/1000);// virtual pin 3
   Blynk.virtualWrite(4,tempC );    // virtual pin 4   
   Blynk.virtualWrite(5,saving);    // virtual pin 4  
   //delay(1000);
}

//========================= 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.184 ; // 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*3331) / 4095); // Gets you mV
  Amps = ((ADCVoltage * CURRENT_SCALE - ACSoffset ) / mVperAmp); // 1.5 is the scaling for voltage divider
  return Amps;
}<br>

Welcome to the forum

Have you looked at the examples that come with the DHT11 and acs713 libraries to see how to read the values ?

yes i tried a lot and i am new to this i have compared with other codes also if you can please help

Work on each component using example code before you try to build a project. Once you master each component by itself, putting together a project will be much easier. If someone just fixes your code for you you will never develop the skils to maintain, improve or change aspects of your project.

i tried a lot with this project initially the code had sh1106 oled and my oled was ssd1306 i changed it into this and it is now working fine i couldn't find ds18b20 .so if anybody can help me please help so that i would know how to solve it

and i am fairly new this

What do you mean by this ?

i couldnt find the sensor so that i brought dht11 i had an old ds18b20 but now its broken.Can you please help me to figure it out

Simply Google for "dht11 with Arduino"...

I tried a lot can u please help me to edit the file or can you plz say where all i want to edit

If I were you, I'd forget about the initial sketch (the one in post #1) and i'd Google for 2 separate things :

  • acs712 with Arduino
  • dht11 with Arduino

Have them working separately, get to know them and then, once mastered, join the 2 together and make sure they don't interfere.

You won't start from scratch but you won't have to swallow a too big of a bite either.

Wokwi (online simulator) did half of the job for you : esp32-dht22.ino - Wokwi Arduino and ESP32 Simulator

1 Like

Add a simple Potentiometer as there is no ACS712 in their Library and you're all set...

1 Like

So what all changes i have do with the code

#include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <DallasTemperature.h> 
    #include <OneWire.h>   
    #include "Wire.h"    
    #include <WiFi.h>          
    #define BLYNK_PRINT Serial
    #include <BlynkSimpleEsp32.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 4
    #define VOLTAGE_SCALE  7.911 // R1+R2 / R2 // ( 47K + 6.8K ) / 6.8K
    #define CURRENT_SCALE  1.5 // R4+R5 / R5 // ( 1K + 2K ) / 2K
    
    double mVperAmp = 200; //Sensityvit of the sensor //  use 100 for 20A Module and 66 for 30A Module
    double ACSoffset = 514; // Ideally it should be ( 0.1 x Vcc ) // measured value is 514mV
    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
    //float tempF = 0; temperature in F
    float saving=0; // cost saving  
   
    
     WiFiClient client;
    // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
    // GPIO where the DS18B20 is connected to
    const int oneWireBus = 2;    
    // Setup a oneWire instance to communicate with any OneWire devices
    OneWire oneWire(TEMP_SENSE_PIN);
    DallasTemperature sensors(&oneWire);
    
//========================= Variables for wifi server setup =============================
     
  // Your WiFi credentials.
  // Set password to "" for open networks.
  char ssid[] = "XXXX"; // WiFi Router ssid
  char pass[] = "XXXX"; // WiFi Router password
  
  // copy it from the mail received from Blynk
  char auth[] = "XXXX";  
  
//========================= Setup Function ================================================ 
    
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
  
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }  
  display.clearDisplay();  
  display.setTextColor(WHITE);
  display.display();  
  delay(500);
}

//========================= Loop Function ================================================ 
    

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

  saving = 6.5 * ( energy /1000 ); // 6.5 is cost per kWh // used just for example
  

  // ================= 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, 10);
  display.print(voltage,1);
  display.print(" V");

  // Display Solar Panel Current 
  
  display.setCursor(70, 10);

  if (current >0 && current < 1 )
  {
   display.print(current*1000,0);
   display.print(" mA");
  }
  else
  {
  display.print(current,2);  
  display.print(" A");
  }

 // Display Solar Panel Power in Watt

  display.setTextSize(2);
  display.setCursor(10,25);
  display.print(power);
  display.print(" W");

 // Display Energy Generated by the Solar Panel 
  display.setCursor(10,45);
  
  if ( energy >= 1000 )
  {
   display.print(energy/1000,3);
   display.print(" kWh");
  }
  else
  {
  display.print(energy,1);  
  display.print(" Wh");
  }
  display.display();
  display.clearDisplay();
  
  
// ================= Display Data on Blynk App ================================================
   Blynk.run();  
   Blynk.virtualWrite(0, voltage ); // virtual pin 0
   Blynk.virtualWrite(1, current ); // virtual pin 1 
   Blynk.virtualWrite(2, power);    // virtual pin 2
   Blynk.virtualWrite(3,energy/1000);// virtual pin 3
   Blynk.virtualWrite(4,tempC );    // virtual pin 4   
   Blynk.virtualWrite(5,saving);    // virtual pin 4  
   //delay(1000);
}

//========================= 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.184 ; // 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*3331) / 4095); // Gets you mV
  Amps = ((ADCVoltage * CURRENT_SCALE - ACSoffset ) / mVperAmp); // 1.5 is the scaling for voltage divider
  return Amps;
}<br>

What i have to do here

// Setup a oneWire instance to communicate with any OneWire devices
    OneWire oneWire(TEMP_SENSE_PIN);
    DallasTemperature sensors(&oneWire)

Also here ?

// read temperature from DS18B20
  sensors.requestTemperatures(); // get temperatures
  tempC = sensors.getTempCByIndex(0);
  //tempF = sensors.getTempFByIndex(0

Bro for me i am doing a small solar setup in my home for that i am using a pwm charger and it doesn't have an display so thats why i am doing this as alternative

Here is the code I ended up with from the Wokwi above (post #13)

/**
   ESP32 + DHT22 Example for Wokwi
   
   https://wokwi.com/arduino/projects/322410731508073042
*/

#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;

void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}

void loop() {
  static unsigned long startTime = 0;
  static uint8_t n = 0;
  if (millis() - startTime >= 1000) {
    startTime += 1000;
    Serial.print("------- TimeStamp: ");
    Serial.print(n++);
    Serial.println(" -------");

    TempAndHumidity  data = dhtSensor.getTempAndHumidity();
    Serial.println("Temp: " + String(data.temperature, 2) + "°C");
    Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  }
}

It shouldn't be hard to pick your code from it...

It seems that you do not use all of this
what components do you really have in your project?

Esp32,acs712 20a, Voltage Detection Sensor Module 25V,dht11,ssd1306