Skip to content

Instantly share code, notes, and snippets.

@HarvsG
Last active August 28, 2024 08:47
Show Gist options
  • Save HarvsG/5c25edac569b0fa7d7674a5e4c144a9e to your computer and use it in GitHub Desktop.
Save HarvsG/5c25edac569b0fa7d7674a5e4c144a9e to your computer and use it in GitHub Desktop.
Getting Load data from current cost energy meter.

You will need:

  • Raspberry pi - any generation will do, RPI Zero with GPIO would suffice.
  • A current cost Envi or Envir Smart Meter
  • And old ethernet patch cord
  • Some method to connect wires to the GPIO pins on the pi (solder, caps, bread-board or nothing at all)
  • contrary to a lot of information you do not need a USB to serial adapter or a current cost data cable

Software that we will be using

No need to download yet

Connecting the cable to the GPio directly on the PI

Taken from this tutorial

  1. Cut the ethernet cable in half, leaving a reasonable length on each end.

  2. Strip wires 1 and 4 (solid brown and solid blue for me).

    RJ45 pins are numbered on the male connector end-on from left to right with the plastic latch pointing UP

  3. Using any preferred method connect the Wire 1 (brown) from the ethernet to the Serial Rx pin for my pi, this was physical pin 10.

  4. Connect wire 4 (blue) to any of the ground pins

  5. Plug the other end (the RJ45) into the currentcost.

  6. Now on a pi with a Rasberry Pi OS install the pi run sudo raspi-config

  7. Select 5 Interfacing Options, then select P6 Serial.

  8. Disable 'login over serial' and enable the hardware serial

  9. Restart you pi with sudo shutdown -r now

  10. Soon aftere boot run dmesg | grep tt

  11. Look for a line like [ 1.049107] 20201000.serial: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2 make a note of ttyAMA0 or whatever it is for you

    • This is our serial device from the GPIO, we will continue to use it on this tutorial.
  12. cd ~ then sudo nano serial_read.py and paste in:

#!/usr/bin/env python
# note that this is a python 2 script
import time
import serial
import sys
if sys.version_info >= (3,0):
    print("Sorry, the rest of this tutorial requires Python 2.x, not Python 3.x")
    print("You ran this script with version: " + str(sys.version_info))
    sys.exit(1)

ser = serial.Serial(
        port='/dev/ttyAMA0', #you will need to change this if you got different results in 11
        baudrate = 57600, #for some earlier CurrentCost devices this was 9600
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=3
)

while 1:
        x=ser.readline()
        print x
        
  1. sudo apt install python-pip -y && sudo pip install pyserial
  2. sudo python serial_read.py wait a good 30seconds and see if any xml rolls in

Installing measureit the very short way

  1. confirm the installs will work by running them manually before the script does sudo apt-get update && apt-get install -y git vim daemontools-run php7.3-fpm php7.3-mysql nginx samba samba-common-bin python-mysqldb python-serial python-setuptools
  2. cd /home/pi, git clone git://github.com/HarvsG/measureit.git
  3. Run the install script with sudo /home/pi/measureit/measureit_system_files/install/install-measureit-image.sh
  4. If the device name found in step 11 above is anything other than ttyAMA0 or ttyUSB0 then: -sudo nano /web/measureit/measureit_system_files/python/data-input.py change ttyAMA0 to whatever you found above.
  5. Navigate to the webpage (the IP of the raspberry pi) and enter your settings.
    1. first step is to open the inspector and switch to the log page - look out for 500 server errors, which indicates changes have not been made.
    2. set your PVoutput data in the system settings.
    3. Then if your sensor has multiple clamps, add each of these, usually 3
    4. for each of the clamps and sensor go to their settings page and fill in each box, you can leave the PVOutput API Key: blank as you have set it in system settings,
    5. For the PVOutput System ID: set the clamps to 0 and the sensor to your pv output ID e.g 45567.
    6. For the timezone as far as I can tell measureit will use the raspberry pi's GMT/UTC and ignore any DST so you need to specify any offset from UTC here. You will also need to update it if DST changes
  6. Navigate to the IP of the pi in your browser, settings and click grabber restart.
  7. When you navigate to the IP address of your pi you should see the KW usage updating regularly, and PV output updating

Debugging

Also see here

  • most errors seem to be in database quieries, turn on logging with sudo mysql, SET GLOBAL general_log_file = '/var/run/mysqld/mysqld.log';,SET GLOBAL general_log = 'ON'; and sudo tail -f /var/run/mysqld/mysqld.log although this won't show errors you can copy and paste the queries (via a text editor, removing indents) to the mysql terminal and see what happens
  • remember to SET GLOBAL general_log = 'OFF'; when you are done
  • sudo tail -f /tmp/measureit.log show the output of the python grabber
  • sudo svc -d /service/measureit, sudo /usr/bin/python /web/measureit/measureit_system_files/python/data-input.py debug will provide a more verbose output to the log file, (and will print the xml rolling in). sudo svc -u /service/measureit to restart the normal grabber service when you are finished.

