Making a datalogger for a Kamstrup electricity / gas meter with Raspberry Pi!
About Raspberry Pi
So finally I received my new Raspberry Pi. This is a small, low power ARM linux board featuring a ethernet adapter, 2 USB ports and even a full-HD HDMI port for a mere $30!. Check out www.raspberrypi.org for more information.
After running XBMC on it (no mpeg2 decoding, lame) and trying it out as a NAS (no SATA interfaces, sloooow) i decided to use it as a combined hardware / software project.
Sparks all around
The energy company here in The Netherlands made the mistake to install a Kamstrup 162 J series meter in my home with an mysterious looking phone jack on it. The phone jack kept intriguing me day after day. A small investigation on the internet indicated it to be a serial interface to get the meter counters and LOTS more information.
This instructable describes all the hardware and software I used to create this interface. The end result will be nice looking graphs on cosm, a free data collecting service found on the internet.
What it will give you
At least you will have fun making this interface. It will teach you about the raspberry pi, compiling a C program, interfacing to your FTDI board. And when things start to work: ultimate satisfaction :^)
And last but not least: it wil make you aware of the footprint you leave on this earth. I wanted to understand how much resources our household uses and in time, reduce the amount of gas and electricity we use.
What you will need
* An electrical meter fitted with a P1 port. In my case this is a Kamstrup 162, other Kamstrups will most likely work in the same manner.
* Linux commandline experience.
* Some basic TTL electrical knowlegde.
* A working Raspberry Pi;
* A FTDI cable or breakout board (as found on Sparkfun);
* A mini-usb cable.
PS: Because Raspberry Pi is a long word, I will (lovingly) refer to it as rPi…
Step 1: Preparing the rPi
Enter the Pi
I have fitted the rPi with a 8G class 4 sdcard containing the standard Debian ARM distribution of the raspberry pi foundation.
This can be found at http://www.raspberrypi.org/downloads. Write the default debian distribution to the sdcard using the dd command (patience!) and make sure the device boots correctly. Also check if it runs stable (the voltage of some power sources can be too much off-spec to drive the rPi in a stable manner) and connects to your network correctly.
I like to use SSH all the time to login into the rpi. I have included a sample session of connecting to the rPi using SSH:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu May 31 20:02:23 2012 from chris-xps15.home
If you use Windows, Putty is a good alternative to the Linux SSH shell.
Because we are doing some hardcore serial communications, I like to have minicom around. This can be installed by issuing the “apt get install minicom” command. Start it by issuing the “minicom” command. If you are as old as me and have owned one of those pre- internet modems, it should remind you of those wonderful years…
Our rPi is now completely configured to get things going!
Step 2: Connecting the FTDI cable to the rPi
The FTDI cable allows us to use the USB port on the rPi to communicate with an RS232 interface. It will connect to one of the rPi USB ports and the other end will connect to the Kamstrup meter.
Inverting the RXD
As the protocol spoken by the Kamstrup is inverted, We have to configure the FTDI cable to invert the RXD pin. This can be done on a Windows machine (it is possible on Linux but the tooling is extremely flaky) using the FT_PROG tool found here.
Making the physical connection
To successfully connect to the meter, I have used a telephone RJ type cable (which should have 4 wires, please check as some cheaper cables only have 2 inside) which I cut into one 20cm end.
Strip the end of the cable and make sure the wires are nicely enclosed with soldering tin. This will allow us to stick them in the FTDI board without using a special header. If you want, you can solder the wires to a 4-pin header.
The pins on the ftdi cable are labelled (at least on mine)
I have numbered the pins on the meter from 1-4. This is viewes when standing right before the meter, 1-4 from left to right. The RJ clip is located at the bottom of the connector.
FTDI Kamstrup P1
DTR —> 1
GND —> 2
RXD —> 4
When you have made all the connections, the rPi should have the USB cable with the FTDI attached to it and the FTDI is hooked up to the meter as described above.
Step 3: And now for some software…
Reading the data from the meter
I have attached a small C program (cryptically called ft) which will read out the FTDI directly. This program is nothing fancy, it will:
1. Lower the DTR to cause the meter to give us the precious information
2. Read until there is no data for 2 seconds
3. Print everything on the screen
To compile this on the rPi, install “ftdilib-dev” on the rPi using “sudo apt-get install libftdi-dev“, copy the tar file to the rPi (under the root account in the /root directory to keep it simple), unpack using tar -xvf datacollector.tar and then issue the “make” command in the /root directory.
If everything goes as planned, the compiler will give us the ft program. If you start this program (enter ./ft and press enter), a block of information is shown like:
If no correct data is shown, the connections to the energy meter are probably not correct. Check them again! If the ft program bombs out with a message that the device cannot be opened: