Instructions to put together a Current measuring system with a Raspberry Pi.

This is a work in progress and is only here for my own reference.


This is a data logger to record power used, but at this stage it does not include a voltage sensor, only current. The controls and display is via a web interface.

Discovered the OpenEnergyMonitor project, so my project is on indefinite hold.


Setting up the Raspberry Pi.

You might need to install “openssh-client” on your main computer, or something similar so that you can talk to the pi.
I used the default Raspian Linux distribution, but I would think the differences to any other Linux distribution would be minimal.
To find your Pi's IP address, at a terminal on a different computer type "ping raspberrypi". Hopefully ping will tell you the IP address. Ctrl + c to stop pinging.

Setup network folder access

If your main computer is Linux based, follow these directions

Install bcm2835.h

This is needed to access the hardware pins on the Broadcom 2835 or 2836 chip as used by the Raspberry Pi.

cd /home/pi/
tar zxvf bcm2835-1.51.tar.gz
cd bcm2835-1.51
sudo make check
sudo make install

The first program reads the analog to digital converter, and stores the values in a sqlite database.
There will only ever be one instance of this program running, it will be loaded as soon as the Pi starts, and it will run all the time.
The second program will interface the database to the web server, and keep the web page updated. There will be one instance of this program running for every web interface.
Dbus has been chosen for the programs to talk to each other.
It is easier to use C++ than C with Dbus, so the programs will be written in C++.

Install dbus-cxx stuff

sudo apt-get install libsigc++-1.2-dev
apt-get install libdbus-1-dev

Install the dbus-cxx.h libary.
sudo add-apt-repository ppa:rvinyard/ppa
sudo apt-get update

Install sqlite stuff

Handy web page:
This is better; and

gcc sqlite.c -lsqlite3

The rest of this was copied from my water project and needs updating.

Configure pi to have a static network address.

These instructions seem to be as good as any..
I used, but this is very flexible and depends on your home network setup.
Cycle power to the pi to make sure the pi boots up to the right IP address.

Update & load needed packages onto your Pi.

First, preferably in a new tab, ( File->New Tab ) type in..
$ ssh pi@
The default password is "raspberry".
This should give you a shell prompt from your pi.

Update the list of available software...
$ sudo apt-get update And we may as well do an upgrade while we are at it..
$ sudo apt-get upgrade
Install a web server...
Apache is not the only choice, and is a bit bigger and possibly slower than other options, but it is very mature and has lots of support and options.
$ sudo apt-get install apache2

Install php for the web server...
$ sudo apt-get install php5

Install a database...
This is a minimal database, we do not need anything fancy.
$ sudo apt-get install sqlite3

Install python...
$ sudo apt-get install python
Install a python interface for sqlite
$ sudo apt-get install python-pysqlite2
Install a php interface for sqlite...
I am not sure if all of these are needed, but this is what I used.
$ sudo apt-get install php5-sqlite php-db php-gettext wwwconfig-common

You might also like to install adminer.
I did not get it to work for me, but I did not try very hard.
$ sudo apt-get install adminer
Follow these instructions to install the wiring pi library.

Follow these instructions to setup the Wifi.

Follow these instructions to change the hostname to "power".

Set the raspberry timezone. apt-get install ntpdate To set the time zone. sudo raspi-config

Setup the database.

This basic database is stored in a single file, so we need to change to the directory where we want to use it, otherwise we will simply create multiple databases and wonder why our database or data is missing.
Change to the web publishing directory where this database file is stored...
$ cd /var/www/html If you put it somewhere else remember to edit all the files to point to the new location.
Then open a database terminal and create the database...
pi@retic:/var/www/html $ sqlite3 default.db Now we need to create a database table to hold the turning on times...
sqlite> CREATE TABLE table_time
seconds INT

And possibly add an entry or two...
sqlite> INSERT INTO table_time (zone, day, hour, min, seconds) VALUES (1, 0, 6, 1, 6);
INSERT INTO table_time (zone, day, hour, min, seconds) VALUES (2, 0, 6, 2, 12);
select * from table_time;

And a second table to keep track of monthly time adjustments...
sqlite> CREATE TABLE table_month
percent SMALLINT

and prefill it with data...
sqlite> INSERT INTO table_month (month, percent) VALUES (1, 130);
INSERT INTO table_month (month, percent) VALUES (2, 120);
INSERT INTO table_month (month, percent) VALUES (3, 110);
INSERT INTO table_month (month, percent) VALUES (4, 100);
INSERT INTO table_month (month, percent) VALUES (5, 90);
INSERT INTO table_month (month, percent) VALUES (6, 80);
INSERT INTO table_month (month, percent) VALUES (7, 70);
INSERT INTO table_month (month, percent) VALUES (8, 80);
INSERT INTO table_month (month, percent) VALUES (9, 90);
INSERT INTO table_month (month, percent) VALUES (10, 100);
INSERT INTO table_month (month, percent) VALUES (11, 110);
INSERT INTO table_month (month, percent) VALUES (12, 120);
select * from table_month;

You can inspect the tables if you like..
sqlite> .tables
shows a list of tables and
sqlite> .fullschema
shows the table structure.

If you think you stuffed up for any reason you can always drop a table and start again..
sqlite> drop table table_time;

and to quit from sqlite type...
sqlite> .quit

Download and install project files.

Download these files into a suitable local directory on your main computer.
css1.css gpio.php index.php script.js time.php

Then using a terminal, ( I am running KDE within Kubuntu, and use Konsole), change to the tab with the prompt from your main computer, and to the directory where you placed the files, then check you are in the right directory by typing ...
$ ls -l ( lower case L )
This should give you a list of files to be transferred to the pi. Then type..
$scp *.* pi@
The default password is “raspberry”
This will copy all the files with a file extension in the currant directory to the pi's web publishing directory.

