From aa882297c3f9f8589d3d95fe2acbea84d3166db1 Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Sun, 24 Jan 2021 21:46:24 +0100 Subject: [PATCH] added script for collecting impulses and sending it to api, also included sample systemd service --- powermeter.py | 25 +++++++++++++++++++++++++ powermeter.service | 13 +++++++++++++ 2 files changed, 38 insertions(+) create mode 100755 powermeter.py create mode 100644 powermeter.service diff --git a/powermeter.py b/powermeter.py new file mode 100755 index 0000000..5f99547 --- /dev/null +++ b/powermeter.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 +import RPi.GPIO as GPIO +import time +import datetime +import requests + +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}) + +try: + GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=callback, bouncetime=500) + while True: + time.sleep(1) +except KeyboardInterrupt: + print("Exiting...") +GPIO.cleanup() diff --git a/powermeter.service b/powermeter.service new file mode 100644 index 0000000..f5f154b --- /dev/null +++ b/powermeter.service @@ -0,0 +1,13 @@ +[Unit] +Description=PowerMeter Service + +[Service] +Type=simple +User=root +ExecStart=/usr/local/bin/powermeter.py +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target +