This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmail_report.py
executable file
·72 lines (63 loc) · 2.18 KB
/
mail_report.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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#
# Description: Script to compare measured values with defined threshold values
# In case measured values are out of bound a notification e-mail will be send
#
# Supported OS: Raspbian Stretch
# Requirements: Host must be able to send mail
# Author: Joerg Kastning
# License: MIT
import configparser
import json
import smtplib
from email.mime.text import MIMEText
# Variables ##################################################################
maxtemp=38.0 # upper limit for temperature
mintemp=-14.0 # lower limit for temperature
maxhumidity=40 # upper limit for humidity
minhumidity=28 # lower limit for humidity
fromaddr="[email protected]" # address for email notification
toaddr="[email protected]"
webroot="/var/www/html/sht21.json"
##############################################################################
# Function to edit ###########################################################
def send_mail(string):
msg = MIMEText(string)
msg['Subject'] = string
msg['From'] = fromaddr
msg['To'] = toaddr
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login("username", "password")
s.sendmail(fromaddr, toaddr, msg.as_string())
s.close
##############################################################################
def tempalarm(temperature):
if (temperature > maxtemp):
string="ALERT: The temperature is above the upper limit"
send_mail(string)
elif (temperature < mintemp):
string="ALERT: The temperature is below the lower limit"
send_mail(string)
def humidalarm(humidity):
if (humidity > maxhumidity):
string="ALERT: The humidity is above the upper limit"
send_mail(string)
elif (humidity < minhumidity):
string="ALERT: The humidity is below the lower limit"
send_mail(string)
def main():
try:
with open(webroot, 'r') as f:
sht21_data = json.load(f)
except:
print("Error: Could not open file sht21.json for reading")
temperature = sht21_data['temp']
humidity = sht21_data['humidity']
tempalarm(temperature)
humidalarm(humidity)
if __name__ == "__main__":
main()