added script for collecting impulses and sending it to api, also included sample systemd service
This commit is contained in:
parent
96e232d2c1
commit
aa882297c3
2 changed files with 38 additions and 0 deletions
25
powermeter.py
Executable file
25
powermeter.py
Executable file
|
@ -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()
|
13
powermeter.service
Normal file
13
powermeter.service
Normal file
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue