Wireless Energy Monitoring System using ESP32 with Blynk Mobile App

Overview

The rising energy costs and global warming has forced us to keep an eye on our energy consumption. An energy management system can help monitor, control and reduce the energy consumption significantly. Above all, being able to monitor it from a phone makes it much more convenient. Statistics reveal that the energy monitoring and managing systems can reduce energy costs from 8% up to 20%, which is a big deal.


Hardware Used

  • ESP32 Doit Board – 1
  • 
    ZMPT101B AC Voltage Sensor Module – 1
  • 
    SCT-013-030 Non-invasive AC Current Sensor – 1

  • 100Ω Resistor – 1
  • 10kΩ Resistor – 2
  • 
    10uF Capacitor – 1
    
  • 
    
  • Breadboard – 1
  • 
    
  • Jumper Wires
  • 
    
  • Power Cord
  • You can buy this product at Createlabz.

    Software Used

    Libraries Used


    Application Description

    ESP32 DevKitC Board


    The ESP32 DevKitC Board is one of the development board created by Espressif to evaluate the ESP-WROOM-32 module. It is based on the ESP32-microcontroller that boasts integrated WiFi and Bluetooth, along with an array of built-in antenna switches, and ultra-low power consumption.

    Pin Mapping

    Device Summary

    • Microcontroller: Tensilica 32-bit Single-/Dual-core CPU Xtensa LX6
    • Operating Voltage: 3.3 V
    • Input Voltage: 7-12 V
    • Digital I/O Pins (DIO): 28
    • Analog Input Pins (ADC): 8
    • Analog Outputs Pins (DAC): 2
    • UARTs: 3
    • SPIs: 2
    • I2Cs: 3
    • Flash Memory: 4 MB
    • SRAM: 520 KB
    • Clock Speed: 240 Mhz
    • Wi-Fi: IEEE 802.11 b/g/n/e/i:
    • Integrated TR switch, balun, LNA, power amplifier and matching network
    • WEP or WPA/WPA2 authentication, or open networks

    How does it work?

    The ESP32 DevKitC Board works similar to an Arduino Uno but has more features and superior capabilities. ESP32 has both WiFi and Bluetooth built into it whereas an Arduino Uno doesn’t. It also has a larger memory and faster clock speed. You can still use the Arduino IDE to program it by installing the ESP32 board in the IDE’s Boards Manager.

    First, open the preferences window from the Arduino IDE and go to File Preferences. After that, enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field as shown in the figure below. Then, click the “OK” button.

    Once that is done, open boards manager by going to Tools > Board > Boards Manager and serch for ESP32, then press the install button for the ESP32 by Espressif Systems.

    ZMPT101B AC Voltage Sensor Module

    The ZMPT101B is a voltage transformer ideal for measuring AC voltage. It has high accuracy and good consistency in voltage and power measurement. The module is simple to use and comes with a multi-turn trim potentiometer for adjusting and calibrating the ADC output.

    Specifications

    • Input Voltage: 0-250 V AC
    • On-board micro-precision voltage transformer
    • Analog output corresponding quantity can be adjusted
    • Output Signal: Analog 0-5 V
    • Operating temperature: 40 °C~+70 °C

    SCT-013-030 Non-invasive AC Current Sensor

    The Non-invasive AC Current Sensor is a current transformer can be clamped around the supply line of an electrical load to measure the current. It does this by acting as an inductor and responding to the magnetic field around a current-carrying conductor. By reading the amount of current being produced by the coil, the current passing through the conductor can be calculated. They are particularly useful for measuring whole building electricity consumption or generation.

    Specifications

    • Input Current: 0-30 A AC
    • Output Signal: DC 0-1 V
    • Non-linearity: 2-3 %
    • Build-in sampling resistance (RL): 62 Ω
    • Turn Ratio: 1800:1
    • Resistance Grade: Grade B
    • Work Temperature: -25 °C~+70 °C
    • Dielectric Strength (between shell and output): 1000 V AC / 1 min 5 mA

    Hardware Setup

    Voltage Sensor Module Connections

    • OUT connected to ESP32 DevKitC Board pin G35
    • Vcc and Gnd connected to 5V and Ground respectively

    Non-invasive AC Current Sensor Connections

    The connections for this sensor is a bit more complicated than the voltage sensor as it requires external components. However, please read these warnings carefully before proceeding, for it could be dangerous if not followed.

    • A burden resistor is required. If an unburdened current transformer is attached to a current-carrying conductor, the current transformer will generate an unsafe voltage at its leads which could break down the insulation and destroy the device.
    • Always connect the current transformer leads before clipping it round a current-carrying conductor, and always unclip the current transformer from the current-carrying conductor before disconnecting its leads.
    • Never open-circuit the current transformer whilst it is on a current-carrying conductor. It is always safe to short-circuit it.
    • Never attempt to fit the current transformer to a bare conductor unless you are absolutely certain it is designed for that.

    Calculating for Suitable Burden Resistor Value

    Now, we must calculate for the value of the burden resistor to use. Since we’re using the SCT-013-030 which has a limit of 30A, our maximum peak current would be:

    Primary peak-current = RMS current × √2 = 30A × 1.414 = 42.4264A

    With the primary peak-current, divide it by the number of turns in the current transformer to give the peak-current in the secondary coil. The number of coils and its other properties can be found in its datasheet.

    Secondary peak-current = Primary peak-current / no. of turns = 42.4264A / 1800 = 0.023570226A

    To maximize measurement resolution, the voltage across the burden resistor at peak-current should be equal to one-half of the analog reference voltage. In this case, we’re using 5V so the voltage across the burden resistor is 2.5V.

    Ideal burden resistance = (AREF/2) / Secondary peak-current = 2.5V / 0.023570226A = 106.6667Ω

    Since 106.6667Ω is not a common resistor value, we pick the nearest smaller value which is 100Ω so the maximum load current will not create a voltage higher than 5V.

    Blynk App Setup

    Download the Blynk app for your smartphone on your phone’s Google Play Store or iTunes App Store.

    After downloading the app, click the app and it will direct you to the Log In page. You may either Log In with your Facebook Account, or Create New Account, or Log In if you’ve used the Blynk app before. Once that’s done, create a new project by clicking “New Project.”

    Name the project and make sure to pick ESP32 Dev Board for the device and WiFi for the connection type since those are what we’re going to use.

    Click the “+” icon on the top right to add components or widgets. For the best visual display in this project, we’ll use the Gauge widget.

    Set up four of them. Two for the AC voltage and current and another two for the power and kilowatt-hour. You can click on them to change their label, color, and set the virtual pin. More importantly, make sure the AC voltage gauge pin is set to V0, the AC current gauge pin is set to V1, power gauge pin is set to V2, and kilowatt-hour gauge pin is set to V3. This will make sense later in the code.


    Code

    #define BLYNK_PRINT Serial
    
    #include "EmonLib.h"
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>
    
    EnergyMonitor emon;
    #define vCalibration 106.8
    #define currCalibration 0.52
    BlynkTimer timer;
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "605c2d780fd84d62b49e87b3d7a31611";
    
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "PLDTMyDSLBiz02510";
    char pass[] = "PLDTWIFIB841D";
    
    float kWh = 0;
    unsigned long lastmillis = millis();
    
    void myTimerEvent() {
        emon.calcVI(20, 2000);
        Serial.print("Vrms: ");
        Serial.print(emon.Vrms, 2);
        Serial.print("V");
        Blynk.virtualWrite(V0, emon.Vrms);
        Serial.print("\tIrms: ");
        Serial.print(emon.Irms, 4);
        Serial.print("A");
        Blynk.virtualWrite(V1, emon.Irms);
        Serial.print("\tPower: ");
        Serial.print(emon.apparentPower, 4);
        Serial.print("W");
        Blynk.virtualWrite(V2, emon.apparentPower);
        Serial.print("\tkWh: ");
        kWh = kWh + emon.apparentPower*(millis()-lastmillis)/3600000000.0;
        Serial.print(kWh, 4);
        Serial.println("kWh");
        lastmillis = millis();
        Blynk.virtualWrite(V3, kWh);
    }
    
    void setup() {
      Serial.begin(9600);
      emon.voltage(35, vCalibration, 1.7); // Voltage: input pin, calibration, phase_shift
      emon.current(34, currCalibration); // Current: input pin, calibration.
      Blynk.begin(auth, ssid, pass);
      timer.setInterval(5000L, myTimerEvent);
    }
    
    void loop() {
      Blynk.run();
      timer.run();
    }

    Code Breakdown

    Pre-Initialization

    #define BLYNK_PRINT Serial
    
    #include "EmonLib.h"
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>

    First, we define the Blynk print serial for debugging purposes. Next, the libraries we need for the program have to be included. EmonLib handles the retrieval of data from both sensors as well as the calculation for the RMS and power values. BlynkSimpleEsp32 is needed to integrate our program to the Blynk Mobile app.

    EnergyMonitor emon;
    #define vCalibration 106.8
    #define currCalibration 41.2
    BlynkTimer timer;

    The EnergyMonitor object emon is then created and calibration values are defined. These values have to be obtained via trial and error depending on the setup so it can be different for everyone. The Blynk timer object is also created to handle the sending of data to the Blynk mobile app.

    char auth[] = "MYAUTH";
    
    char ssid[] = "MYSSID";
    char pass[] = "MYPASSWORD";

    This is where you input the auth token of your Blynk mobile app, your WiFi SSID, and its password. You can obtain the auth token from your app by going to the project settings (nut icon). You can choose to copy it from there or have them email it to you.

    float kWh = 0;
    unsigned long lastmillis = millis();

    These values have to be initialized to be used later on in the calculations. The kWh starts at 0 and will slowly build up as time goes on.

    Timer Event Function

    void myTimerEvent() {
        emon.calcVI(20, 2000);
        Serial.print("Vrms: ");
        Serial.print(emon.Vrms, 2);
        Serial.print("V");
        Blynk.virtualWrite(V0, emon.Vrms);
        Serial.print("\tIrms: ");
        Serial.print(emon.Irms, 4);
        Serial.print("A");
        Blynk.virtualWrite(V1, emon.Irms);
        Serial.print("\tPower: ");
        Serial.print(emon.apparentPower, 4);
        Serial.print("W");
        Blynk.virtualWrite(V2, emon.apparentPower);
        Serial.print("\tkWh: ");
        kWh = kWh + emon.apparentPower*(millis()-lastmillis)/3600000000.0;
        Serial.print(kWh, 4);
        Serial.println("kWh");
        lastmillis = millis();
        Blynk.virtualWrite(V3, kWh);
    }

    This section of the code is where the retrieval of values from the sensors are happening and where they are being calculated. In emon.calcVI(20, 2000), the real power, apparent power, power factor, Vrms, and Irms are being calculated. In our case, we only need the Vrms, Irms, and apparent power so we get those using emon.Vrms, emon.Irms, and emon.apparentPower respectively then print them in the Serial monitor for debugging purposes. We then use Blynk.virtualWrite to send the data to Blynk based on the virutal pins set.

    For the kilowatt-hour, we slowly add it up as times goes. The formula is power*timePassed/3600*1000*1000. 3600 is there to convert milliseconds to seconds, 1000 is to convert seconds to hours, and another 1000 is to convert watts to kilowatts.

    void setup()

    Serial.begin(9600);
      emon.voltage(35, vCalibration, 1.7); // Voltage: input pin, calibration, phase_shift
      emon.current(34, currCalibration); // Current: input pin, calibration.
      Blynk.begin(auth, ssid, pass);
      timer.setInterval(5000L, myTimerEvent);

    Inside the setup() function, we set the serial baud rate to 9600 bps which is the default for serial communication. Then we set the voltage and current sensor pins along with their calibration values. Blynk has to be initialized here as well as the timer. Finally, we set it to 5000L for an update time of 5 seconds. The reason for this is because we cannot send data too fast onto the Blynk server otherwise they will force-cut our connection.

    void loop()

    Blynk.run();
    timer.run();

    Inside the loop() function, we only need to run Blynk and the timer – nothing else.

    Accuracy of Values

    These values are obtained from a 3W light bulb.

    Conclusion

    AC is dangerous to work with. If not careful, it could injure or even kill people. One has to be extra careful when dealing with AC applications. Experienced or not, it is advisable to have someone observing your safety at all times. This individual should certainly know the circuits and location of the switches controlling the equipment, and should be given instructions to pull the switch immediately if anything unforeseen happens.

    Blynk is an easy-to-use IoT platform that helps with any form of wireless communication. The app is customizable and it supports a lot of devices such as, but not limited to, Arduinos, Wemos D1, Raspberry Pi, ESP8266, and ESP32.

    References

    The post Wireless Energy Monitoring System using ESP32 with Blynk Mobile App appeared first on CreateLabz.

    Ac voltage sensorBlynkCurrentDevkitc boardEnergyEnergy monitoringEsp32KnowledgebaseNon-invasive ac current sensorPowerVoltageWifiWirelessZmpt101b

    Leave a comment

    All comments are moderated before being published