OPEN-SOURCE DC ENERGY METER FEATURING BLYNK CLOUD MONITORING
by adriancubas in Circuits > Arduino
1145 Views, 19 Favorites, 0 Comments
OPEN-SOURCE DC ENERGY METER FEATURING BLYNK CLOUD MONITORING
If you’ve ever worked on a tech project involving voltage and current measurements, you’ve likely felt the need to record those values for future analysis and decision-making. Personally, I’ve faced this challenge many times, especially when trying to determine the performance of Windturer Wind Turbine. Dealing with interconnecting cables to multimeters, ensuring the wind turbine doesn’t interfere with the cables, and making sure the camera correctly captures the values can be tedious and error-prone due to the number of variables to manage simultaneously.
In this project, I present the solution I developed to overcome this problem. Unable to find any commercial solution, I decided to create my own. I designed a device that measures current and voltage at short intervals and calculates other variables such as consumed energy and power. These short-interval measurements are then integrated to continuously record the values for the duration of the measurement. All these values are sent via a WiFi connection to Blynk’s cloud servers, from where they can be monitored from any internet-connected device anywhere in the world.
To ensure the device operates effectively in regions that are difficult to access but not too far from civilization, an internet connection can be established using a combination of technologies. One viable option is to use a cellular network, leveraging 4G or 5G connectivity to provide reliable internet access. In areas where cellular coverage is limited, a satellite internet connection can be employed as a backup. Additionally, the use of long-range WiFi extenders or mesh networks can help bridge the gap between the device and the nearest internet access point. These solutions ensure that the device remains connected to the internet, allowing for continuous monitoring and control, even in challenging environments.
The Blynk platform also allows for automation, such as triggering alarms in case of overvoltage, controlling other safety devices like electromagnetic brakes for wid turbines, suspending battery charging, among other possibilities.
One of the key advantages of this project is its versatility. It can be used to monitor remote devices such as a solar charging system or a remote camera. Additionally, it can monitor multiple devices simultaneously and program automations to manage them efficiently. This capability is crucial for ensuring the reliability and safety of various systems, especially those located in hard-to-reach areas.
The importance of this project cannot be overstated. By providing a reliable and efficient way to monitor and control electrical systems, it helps prevent potential failures and optimizes performance. This is particularly valuable in applications like renewable energy systems, where maintaining optimal performance is essential for sustainability. Moreover, the ability to automate responses to specific conditions enhances safety and reduces the need for constant human supervision.
As always, I hope you enjoy this project and, if you find it useful, let me know in the comments. Feel free to reach out if you have any modification proposals or need any answers.
I have included two models for this project. One of the models includes a space to place a single-cell 18650 Power Bank. This way, the module can be powered directly through a small USB cable. The other model does not include this compartment for the Power Bank, and you will need to power the module using another Power Bank or a 5V USB adapter. When it is required for this project to operate for very long periods, the compact solution of a single-cell 18650 Power Bank may not be sufficient.
Supplies
The components selected for this invention were chosen based on their unique features and advantages that make them ideal for creating a precise, efficient, and remotely controllable monitoring system. Each component plays a crucial role in ensuring the device’s functionality and reliability.
- ESP8266 WiFi Module:
- Advantages:
- WiFi Connectivity: Enables connection to WiFi networks, facilitating remote communication and control of the device from anywhere.
- Low Cost: An economical module, perfect for IoT projects.
- Compatibility: Compatible with various platforms and programming languages.
- Adafruit INA219:
- Advantages:
- Precise Measurement: Provides accurate measurements of voltage, current, and power.
- I2C Interface: Simplifies communication with microcontrollers via the I2C protocol.
- Low Power Consumption: Ideal for applications where power consumption is critical.
- Adafruit SSD1306 OLED Display:
- Advantages:
- High Resolution: Offers clear and sharp resolution for displaying information.
- Low Power Consumption: Consumes less power compared to other types of displays.
- Compact Size: Its small size makes it ideal for portable and compact projects.
This project is significant for several reasons:
- Remote Monitoring: Thanks to the WiFi connectivity provided by the ESP8266 and integration with Blynk, you can monitor and control the device remotely. This is especially useful in IoT applications where constant supervision is required.
- Accurate Energy Measurement: With the INA219 sensor, you can obtain precise measurements of voltage, current, and power, which is crucial for applications requiring detailed energy consumption monitoring.
- Real-Time Visualization: The use of the OLED display allows real-time visualization of the measured data, facilitating immediate interpretation and analysis of the information.
- Diverse Applications: This type of device can be used in a wide range of applications, from educational projects to industrial monitoring systems.
“As an Amazon affiliate, I earn money from qualifying purchases made through the affiliate links provided below.”
- ESP8266 WeMos Mini D1 Module
- INA219 I2C Bi-Directional Power Sensor
- 2.1mm Female Socket
- I2C Display Module 0.91 Inch I2C OLED Display Module
- Pin Header Strip (2.54 mm) for Arduino Connector
- PCB for interconnection
- USB C DIY Connector 24Pin Female Socket Port or Micro USB Interface Power Adapter
- Single-cell 18650 Power Bank (No Battery)
- 18650 Battery Pack
The PCB for component interconnection has been uploaded as a shared project on the PCBWay platform. From there, you can download the G-code files if you want to make your own PCB, or you can directly order its fabrication from the platform. This way, you ensure that you will get the same PCB that I have tested, free of errors and with exceptional quality.
There was an error when writing Blynk on the PCB silkscreen; instead, it was written as Blink.
Optimizing Your 3D Printer to Print the Container Box With PETG Filament
If you are looking for a suitable material to print the case of this Energy Meter, I recommend considering the use of PETG (Polyethylene Terephthalate Glycol). This material offers several advantages that make it ideal for this type of application:
- Strength and Durability: PETG is known for its high mechanical strength and durability. This ensures that the energy meter case can withstand impacts and stresses without breaking or deforming easily.
- Temperature Resistance: PETG has good temperature resistance, which is crucial for electronic devices that may generate heat during operation.
- Ease of Printing: PETG is relatively easy to print compared to other materials like ABS. It has good adhesion to the print bed and fewer warping issues, resulting in more precise and high-quality prints.
- Chemical Resistance: PETG is resistant to many chemicals, making it suitable for environments where it may be exposed to corrosive substances.
I recommend the following parameters that you should configure in your favorite slicer.
- Nozzle Temperature: Set your nozzle temperature between 220 to 240 degrees Celsius. For this tutorial, we’ll use 235 degrees Celsius.
- Bed Temperature: Adjust your bed temperature to 50 to 85 degrees Celsius. We’ll set it to 85 degrees Celsius for optimal adhesion.
- Layer Height: Use a layer height of 0.16 mm for finer details. This setting ensures a smooth finish and accurate prints. If you want a faster print, you can set it to 0.28mm.
- Perimeters: Set your slicer to print with 4 perimeters. This provides additional strength and durability to your prints.
- Print Speed: Adjust your print speed to 40 to 60 millimeters per second. This range balances quality and efficiency.
- Fan Speed: Set your fan speed between 0% to 100%. Lower speeds enhance strength, while higher speeds improve visual quality, especially for bridges and overhangs.
- Retraction Distance: For a stock Bowden extruder, set the retraction distance to 5 to 7 millimeters. This helps prevent stringing and ensures clean prints.
- Brim: Use a brim to improve adhesion and prevent warping. A brim increases the surface area of the first layer, helping it stick better to the print bed. It also provides additional support to the base of the print and makes it easier to remove the print from the bed without damaging the model.
Feel free to adjust the settings based on your specific needs and printer performance.
During the execution of this project, I had issues with the 3D printer, specifically with the extruder mechanism (See the PIC), and the parts I obtained didn’t turn out to be as attractive as I would have liked LOL.
Interconnection of Hardware Elements
The Energy Meter is composed of several key components that work together to measure and display energy consumption. Below is a description of the interconnection of these components and the reasoning behind their configuration:
- ESP8266 Microcontroller: This is the brain of the system. It handles WiFi communication and connects to the Blynk platform for remote monitoring.
- INA219 Current Sensor: This sensor measures the current and voltage in the circuit. It is connected to the ESP8266 via the I2C bus (address 0x40).
- SSD1306 OLED Display: This display shows the measured data in real-time. It is also connected to the ESP8266 via the I2C bus (address 0x3C).
- Built-in LED: Indicates the connection status with Blynk. It blinks at different speeds depending on whether it is connected or not.
Connection Details
ESP8266 Connections
- VCC: Connected to the 3.3V or 5V power supply.
- GND: Connected to ground.
- SDA (GPIO4): Connected to the SDA pin of the INA219 sensor and the OLED display.
- SCL (GPIO5): Connected to the SCL pin of the INA219 sensor and the OLED display.
- D4 (GPIO2): Connected to the built-in LED.
INA219 Sensor Connections
- VCC: Connected to the 3.3V power supply.
- GND: Connected to ground.
- SDA: Connected to the SDA pin of the ESP8266.
- SCL: Connected to the SCL pin of the ESP8266.
SSD1306 OLED Display Connections
- VCC: Connected to the 3.3V power supply.
- GND: Connected to ground.
- SDA: Connected to the SDA pin of the ESP8266.
- SCL: Connected to the SCL pin of the ESP8266.
Connection Variants
Depending on the available components and specific project needs, the following variants can be considered:
- Using Different GPIO Pins: If GPIO4 and GPIO5 are occupied, other available GPIO pins on the ESP8266 can be used for the SDA and SCL connections.
- Power Supply: Although a 3.3V power supply is used in this case, some components may work with 5V. It is important to check the specifications of each component.
- LED Connection: The built-in LED is connected to GPIO2, but any other available GPIO pin can be used if necessary.
Advantages of Using Fritzing for Initial Design
The initial design of the connections was done using Fritzing software. This offers several advantages:
- Clear Visualization: Fritzing allows for the creation of clear and easy-to-understand visual schematics, facilitating circuit planning and assembly.
- Error Detection: Designing the circuit in Fritzing makes it easier to detect and correct errors before physical assembly.
- Documentation: Schematics created in Fritzing serve as visual documentation of the project or sharing with others.
Download the Fritzing File and Software
I have shared the Fritzing file for you to download and review. You can download the Fritzing software from its official website https://fritzing.org/
I have included the datasheet of the INA219 in this project. It provides detailed technical specifications and performance characteristics, which are essential for future design changes and ensuring reliable operation.
Electrical Connections Between Components
Soldering Header Pins:
First, solder the Header Pins to the interconnection board.
Soldering the ESP8266 Module:
Next, solder the ESP8266 module to these pins, respecting the position shown on the interconnection board and in the images. Maintain a separation between the ESP8266 board and the interconnection PCB to improve cooling and allow the connection of the micro USB programming cable to the integrated micro USB port on the ESP8266 module.
Soldering the INA219:
After soldering the ESP8266, solder the INA219 following the same recommendations as for the ESP8266.
Connecting Wires to the ESP8266:
To the 5V and GND pins of the ESP8266, solder two thin wires to the USB chassis connector. This way, when the USB cable is externally inserted, it provides power to the entire assembly.
Connecting the LCD:
The connection to the small LCD is indicated in the initial interconnection diagrams in Freezing.
Connecting the INA219:
In the Terminal Block of the INA219, screw in the wires that will be soldered to the side legs of the DC connectors for the chassis, as shown in the photos. The central leg of each DC connector is connected to each other and, in turn, to the GND connection pin indicated on the interconnection board.
Configuration of the Arduino IDE to Load the Firmware on the ESP8266
To get started with programming your ESP8266 WeMos D1 Mini, you’ll need to install the Arduino IDE on your PC. This guide will walk you through the process of downloading and installing the Arduino IDE, configuring it to support the ESP8266, and writing and uploading the proposed code to the WeMos D1 Mini. In this case, the code provided is for an energy meter, which you will need to upload after replacing the placeholders (X) with the appropriate values derived from the next step of configuring the Blynk platform. By following these steps, you’ll be able to connect your device to a WiFi network and monitor the variables on the Blynk Console.
Installing the Arduino IDE
- Download the Arduino IDE:
- Go to the official Arduino website and download the version suitable for your operating system (Windows, macOS, Linux).
- Install the Arduino IDE:
- Run the downloaded file and follow the installer instructions.
Configuring the Arduino IDE for ESP8266
- Open the Arduino IDE:
- Once installed, open the Arduino IDE.
- Add support for ESP8266:
- Go to File > Preferences.
- In the “Additional Boards Manager URLs” field, add the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json.
- Click OK.
- Install the ESP8266 boards:
- Go to Tools > Board > Boards Manager.
- Search for esp8266 and click Install.
Programming the ESP8266 WeMos D1 Mini
- Select the board:
- Go to Tools > Board and select LOLIN(WEMOS) D1 R2 & mini.
- Select the port:
- Connect your WeMos D1 Mini to the PC using a USB cable.
- Go to Tools > Port and select the port corresponding to your device.
- Copy and Paste this code:
You should not upload the code to the ESP8266 yet without first advancing and completing the step of configuring Blynk. It would not be able to connect correctly to Blynk servers. You will need to replace the Xs with the values provided by Blynk after creating an account and setting up a new template and a new device. You also need to enter the SSID of your WiFi network and its password into the code.
Explaining the Code
Overview
This code is for an ESP8266 microcontroller that connects to a WiFi network and uses the Blynk platform to monitor and display sensor data. It reads data from an INA219 current sensor and displays it on an OLED screen. It also sends the data to the Blynk app.
Code Breakdown
Definitions and Libraries:
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxx"
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
- Defines Blynk template ID and name.
- Includes necessary libraries for WiFi, Blynk, INA219 sensor, and OLED display.
WiFi and Blynk Credentials:
char auth[] = "xxxxxxxxxx";
char ssid[] = "your wifi name";
char pass[] = "your wifi password";
- Stores the authentication token for Blynk and WiFi credentials.
Sensor and Display Initialization:
#define I2C_ADDRESS 0x40
Adafruit_INA219 ina219(I2C_ADDRESS);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- Initializes the INA219 sensor and the OLED display with specified parameters.
Variables for Sensor Data:
float shuntVoltage_mV = 0.0;
float Load_Voltage = 0.0;
float Source_Voltage = 0.0;
float Current = 0.0;
float Power = 0.0;
float Wh = 0;
unsigned long lastmillis = millis();
unsigned long previousMillis = 0;
- Declares variables to store sensor data and time.
Setup Function:
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10,10);
display.print("Powered and");
display.setCursor(10,20);
display.print("trying to connect...");
display.display();
Blynk.begin(auth, ssid, pass);
delay(1000);
while (!Serial);
Wire.begin();
ina219.begin();
display.clearDisplay();
Serial.println("INA219 Current Sensor Example Sketch - Continuous");
#define LED_BUILTIN 2
pinMode(LED_BUILTIN, OUTPUT);
}
- Initializes serial communication, OLED display, Blynk, and INA219 sensor.
- Displays a message on the OLED screen.
- Sets up the built-in LED pin.
Loop Function:
void loop() {
Blynk.run();
unsigned long currentMillis = millis();
if (Blynk.connected()) {
shuntVoltage_mV = ina219.getShuntVoltage_mV();
Source_Voltage = ina219.getBusVoltage_V();
Current = ina219.getCurrent_mA() / 1000;
Power = ina219.getPower_mW() / 1000;
Load_Voltage = Source_Voltage + (shuntVoltage_mV / 1000);
if (Source_Voltage < 1.0 && Source_Voltage > 0.0) {
Source_Voltage = 0.0;
}
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
Wh = Wh + Power / 3600.0;
}
Blynk.virtualWrite(V0, Source_Voltage);
Blynk.virtualWrite(V1, Load_Voltage);
Blynk.virtualWrite(V2, Current);
Blynk.virtualWrite(V3, Power);
Blynk.virtualWrite(V4, Wh);
display.clearDisplay();
display.setCursor(10,10);
display.print("V: ");
display.print(Source_Voltage, 2);
display.setCursor(74,10);
display.print("A: ");
display.print(Current, 2);
display.setCursor(10,20);
display.print("W: ");
display.print(Power, 2);
display.setCursor(74,20);
display.print("Wh: ");
display.print(Wh, 2);
display.display();
} else {
display.clearDisplay();
display.setCursor(10,10);
display.print("Failed to");
display.setCursor(10,20);
display.print("connect to Blynk");
display.display();
}
blinkLED();
}
- Runs the Blynk process.
- Reads sensor data and updates variables.
- Sends data to Blynk and displays it on the OLED screen.
- Displays a message if not connected to Blynk.
- Calls the blinkLED function.
Blink LED Function:
void blinkLED() {
if (Blynk.connected()) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
} else {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
- Blinks the built-in LED at different rates depending on the Blynk connection status.
Downloads
How to Configure Blynk.Cloud
1. Accessing Blynk
- Type Blynk.cloud in the address bar, and the authentication screen will appear.
- If you don’t have an account, select the option to create a new account.
- Enter your email and confirm that you agree with the terms and conditions.
- You will receive a link in your email to create a password for accessing the platform. We suggest using a secure password.
- Enter a username, and you will automatically access the Blynk console.
2. Getting Familiar with Blynk
- I suggest reading the Blynk Tour to familiarize yourself with the possibilities it offers.
- Pause on each screen if you wish to read it.
3. Blynk Plans
- The Blynk platform offers several plans, but the free option will provide us with the necessary tools for our basic objectives in this example.
4. Organization and Templates
- Each user must be integrated into an organization. By default, the platform assigns a predetermined name that we will not change in this case.
- The first thing you should do after completing the organization information is to create a new template.
- Templates are digital variants of our physical devices like Arduino, ESP, etc.
- In our case, we associate this template with an ESP8266 that we will use in our DC Power Meter.
- Communication will be established via WiFi, so select that option. You can add a description if you wish.
5. Template Configuration
- Once inside the template you just created, you will need to complete four steps.
- Before that, I suggest copying and pasting the Power Meter code into the Arduino, specifically the template name and its ID.
- By clicking on the corresponding icon, it will be copied to the clipboard.
- The copied data should replace the ones indicated in lines 1 and 2 of the code you downloaded.
- Then, add a new device associated with this template, we call it ENERGY METER DEVICE ESP8266.
- Immediately after, an authentication token will be generated, which you should also paste into the code, replacing the Xs in line 12.
- This way, the code will be ready to upload to the ESP8266.
6. Datastreams Configuration
- To monitor the variables in Blynk sent by the ESP8266, it will be necessary to configure the Datastreams.
- Create a new Datastream by selecting the Virtual PIN option.
- Fill in this information as shown to succeed in this step.
7. Dashboard Configuration
- The way the variables sent by the ESP8266 will be displayed can vary and will depend on the options you use for your dashboard.
- Move and arrange the widgets on the canvas. You can follow the way I do it here or make changes according to your preferences.
- When you complete this step, you will have everything you need to receive and monitor the variables sent by the ESP8266 in the Blynk console.
If you want to enable other functionalities of the Blynk Platform in addition to displaying the data obtained by the ESP8266, I suggest changing the data type from String to Double as shown in the images. This way, you can indicate the units of measurement for each magnitude.
How to Set Up the Blynk App for Your Energy Meter
- Download and install Blynk: Download the Blynk app on your mobile phone and install it.
- Log in: Open the app and select “login” to enter with your existing account.
- Configuration: Go to the configuration section in Blynk.
- Add widget: Add a new widget. With the free plan, some options won’t be available, but you can use the “value display” widget.
- Select data: Tap on the white rectangle of the created widget and select the Datastream you want to display (e.g., voltage).
- Customization: Configure the title, color, and size of the widget according to your preferences.
- Adjust size and position: Press and hold the widget to adjust its size and position on the dashboard.
- Duplicate widget: You can make several duplicates of the widget and customize them with different titles and colors based on the selected Datastream.
A Brief Demonstration and Final Words.
Thank you for following along with this project on creating an energy meter using the ESP8266, INA219, and an OLED display. In this guide, we have covered how to configure the Blynk platform to view the results on your mobile device and the web. We also explained how to print the enclosure and the PCB to support the ESP8266, INA219, and LCD, and how to interconnect them.
If you have any questions or need further assistance, feel free to reach out. I am more than happy to help with any doubts you may have. Additionally, I encourage you to make your own modifications and improvements to this project.
Happy Making!!!