I want to log my house electrical usage since long time ago, but somehow some hardware and time is not allow. Until recently, I bought a power meter from Taobao, single phase max. 65A input. It's a LCD power meter with lot of information including voltage, ampere, power, power factor, frequency, and
energy. On top of that, the power meter has S0 pulse output for me to connect into my Pi for power monitoring and logging.
I had put this meter for more than a month due to my workload, and until yesterday i used my New Year holiday to install this power meter into very packed distribution board. Pull the signal cable from power meter to my Pi. The connection diagram as follow.
I had try few different value of resistor and I found that when the resistor is high e.g. 10kohm, there are lot of false trigger and causing the pulse count meaningless. After try and error, I found that 330ohm resistor is the most suitable value, it is high enough to prevent overburden, and it is low enough to prevent false trigger.
I use GPIO 23 as my input cause it got internal pull-up resistor to make the circuit more easier.
Until this moment, the hardware is ready. The rest is more on the software.
First, need to have Pi code to analyze this input and post to internet. Then the second step is to have portal to record the data and visualization.
After searching around, I found code from Edward O'Regan and KieranC is a good place to start. I didn't modify much of the quote except the json post url, which I will explain later. The document can get from
GitHub, it give pretty much all you the requirements you needed. Once you have setup the program in Pi, then replace the code monitor.py with this one.
#!/usr/bin/python
"""
****** 2015/01/01 *******
Modified by SK to post power data to EmonCMS portal using EmonCMS API.
*************************
Modified by KieranC to submit pulse count to Open Energy Monitor EmonCMS API
Power Monitor
Logs power consumption to an SQLite database, based on the number
of pulses of a light on an electricity meter.
Copyright (c) 2012 Edward O'Regan
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.
"""
import time, os, subprocess, httplib, datetime
from apscheduler.scheduler import Scheduler
#Uncomment the following 3 lines if the code didn't work
#import RPi.GPIO as GPIO
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# The next 2 lines enable logging for the scheduler. Uncomment for debugging.
#import logging
#logging.basicConfig()
pulsecount=0
power=0
# Start the scheduler
sched = Scheduler()
sched.start()
# This function monitors the output from gpio-irq C app
# Code from vartec @ http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output
def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
yield line
if(retcode is not None):
break
#Pulse in monitoring
#GPIO.wait_for_edge(23, GPIO.FALLING)
#pulsecount += 1
# Every minute this function converts the number of pulses over the last minute into a power value and sends it to EmonCMS
@sched.interval_schedule(minutes=1)
def SendPulses():
global pulsecount
global power
#print ("Pulses: %i") % pulsecount # Uncomment for debugging.
# The next line calculates a power value in watts from the number of pulses, my meter is 1600 pulses per kWh, you'll need to modify this if yours is different.
power = pulsecount * 37.5
#print ("Power: %iW") % power # Uncomment for debugging.
pulsecount = 0;
timenow = time.strftime('%s')
url = ("/input/post.json?time=%s&node=1&json={power:%i}&apikey=<YOUR API KEY>") % (timenow, power)
# You'll need to put in your API key here from EmonCMS
connection = httplib.HTTPConnection("emoncms.org")
connection.request("GET", url)
for line in runProcess(["/usr/local/bin/gpio-irq", "23"]):
pulsecount += 1
#print ("Count: %i") % pulsecount
Since you are going to post the data to EmonCMS portal, therefore, you do not need to setup the EmonCMS on you Pi.
The next step is to create an account with
EmonCMS. After that, copy the write API key and paste it to the code and you are ready to monitor your power consumption online.
You can show off your newly setup power monitoring like this
Somehow, Pi is not very good in real-time data collecting, as you can see in the chart, it will miss some schedule and causing data is very odd. This will be my next version of code to eliminate it.