forked from jamieboyd/AutoHeadFix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwater_failsafe.py
59 lines (57 loc) · 2.12 KB
/
water_failsafe.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
##############################################################################
############ This program is used by AHF to monitor the water valve. ########
############ If at any point, the water valve malfunctions and stays ########
############ open for more then 5 seconds the pi will reboot and ########
############ restart autoheadfix. This script should be run from cron ########
############ at a regular interval while the cage is operating. ########
##############################################################################
##############################################################################
import RPi.GPIO as GPIO
import pymysql
from ast import literal_eval
from time import sleep, time
import os
GPIO.setmode(GPIO.BCM)
cageID = ""
user = ""
pwd = ""
db = ""
with open("/home/pi/config.txt", "r") as file:
configs = file.readlines()
for config in configs:
config = config.split("=")
if config[0] == "cageID":
cageID = config[1].rstrip("\n")
if config[0] == "user":
user = config[1].rstrip("\n")
if config[0] == "pwd":
pwd = config[1].rstrip("\n")
if config[0] == "db":
db = config[1].rstrip("\n")
db = pymysql.connect(host="localhost", user=user, db=db, password=pwd)
query_sources = """SELECT* FROM `configs` WHERE `Cage` = %s AND `Tag` = %s AND
`Dictionary_source` = "RewarderDict" ORDER BY `Timestamp` DESC LIMIT 1 """
cur = db.cursor()
cur.execute(query_sources, [cageID, "changed_hardware"])
#print(cur.fetchall()[0])
mouse, source, dictio, _, _ ,_ = cur.fetchall()[0]
data = {str(source): literal_eval("{}".format(dictio))}
print(data)
water_pin = data["changed_hardware"]["rewardPin"]
GPIO.setup(water_pin, GPIO.OUT)
water_on_time = 0.0
startTime = time()
while True:
if time() > startTime + 300:
break
if GPIO.input(water_pin):
water_on_time += 0.05
sleep(0.05)
if water_on_time >= 5:
GPIO.setup(water_pin, GPIO.OUT)
GPIO.output(water_pin, GPIO.LOW)
#send text
os.system("sudo reboot")
pass
else:
sleep(3)