/
light_controller.py
86 lines (54 loc) · 1.6 KB
/
light_controller.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# sudo python3 /home/pi/Desktop/light_controller.py
import json, requests
import wiringpi as wirePi
import RPi.GPIO as GPIO
import time
#url = 'http://192.168.1.75/light_controller/index.php?need_for=json'
url = 'http://stardev.net/light_controller/index.php?need_for=json'
params = dict(
need_for='json',
asking_by='raspberry_pi',
)
response = requests.get(url)
data = json.loads(response.text)
print("red = "+str(data["red"])+" yellow = "+str(data["yellow"])+" blue = "+str(data["blue"]))
GPIO.setmode(GPIO.BCM)
red = 5
yellow = 6
blue = 13
while True:
time.sleep(5)
response = requests.get(url)
data = json.loads(response.text)
print("red = "+str(data["red"])+" yellow = "+str(data["yellow"])+" blue = "+str(data["blue"]))
if data["red"]=="1":
GPIO.setup(red, GPIO.OUT)
GPIO.output(red, GPIO.HIGH)
else:
GPIO.setup(red, GPIO.OUT)
GPIO.output(red, GPIO.LOW)
if data["yellow"]=="1":
GPIO.setup(yellow, GPIO.OUT)
GPIO.output(yellow, GPIO.HIGH)
else:
GPIO.setup(yellow, GPIO.OUT)
GPIO.output(yellow, GPIO.LOW)
if data["blue"]=="1":
GPIO.setup(blue, GPIO.OUT)
GPIO.output(blue, GPIO.HIGH)
else:
GPIO.setup(blue, GPIO.OUT)
GPIO.output(blue, GPIO.LOW)
#-------------------------------------------
'''
# input pin
button = 4
wirePi.wiringPiSetupGpio()
wirePi.pinMode(button, 0)
wirePi.pullUpDnControl(button, wirePi.PUD_DOWN)
while True:
if wirePi.digitalRead(button):
print ("Button ON")
else:
print ("Button OFF")
'''