This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schalter.py
executable file
·63 lines (54 loc) · 2.1 KB
/
schalter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
#########################################################################
# Spaceapi Implementation for a 63A switch
# (using 5 V 150 mA Raspberry Pi GPIO pin)
#
#
#########################################################################
# to make sure that everything does not happen at once
import time
# json and raspi imports
import json
# for using GPIO pins of the raspi
import RPi.GPIO as GPIO
# for logging
import logging
from systemd import journal
########################################################
#start init
journal.write("CCCHB space api switch v. 0.3")
currentState = False
#################################################################################
#function definitons:
#################################################################################
# will read the GPIO header of the pi and write the current state in the www spaceapi json file
def switched(pos):
global currentState
wert_des_schalters = not bool(GPIO.input(pin_number))
if pos != wert_des_schalters:
journal.write("state changed to: %s " %wert_des_schalters)
chn_time = time.time()
icons = data.get("state").get('icon')
data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}})
currentState = wert_des_schalters
with open('/var/www/html/spaceapi.json', 'w') as outfile:
json.dump(data, outfile,sort_keys=True)
#################################################################################
#init GPIO pin on raspi
pin_number = 18; #set GPIO Pin number
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600)
journal.write("init complete... starting json foo")
#read json file
with open('spaceapi.json','r') as infile:
data = json.load(infile)
#run switched once to get current switch status and change if needed.
#switched(wert_des_schalters)
try:
while True:
time.sleep(5)
switched(currentState)
#do other stuff otherwise just don't do anything?!
except KeyboardInterrupt:
GPIO.cleanup()