Home Monitoring (home made) – Export

Reading the export meter

Reading the export meter is relatively easy as the Arduino in the meter cupboard can be extended to have another light voltage converter to read the export meter.

So we purchased another part from CPC (for those interested in replicating this, you can use one of these: http://cpc.farnell.com/taos/tsl257-lf/sensor-light-voltage-converter/dp/SC12392?Ntt=SC12392)

The Arduino program then needed extending to attach another interrupt for the export meter. The only gotcha here is that the export meter flashes 1000 times for 1kh, but the import meter only 800 times for 1kwh.

And it looks like this:

arduino-meter-reader

The complete Arduino program:

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUDP.h>

///////// CHANGEABLE VALUES /////////

double importMultiplier = 1.25;
double exportMultiplier = 1.0;

char ssid[] = "REPLACE THIS";
char pass[] = "REPLACE THIS";

char pompeii[] = "192.168.0.16";
int pompeiiPort = 80;

double minutesBetweenCalls = 1.0;

///////// CHANGEABLE VALUES ABOVE /////////

int status = WL_IDLE_STATUS;

WiFiClient pompeiiClient;
char pompeiiService[] = "/pvoutput-post.php";

unsigned long importImpulseCount = 0;
unsigned long exportImpulseCount = 0;

unsigned long lastTimeUploaded = millis();

boolean importInterruptAttached = false;
boolean exportInterruptAttached = false;

double millisecondsPerMinute = 60000.0;
double timeBetweenCalls = minutesBetweenCalls * millisecondsPerMinute;

void setup() {
  Serial.begin(9600);
  connectToWiFi();
}

void connectToWiFi()
{
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:    
    status = WiFi.begin(ssid, pass);

    delay(5000);
  }

  Serial.print("Connected to the network");
  printWifiData();
}

void loop() {
  addImportFlashInterrupt();
  addExportFlashInterrupt();

  if (isTimeToUploadData())
  {
    Serial.println("Uploading data");
    sendResultsToPompeii();
  }
}

void addImportFlashInterrupt()
{
  if (!importInterruptAttached)
  {
    importInterruptAttached = true;
    Serial.println("Attaching import interrupt on pin 3");
    attachInterrupt(1, flashImport, FALLING);
  }
}

void addExportFlashInterrupt()
{
  if (!exportInterruptAttached)
  {
    exportInterruptAttached = true;
    Serial.println("Attaching export interrupt on pin 2");
    attachInterrupt(0, flashExport, FALLING);
  }
}

boolean isTimeToUploadData() {
  unsigned long time = millis();

  if( (time - lastTimeUploaded) >= timeBetweenCalls) {
    Serial.println("Time to upload");
    lastTimeUploaded = time;
    return true;
  }
  return false;
}


void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

/* Handles the interrupt flash logic */
void flashImport() {
  importImpulseCount++;
}

void flashExport() {
  exportImpulseCount++;
}

void sendResultsToPompeii() {
  Serial.println("sendResultsToPompeii");

  String postData = getPostData();
  Serial.println(postData);

  if (pompeiiClient.connect(pompeii, pompeiiPort)) {
    Serial.println("connected to pompeii");
    // Make a HTTP request:
    pompeiiClient.print("POST ");
    pompeiiClient.print(pompeiiService);
    pompeiiClient.println(" HTTP/1.1");
    pompeiiClient.print("Host: ");
    pompeiiClient.print(pompeii);
    pompeiiClient.print(":");
    pompeiiClient.println(pompeiiPort);
    pompeiiClient.println("Accept: text/html");
    pompeiiClient.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    pompeiiClient.print("Content-Length: ");
    pompeiiClient.println(postData.length());
    pompeiiClient.println("Pragma: no-cache");
    pompeiiClient.println("Cache-Control: no-cache");
    pompeiiClient.println("Connection: close");
    pompeiiClient.println();

    pompeiiClient.println(postData);
    pompeiiClient.println();

    pompeiiClient.stop();
    pompeiiClient.flush();
    Serial.println("Called pompeii");
  }
}

String calculateImportWattsAndResetFlashes()
{
  if (importImpulseCount == 0UL) {
    return "0";
  }

  double watts = (importImpulseCount * importMultiplier);
  Serial.print("Calculated Import Watts: ");
  Serial.println(watts);
  
  importImpulseCount = 0UL;
  
  return doubleToString(watts);
}

String calculateExportWattsAndResetFlashes()
{
  if (exportImpulseCount == 0UL) {
    return "0";
  }

  double watts = (exportImpulseCount * exportMultiplier);
  Serial.print("Calculated Export Watts: ");
  Serial.println(watts);
  
  exportImpulseCount = 0UL;
  
  return doubleToString(watts);
}

String doubleToString(double valueToConvert)
{
  char doubleChar[15];
  dtostrf(valueToConvert,9,2,doubleChar);
  String dts = String(doubleChar);
  Serial.println(dts);
  return dts;
}

String getPostData()
{
  String importWatts = calculateImportWattsAndResetFlashes();
  String exportWatts = calculateExportWattsAndResetFlashes();
  
  String wattHourPeriod = doubleToString(timeBetweenCalls);
  
  return "iw=" + importWatts + "&ew=" + exportWatts + "&msBetweenCalls=" + wattHourPeriod;
}

Next Part

Part 3: Reading the temperature of the hot water tank

Home Monitoring (home made) – Overview

