Some words of warning …
This project post describes what I actually did to monitor household power. It is fairly technical and I am guessing it will only be of interest to you if you are contemplating something similar, or if you are me and trying to remember what exactly I did.
I am looking at residential line voltage (240 volts AC single phase in North America) and it can hurt or kill you. If you are not confident working with this voltage find somebody who is … you really want to understand what you are playing with.
The Nuts and Bolts of Power Monitoring
As mentioned in an earlier post my residential BC Hydro account has a commercial designation and as such is not eligible for the real time monitoring options offered by BC Hydro. However, I wanted to capture all of the hourly consumption data provided by BC Hydro (past, present and future).
Recapturing the Past
You can download all of your BC Hydro consumption data fairly easily if you have an on-line account. You just have to navigate to the ‘export data’ popup and enter the appropriate parameters. You will only able to download one year of data at a time so to get 3 years of data I made 3 separate downloads.
Once the CSV (comma separated values) files were downloaded I edited them to remove the header lines and to remove fields I was not interested in. I used a simple text editor (vi in my case) to accomplish this. When this was completed the file pictured at the right no longer had the five header lines and I removed the “,N/A,N/A” from the end of every line of data. Each line of data now consists of:
- the account number,
- the date and time of the reading,
- the power consumed in kilowatt-hours.
Now that I had all of this data I needed to store it somewhere. I decided to use the SQL database used by our website. It provided a central, ‘cloud-based’ storage location and the power of an SQL database. This may not be the best option but it worked for me. It would not be impossible to setup an SQL database from scratch but is beyond the scope of what I am prepared to talk about. You could alternatively leave the data as a CSV file but working with the data would be harder.
The database I am using is a MySQL implementation and there are lots of free tools available for working on these databases. I used the tool phpMyAdmin to create a new table called BCHYDRO_DATA. I should probably have called this table something else like RESIDENTIAL_CONSUMPTION_DATA as it is used to store the consumption data of all the circuits I am monitoring.
Once I created this table I used phpMyAdmin to import the CSV files described above into this new database table.
Great. I’ve captured all of this data in my own database, now what to do with it?
A side benefit of working with a wordpress database is that the associated website is PHP enabled. I was able to write a PHP database access script (with the original name of data.php) allowing me to access the database and look at different slices of data depending on the parameters it was called with. I have done some PHP work in the past, but would not call myself an expert. I suspect there are better ways to do what I did, but again it works for this project.
This file was placed in the root directory of the website.
So, how do I visualize it?
d3.js is open source and is not the easiest library to work with; I spent a lot of time reading and looking at examples. However, it is very powerful and it is quite amazing what you can do with your data once you have figured it out. The WordPress plugin for d3 is fairly new and does not come with great documentation, but it works well once you understand what is going on.
I started by modelling my power use visualization after the BC Hydro website and am still extending my capabilities beyond this starting point.
I am now able to display the total power consumption by the hour, day, month or year and can compare this consumption to other time periods (just like the BC Hydro website).
Additionally, I can superimpose the separate power consumption of monitored branch circuits as a stacked bar chart on top of the total consumption. The above chart shows total residential consumption in blue and the contribution of the hot water tank is superimposed in orange.
I want to document the d3 code implemented for this but I think to do it justice it is the subject of another post. I am very pleased with what I have been able to accomplish visualizing the data and there is a lot more that could be done here.
How am I measuring power consumption on branch circuits?
I’m using a Hall-Effect linear current sensor; the Allegro ACS765 purchased from Lee’s Electronics for $12.00. This sensor can measure up to a 50 amp AC waveform (or 50V DC); I still am not sure if this 50 amp limit is an rms or peak-to-peak value.
The circuit to use this sensor is as shown, with the circuit components sized as indicated in red on the circuit diagram. The hot wire for a 120 volt circuit (or one of the hot wires in a 240 volt circuit) is fed through pins 4 and 5. Vout is an analog voltage whose level depends on the magnitude of the current passing through pins 4 and 5.
Right now I am only measuring instantaneous current to calculate power. This should be fine as long as I stick to:
- a dependable sinusoidal power source like that supplied BC Hydro (this would probably not work for anything we generate),
- purely resistive loads (like a hot water tank or an electric stove)
Once I start looking at loads with motors (like a fridge) I will also have to record instantaneous voltage and measure the phase shift between the current and voltage readings so I can make a power factor correction. Even thinking about this gives me a headache so I have not done it yet … see the resources listed at the end of this post for more detail about all of this.
The details of my instantaneous current measurements can be parsed out of the following Arduino sketch.
Finally, Recording the Values …
The final step in all of this is going from the raw Irms readings coming from the Arduino to meaningful power measurement stored in the database table I setup.
I did this by connecting the USB port of the Arduino Uno to one of the USB ports on a raspberry pi so they could communicate serially. I use a python script to:
- capture the current readings from the Uno,
- convert these readings to power consumption in kw-hrs assuming 120 or 240 volts (depending on the circuit),
- and FINALLY upload these readings on an hourly basis to the database.
The details for all of this can be found in the python script shown below.
The Raspberry Pi has a WiFi dongle that it uses to connect to the internet.
Wrapping it up
Amazingly, this all works … it is neat to see branch circuit consumption readings appear ‘magically’ every hour on a page on my website.
There are a lot of things that need to be fixed:
- I have to manually upload BCHydro consumption data to my database; I am waiting to see if I can get real-time monitoring equipment from BCHydro or alternatively if they have an API to automatically download data from their website.
- The raspberry pi does not handle losing its internet connection gracefully; it has to be rebooted. This appears to be a power management issue and should be easy to fix.
- The python script does not handle errors like failing to write to the database; these errors need to be handled.
- Need to design a case for the actual circuit; it is on a breadboard right now.
- Very good write-up … the associated video goes through rms calc’s (http://corgitronics.com/2014/06/30/acs756-current-measurement-tests/)
- Excellent explanation of 120/240 volt single phase current flow (https://www.bluesea.com/resources/86)
- breadboarded circuit (http://www.stuffandymakes.com/blog/2012/04/25/ac-current-detection-with-allegro-ac756-linear-current-sensors)
- example of reading AC voltage (http://openenergymonitor.org/emon/buildingblocks/acac-component-tolerances)