Repository navigation
Expand file tree
/
Copy pathhardware_test.py
More file actions
executable file
·168 lines (129 loc) · 4.93 KB
/
Copy pathhardware_test.py
File metadata and controls
executable file
·168 lines (129 loc) · 4.93 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python3
import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
import subprocess
import datetime
from os import listdir
from os.path import join
import gaia_client.gpio_init as gpios
import gaia_client.constants as constants
from RPLCD.i2c import CharLCD
from gaia_client.DHT22 import DHT22Sensor
import time
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Is this running with sudo ?")
sys.exit(1)
separator = "*" * 60
def shell(cmd):
result = subprocess.run(cmd, stdout=subprocess.PIPE)
return result.stdout.decode('utf-8')
print(separator)
print("*** The AM2301 temperature/humidity ***\n")
sensor = DHT22Sensor(gpio=gpios.GPIO_PIN_AM2301)
try:
i = 0
while i < 10:
print("Attempt", i, sensor.read())
time.sleep(2)
i += 1
except Exception as e:
print("Exception", e)
sensor.cleanup()
print("Continue ? [Enter]")
input()
print(separator)
print("*** Testing for the W-1 sensors ***\n")
w1sensors = [f for f in listdir(constants.ONE_WIRE_DEVICES_PATH) if f.startswith(constants.ONE_WIRE_SENSOR_NAME_STARTSWITH)]
print("There should be two sensors shown:")
print(",".join(w1sensors) + "\n")
for w1sensor in w1sensors:
print("Reading from", w1sensor)
with open(join(constants.ONE_WIRE_DEVICES_PATH, w1sensor, constants.ONE_WIRE_SENSOR_DATA_LOCATION) , 'r') as content_file:
print(content_file.read())
print("Continue ? [Enter]")
input()
print(separator)
print("*** Testing for the LCD screen ***\n")
print("i2cdetect -y 1 should show exactly one address")
print(shell(['i2cdetect', '-y', '1']))
print("Pin is expected to be", gpios.LCD_I2C_ADDRESS, ". If not, edit the constants. Continue ? [Enter]")
input()
lcd = CharLCD('PCF8574', int(gpios.LCD_I2C_ADDRESS, 16))
lcd.clear()
lcd.write_string('LCD OK ' + str(datetime.datetime.now().time())[0:8])
print("LCD should read \"OK\". Continue ? [Enter]")
input()
print(separator)
print("*** Testing the feeder's led ***\n")
GPIO.output(gpios.GPIO_PIN_LED_FEEDING, GPIO.HIGH)
print("Feeder's LED should be ON. Continue ? [Enter]")
input()
GPIO.output(gpios.GPIO_PIN_LED_FEEDING, GPIO.LOW)
print("Feeder's LED should be OFF. Continue ? [Enter]")
input()
print(separator)
print("*** Testing the feeder's button ***\n")
print("Press on the button. The LCD should display the event.")
def callback(channel):
if GPIO.input(gpios.GPIO_PIN_BUTTON_FEEDING) == GPIO.HIGH:
lcd.clear()
lcd.write_string('BUTTON1 ___ ' + str(datetime.datetime.now().time())[0:8])
print("Feeder's Button pressed ___")
else:
lcd.clear()
lcd.write_string('BUTTON1 _-_ ' + str(datetime.datetime.now().time())[0:8])
print("Feeder's Button released _-_")
GPIO.setup(gpios.GPIO_PIN_BUTTON_FEEDING, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(gpios.GPIO_PIN_BUTTON_FEEDING, GPIO.BOTH, callback=callback, bouncetime=200)
print("Continue ? [Enter]")
input()
print(separator)
print("*** Feeder's continuous servo ***\n")
print("This will start the motor. Press Enter to stop afterwards. Start ? [Enter]")
input()
GPIO.setup(gpios.GPIO_PIN_SERVO_FEEDING, GPIO.OUT)
p = GPIO.PWM(gpios.GPIO_PIN_SERVO_FEEDING, 2500)
p.start(1)
input('Press return to stop:') # use raw_input for Python 2
p.stop()
print(separator)
print("*** Testing the watering system's led ***\n")
GPIO.output(gpios.GPIO_PIN_LED_WATERING, GPIO.HIGH)
print("Watering system's LED should be ON. Continue ? [Enter]")
input()
GPIO.output(gpios.GPIO_PIN_LED_WATERING, GPIO.LOW)
print("Watering system's LED should be OFF. Continue ? [Enter]")
input()
print(separator)
print("*** Testing the watering systems's button ***\n")
print("Press on the button. The LCD should display the event.")
def callback2(channel):
if GPIO.input(gpios.GPIO_PIN_BUTTON_WATERING) == GPIO.HIGH:
lcd.clear()
lcd.write_string('BUTTON2 ___ ' + str(datetime.datetime.now().time())[0:8])
print("Watering System's Button pressed ___")
else:
lcd.clear()
lcd.write_string('BUTTON2 _-_ ' + str(datetime.datetime.now().time())[0:8])
print("Watering System's Button released _-_")
GPIO.setup(gpios.GPIO_PIN_BUTTON_WATERING, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(gpios.GPIO_PIN_BUTTON_WATERING, GPIO.BOTH, callback=callback2, bouncetime=200)
print("Continue ? [Enter]")
input()
print(separator)
print("*** Testing the watering system's led ***\n")
def test_relay(id, pin):
GPIO.output(pin, GPIO.HIGH)
print("Watering system's Relay", id, "should be ON. Continue ? [Enter]")
input()
GPIO.output(pin, GPIO.LOW)
print("Watering system's Relay", id, "should be OFF. Continue ? [Enter]")
input()
test_relay(1, gpios.GPIO_PIN_RELAY1_WATERING)
test_relay(2, gpios.GPIO_PIN_RELAY2_WATERING)
test_relay(3, gpios.GPIO_PIN_RELAY3_WATERING)
test_relay(4, gpios.GPIO_PIN_RELAY4_WATERING)
print("All done, GPIO's should cleanup automatically.")