-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsensor.py
More file actions
executable file
·167 lines (145 loc) · 4.41 KB
/
Copy pathsensor.py
File metadata and controls
executable file
·167 lines (145 loc) · 4.41 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
#!/usr/bin/env python
#---------------------------------------------------------------------
# Read humidity and temperature readings from a set of DHT22 sensors
# Display readings on a 20x4 I2C LCD screen
# Log the readings to a database
# Send information to an Arudino Nano on colors for an WS2812B LED
# strip based on humidity readings obtained
#---------------------------------------------------------------------
# Imports
import i2c_lcd_driver
import serial_driver
import time
import Adafruit_DHT as dht
from datetime import datetime
import MySQLdb as mysql
import signal
# DHT22 sensor constants
DHT22_PINS = [17, 14, 15, 4, 18]
HIGH_HUMIDITY = 20
# Database connection and cursor for logging: database is "drybox" and table is "readings"
db = mysql.connect("localhost", "sensor", "sensorpass", "drybox")
db_cursor = db.cursor()
# LCD settings
mylcd = i2c_lcd_driver.lcd()
# Serial settings to talk to Arduino controlling the LEDs
fil_starts = [20, 34, 48, 62, 75]
fil_ends = [25, 39, 53, 67, 80]
ser = serial_driver.serial_device("/dev/ttyUSB0", 57600)
baseColor = chr(32) + chr(32) + chr(32)
highHumidityColor = chr(1) + chr(255) + chr(1)
def exit_gracefully(signum, frame):
db.close()
mylcd.lcd_clear()
mylcd.backlight(0)
ser.close()
exit()
def main():
# Process exit gracefully
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
# Clear the LCD and turn the backlight on
mylcd.lcd_clear()
mylcd.backlight(1)
while True:
# Read humidity and temperature from each DHT22
humi = []
temp = []
for pin in DHT22_PINS:
h, t = dht.read_retry(dht.DHT22, pin)
humi.append(h)
temp.append(t)
head_str = " 1 2 3 4 5"
# Send text to LCD
mylcd.lcd_display_string(head_str, 1)
mylcd.lcd_display_string(format_humidity_line(humi), 2)
mylcd.lcd_display_string(format_temperature_line(temp), 3)
mylcd.lcd_display_string(format_status_line(humi, temp), 4)
# Wait for 10 minutes
time.sleep(600)
def log_high_humidity(humi, temp):
sql_command = "insert into readings (humidity_1, temperature_1, humidity_2, temperature_2, humidity_3, temperature_3, "
sql_command += "humidity_4, temperature_4, humidity_5, temperature_5) values (";
for i in range(5):
h = None
t = None
if humi[i] is None or humi[i] > 100 or humi[i] < 0:
h = "NULL"
else:
h = humi[i]
if temp[i] is None:
t = "NULL"
else:
t = fahrenheit(temp[i])
sql_command += "{0}, {1}".format(h, t)
if i == 4:
sql_command += ")"
else:
sql_command += ", "
try:
db_cursor.execute(sql_command)
db.commit();
except:
print("ERROR executing SQL: " + sql_command)
db.rollback()
return True
def make_default_color():
return baseColor
def make_filament_color(humidity):
if humidity >= HIGH_HUMIDITY:
return highHumidityColor
else:
return baseColor
def make_serial_message(humi):
# The serial message consists of the following byte sequence, total 28 bytes for 5 dryboxes
# First three bytes: Default GRB color (for LEDs that are not above a particular drybox,
# or above a drybox whose humidity is not "high")
# Five bytes for each drybox:
# Begin and end index of LEDs above that drybox, followed by GRB color for it
data = ""
data += make_default_color()
for i in range(5):
data += chr(fil_starts[i])
data += chr(fil_ends[i])
data += make_filament_color(humi[i])
return data
def send_light_colors(humi, temp):
message = make_serial_message(humi);
print("Sending to Arduino: " + message)
ser.sendToArduino(message)
time.sleep(1)
if ser.inWaiting() > 0:
dataRecvd = ser.recvFromArduino()
if dataRecvd[0] == 0:
ser.displayDebug(dataRecvd[1])
if dataRecvd[0] > 0:
ser.displayData(dataRecvd[1])
print("Reply Received")
def format_status_line(humi, temp):
log_high_humidity(humi, temp)
send_light_colors(humi, temp)
now = datetime.now()
return now.strftime("%b %-d, %-I:%M %p").center(20)
def format_humidity_line(humi):
line = "%"
for reading in humi:
if reading is None:
line += " "
else:
line += "{0:>3.0f} ".format(reading)
return line[:-1] # remove final space for formatting
def format_temperature_line(temp):
line = "F"
for reading in temp:
if reading is None:
line += " "
else:
line += "{0:>3.0f} ".format(fahrenheit(reading))
return line[:-1] # remove final space for formatting
def fahrenheit(celsius):
return celsius*9/5+32
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit_gracefully()