Smart Home Automation with Energy Meter using NodeMCU
2224 55
Tarun Hareshbhai Katariya

Smart Home Automation with Energy Meter using NodeMCU

Hello guys Our project is about SMART FUTURISTIC HOME as with the advancement of technology, who...

Description:-

Hello guys Our project is about SMART FUTURISTIC HOME as with the advancement of technology, who doesn’t love being able to control appliances remotely, with just a few taps on their smartphones? We have came up with a project that will make your home smart, energy-efficient, Increase convenience, it will also allow you to have greater control of your energy use, it provides a platform for communication between user and device. Here are features about the project - It gives multiple choices to control electrical appliances such as manually control, control through mobile app, and control by IR remote. - We can monitor electrical quantities of energy meter in mobile app and on I2C LCD. Project work on Node MCU which is used to control and monitor data of sensors. We have used an IR receiver to receive signals from the remote and transmit it to Node MCU to control appliances. We have made mobile app through which we can also control appliances. And we can control the appliances manually also. We can also control the appliances by using both IR remote and mobile app like if we have turned ON appliance using the mobile app, we can turn OFF that appliance using remote and vice versa. We have created a real-time database in google firebase and made a mobile app using MIT app inventor for controlling & monitoring. We have used PZEM-004T through which we can measure electrical quantities. We have used another NodeMCU by using it we have received the Energy meter data from Google firebase and displayed it on I2C LCD. And if there is any issue with automatic control them we can control the appliance manually. We have used a two-way switch, by using it we can convert the operation of the project in automatic or manual mode.

Project Used Hardware

Energy Meter Hardware

NodeMCU, Relay Module, PZEM-004T, IR Receiver, I2C Display (16*2), 5V supply, switches, bulb as load.

Project Used Software

Energy Meter HardwareEnergy Meter Hardware

Arduino IDE, MIT App inventor, Google Firebase

Project Hardware Software Selection

Energy Meter Hardware

In hardware we have selected the following components: • NodeMCU: We have used NodeMCU as a controller. As it is a WiFi module so by taking the advantage of it we have controlled the appliance by mobile app. And it is also used to send and receive the data of the sensor from Google firebase. So it provides a communication medium. • PZEM-004T: We have used PZEM-004T to measure different electrical quantities. And used it as an energy meter in our project. It communicates with Node MCU using serial communication. • IR Receiver: We have used IR Receiver to control the appliance by using the remote control. IR Receiver receives the signal from remote and it transmits it to Node MCU to control the appliances. • Relay Module: We have used relay module to control the appliance. • I2C LCD: We have used LCD to display the energy meter data. And I2C module provides us the advantages as it only uses 2 bidirectional wires/pins to establish communication among devices. • 5V supply: We have used a 5V supply to power up the relay, IR Receiver, and other components. In software, we have used Arduino IDE, Google Firebase, and MIT App inventor. • Arduino IDE: We have used Arduino IDE software to program our NODE MCU. • MIT App Inventor: We have used MIT app inventor to create an android application for monitoring and controlling purposes. • Google Firebase: We have used Google Firebase as a cloud database it will work as a medium between the mobile app and NodeMCU. It stores the device status and also energy meter data which we can retrieve back and can see the data in mobile app and also in LCD.

Circuit Diagram

Automation Circuit Diagram

As the circuit is very simple. You can connect the sensor and relay module to NodeMCU as shown in the circuit diagram. The IR Receiver is connected to D1 pin of NodeMCU. PZEM-004T can be connected to D1 AND D2 or D4 and D5 or D7 and D8, here we have connected D4 and D5 pins to RX and TX pin of PZEM 004T respectively. The relay module is connected to D0, D2, D3, and D6 respectively. The load side terminals of the relay are connected to NO contact (Normally open). In I2C LCD there are SCL and SDA pins that are connected to D1 and D2 pins of Node MCU. Connect all the Vcc of sensors to 5V supply and GND to ground and connect a common ground between Node MCU and supply.

Code

// Program for NodeMCU 1 
#include "FirebaseESP8266.h"  
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <PZEM004Tv30.h>
#include <SoftwareSerial.h>
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
#define PZEM_RX_PIN D5 //(TX OF PZEM)
#define PZEM_TX_PIN D4 //(RX OF PZEM)
#endif

SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN);
PZEM004Tv30 pzem(pzemSWSerial);

const uint16_t kRecvPin = D1;
unsigned long key_value = 0;
int relay0 = D0;
int relay1 = D2;
int relay2 = D3;
int relay3 = D6;
IRrecv irrecv(kRecvPin);

decode_results results;
#define FIREBASE_HOST "YOUR FIREBASE HOST NAME" //Without http:// or https:// schemes
#define FIREBASE_AUTH "YOUR FIREBASE AUTHENTICATION"
#define WIFI_SSID "YOUR WIFI SSID"
#define WIFI_PASSWORD "YOUR WIFI PASSWORD"
FirebaseData firebaseData;
FirebaseData relayData;

FirebaseJson json;

void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn();  
  while (!Serial)  
    delay(50);
  Serial.println();
  Serial.print("IRrecv is now running");
  Serial.println(kRecvPin);
  pinMode(relay0, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  if (Firebase.getString(relayData, "/TM_IOT/device1")){
    Serial.print("Relay 1:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
      digitalWrite(relay0, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay0, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device2")){
    Serial.print("Relay 2:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay1, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay1, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device3")){
    Serial.print("Relay 3:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay2, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay2, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device4")){
    Serial.print("Relay 4:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay3, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay3, LOW);
    }
  }
}

void loop() {

  Serial.print("Custom Address:");
    Serial.println(pzem.readAddress(), HEX);

    float voltage = pzem.voltage();
    float current = pzem.current();
    float power = pzem.power();
    float energy = pzem.energy();
    float frequency = pzem.frequency();
    float pf = pzem.pf();
    Firebase.setFloat(firebaseData, "/TM_IOT/current", current);
    Firebase.setFloat(firebaseData, "/TM_IOT/voltage", voltage);
    Firebase.setFloat(firebaseData, "/TM_IOT/power", power);
    Firebase.setFloat(firebaseData, "/TM_IOT/energy", energy);
    Firebase.setFloat(firebaseData, "/TM_IOT/frequency", frequency);
    Firebase.setFloat(firebaseData, "/TM_IOT/power_factor", pf);
    if(isnan(voltage)){
        Serial.println("Error reading voltage");
    } else if (isnan(current)) {
        Serial.println("Error reading current");
    } else if (isnan(power)) {
        Serial.println("Error reading power");
    } else if (isnan(energy)) {
        Serial.println("Error reading energy");
    } else if (isnan(frequency)) {
        Serial.println("Error reading frequency");
    } else if (isnan(pf)) {
        Serial.println("Error reading power factor");
    } else {

        // Print the values to the Serial console
        Serial.print("Voltage: ");      Serial.print(voltage);      Serial.println("V");
        Serial.print("Current: ");      Serial.print(current);      Serial.println("A");
        Serial.print("Power: ");        Serial.print(power);        Serial.println("W");
        Serial.print("Energy: ");       Serial.print(energy,3);     Serial.println("kWh");
        Serial.print("Frequency: ");    Serial.print(frequency, 1); Serial.println("Hz");
        Serial.print("PF: ");           Serial.println(pf);
    }

    Serial.println()   
  if (Firebase.getString(relayData, "/TM_IOT/device1")){
    Serial.print("Relay 1:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay0, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay0, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device2")){
    Serial.print("Relay 2:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay1, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay1, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device3")){
    Serial.print("Relay 3:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay2, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay2, LOW);
    }
  }
  if (Firebase.getString(relayData, "/TM_IOT/device4")){
    Serial.print("Relay 4:-");
    Serial.println(relayData.stringData());
    if (relayData.stringData() == "1") {
    digitalWrite(relay3, HIGH);
    }
  else if (relayData.stringData() == "0"){
    digitalWrite(relay3, LOW);
    }
  }
  if (irrecv.decode(&results)) {
    serialPrintUint64(results.value, HEX);
    Serial.println("");

    switch(results.value){
          case 0xC00001:
          Serial.println("1-on");
          Firebase.setFloat(firebaseData, "/TM_IOT/device1", 1);
          digitalWrite(relay0, HIGH);
          delay(500);
          break ;
          case 0xC00002:
          Serial.println("1-off");
          digitalWrite(relay0, LOW);
          Firebase.setFloat(firebaseData, "/TM_IOT/device1", 0);
          delay(500);
          break ;
          case 0xC00003:
          Serial.println("2-on");
          digitalWrite(relay1, HIGH);
          Firebase.setFloat(firebaseData, "/TM_IOT/device2", 1);
          delay(500);
          break ;
          case 0xC00004:
          Serial.println("2-off");
          digitalWrite(relay1, LOW);
          Firebase.setFloat(firebaseData, "/TM_IOT/device2", 0);
          delay(500);
          break ;
          case 0xC00005:
          Serial.println("3-on");
          digitalWrite(relay2, HIGH);
          Firebase.setFloat(firebaseData, "/TM_IOT/device3", 1);
          delay(500);
          break ;
          case 0xC00006:
          Serial.println("3-off");
          digitalWrite(relay2, LOW);
          Firebase.setFloat(firebaseData, "/TM_IOT/device3", 0);
          delay(500);
          break ;
          case 0xC00007:
          Serial.println("4-on");
          digitalWrite(relay3, HIGH);
          Firebase.setFloat(firebaseData, "/TM_IOT/device4", 1);
          delay(500);
          break ;
          case 0xC00008:
          Serial.println("4-off");
          digitalWrite(relay3, LOW);
          Firebase.setFloat(firebaseData, "/TM_IOT/device4", 0);
          delay(500);
          break ;      
        }
        key_value = results.value;
    irrecv.resume();  
  }
  delay(100);
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------

// Program for NodeMCU 2

#include "FirebaseESP8266.h"  // Install Firebase ESP8266 library
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define FIREBASE_HOST "YOUR FIREBASE HOST NAME" //Without http:// or https:// schemes
#define FIREBASE_AUTH "YOUR FIREBASE AUTHENTICATION"
#define WIFI_SSID "YOUR WIFI SSID"
#define WIFI_PASSWORD "YOUR WIFI PASSWORD"
FirebaseData firebaseData;
FirebaseData voltage_data;

FirebaseJson json;

String voltage_reading;
String current_reading;
String power_reading;
String frequency_reading;
String pf_reading;
String energy_reading;
String line1;
String line2;
int x = 0;
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  Wire.begin(D2, D1);

  lcd.begin();

  lcd.home();

  lcd.print("Welcome to TM-IoT!!!");

}

void loop() {
  if (Firebase.getString(voltage_data, "/TM_IOT/voltage")){
    Serial.print("Voltage:-  ");
    Serial.println(voltage_data.stringData());
      voltage_reading = voltage_data.stringData();
}
  if (Firebase.getString(voltage_data, "/TM_IOT/current")){
    Serial.print("Current:-   ");
    Serial.println(voltage_data.stringData());
      current_reading = voltage_data.stringData();
}
  if (Firebase.getString(voltage_data, "/TM_IOT/power")){
    Serial.print("Power:-   ");
    Serial.println(voltage_data.stringData());
      power_reading = voltage_data.stringData();

  if (Firebase.getString(voltage_data, "/TM_IOT/frequency")){
    Serial.print("Frequency:-   ");
    Serial.println(voltage_data.stringData());
      frequency_reading = voltage_data.stringData();
}
  if (Firebase.getString(voltage_data, "/TM_IOT/power_factor")){
    Serial.print("Power Factor:-   ");
    Serial.println(voltage_data.stringData());
    pf_reading = voltage_data.stringData();
}
  if (Firebase.getString(voltage_data, "/TM_IOT/energy")){
    Serial.print("Energy:-   ");
    Serial.println(voltage_data.stringData());
    energy_reading = voltage_data.stringData();
}
  if (x==0){
//    line1 = "V: "+voltage_reading+" "+"I: "+current_reading;
//    line2 = "P: "+power_reading+" "+"F: "+frequency_reading;
    line1 = "V: "+voltage_reading;
    line2 = "I: "+current_reading+" "+"F: "+frequency_reading;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(line1);
    lcd.setCursor(0,1);
    lcd.print(line2);
    delay(3000);
    x = 1;  
  }
  if (x==1){
    line1 = "P: "+power_reading+" "+"PF: "+pf_reading;
    line2 = "E: "+energy_reading;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(line1);
    lcd.setCursor(0,1);
    lcd.print(line2);
    delay(2000);
    x = 0;
  }     
}