-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairMonitor.py
99 lines (79 loc) · 2.51 KB
/
airMonitor.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
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
#! /usr/bin/python3
import sys
import os, os.path
import time
import aqi
import getopt
import string
import math
import random
import serial
import smtplib, ssl
debug = False
usbfile = "/dev/ttyUSB0"
aqiThreshold = 100
#threshold to trigger alerts
emailPassword = ""
emailSender = ""
#username to send from
smtpServer = "smtp.gmail.com"
emailReceiver = ""
emailTimeout = 45
#time between each email, in minutes
emailPort = 465
def sendEmail(airQuality, twoFive, ten):
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", emailPort, context=context) as server:
message = "Subject: Air Quality Alert\n\nAQI: "+str(airQuality)+"\nAir Conditions: "+calculateAirQualityConditions(airQuality)+"\n\nPM2.5 Levels: "+str(twoFive)+"\nPM10 Levels: "+str(ten)
server.login(emailSender, emailPassword)
server.sendmail(emailSender, emailReceiver, message)
def calculateAirQualityConditions(aqi):
if (aqi < 51):
return "Good"
elif (aqi < 101):
return "Moderate"
elif (aqi < 151):
return "Unhealthy for Sensitive Groups"
elif (aqi < 201):
return "Unhealthy"
elif (aqi < 301):
return "Very Unhealthy"
else:
return "Hazardous"
def startupEmail():
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtpServer, emailPort, context=context) as server:
message = "Subject: Air Quality Alert\n\nAir Quality Monitor Has Started"
server.login(emailSender, emailPassword)
server.sendmail(emailSender, emailReceiver, message)
def main():
pastThreshold = False
try:
ser = serial.Serial(usbfile)
except IOError as e:
print("airMonitor.py: I/O error -- %s" % (e.args[0]))
print("\nRetrying in 20 seconds...")
time.sleep(20)
main()
while True:
data = []
for index in range(0,10):
datum = ser.read()
data.append(datum)
pmtwofive = int.from_bytes(b''.join(data[2:4]), byteorder='little') / 10
pmten = int.from_bytes(b''.join(data[4:6]), byteorder='little') / 10
airQuality = aqi.to_aqi([
(aqi.POLLUTANT_PM25, pmtwofive),
(aqi.POLLUTANT_PM10, pmten),
])
if (airQuality >= aqiThreshold):
sendEmail(airQuality, pmtwofive, pmten)
pastThreshold = True
if (pastThreshold):
time.sleep(emailTimeout * 60)
else:
time.sleep(120)
if __name__=='__main__':
startupEmail()
main()
sys.exit()