This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iot_moisture_monitor.py
83 lines (53 loc) · 1.84 KB
/
iot_moisture_monitor.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
# import iot_serial
import testmail as tm
import pymysql as mdb
import csv
import serial
import datetime
import time
import smtplib, ssl, html_builder
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import logging
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',
)
def select_sql(database, table, cursor):
query = "SELECT id_plant, ROUND(AVG(humidity_level)) AS avg_humidity_level FROM "+database+"."+table+" "
query += "WHERE time_stamp BETWEEN date_sub(current_timestamp(), INTERVAL 1000 MINUTE) AND current_timestamp()"
query += " GROUP BY id_plant;"
print(query)
cursor.execute(query)
result = cursor.fetchall()
print(result)
return result
def read_send_status():
while(1):
logging.debug('Starting')
con = mdb.connect(host='localhost',user='isa',password='demoisa',db='moisture')
cur = con.cursor()
db = "moisture"
tb = "plants"
plants_status = select_sql(db, tb, cur)
status = []
for plant in plants_status :
if plant[1] > 700:
status.append(0)
else:
status.append(1)
print(status)
message = MIMEMultipart("alternative")
message = tm.attach_images('./images/wet-plant.png', 0 ,message)
message = tm.attach_images('./images/dry-plant.png', 1 ,message)
html = html_builder.build(status)
html_part = MIMEText(html, "html")
message.attach(html_part)
tm.send_email(message)
logging.debug("Sent e-mail!")
time.sleep(60*5)
con.close()
logging.debug('Exiting')
# if __name__ == '__main__':
# read_send_status()