Change back to the konsole tab with the Raspberry Pi prompt.
Change to the web publishing directory...
$ cd /var/www/html Then type
$ ls -l again to make sure the files are in the right place.

You will need to make all the python scripts executable...
$ chmod +x *.py If this does not work you can either try..
$ sudo chmod +x *.py or drop back one directory
$ cd .. And change permissions on the “html” directory
$ sudo chmod 777 html This second option is not recommended if the Pi will be accessible from the Internet.

To run “/var/www/html/” on startup I created “” which needs to go in “/etc/init.d/”
To make it executable...
$ sudo chmod +x *.sh Then move it...
$ sudo mv /etc/init.d/ Ideally it would be started last, but I do not know how to change the order and it seems to work as it is anyway.

You need to remove the original “index.html” file in /var/www/html so that the web server does not send you the wrong file.
pi@retic:/var/www/html $ rm index.html
At this point in time you should be able to point a browser, on a computer connected to the local network, at your pi's IP address and see a bunch of buttons ready to turn the outputs on and off. or just
If you click on the “Auto” button you should be able to change the times each zone turns on and runs for.

Local time zone

Hopefully the system time is the same as your local time, if not you may need to change the pi time zone. This can be done via raspi-config as shown on this web page...

Power Monitor design considerations.

The current transformers are available in two versions, with and without a shunt resistor. I have chosen the shunt resistor option because the current transformers are not hard wired, and if the current transformers are unplugged with current in the primary the secondary side will have a very high voltage on it, and this inbuilt shunt resistor will stop this voltage from getting to high.
The currant transformers are rated for 1 volt out for 30 amps in.
I will assume that the voltage will be greater if more than 30 amps is applied to the input, but the output will not be linear above 30 amps.
I am assuming that these values are all RMS, so 1 volt RMS x √2 becomes 1.414 volts peak and 2.828 volts peak to peak from the current transformer.

The maximum analogue input voltage of the ADS1256 chip is -0.1V min to AVDD + 0.1V. As the USB supply for both the analogue and digital is 5V, the maximum analogue input voltage is 5.1v.
One option to prevent the analogue input from going too low would be to short out the negative half cycle of the current transformer with a diode with less than 0.1 Volts forward voltage drop, and simply not measure the current drawn in the negative half cycle. This might matter if the load uses switch-mode power supplies as these may not draw balanced power from both half cycles. Computers, TV's, most 12v down lights, and a lot of other stuff uses switch-mode power supplies, so this is not the preferred option.
It is possible to introduce a DC offset to the input signal, but this is fiddly, and may leave the ADS1256 sensitive to damage if there is a short in the circuit being measured, or a brief surge such as a big motor turning on or similar.
Another way of reading the input is to use the differential mode of the ADS1256 chip, losing half the inputs but being able to read both half's of the cycle and less chance of damaging the ADS1256 if the currant goes too high. This is the preferred option because I am lazy, and it still gives me 4 inputs to work with, which for me should be enough. If I need more I may simply get another ADS1256 rather than try to give the input signals a DC offset. The ADS1256 has an adjustable gain, so I still have 24 bits of resolution.

The Voltage of the circuit I am measuring is of some interest, but as it is relatively constant, and my main interest is the current of each circuit I am not going to measure the Voltage. This may change at a later date as measuring the Voltage will also allow me to show the power factor.

======== To be deleted ==================

Build the Interface board.

Next we need to interface a 40 pin header from the Pi to a 10 pin relay board, and change the 3.3v signal from the Pi to the 5v that the relay board needs.
The quality of the prototype PCB I used is not that great, the plating is only on one side of the board, but it is good enough. Do not go smaller than 5cm x 7cm, the 40 pin header takes almost all of the 7cm length.
The pinout of the Pi header can be found in a few places including here...
And the pinout of the relay board and interface voltage adaptors can be found on the circuit board overlays..
Run links between the 40 pin header and the voltage adaptors, relay pin pin interface point.
I used a couple of jumpers for the longer runs, but mostly I used a single stand of copper wire stripped from the same cable running to the solenoids. You might be able to avoid jumper cables if you had double sided board.
I cut a 20cm female to female rainbow ribbon cable in half, and soldered one end to the interface board, but in hindsight it may have been better to use male to female ribbon cable, and solder the male end to the interface board. 10cm is ample.
I only used one side of the 40 pin header, which is why all my pin numbers are even.
In hindsight a bigger interface board would have been better, with provision for a rain sensor input and / or an IC2 temperature / humidity sensor.
By pure luck I needed 8 zones, which worked in nicely with the relay board and the voltage interface, but it should be easy to increase or decrease this number.

Plug it all in.

The 24vAC side of the controller should be simple enough, my setup included 3 banks of solenoids, so even using 2 wires as the return I still only needed 6 neutral terminals.

Other notes.

The manual web control is independent from the timers, except that there is a watchdog timer in that turns the water off after 60 minutes. Just in case you forget to turn it off.
You can toggle a pin directly by pointing your browser at gpio.php?pin=12, and it will return a list of what pins are on.
The database stores seconds run time, but the web interface shows time in minutes.

The included programs spend 99% ish of their time asleep, with a new thread being started each time the program starts a new timer, so you can have one or all of them on at the same time, and still count the correct seconds.
With no outputs on, my Pi draws 0.2 amps @ 5 volts, but with all 8 relays on it draws 0.83 amps @ 5 v.
I could not find a reference to the maximum currant the Pi can draw, but as my water pressure tends to dip when a solenoid comes on I only have one solenoid on at a time anyway.

========== End of deleted stuff ================= Back to Andrews page.