Installing measureit the long way

  1. sudo apt update && sudo apt upgrade -y

  2. sudo apt install apache2 mariadb-server git python-mysqldb python-serial python-setuptools

  3. sudo python /usr/lib/python2.7/dist-packages/easy_install.py pip, sudo pip install twython==2.7.0 requests requests_oauthlib

  4. sudo bash, mkdir /var/www, mkdir /var/www/measureit

  5. cd /var/www/measureit, git clone git://github.com/lalelunet/measureit.git . include the .

  6. cd /var/www/measureit/measureit_system_files/install, sudo service mysql start, cat createdb.sql | mysql -uroot -p

    You will be asked for a password, easiest to give the login password of your pi

  7. Exit exit

  8. Now type the local IP address of your pi into a browser on another device, in my case 192.168.1.100, you should see the apache 'It works!' page

  9. sudo nano /etc/apache2/sites-available/000-default.conf

    change DocumentRoot /var/www/html to DocumentRoot /var/www/measureit/measureit_public_html/

  10. sudo service apache2 restart

  11. sudo mkdir -p /package, sudo chmod 1755 /package, sudo wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz

  12. sudo tar xvzf daemontools-0.76.tar.gz, cd admin/daemontools-0.76/src, sudo perl -pi -le 's/extern int errno;/#include <errno.h>/' *.[hc]

  13. sudo ../package/install, sudo mkdir /etc/servers, sudo mkdir /etc/servers/measureit

  14. sudo cp -R /var/www/measureit/measureit_system_files/install/svc-run-measureit /etc/servers/measureit/run, chmod +x /etc/servers/measureit/run

  15. cd /service, sudo ln -s ../etc/servers/measureit

Bodging a cable

This was the old plan but is mostly redudant

Pin pairings on RS232 Serial port connector. Pins are numbered as you look at the male connector end on with the row of 5 on top. Transmit/recieve are from the perspective of the PC, not the Current cost unit.

9-pin pin definition Direction (PC view)
1 DCD (Data Carrier Detect) input
2 RX (Receive Data) input
3 TX (Transmit Data) output
4 DTR (Data Terminal Ready) output
5 GND (Signal Ground) -
6 DSR (Data Set Ready) input
7 RTS (Request To Send) output
8 CTS (Clear To Send) input
9 RI (Ring Indicator) input

RJ45 Pinout for serial connection to the CurrentCost Envi. RJ45 pins are numbered on the male connector end-on from left to right with the plastic latch pointing UP. The colours may not be reliable.

Pin Colour Definition
4 Blue Ground
2 Brown/white Current cost recieve
1 Brown Current cost Transmit

So pin pairings will be:

RJ45 pin RS232 pin
1 (Tx) 2 (Rx)
5 (GND) 5 (GND)
2 (Rx) 3 (Tx)

The final pairing is probably uneccessary as some have said that the Envi is transmit only. However according to the Current Cost website, depending on the unit firmware, it may store some historical data. Presumably this would require some send information.

Other Guides/ links:

@HarvsG
Copy link
Author

HarvsG commented Nov 19, 2019

Screenshot 2019-11-19 at 14 18 53

@HarvsG
Copy link
Author

HarvsG commented Nov 19, 2019

@RoganDawes
Copy link

I have made a very simple WiFi interface to my CurrentCost receiver using an ESP07, and esplink firmware, if anyone is interested. In my setup, I realised that the provided 3v3 power supply was inadequate to power the ESP8266 as well as the receiver, so I bodged in a AMS1117 linear regulator between the DC input jack and the rest of the receiver (cutting a track in the process), then wired the 3v3 line to a pin of the RJ45 connector. I did a minimal wire-up of the ESP07, with pull-up and pull-down resistors as required to make it run the firmware appropriately. I then connected the Vcc, Gnd, RX and TX pins of the ESP07 to an RJ45 jack on a very short pigtail, so that the entire thing just fits under the base of the receiver.
Running esplink firmware allows me to expose the serial data to the network via telnet or netcat. I then made use of xml2json (https://github.com/Cheedoong/xml2json) to allow posting the data to MQTT, and from there to Home Assistant:

nc currentcost.lan 23 < /dev/null | while read line; do
	printf "%s\n" $line | ./xml2json | \
		mosquitto_pub -h mqtt_server.lan -t 'tele/currentcost/SENSOR' -s
done

The intention is to move to a custom build of Tasmota, which has native support for reading from the serial port and posting that to an MQTT server, I will just need to include the xml2json conversion step first.
Let me know if any of this is of interest, and I can provide more details.

@mande01
Copy link

mande01 commented Nov 4, 2022

I've got one of these energy monitors. But I've no power supply for the display, do you know what the voltage and amp is?

@RoganDawes
Copy link

RoganDawes commented Nov 5, 2022 via email

@ae1grij
Copy link

ae1grij commented Oct 7, 2023

I've been using the CurrenCost setup with Measureit for many years and overall has been a great success. However I have always been a bit suspect of the accuracy of the clamps supplied. I have an alternate system using a Shelly EM module which outputs a more accurate value for both Grid and Solar outputs. I use HA to integrate the data into the Energy dashboard. I've gone a step further to use Node Red to send these data to PV Output with mixed results. Has anyone had any experience regards replacing the clamp outputs of the Current cost with those from the Shelly? Would be interested to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment