skip to navigation
skip to content

Not Logged In

phase.currentcost 2.4.5

Python script to collect data from current cost EnviR

CurrentCost data collector module for phase project.

CurrentCost EnviR 128 is an appliance that monitor your electricity consumption and provide an API to retrieve data from USB cable.

phase.currentcost allow us to retrieve data from usb cable using pyserial and transmit information to stdout or to RabbitMQ using Pika module.

More precisly, this script collect instantaneous and historical data from Current Cost. It also take care of usb disconnection, Current Cost bad message format, timeout and power disconnection and send an error on stdout or on RabbitMQ each time for later anlysis. Also it create an udev rule to transform /dev/ttyUSB* to /dev/currentcost for serial port connection.

For more information, full documentation is available on readthedocs.org

Installation

Requirements:

  • Platform: Unix (only tested on Ubuntu 12.04)
  • Python 2.7
  • Pip (last version)
  • RabbitMQ (last version)

To install this software, you need to install python 2.7 and pip

sudo apt-get install python python-pip
sudo pip install --upgrade pip

Then if you want to use it, you can install and configure RabbitMQ (change “admin” and “password” by your credential)

USERNAME="admin"
PASSWORD="password"
echo "deb http://www.rabbitmq.com/debian/ testing main" > sudo /etc/apt/sources.list.d/rabbitmq.list
wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
sudo apt-key add rabbitmq-signing-key-public.asc
sudo apt-get update
sudo apt-get install rabbitmq-server -y
sudo service rabbitmq-server start
sudo rabbitmq-plugins enable rabbitmq_management
# If you get a 'command not found' error for this line, use:
# sudo /usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
# sudo /usr/lib/rabbitmq/bin/rabbitmq-activate-plugins enable rabbitmq_management
sudo rabbitmqctl add_user $USERNAME $PASSWORD
sudo rabbitmqctl set_permissions -p / $USERNAME ".*" ".*" ".*"
sudo rabbitmqctl delete_user guest
sudo service rabbitmq-server restart

Then you can install phase.currentcost

sudo pip install phase.currentcost

To verify version of phase.currentcost installed package

pip show phase.currentcost

Usage

$ sudo phase-currentcost -h
usage: phase-currentcost [-h] [-t TTY_PORT] [-r RABBITMQ_CREDENTIAL]
                         [-l LOG_CONF] [-v] [--ch1-w CH1_W] [--ch2-w CH2_W]
                         [--ch3-w CH3_W] [--tmpr TMPR] [-d DATA_FOLDER]
                         variable_id site_id

positional arguments:
  variable_id           Unique device identifier
  site_id               Unique location identifier

optional arguments:
  -h, --help            show this help message and exit
  -t TTY_PORT, --tty-port TTY_PORT
                        tty port to connect to current cost
  -r RABBITMQ_CREDENTIAL, --rabbitmq-credential RABBITMQ_CREDENTIAL
                        credential for rabbitMQ. By default, RabbitMQ is
                        deactivated. To activate it you have to give your
                        credential. Format: username:password.
  -l LOG_CONF, --log-conf LOG_CONF
                        path to log configuration
  -v, --verbose         activate verbose mode
  --ch1-w CH1_W         Name of the variable in W powered by ch1
  --ch2-w CH2_W         Name of the variable in W powered by ch2
  --ch3-w CH3_W         Name of the variable in W powered by ch3
  --tmpr TMPR           Name of the variable in °C powered by tmpr
  -d DATA_FOLDER, --data-folder DATA_FOLDER
                        Name of the folder where store data

By default:

  • We targetting /dev/currentcost as tty port. You can over-write it using –tty-port argument.
  • RabbitMQ is not activated. We send message to stdout so you can collect it on file. If you want to share your message over the network using RabbitMQ, you can activate this function using –rabbitMQ-credential argument and giving your credential following this format: username:password.
  • Log configuration file is located in /opt/phase/phase-currentcost.conf and log file is in /var/logs/phase/phase-currentcost.log. You can set log configuration file using -l option with a path to your log.conf file.

Examples

To see the current consumption on Current cost on stdout use:

