Power meter

The power meter was my first goal as I noted that our gas meter has no mirror etc… so I shouldn’t make it toooo difficult om myself.

Having had a look on the net at various sites with information my setup consists of the following parts:

To setup and measure our power meter I use the following parts;

  • breadboard
  • Light Dependent Resistor (LDR)
  • 1 kohm resistor
  • 3 wires for connection

When wired up I tested the light meter and was working pretty good from the start. Next point was to read the ‘light flashes’ from the meter. One of the points I needed to fix was that occasionally 1 flash was recording 2 or 3 times. To do this I created an array with the last 10 readings. When the current average is less then the previous average * a set percentage, then it is game on for counting. So far I get about a 99% accuracy using this method. Enough for me.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
const int chipSelect = 4;
 
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x8E, 0xDB };
char server[] = "yourweb.com"; 
 
String URLdata="";
 
unsigned long lastConnectionTime = 0;  			// last time uploaded       
const unsigned long postingInterval = 600000;	// upload frequency in mill-sec (10 min)
unsigned long stand=33456000;  					// 3-2-13 current meterreading
int PulsesinTimePeriod=0;
const int numReadings = 10;
 
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int paverage = 0;				// the previous average
int inputPin = A1;				// analog input pin used
 
// Initialize the Ethernet client library
EthernetClient client;
 
void setup() {
//  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
 
   Serial.print("Initializing SD card...");
   pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
   // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  Serial.println("Starting.....");
  delay(5000);
}
 
void loop()
{
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    
 
  // if we're at the end of the array, wrap to start
  if (index >= numReadings) index = 0;                           
 
  // calculate the average:
  average = total / numReadings;         
 
  if (average < (paverage*0.96)) {
        stand+=1;  				// add 1 to total number of pulse count
        PulsesinTimePeriod+=1;  // number of pulses sinds last upload
        Serial.print(stand);
        Serial.print(" - ");
        Serial.print(millis());
       Serial.print("\n"); 
        delay(200);
  }
  paverage=average;
  delay(20);       				// add for stability
  // check to see if time has lapsed before recording
  if(millis() - lastConnectionTime > postingInterval){
    sendData();
  }
}
 
// this method makes a HTTP connection to the server:
void sendData() {
 
  // put together the data part
  URLdata="type=";
  URLdata+="S";
  URLdata+="&stand=";
  URLdata+=stand;
  URLdata+="&kliks=";
  URLdata+=PulsesinTimePeriod;
  URLdata+="&kliktijd=";
  URLdata+=millis() - lastConnectionTime;
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("GET /your-upload-page.php?");
    client.print(URLdata);
    client.println(" HTTP/1.1"); 
    client.println("Host: yourweb.com");
    client.println();
    Serial.print(URLdata);
    delay(100);
    client.stop();
    PulsesinTimePeriod=0; // reset counter
  } 
  else 
  {
    // if you couldn't make a connection:
    // write to SD card as backup
    writeSDcard(URLdata);
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}
 
void writeSDcard(String datastring) {
      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      File dataFile = SD.open("log.csv", FILE_WRITE);
 
      // if the file is available, write to it:
      if (dataFile) 
      {
            dataFile.println(datastring);
          	dataFile.close();
          	// print to the serial port too:
          }  
          // if the file isn't open, pop up an error:
          else 
          {
            Serial.println("error opening datalog.csv");
      } 
}

A few photo’s of my setup.

IMAG0456 IMAG0458 IMAG0460 IMAG0463 IMAG0465