-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpump.py
More file actions
executable file
·46 lines (30 loc) · 1.2 KB
/
pump.py
File metadata and controls
executable file
·46 lines (30 loc) · 1.2 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
import arduinoserial
#from datetime import datetime, date, time
from time import sleep
import logging
arduino_location="/dev/ttyACM0"
arduino_pulse_time=300 #milliseconds to pulse 433Mhz signal
arduino_quiet_time=3 # seconds to wait for arduino to be ready after connecting and sending
arduino_between_retry_time=1 #seconds to wait between consecutive pulses
arduino_retry_times=10 #number of times to retry
def turn_on():
trigger_pump(1)
def turn_off():
trigger_pump(0)
def trigger_pump(state):
arduino = arduinoserial.SerialPort(arduino_location, 9600)
sleep(arduino_quiet_time) # wait for arduino to reboot :(
times=arduino_retry_times
while(times > 0) :
arduino.write("%u,%u\n" % (state,arduino_pulse_time))
#wait for pulse to finish, then some more
sleep((arduino_pulse_time/1000)+arduino_quiet_time)
response= arduino.read_until("\n").strip()
if (response!="ok") :
logging.info("Pump trigger failed")
raise Exception("Pump trigger failed")
#libary recognized cr as lf, ardunio ends line with cr/lf, so need to read twice to clear buffer
response= arduino.read_until("\n").strip()
sleep(arduino_between_retry_time)
times -= 1
logging.info("Pump triggered OK")