$ sudo phase-currentcost electric_meter liogen_home --tty-port /dev/currentcost
{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:17.754959", "value": "CurrentCost electric_meter in liogen_home: TTY connection problem: /dev/currentcost is unreachable. Retry connection in 5 seconds.", "nonDstTimezone": "UTC"}
{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "CurrentCost electric_meter in liogen_home: TTY connection problem: /dev/currentcost is unreachable. Retry connection in 5 seconds.", "nonDstTimezone": "UTC"}
{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "<msg><src>CC128-v1.29</src><dsb>00786</dsb><time>00:31:36</time><tmpr>19.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00405</watts></ch1></msg>", "nonDstTimezone": "UTC"}

With rabbitMQ message over the network and verbose mode activated:

$ sudo phase-currentcost electric_meter liogen_home --tty-port /dev/currentcost --rabbitmq-credential admin:password -v
Starting current cost application
Current time: 2014-04-25 12:01:34.350781
Variable name: electric_meter
Site name: liogen_home
TTY port: /dev/currentcost

{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "<msg><src>CC128-v1.29</src><dsb>00786</dsb><time>00:31:36</time><tmpr>19.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00405</watts></ch1></msg>", "nonDstTimezone": "UTC"}
{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "<msg><src>CC128-v1.29</src><dsb>00786</dsb><time>00:31:36</time><tmpr>19.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00405</watts></ch1></msg>", "nonDstTimezone": "UTC"}

If you want to send timeseries formatted message over RabbitMQ:

$ sudo phase-currentcost electric_meter liogen_home --tty-port /dev/currentcost --rabbitmq-credential admin:password -v --ch1-w main_consumption --tmpr living_room_temperature
Starting current cost application
Current time: 2014-04-25 12:01:34.350781
Variable name: electric_meter
Site name: liogen_home
TTY port: /dev/currentcost

{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "<msg><src>CC128-v1.29</src><dsb>00786</dsb><time>00:31:36</time><tmpr>19.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00405</watts></ch1></msg>", "nonDstTimezone": "UTC"}
{"variableID": "main_consumption", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "405", "nonDstTimezone": "UTC"}
{"variableID": "main_consumption_kwh", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "0.0", "nonDstTimezone": "UTC"}
{"variableID": "living_room_temperature", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "19.3", "nonDstTimezone": "UTC"}
{"variableID": "electric_meter", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "<msg><src>CC128-v1.29</src><dsb>00786</dsb><time>00:31:36</time><tmpr>19.3</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00405</watts></ch1></msg>", "nonDstTimezone": "UTC"}
{"variableID": "main_consumption", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "405", "nonDstTimezone": "UTC"}
{"variableID": "main_consumption_kwh", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "0.000123", "nonDstTimezone": "UTC"}
{"variableID": "living_room_temperature", "dstTimezone": "UTC", "siteID": "liogen_home", "date": "2014-04-25T12:00:22.769256", "value": "19.3", "nonDstTimezone": "UTC"}

Message send through RabbitMQ

A message is a JSON containing this properties:

variableID:Unique device identifier
siteID:Name of the site
date:Date in UTC
dstTimezone:Timezone with DST
nonDstTimezone:Timezone without DST
message:Message to deliver through RabbitMQ

Messages list:

Channel Message Description
error utils.TTY_CONNECTION_PROBLEM Send when TTY port is not reachable
error utils.CURRENTCOST_TIMEOUT Send when TTY port is connected but reach timeout
error utils.CC_INCORRECT_MESSAGE Send when Currentcost send an invalid message
currentcost CurrentCost XML message Send Currentcost XML message

Contribute

I am more than happy to accept external contributions like feedback, bug reports and pull requests.

Do not hesitate to post an issue if you have any problem to install or to use this software.

You can also use this way to ask for features request. I am also available to answer you on Stack Overflow

Here is the development process to test and validate your features.

  1. Prepare your development environment:

    # `Socat <http://www.dest-unreach.org/socat/>`_ is usefull to create socket connection between 2 files.
    $ sudo apt-get install socat vim git-core
    # Install virtualenv and virtualenvwrapper if it's not done.
    $ sudo pip install virtualenvwrapper
    $ mkdir ~/.virtualenvs
    $ vim ~/.bashrc
    # Modify your ~/.bashrc and add this 2 lines:
    # export WORKON_HOME=~/.virtualenvs
    # source /usr/local/bin/virtualenvwrapper.sh
    $ bash
    
  2. Go on github and fork this project.

  3. Clone it on your conputer:

    $ cd <your_workspace>
    $ git clone git@github.com:<username>/phase.currentcost.git
    $ git checkout develop
    
  4. Prepare your virtualenv

    $ mkvirtualenv phase
    (phase)$ pip install paver
    (phase)$ paver prepare
    
  5. Add functional or unit tests

  6. Code your features

  7. To validate your implementation, launch:

    (phase)$ paver validate
    
  8. Modify Todo, Changelog and update documentation

  9. Commit and push on github:

    (phase)$ git add .
    (phase)$ git commit -a -m "<your commit message>"
    (phase)$ git push origin develop
    
  10. Propose a pull request on github

License

The MIT License (MIT)

Copyright (c) 2014 Pierre Leray

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 
File Type Py Version Uploaded on Size
phase.currentcost-2.4.5.tar.gz (md5) Source 2014-11-14 13KB
  • Downloads (All Versions):
  • 201 downloads in the last day
  • 1125 downloads in the last week
  • 3740 downloads in the last month