-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (56 loc) · 2.09 KB
/
main.py
File metadata and controls
72 lines (56 loc) · 2.09 KB
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
"""
Read water level from a water level sensor and turn on or off a light, depending on the reading
"""
import json
import time
import RPi.GPIO as GPIO
THRESHOLDS = {}
LIGHT_CHANNEL = 27
SENSOR_CHANNEL = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(LIGHT_CHANNEL, GPIO.OUT)
GPIO.setup(SENSOR_CHANNEL, GPIO.IN)
def wait(seconds=10):
for second in range(seconds, 0, -1):
time.sleep(1)
print(f"{second}.")
def set_thresholds():
if not THRESHOLDS:
try:
with open("thresholds.json") as f:
thresholds = json.load(f)
THRESHOLDS.update(thresholds)
except (FileNotFoundError, json.decoder.JSONDecodeError):
print("Thresholds not set")
if all(_ in THRESHOLDS for _ in ("high", "low", "med")):
return
input("Press enter when the water sensor is connected and not in water")
THRESHOLDS["low"] = GPIO.input(SENSOR_CHANNEL)
input("Press enter when the water sensor is connected and immersed in water")
THRESHOLDS["high"] = GPIO.input(SENSOR_CHANNEL)
# set the medium threshold
THRESHOLDS["med"] = (THRESHOLDS["high"] - THRESHOLDS["low"]) / 2
with open("thresholds.json", "w") as f:
json.dump(THRESHOLDS, f, indent=2)
def test_light():
for state in "on", "off":
new_state = GPIO.HIGH if state == "on" else GPIO.LOW
GPIO.output(LIGHT_CHANNEL, new_state)
light_input = input(f"Is the light {state}? (enter Y/y then press enter)")
if light_input.lower().strip() != "y":
raise SystemExit("Check light sensor connections")
def main():
while True:
reading = GPIO.input(SENSOR_CHANNEL)
print(f"Sensor reads {reading}")
new_state = GPIO.HIGH if reading >= THRESHOLDS["med"] else GPIO.LOW
print(f"Setting light to {new_state}")
GPIO.output(LIGHT_CHANNEL, new_state)
wait()
print("Press CTRL+C to quit")
# Press the green button in the gutter to run the script.
if __name__ == "__main__":
test_light()
set_thresholds()
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/