Abid hossain
Published © GPL3+

IOT based Power and Energy meter

This is a IOT based Power and Energy Meter with Blynk iot platform.

IntermediateFull instructions provided10,971
IOT based Power and Energy meter

Things used in this project

Hardware components

Arduino Pro Mini 328 - 5V/16MHz
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
INA219 DC current sensor
×1
0.96" i2c OLED display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

powe_energy_bb_jeWHOYckNG.png

Code

Code for Arduino

Arduino
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial nodemcu(2,3);
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
long int data; 

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_INA219 ina219;

unsigned long previousMillis = 0;
unsigned long interval = 100;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float energy = 0;

float sdata1 = 0; 
float sdata2 = 0; 
float sdata3 = 0;
float sdata4 = 0; 

String cdata; 

void setup()
{
Serial.begin(9600); 
nodemcu.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  ina219.begin();
}


void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    ina219values();
    displaydata();
  }
if(nodemcu.available() == 0 )
{
    sdata1 = loadvoltage;
    sdata2 = current_mA;
    sdata3 = loadvoltage * current_mA;
    sdata4 = energy;

    
   cdata = cdata + sdata1+","+sdata2+","+sdata3+","+sdata4; 
   Serial.println(cdata); 
   nodemcu.println(cdata);
   delay(1000); 
   cdata = ""; 
}
}


void displaydata() {
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println(loadvoltage);
  display.setCursor(31, 0);
  display.println("V");
  display.setCursor(62, 0);  
  display.setCursor(75, 0);
  display.println(current_mA);
  display.setCursor(110, 0);
  display.println("mA");
  display.setCursor(0, 6);
  display.println("--------------------"); 

  
    display.setCursor(101, 14);
    display.println("A");
    display.setCursor(108, 17);
    display.println("b");
    display.setCursor(114, 20);
    display.println("i");
    display.setCursor(120, 23);
    display.println("d");


    
  display.setCursor(0, 13);
  display.println(loadvoltage * current_mA);
  display.setCursor(57, 13);
  display.println("mW");
  display.setCursor(0, 23);
  display.println(energy);
  display.setCursor(57, 23);
  display.println("mWh");
  display.display();
}

void ina219values() {
  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  energy = energy + loadvoltage * current_mA / 3600;
}

Code for Nodemcu

Arduino
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>

char auth[] = "nSC5994bDxGyYpYM4ds3sFeKoDSHBdKz";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ABID";
char pass[] = "8901234567";

SimpleTimer timer;

String myString; // complete message from arduino, which consistors of snesors data
char rdata; // received charactors

float firstVal, secondVal,thirdVal,forthVal; // 
// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000);
  
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

    timer.setInterval(1000L,sensorvalue1); 
     timer.setInterval(1000L,sensorvalue2); 
     timer.setInterval(1000L,sensorvalue3);
      timer.setInterval(1000L,sensorvalue4);
}

void loop()
{
   if (Serial.available() == 0 ) 
   {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
   }
   
  if (Serial.available() > 0 ) 
  {
    rdata = Serial.read(); 
    myString = myString+ rdata; 
   // Serial.print(rdata);
    if( rdata == '\n')
    {
String l = getValue(myString, ',', 0);
String m = getValue(myString, ',', 1);
String n = getValue(myString, ',', 2); 
String o = getValue(myString, ',', 3); 

firstVal = l.toFloat();
secondVal = m.toFloat();
thirdVal = n.toFloat();
forthVal = o.toFloat();

  myString = "";
// end new code
    }
  }

}

void sensorvalue1()
{
float sdata = firstVal;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, sdata);

}
void sensorvalue2()
{
float sdata = secondVal;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V3, sdata);

}

void sensorvalue3()
{
float sdata = thirdVal;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V4, sdata);

}
void sensorvalue4()
{
float sdata = forthVal;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, sdata);

}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Credits

Abid hossain

Abid hossain

4 projects • 48 followers
Student of BSEEE in United International University, Bangladesh. Loves to learn and make new stuff.

Comments