Raspberry Pi project for logging OCR-ed readings from an energy meter.
This script is used to recognize readings from an energy meter like this:
and log them to Google Sheets for further analysis.
The project is merely an exercise with OpenCV and OCR, so the code could be helpful in situations when there's no other interfaces to get the readings. For more reliable setup it's probably better to use wired connections like P1 port or an optical interface. There's a number of projects that deal with IR readers:
- https://github.com/golles/ha-kamstrup_403
- https://github.com/matthijsvisser/kamstrup-402-mqtt
- https://github.com/gertvdijk/PyKMP
In terms of the hardware the code has been tested on Raspberry Pi Zero W with ZeroCam module and the setup looks pretty much like this:
For highlighting the scene 2 clear LEDs are used that work at 3.1V and draw 20mA. To turn them on we'll output a 3.3V signal on GPIO pins. Since the working voltage of an LED is lower we need to drop 3.3V down to 3.1V using dropping resitor. In order to calculate resistor parameters I used this calculator which gave me 10 ohm. More about controlling LEDs via GPIO.
The image processing and readings extraction algorithm with experiments can be found in this Jupyter notebook. In order to run the notebook we'll need Python 3 and tesseract installed. My development box is running Mac OS, so the following steps will assume it:
user@macos$ brew install tesseract
It is advisable to install virtualenv and create new isolated environment in the project difrectory:
user@macos$ virtualenv venv
user@macos$ source venv/bin/activate
Then run pip to install development dependencies:
user@macos$ pip install -r requirements.txt
After that one should be able to run included Jupyter notebook in an environment of choice, for instance in VS Code.
To run the energy logger code on Raspbian OS we need to install a number of dependencies. First, we need OpenCV dependencies and tesseract OCR engine. Note, that all the commands prefixed with user@raspberrypi
will need to be executed on Raspberry Pi:
user@raspberrypi$ sudo apt-get update
user@raspberrypi$ sudo apt-get install python3-opencv tesseract-ocr
Then create energy-logger
user for running the app and add it to gpio
and video
groups:
user@raspberrypi$ sudo adduser energy-logger
user@raspberrypi$ sudo adduser energy-logger gpio
user@raspberrypi$ sudo adduser energy-logger video
Now, copy the app
directory to the home directory of the energy-logger
user. On the host computer run:
user@macos$ scp -r ./app [email protected]:~
[email protected]'s password:
Type energy-logger
's password when asked. After command completes the app
directory should appear in energy-logger
user home directory. Note that it's rather bad security practice to allow ssh password authentication, but for the sake of simplicity we use Raspbian defaults here. Read here to learn how to disable SSH password authentication.
And then we can install energy logger Python dependencies (on the Raspberry Pi):
user@raspberrypi$ sudo su energy-logger
energy-logger@raspberrypi$ cd ~/app
energy-logger@raspberrypi$ pip install -r requirements.txt
Check this guide for installing camera on Raspberry Pi.
Additionally, you'll need to create new Google API project, create a service account and retrieve Google Sheets API access credentials in order to be able to log readings to a spreadsheet. Follow the guide here and save credentials to credentials.json
file and put it to the app
directory on Raspberry Pi. Also, don't forget to lock down file permissions (those are secrets after all):
energy-logger@raspberrypi$ chmod 600 credentials.json
Now, we need to schedule the logger to be executed let's say once every hour. To do that add the following line to /etc/crontab
:
10 * * * * energy-logger SHEET_ID="{YOUR-SHEET-ID}" SHEET_RANGE="{YOUR-SHEET-RANGE}" /home/energy-logger/app/energy_logger.py 2>&1 | /usr/bin/logger -t energy-logger
where {YOUR-SHEET-ID}
can be obtained from a Google Sheets document URL and {YOUR-SHEET-RANGE}
should be something like Sheet1!A1:B1
.
Tesseract traineddata for recognizing Seven Segment Display was taken from this great repository