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 +