Updates! InfluxDB and .env support

This commit is contained in:
Finn Christiansen 2024-10-08 22:04:32 +02:00
parent 0df6f5956f
commit 5900fec8ff
6 changed files with 88 additions and 10 deletions

7
.env.example Normal file
View file

@ -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

1
.gitignore vendored
View file

@ -69,3 +69,4 @@ pip-selfcheck.json
# custom
tags
.env

View file

@ -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)

View file

@ -4,17 +4,51 @@ import time
import datetime
import requests
SENSOR_PIN = 27
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)

BIN
screenshots/grafana.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

BIN
screenshots/nodered.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB