diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..277fcb0 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +INFLUXDB_TOKEN="" +INFLUXDB_ORG="" +INFLUXDB_URL="http://localhost:8080" +INFLUXDB_BUCKET="electricity" +INFLUXDB_ENABLED=True +FLASK_APP_URL="http://localhost/impulses" +FLASK_APP_ENABLED=True diff --git a/.gitignore b/.gitignore index 25923b5..a137123 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ pip-selfcheck.json # custom tags +.env diff --git a/README.md b/README.md index 89c594b..437bdd1 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,39 @@ # powermeter -Simple Flask based application to receive impulses via cURL from a power meter. + +A simple Pyhton script to mesaure you electricity consumption using the LED on a electricity meter and a digital light sensor. + +You either can use InfluxDB to store the data or a built in Flask application. + +## Hardware setup + +- Raspberry Pi +- Digital light sensor like this: https://www.berrybase.de/lichtsensor-mit-digitalem-ausgang + +Connect the light sensor to your Pi: + +``` +VCC: 5V +DATA: GPIO 27 +GND: GND +``` + +## Software setup + +- Copy the `.env.example` to `.env` and change it to your needs. +- Acivate at least InfluxDB (which I would recommend) or Flask output. Clone this repository and create a virtualenv within it: ``` -git clone https://github.com/Finn10111/powermeter.git +git clone https://code.f2n.me/finn/powermeter.git cd powermeter -virtualenv -p /usr/bin/python3 . -# or if python3 is your default: virtualenv . pip install -r requirements.txt ``` +## Developing (Flask app) + For developing run this (you need to create database and user first): ``` @@ -20,3 +41,18 @@ export FLASK_ENV=development export APP_DEVELOPMENT_DATABASE_URI=postgres://username:passwort@hostname/database flask run ``` + +## Screenshots + +Node-RED usage example: + +![Node-RED Dashboard](/screenshots/nodered.png) + +Grafana Dashboard example (InfluxDB datasource): + +![Grafana Dashboard](/screenshots/grafana.png) + + + + + diff --git a/powermeter.py b/powermeter.py index 5f99547..9d425ef 100755 --- a/powermeter.py +++ b/powermeter.py @@ -3,21 +3,55 @@ import RPi.GPIO as GPIO import time import datetime import requests - + +import influxdb_client, os +from influxdb_client import InfluxDBClient, Point, WritePrecision +from influxdb_client.client.write_api import SYNCHRONOUS + + +if os.getenv("FLASK_APP_ENABLED", False).lower() == "true": + flask_app_enabled = True + flask_app_url = os.getenv("FLASK_APP_URL") +if os.getenv("INFLUXDB_ENABLED", False).lower() == "true": + influxdb_enabled = True + token = os.environ.get("INFLUXDB_TOKEN") + org = os.environ.get("INFLUXDB_ORG") + url = os.environ.get("INFLUXDB_URL") + bucket = os.environ.get("INFLUXDB_BUCKET") + client = influxdb_client.InfluxDBClient(url=url, token=token, org=org) + write_api = client.write_api(write_options=SYNCHRONOUS) + SENSOR_PIN = 27 - GPIO.setmode(GPIO.BCM) GPIO.setup(SENSOR_PIN, GPIO.IN) - + seconds = 0 + def callback(channel): print(str(datetime.datetime.now()) + ' Impulse detected') - url = 'http://powermeter.local/impulses' - requests.post(url, json = {'power': 2}) + if flask_app_enabled: + flaskapp() + if influxdb_enabled: + influxdb() + + +def flaskapp(): + requests.post(flask_app_url, json={'power': 2}) + + +def influxdb(): + point = ( + Point("electricity") + .field("consumption", 2) + ) + write_api.write(bucket=bucket, org="finn", record=point) + + +influxdb() try: - GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=callback, bouncetime=500) + GPIO.add_event_detect(SENSOR_PIN, GPIO.RISING, callback=callback, bouncetime=500) while True: time.sleep(1) except KeyboardInterrupt: diff --git a/screenshots/grafana.png b/screenshots/grafana.png new file mode 100644 index 0000000..ed426ca Binary files /dev/null and b/screenshots/grafana.png differ diff --git a/screenshots/nodered.png b/screenshots/nodered.png new file mode 100644 index 0000000..b1ac820 Binary files /dev/null and b/screenshots/nodered.png differ