Non-Invasive Smart Electricity Meter

A non invasive current sensor is connected to the spark core (with a few components), and clamped around a cable in the Mains distribution unit. No wiring is required, however Do not try this at home, as the Mains Distribution Unit should not be tampered with unless one is Licensed to do so.

The back end is Web Application hosted on a LAMP (Linux Apache MySql PHP) setup. Initially it was hosted on a raspberry pi on a LAN, but then I decided to host it on a hosted server. Both instances worked well, its just a matter of preference.
The spark core simply takes periodic readings and sends them to the server, where all of the calculations and filtering is done.

The project uses the EmonLib Library for Electricity
Non-Invasive Smart Electricity Meter

Untitled file
// This #include statement was automatically added by the Spark IDE.

#include "HttpClient/HttpClient.h"
#include "application.h"
#include "EmonLib.h"
#include <string.h>

HttpClient http;
http_request_t request;
http_response_t response;
http_header_t headers[] = {

      { "Content-type", "application/x-www-form-urlencoded" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

EnergyMonitor emon1;                   // Create an instance
TCPClient client;
const int voltage = 220;
double average = 0.0; // interval at which to do something (millisecond)
double sum = 0.0; // interval at which to do something (milliseconds)
int count = 0; // interval at which to do something (milliseconds)

static unsigned long secondInterval = 1000;
static unsigned long minuteInterval = 57000;
static unsigned long prevMinute = 0;
static unsigned long prevSecond = 0;
unsigned long now;

void setup()
{
  Serial.begin(9600);
  emon1.current(0, 103.1);
}



void loop()

{
   unsigned long currentMillis = millis();
    if ((unsigned long)(currentMillis - prevSecond) < secondInterval){
    return;
        }
    else{

        double Irms = emon1.calcIrms(1480);  // Calculate Irms only
        //double watts = (Irms*voltage) - 16;  // Calculate Irms only
        double watts = (Irms*1000) ;  // Calculate Irms only
        sum = sum + watts;
        count = count + 1;
        if ((unsigned long)(currentMillis - prevMinute) < minuteInterval){
        return;
        }
        else{
            average = sum/count;
            sendWatts(average);
            average = 0.0;
            sum = 0.0;
            count = 0;
            prevMinute = currentMillis;
        }
        prevSecond = currentMillis;
        }
}

void sendWatts(double wattreading){
    String val = "watts=" + doubleToString(wattreading,2);
    request.hostname = "yourhost.com";
    request.port = 80;
    request.path = "/php/insertConsumption.php";
    request.body = val ;
    http.post(request, response, headers);
}


String doubleToString(double input,int decimalPlaces){
    if(decimalPlaces!=0){
    String string = String((int)(input*pow(10,decimalPlaces)));
    if(abs(input)<1){
    if(input>0)
    string = "0"+string;
    else if(input<0)
    string = string.substring(0,1)+"0"+string.substring(1);
    }

    return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);

    }
    else {
    return String((int)input);
    }

}

Untitled file
<?php



include_once('IConnectInfo.php');



$link = mysql_connect(IConnectInfo::HOST, IConnectInfo::UNAME, IConnectInfo::PW)

or die('Could not connect: ' . mysql_error());

//echo "Connected successfully \n";

mysql_select_db(IConnectInfo::DBNAME) or die('Could not select database');



  	

 if(isset($_POST['watts']) ) {

   if(ctype_digit($_POST['watts']) && is_int($_POST['watts'])) {

   	$poswatts = $_POST['watts']; 

  	if($poswatts<0 ) $poswatts  = 0;

          date_default_timezone_set("UTC"); 

          $currutctime = date("Y-m-d H:i:s", time());

  

      $pdo = new PDO('mysql:store.db');

      $stmt = $pdo->prepare('INSERT INTO Consumption (watts,unixtime)   VALUES (:watts, :unixtime);');

      $stmt->bindParam(':watts', $poswatts, PDO::PARAM_INT);

      $stmt->bindParam(':unixtime', $currutctime, PDO::PARAM_STR);

      $stmt->execute();

  

  		} else {

      // reject id value and report error to user

    }

	}

 

?>

Read More: Non-Invasive Smart Electricity Meter

Leave a Comment

Your email address will not be published. Required fields are marked *