We have been working towards monitoring more and more items within the house to see the benefits of the solar array. Last time I posted an article about the meter reader which uses an Arduino and a light voltage converter to read the impulses from the import meter (a huge thanks goes out to Lars for his post here: http://www.lindnilsson.dk/lars/en/powermeter.php) but we wanted to expand on that and add in:

(Because of the size of the code, this article has been split in to separate pages)

And finally the results of all work!

Arduino Meter Reader

I’ll post soon about my latest purchase – an Arduino and Wifi Shield. The reason I purchased this little toy was to read the impulses from our meter and upload the results to PVOutput. Yeah, I know… sad or what… but when you’re in software integration, it’s oddly good fun and allowed me to see what these Arduino things are like + it might save me money in the future.

It took a few iterations to get a working program which would send results to PVOutput and wasn’t originally as planned. Due to a lack of a clock on the Arduino, there’s a piece of software on my server which receives a post request from the Arduino, adds the date and time and then sends the result on to PVOutput.

The Server script is relatively straight forward. Receive a post request, get the date and time, create the curl request and then send it.

pvoutput-post.php

<?php

$dte = date('Ymd');
$tme = date('H:i');

//echo "Watts Consumed " . $_POST["w"] . " " . $dte . " " . $tme;

$url = 'http://pvoutput.org/service/r2/addstatus.jsp';

$import = $_POST["w"];
$myvars = 'd=' . $dte . '&t=' . $tme . '&v4=' . $import;

//echo "Post Data: " . $myvars;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Pvoutput-Apikey: REPLACE THIS',
                                        'X-Pvoutput-SystemId: REPLACE THIS'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);

?>

The program is the one running on the Arduino. Every minute (or defined interval), the number of impulses from the meter will be used to calculate the watt hours over the time period. This result is then posted to the server for upload.

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUDP.h>

///////// CHANGEABLE VALUES /////////

double multiplier = 1.25;

char ssid[] = "CHANGE THIS";
char pass[] = "CHANGE THIS";

char pompeii[] = "CHANGE THIS FOR IP ADDRESS OF SERVER";
int pompeiiPort = 80;

float minutesBetweenCalls = 1;

///////// CHANGEABLE VALUES ABOVE /////////

int status = WL_IDLE_STATUS;

WiFiClient pompeiiClient;
char pompeiiService[] = "/pvoutput-post.php";

unsigned long impulseCount = 0;

unsigned long lastTimeUploaded = millis();

boolean interruptAttached = false;

unsigned long millisecondsPerMinute = 60000;
unsigned long minutesInHour = 60;
unsigned long timeBetweenCalls = minutesBetweenCalls * millisecondsPerMinute;

void setup() {
  Serial.begin(9600);
  connectToWiFi();
}

void connectToWiFi()
{
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:    
    status = WiFi.begin(ssid, pass);

    delay(5000);
  }

  Serial.print("Connected to the network");
  printWifiData();
}

void loop() {
  addFlashInterrupt();

  //getTimeFromPompeii();
  if (isTimeToUploadData())
  {
    Serial.println("Uploading data");
    sendResultsToPompeii();
  }
}

void addFlashInterrupt()
{
  if (!interruptAttached)
  {
    interruptAttached = true;
    Serial.println("Attaching interrupt on pin 3");
    attachInterrupt(1, flash, FALLING);
  }
}

boolean isTimeToUploadData() {
  unsigned long time = millis();

  if( (time - lastTimeUploaded) >= timeBetweenCalls) {
    Serial.println("Time to upload");
    lastTimeUploaded = time;
    return true;
  }
  return false;
}


void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

/* Handles the interrupt flash logic */
void flash() {
  impulseCount++;
}

void sendResultsToPompeii() {
  Serial.println("sendResultsToPompeii");

  int watts = calculateWattsAndResetFlashes();
  String postData = "w=" + String(watts);
  Serial.println(postData);

  if (pompeiiClient.connect(pompeii, pompeiiPort)) {
    Serial.println("connected to pompeii");
    // Make a HTTP request:
    pompeiiClient.print("POST ");
    pompeiiClient.print(pompeiiService);
    pompeiiClient.println(" HTTP/1.1");
    pompeiiClient.print("Host: ");
    pompeiiClient.print(pompeii);
    pompeiiClient.print(":");
    pompeiiClient.println(pompeiiPort);
    pompeiiClient.println("Accept: text/html");
    pompeiiClient.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    pompeiiClient.print("Content-Length: ");
    pompeiiClient.println(postData.length());
    pompeiiClient.println("Pragma: no-cache");
    pompeiiClient.println("Cache-Control: no-cache");
    pompeiiClient.println("Connection: close");
    pompeiiClient.println();

    pompeiiClient.println(postData);
    pompeiiClient.println();

    pompeiiClient.stop();
    pompeiiClient.flush();
    Serial.println("Called pompeii");
  }
}

int calculateWattsAndResetFlashes()
{
  if (impulseCount == 0UL) {
    return 0;
  }

  double watts = (impulseCount * multiplier) / (minutesBetweenCalls / minutesInHour);
  Serial.print("Calc'd Watts: ");
  Serial.println(watts);
  impulseCount = 0UL;
  return int(watts);
}

Combined with the inverted data upload from my server, I now have the ability to see the electricity import and generation at home from work 🙂

pvoutput import

And for my colleague, that’s real coding – not a single unit test in sight 😉 (joke)