This is the same exercise that we did in Chapter 7, Internet of Things with BeagleBone. The Python package flask-mail
can be used to create a local mail server inside BeagleBone to send e-mails. But to match the same example we did in Chapter 5, Reading from Analog Sensors we will use Gmail. An Internet connection is essential for this exercise. It is recommended to create a new temporary Gmail account for this exercise. You will have to visit the webpage https://myaccount.google.com/security and turn on Allow less secure apps. If you have enabled two-factor authentication, you will have to generate an app password from the same webpage. An in-depth explanation about this is covered in Chapter 7 for the same over-temperature exercise.
Create a circuit setup the same as we did for temperature sensing using TMP36 in Chapter 5, Reading from Analog Sensors. Open Cloud9. Write the following program. Change the e-mail addresses and password at the beginning of the program. Then save it as emailAlert.py
and run it. Touch TMP36 with semi-hot metal and you should hit the 50 degree Celsius threshold. Then check if you got an e-mail at the e-mail address you specified in variable emailTo
. If you are using an e-mail provider other than Gmail, you will have to change the variables emailServer
and emailServerPort
accordingly. The code for emailAlert.py
is as follows:
#!/usr/bin/python import Adafruit_BBIO.ADC as ADC import smtplib from email.MIMEText import MIMEText import time tmp36 = "P9_40" check_interval = 4 threshold = 50 emailFrom = "[email protected]" ##gmail address emailPasswd = 'yyyyyy' ##gmail {app} password emailTo = "[email protected]" ## email address where emails will be sent emailServer = 'smtp.gmail.com' emailServerPort = 587 ADC.setup() while True: time.sleep(check_interval) ## wait 4 seconds to measure again volts = ADC.read(tmp36)* 1.8 temperature = (volts * 100) - 50 print " Current Temperature is " + str(temperature) if (temperature>threshold): msg = MIMEText("Current temperature is " + str(temperature)) ##email body msg['From'] = emailFrom msg['To'] = emailTo #msg['Reply-to'] = emailFrom msg['Subject'] = "Overtemperature Alert!!!" server = smtplib.SMTP(emailServer,emailServerPort) server.ehlo() server.starttls() server.ehlo() server.login(emailFrom,emailPasswd) server.sendmail(emailFrom,emailTo,str(msg)) server.close() print 'Email sent' time.sleep(3600) ##wait 1 hour if email is sent
We imported the module smtplib
to use the SMTP()
function. We also imported the MIMEText
(Multipurpose Internet Mail Extensions) part of the module email
. It is e-mail format standard. The first part of this program is very similar to a program that prints the temperature, which we have seen in Chapter 8, Physical Computing in Python. It calculates temperature. If the temperature
is greater than the threshold
value, it sends an e-mail. We first crafted an e-mail message with appropriate e-mail addresses, subject and e-mail body in mime format. Then we created an SMTP connection to Gmail server smtp.gmail.com
on port number 587
. We changed the connection mode to Transport Layer Security (TLS). All SMTP commands and data will be encrypted here onwards. Here authentication is done with an e-mail ID and password. Then we sent an e-mail that we crafted earlier. Finally, we closed the SMTP connection and sleep for 3600
seconds (1 hour) before starting the temperature measurement again to avoid too many e-mails. Please note there is limit of how many e-mails you can send in a day. If you send too many e-mails in a short time, you will be blocked. This program can be combined with the push button program in Chapter 8, Physical Computing in Python to allow us to send e-mails by button presses.
3.142.40.32