IoT Energy Meter with Cayenne Dashboard using PZEM-004T v3 and Wemos D1 Mini

Overview:

Due to rising energy costs, people are finding ways to monitor their energy consumption to make energy saving measures for their home. The goal for this project is to make a DIY power meter using the PZEM-004T v3 to monitor your power consumption, and since IoT is the new norm for remote monitoring, we can also interface the power meter with an IoT dashboard through Wi-Fi connection using the Wemos D1 mini microcontroller to visualize the meter readings online where you can access it using your computer or smartphone.

 

Hardware and Software Components:

Hardware:

  • PZEM-004T-100A v3 Energy Meter

  • Wemos D1 Mini w/ Cable
  • 
    
  • Male – Female Jumpire Wires
  • 
    
  • Breadboard
  • 
    
    

    Software:

    Online Platform:

     

    Application Discussion:

    Online Platform:

    • PZEM-004T-100A v3 Energy Meter

     

      • Measuring range of 100A using an external transformer
      • Mainly used for measuring AC voltage, current, power, energy, frequency, and power factor
      • No display function
      • Physical layer uses UART to RS485 communication interface
      • Application layer uses Modbus-RTU protocol to communicate
      • Data is read through TTL interface which can be directly connected with Arduino or ESP based microcontrollers without any additional converter needed
    How it measures?

    The meter uses a current transformer to measure current. It is designed to produce an alternating current in its secondary winding which is proportional to the current being measured in its primary. It reduces high voltage currents to a much lower value and provides a convenient way of safely monitoring the actual electrical current flowing in an AC transmission line.

     

    • Wemos D1 Mini ESP8266 ESP-12 Wifi Development Board

      • Operating voltage of 3.3V
      • Miniature wireless 802.11 (Wi-Fi) development board
      • Turns the ESP8266 wireless microcontroller into a fully fledged development board
      • Built-in microUSB interface allowing to be programmed directly  from the Arduino IDE
      • Programming D1 mini is same as programming Arduino based microcontroller
      • Requires the ESP8266 support to be added via the Board Manager of the IDE without any additional hardware needed

    Online Platform:

        •  Cayenne IoT
            • Cayenne is the world’s first drag and drop IoT project builder that empowers developers, designers and engineers to quickly prototype and share their connected device projects
            • Cayenne was designed to help users create Internet of Things prototypes and then bring them to production
            • Cayenne is a product from myDevices that allows you to not only display data but also set up triggers, monitor devices, control devices, etc.
            • The Cayenne MQTT API is used to connect any device that you have with the Cayenne Cloud
          What is MQTT?
          MQTT stands for Message Queuing Telemetry Transport. It is an extremely simple and lightweight messaging protocol (subscribe and publish) designed for limited devices and networks with high latency, low bandwidth or unreliable networks. With MQTT, resource-constrained IoT devices can send or publish information on a specific topic to a server that acts as an MQTT message broker. The broker then transmits the information to those customers who have previously subscribed to the customer’s topic. For this project, the Cayenne Dashboard acts as the MQTT message broker.

           

          Hardware Set-up:

           Schematic Diagram:

           

           Connections:


           
              
          • PZEM-004T-100A v3
            • Load Connection
              • Current Transformer (CT) VCC wire connected to PZEM-004T v3 Live (+) Terminal 
              • Current Transformer (CT) GND wire connected to PZEM-004T v3 Neutral (-) Terminal 
              • Load Live (+) wire connected to PZEM-004T v3 Load Live (+) Terminal 
              • Load Neutral (-) wire connected to PZEM-004T v3 Load Neutral (-) Terminal 
            • Wemos D1 Mini Connection
              • VCC lead connected to Wemos 5V pin
              • GND lead connected to Wemos GND pin
              • TX (Transmit) lead is connected to Wemos D7 pin
              • RX (Receive) lead is connected to Wemos D8 pin

            

          • Wemos D1 Mini
            • VCC pin connected to PZEM-004T v3 VCC lead
            • GND pin connected to PZEM-004T v3 GND lead
            • D7 (RX) pin connected to PZEM-004T v3 TX lead
            • D8 (TX) pin connected to PZEM-004T v3 RX lead

          Software Set-up:

          Before setting up the Arduino code, we must install the ESP8266 board in the Arduino IDE.

          ESP8266 Board Support Installation:

          • Click the Preferences section in the IDE
          • Paste the following URL to the Additional Boards Manager URLs to add the ESP8266 Board Manager: https://arduino.esp8266.com/stable/package_esp8266com_index.json 
          • Download the esp8266 package in the Board Manager
              

            After adding the ESP8266 board, it must be interfaced with the Cayenne Dashboard.

            Cayenne Dashboard ESP8266 Setup + Connection: 

            • After account registration, add a new device to be connected to the dashboard 
            • Choose Generic ESP8266 at the Microcontrollers section
              • Before connecting, we must download the Cayenne MQTT ESP library in Github
               
                • Click the ESP8266 sketch in the Cayenne MQTT ESP example
                  •  Before running the sketch, copy the Cayenne MQTT credentials provided
                    
                    • Edit the Cayenne credentials along with the Wi-Fi credentials in the sketch
                      • Run the sketch, and wait until the Wemos D1 Mini (ESP8266) is connected to Cayenne
                        • The Wemos D1 Mini (ESP8266) is finally connected to the dashboard 

                           

                          After setting-up the Cayenne Dashboard, we must test the PZEM-004T V3 meter before interfacing with the dashboard. 

                           

                          Code (PZEM-004T V3 Testing): 

                          //Libraries
                          #include <Arduino.h>
                          #include <PZEM004Tv30.h>
                          
                          //PZEM object constructor
                          PZEM004Tv30 pzem(D7,D8);
                          
                          void setup() {
                            Serial.begin(115200);
                          
                            while(!Serial); 
                            delay(100);
                            
                            Serial.println("PZEM-004T-100A Wemos D1 Mini (ESP8266) Test"); 
                          }
                          
                          void loop() {
                            Serial.println("Measuring...");
                            //Result Variables
                            float volts = pzem.voltage();
                            float amps = pzem.current();
                            float watts = pzem.power();
                            float kiloWatts = pzem.energy();
                            float hertz = pzem.frequency();
                            float factor = pzem.pf();
                          
                            delay(2000);
                          
                            Serial.println("");
                          
                            Serial.println("Results:");
                            Serial.println("");
                          
                            Serial.print("Voltage: "); Serial.print(volts,3); Serial.println("V");
                            Serial.print("Current: "); Serial.print(amps,3); Serial.println("A");
                            Serial.print("Power: "); Serial.print(watts,3); Serial.println("W");
                            Serial.print("Energy: "); Serial.print(kiloWatts,3); Serial.println("kWh");
                            Serial.print("Frequency: "); Serial.print(hertz,2); Serial.println("Hz");
                            Serial.print("Power Factor: "); Serial.println(factor,3);
                          
                            Serial.println("");
                            
                            delay(2000);
                          }

                          Code Breakdown:

                          Libraries:

                          • Arduino.h
                            • The ESP8266 support for Arduino 
                            • Able to write sketches, use Arduino functions and libraries
                            • Run sketches with no external microcontroller required
                            • Built-in library in the IDE
                            • More information about the library, along with the ESP8266 Arduino setup in Github
                          • PZEM004Tv30.h
                            • Peacefair (PZEM-004T v3) Energy Meter library
                            • Upgraded version from the older PZEM-004T library to support newer versions
                            • Provides full features of the PZEM-004T v3 energy monitor
                            • More information about the library, and download link in Github
                            • Can also be downloaded in the Arduino Library Manager

                          Variables:

                          • PZEM004Tv30 pzem(D7,D8)
                            • The object constructor for the energy meter along with the pin connections
                          • Voltage
                            • Voltage sensor value in volts (V)
                          • Current
                            • Current sensor value in amperes (A)
                          • Power
                            • Power sensor value in watts (W)
                          • Energy
                            • Energy sensor value in kilo watt hour (kWh)
                          • Frequency
                            • Frequency sensor value in hertz (Hz)
                          • PowerFactor
                            • Calculated result based on sensor values

                            Functions:

                            • pzem.voltage()
                              • fetch voltage value
                            • pzem.current()
                              • fetch current value
                            • pzem.power()
                              • fetch power value
                            • pzem.energy()
                              • fetch energy value
                            • pzem.frequency()
                              • fetch frequency value
                            • pzem.pf()
                              • fetch power factor value

                            Output:

                             

                             

                            After testing the sensor, we can now interface it with the dashboard to upload energy readings online. 

                             

                            Code (PZEM-004T V3 with Cayenne Dashboard): 

                            //Libraries
                            #include <Arduino.h>
                            #include <CayenneMQTTESP8266.h>
                            #include <PZEM004Tv30.h>
                            
                            //Debug Cayenne Connection
                            #define CAYENNE_DEBUG
                            
                            //Enable Serial Print
                            #define CAYYENE_PRINT Serial
                            
                            //Cayenne Channels to display meter values
                            #define VOLTAGE_CHANNEL 1 //voltage
                            #define CURRENT_CHANNEL 2 //current
                            #define POWER_CHANNEL 3 //power
                            #define ENERGY_CHANNEL 4 //energy
                            #define FREQUENCY_CHANNEL 5 //frequency
                            #define POWERFACTOR_CHANNEL 6 //power factor
                            
                            //RX pin = D7 connected to the TX pin of PZEM-004T v3
                            //TX pin = D8 connected to the RX pin of PZEM-004T v3
                            
                            //Peacefair device object constructor
                            PZEM004Tv30 pzem(D7,D8); 
                            
                            //WiFi network info.
                            char ssid[] = "HOTSPOTniKOYA";
                            char wifiPassword[] = "09771665851";
                            
                            //ESP8266 Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
                            char username[] = "439049b0-0660-11ed-8df2-dd50487e509b";
                            char password[] = "1f5cf9c47e9fc2b28eaa1ffb054b62003a71127a";
                            char clientID[] = "349a2920-1bf1-11ed-baf6-35fab7fd0ac8";
                            
                            //Meter values
                            float Voltage;
                            float Current;
                            float Power;
                            float Energy;
                            float Frequency;
                            float PowerFactor;
                            
                            void setup() {
                              Serial.begin(115200);
                            
                              //Wait for the Serial Monitor to open before proceeding
                              while(!Serial);
                              delay(100);
                              
                              //Start Cayenne connection 
                              Cayenne.begin(username, password, clientID, ssid, wifiPassword);
                            
                              Serial.println("PZEM-004T-100A Energy Meter Cayenne Interface using Wemos D1 Mini (ESP8266)");
                              Serial.println("");
                            }
                            
                            void loop() {
                              //get meter values
                              Voltage = pzem.voltage();
                              Current = pzem.current();
                              Power = pzem.power();
                              Energy = pzem.energy();
                              Frequency = pzem.frequency();
                              PowerFactor = pzem.pf();
                              
                              Cayenne.loop();
                            }
                            
                            //Display Voltage Value
                            CAYENNE_OUT(VOLTAGE_CHANNEL)
                            { 
                              Serial.println("Measurement Results: ");  
                              Cayenne.virtualWrite(VOLTAGE_CHANNEL, Voltage);
                              Serial.print("Voltage: "); Serial.print(Voltage,3); Serial.println("V");
                            }
                            
                            //Display Current Value
                            CAYENNE_OUT(CURRENT_CHANNEL)
                            {   
                              Cayenne.virtualWrite(CURRENT_CHANNEL, Current);
                              Serial.print("Current: "); Serial.print(Current,3); Serial.println("A");
                            }
                            
                            //Display Power Value
                            CAYENNE_OUT(POWER_CHANNEL)
                            {   
                              Cayenne.virtualWrite(POWER_CHANNEL, Power);
                              Serial.print("Power: "); Serial.print(Power,3); Serial.println("W");
                            }
                            
                            //Display Energy Value
                            CAYENNE_OUT(ENERGY_CHANNEL)
                            {   
                              Cayenne.virtualWrite(ENERGY_CHANNEL, Energy);
                              Serial.print("Energy: "); Serial.print(Energy,3); Serial.println("kWh");
                            }
                            
                            //Display Frequency Value
                            CAYENNE_OUT(FREQUENCY_CHANNEL)
                            {   
                              Cayenne.virtualWrite(FREQUENCY_CHANNEL, Frequency);
                              Serial.print("Frequency: "); Serial.print(Frequency,2); Serial.println("Hz");
                            }
                            
                            //Display Power Factor Value
                            CAYENNE_OUT(POWERFACTOR_CHANNEL)
                            {   
                              Cayenne.virtualWrite(POWERFACTOR_CHANNEL, PowerFactor);
                              Serial.print("Power Factor: "); Serial.println(PowerFactor,3);
                              Serial.println("");
                            }
                            

                            Code Breakdown:

                            Libraries:
                            • Arduino.h
                              • The ESP8266 support for Arduino 
                              • Able to write sketches, use Arduino functions and libraries
                              • Run sketches with no external microcontroller required
                              • Built-in library in the IDE
                              • More information about the library, along with the ESP8266 Arduino setup in Github 
                            • CayenneMQTTESP8266.h
                              • Cayenne MQTT ESP library for Cayenne IoT project builder connection
                              • Supports ESP8266 and ESP32 Wi-Fi modules
                              • Read and send data to Cayenne Dashboard
                              • More information about the library, and download link in Github
                            • PZEM004Tv30.h
                              • Peacefair (PZEM-004T v3) Energy Meter library
                              • Upgraded version from the older PZEM-004T library to support newer versions
                              • Provides full features of the PZEM-004T v3 energy monitor
                              • More information about the library, and download link in Github
                              • Can also be downloaded in the Arduino Library Manager
                              Variables:
                              @PZEM-004T v3
                              • PZEM004Tv30 pzem(D7,D8)
                                • The object constructor for the energy meter along with the pin connections
                              • Voltage
                                • Voltage sensor value in volts (V)
                              • Current
                                • Current sensor value in amperes (A)
                              • Power
                                • Power sensor value in watts (W)
                              • Energy
                                • Energy sensor value in kilo watt hour (kWh)
                              • Frequency
                                • Frequency sensor value in hertz (Hz)
                              • PowerFactor
                                • Calculated result based on sensor values
                              @Cayenne
                              • CAYENNE_DEBUG
                                • Enables Cayenne Serial Print
                              • CAYENNE_PRINT Serial
                                • Enables the serial monitor to print data
                              • VOLTAGE_CHANNEL 1
                                • The Cayenne channel assigned to project voltage readings from the meter
                              • CURRENT_CHANNEL 2
                                • The Cayenne channel assigned to project current readings from the meter
                              • POWER_CHANNEL 3
                                • The Cayenne channel assigned to project power readings from the meter
                              • ENERGY_CHANNEL 4
                                • The Cayenne channel assigned to project energy readings from the meter
                              • FREQUENCY_CHANNEL 5
                                • The Cayenne channel assigned to project frequency readings from the meter
                              • POWERFACTOR_CHANNEL 6
                                • The Cayenne channel assigned to project power factor readings from the meter
                              • ssid[]
                                • The Wi-Fi connection name to connect the Wemos D1 mini
                              • wifiPassword[]
                                • The Wi-Fi connection password to connect the Wemos D1 mini
                              • char username[]
                                • Device's Cayenne username (provided in the Cayenne link code)
                              • char password[]
                                • Device's Cayenne password (provided in the Cayenne link code)
                              • char clientID[]
                                • Device's Cayenne client ID (provided in the Cayenne link code)
                                Functions:
                                @PZEM-004T V3
                                • pzem.voltage()
                                  • fetch voltage value
                                • pzem.current()
                                  • fetch current value
                                • pzem.power()
                                  • fetch power value
                                • pzem.energy()
                                  • fetch energy value
                                • pzem.frequency()
                                  • fetch frequency value
                                • pzem.pf()
                                  • fetch power factor value
                                  @Cayenne
                                  • Cayenne.begin(username, password, clientID)
                                    • Starts cayenne connection
                                    • Displays connection status in serial monitor
                                  • Cayenne.loop()
                                    • Calls the CAYENNE_OUT(VIRTUAL_CHANNEL) class 
                                  • Cayenne.virtualWrite(Channel, Output)
                                    • Display the meter values on the serial monitor
                                    • Uploads the output values to the dashboard's virtual channel

                                  Fix:

                                  When uploading the code to the microcontroller, we may encounter this problem

                                  We can fix these by the following steps:

                                  • Check if a CH340G chip is embedded. CH340G provides TTL  to USB connectivity to Wemos D1 Mini microcontroller

                                  • Download and install the driver through the manufacturer's website

                                  • If error persists after reuploading the code, unplug the 5V and GND connection from the microcontroller before reuploading again

                                  • The code is successfully uploaded and reconnect the 5V and GND connection 
                                   

                                     

                                    Video Output:

                                     

                                     

                                    Conclusion:

                                    Smart Energy Meters helps in tracking daily electricity usage in our homes. DIY meters tend to be cheaper than pre-built ones, but may have similar efficiency and easier to use. Interfacing it with an IoT platform makes it more accessible when away from home. 

                                    References:

                                    • https://innovatorsguru.com/wp-content/uploads/2019/06/PZEM-004T-V3.0-Datasheet-User-Manual.pdf
                                    • https://www.electronics-tutorials.ws/transformer/current-transformer.html
                                    • https://www.tinyosshop.com/index.php?route=product/product&product_id=908
                                    • https://developers.mydevices.com/cayenne/docs/cayenne-mqtt-api/
                                    • https://www.opc-router.com/what-is-mqtt/

                                     

                                    Cayenne dashboardEnergy meterEnergy monitoringIotPzem-004t v3Wemos d1 mini

                                    Leave a comment

                                    All comments are moderated before being published