Skip to content

Domestic Power Consumption Analyzer

Domestic Power Consumption Analyzer

WIZnet

Published February 10, 2023

Original author: justkidding3000Original source (new tab)

Domestic Power Consumption Analyzer

Project description

ABOUT THIS PROJECT

Hello everyone !! We are from Minute Physics and our project is about display Power Consumption used by each MCB’s and display it to user through displaying it in ThingSpeak Website. Note that this project is for Young Innovators Challenge Competition and it is still a prototype.

CODE:

#include <SPI.h>
#include <Ethernet.h>
int m[] = {0,1,2,3,4,5};
double AnalogOutput[] = {0,1,2,3,4,5};
double SumSqrAnalogOutput[] = {0,1,2,3,4,5};
double offset[] = {0,1,2,3,4,5};
double OutputRms[]  = {0,1,2,3,4,5};
double RmsCurrent[] = {0,1,2,3,4,5};
double PowerOutput_Watt[] = {0,1,2,3,4,5};
double SumPowerOutput_Watt[] = {0,1,2,3,4,5};

// Local Network Settings
byte mac[]     = { 0xD4, 0xA8, 0xE2, 0xFE, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[]      = { 192,168,100,29};                // Must be unique on local network
byte gateway[] = { 192,168,0,1};
byte subnet[]  = { 255, 255, 255, 0 };

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "5XNDH9M99EEYY7U3";    // Write API Key for a ThingSpeak Channel
const int updateInterval = 1000;        // Time interval in milliseconds to update ThingSpeak

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{
  offset[0] = 510.09;
  offset[1] = 510.35;
  offset[2] = 510.73;
  offset[3] = 510.43;
  offset[4] = 509.73;
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.print("ETHERNET SHIELD ip  is     : ");
Serial.println(Ethernet.localIP());
// Start Ethernet on Arduino
startEthernet();
}

void loop()
{
for (int x = 0; x < 5; x++)
    {
      SumSqrAnalogOutput[x] = 0;
      
      
        for(int i=0; i<1000;i++)
        {
            AnalogOutput[x] = analogRead(x)-offset[x];
            SumSqrAnalogOutput[x] = pow(AnalogOutput[x],2) + SumSqrAnalogOutput[x];
            //delay(1);
        }
        OutputRms[x] = sqrt(SumSqrAnalogOutput[x]/1000); 
        RmsCurrent[x] = 0.042*OutputRms[x]-0.055;
        PowerOutput_Watt[x] = RmsCurrent[x]*247.8;       
    }
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("…disconnected.");
Serial.println();

client.stop();
}

//Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+String(PowerOutput_Watt[0])+"&field2="+String(PowerOutput_Watt[1])+"&field3="+String(PowerOutput_Watt[2])+"&field4="+String(PowerOutput_Watt[3])+"&field5="+String(PowerOutput_Watt[4]));
}
lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1n");
client.print("Host: api.thingspeak.comn");
client.print("Connection: closen");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"n");
client.print("Content-Type: application/x-www-form-urlencodedn");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("nn");

client.print(tsData);

lastConnectionTime = millis();

if (client.connected())
{
Serial.println("Connecting to ThingSpeak…");
Serial.println();

failedCounter = 0;
}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}

}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();

lastConnectionTime = millis();
}
}

void startEthernet()
{

client.stop();

Serial.println("Connecting Arduino to network…");
Serial.println();

delay(1000);

// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else {
Serial.println("Arduino connected to network using DHCP");
Serial.println();
Serial.println("Data being uploaded to THINGSPEAK Server…….");
Serial.println();
}

delay(1000);
}

CIRCUIT OF CURRENT SENSOR WITH ETHERNET SHEILD

Image 2021 09 14 202448 ni54gn83v7

Image 2021 09 14 202448 ni54gn83v7


 

Comments

Comments