## Tuesday, 24 February 2015

### Pi-based Domestic Electricity Monitor - Part 5 - 'Connecting' the LDR + RPi to the meter

In this post, I've connected the Raspberry Pi to the electricity meter and created a python program to work out the consumption rate:

With the Raspberry Pi and the LDR sensor able to detect changes in light levels, it was time to 'connect' the LDR to the electricity meter's LED. Since I've read that you are not permitted to tamper with the meter at all, this consisted of taping the LDR to the front of the meter with insulating tape. (Image to follow). Should hold long-term, but easy to remove.

The meter's LED flashes 800 times for 1 kWh. From the interval between flashes, the instantaneous rate of consumption can be calculated. I made it (1/interval x 4.5). See working out below.

I made a few errors along the way, by waiting for the LED to go off but not factoring in this wait time, and messing up my maths the first time round.

Here's the output. The first colomn is the interval between flashes in seconds, the second is the power consumption in kW:

That's if my code is correct:

``` ```
``````#!/usr/bin/env python

#First go with the LDR picking up the flashes from the electrcity meter.

import RPi.GPIO as GPIO, time, os

#procedure to wait for LED flash front edge
def wait_for_flash():
try:
GPIO.wait_for_edge(23,GPIO.RISING)
except KeyboardInterrupt:
GPIO.cleanup()       # clean up GPIO on CTRL+C exit

#initialise pins
DEBUG = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

#timing and kW calcs based on 800 imp / kWh

start_time=time.time()

for i in range (0,20):
wait_for_flash()
elapsed_time = time.time()-start_time
start_time=time.time()
kW=((1/elapsed_time)*4.5)
print (elapsed_time,kW)
time.sleep(0.3) #to allow for LED to turn off

GPIO.cleanup()           # clean up GPIO on normal exit

print("Ending PRogram")

``````

The next steps are to write values to a database with SQLite:
current consumption rate to 3 d.p., date and time, calculated meter reading as a check of accuracy against actual meter reading.

Then I would like to have a new program to process and analyse the data.