Energy Monitoring through a Raspberry Pi

In this application we will setup a CE current monitor with a Raspberry Pi and display current readings for circuits through a web interface

Story

Hit ctrl+x to exit.  When prompted to save say yes.  When prompted to enter file name leave it as is.

Back in the terminal enter:

sudo a2enmod cgi

Step 1.3 I2C Tools setup

Now we need to install a package called I2C tools and another package called Python-SMBus  These two packages together will allow our Python script to communicate with the ControlEverything.com current monitoring board.

In the SSH terminal enter:

sudo apt-get update

This will update the apt-get package installer so we can be sure we get the latest packages.

Next in the SSH terminal enter:

sudo apt-get install 12c-tools

After that package is installed enter:

sudo apt-get install python-smbus

After that package is installed it is a good idea to reboot the pi so in the SSH terminal enter:

sudo reboot

Wait a few seconds for the Pi to reboot and re-associate with the network, then re connect to the Pi via SSH.

Lets test i2c-tools to make sure everything is working.  Make sure the current monitoring board is connected to the Pi using the included I2C cable and is powered up with a 12VDC power supply source.  Then in the SSH terminal enter:

i2cdetect -y 1

Hopefully the Pi will recognize our Current monitoring board which will be registered at address 0x2a  This is what you should see in the terminal:

If you see the controller listed there then we are good to move on.

Step 1.4 Upload Web interface/Python files to Pi

We will use sftp to upload our files to the Pi.  You will need an ftp client to do this.  I highly recommend filezilla if you do not already have an ftp client.  It can be obtained here(Be sure to get the client and not the server).  They provide documentation on FTP but it is pretty easy.  Once you have that downloaded and installed we are ready to proceed.

We need to alter some permissions on the Apache file directories in order upload our files to the Pi so back in the SSH Terminal enter:

sudo chown -R pi:www-data /var/www/html

Now open Filezilla and connect to the Pi.  Enter the Pi's IP address as the host, pi as the username, raspberry as the password, and 22 as the port.  Connected?  Great!  Lets shove some files in there!

The files can be obtained from our github repo here:

https://github.com/ControlEverythingCom/Pi-Current-Monitor-Web-1

In Filezilla navigate to the local directory where you downloaded/cloned those files.  In the Remote Side pane of Filezilla for the Pi navigate to /var/www/html directory and upload the files there.

Back in the terminal move over to the /var/www/html directory by entering:

cd /var/www/html

Now enter:

python CurrentMonitor.py

The Python application should print the current readings to the console like this:

Upload Web interface Python files to Pi 1

Step 1.5 Final configuration settings

We need to change the owner of all files in the html directory now.  In the SSH terminal enter:

sudo chown -R pi:www-data /var/www/html

Now we need to correct permissions on the CurrentMonitor.py to do that enter:

sudo chmod g+x /var/www/html/CurrentMonitor.py

Now since Apache is going to run the Python script to interface with the i2c port on the Pi we have to make sure Apache(www-data) is allowed to do that.  So we will add www-data to the i2c group like this:

sudo usermod -a -G i2c www-data

And finally…(drum roll please):

sudo reboot

Close that SSH terminal.  I'm sure you're as tired of it as I am!

Step 2 Wire up and install the board.

We must note here that you will be messing with 120 VAC wiring here and electrical shock is no laughing matter.  If you do not feel comfortable with this please PLEASE hire a registered electrician to do this step for you.  They know what they are doing.

Before proceeding you MUST shut down all power to the circuit breaker panel using the main off switch.  If possible we highly recommend also shutting the breaker off at the pole/box that supplies power to the panel.  This will ensure you do not get shocked.  Be sure to test all points in the Circuit breaker panel for voltage before proceeding.

I also recommend turning off all breakers in the panel just to be on the safe side.

The current monitoring board monitors the hot leg of an AC circuit.  This wire is generally the black leg in a 3 conductor cable.  White is generally Neutral and bare or green is generally ground.  So run the Black leg of each circuit you want to monitor through each of the on board current monitoring sensors as shown here:

Wire up and install the board

After all wires are run through the on board sensors and connected back to the circuit breakers in the panel connect a USB power supply to the Raspberry Pi and a regulated 12VDC power supply source to the Current monitoring board.  Do not worry if these power supply sources are off due to all the circuits being off.  It will not hurt anything if the Pi and Current Monitoring board power up later.

Now it's time to power everything back up.  Be Careful!!!!  Do one more sanity check over absolutely everything and make sure everyone is clear of the area and everything is connected properly.  Also make sure you have the cover back on the Circuit Panel if you removed it during installation.  Turn power back on at the pole/box.  Then go back to the Circuit panel and turn on the main power switch there.  Now turn on each circuit breaker in the panel one at a time.  If all the breakers turn on without tripping and everything is powered up in the house/business then congratulations on a job well done!  Feel free to celebrate in any way you see fit.

Step 3 Lets have some fun!

Make sure your Pi is back on the network.  Note that it's IP address may have changed if the router rebooted so be sure to check that.  Once you know the Pi's IP make sure your computer, phone, or tablet is on the same network as the Pi, then enter the Pi's IP address in your web browser.  You should now see real time current levels for each circuit you wired up.

Now run around and turn things on and off like a mad man/woman.  I know I thoroughly irritated my wife by flipping the lights in the living room on and off erratically for several minutes laughing and jumping up and down like a complete idiot.  Do not feel bad if this happens to you.

Ending Notes

While this was fun and all you may find yourself a bit bored with it after a few days.  At that point I highly recommend having some fun with customizing the web interface for the Pi.  You can do some really cool stuff there.

Some other things you can do are register a static IP address for the Pi on your network so it does not change (DHCP).  I generally reserve the IP on my router as I feel this is the safest thing and if your router supports it, it is super easy.

Also you can open a port on your router firewall to allow external access so you can monitor everything while you are away from home.  You can really go all out here and setup DNS.  Make sure to take security into consideration if you decide to do this and do it right.

Another really cool thing is to monitor the current of each circuit and calculate kilowatt hour usage for the circuits.  This is absolutely possible to do, but a bit more complex.  We may do another post on that later.  This would allow you to see how many pennies each of the circuit in your house is costing you!

The possibilities here really are abundant so be creative and let us know what you come up with!

Source: Energy Monitoring through a Raspberry Pi


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

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

Scroll to Top