We've looked at various forms of displays that you can attach to your BeagleBone, but those are only useful when you're looking at it. If your BeagleBone is not accessible, for instance, it's monitoring something at your home while you're out, you might still need a way for it to send you information. One simple way to achieve this is to use Simple Mail Transfer Protocol (SMTP) to send e-mails from your BeagleBone. Save this code to a new file called email_sender.py
by filling in your account's SMTP details:
import smtplib from email.mime.text import MIMEText SMTP_host = 'smtp.gmail.com' SMTP_email = '[email protected]' SMTP_pass = 'password' def send_email(to, subject, body): msg = MIMEText(body) msg['Subject'] = subject msg['From'] = SMTP_email msg['To'] = to server = smtplib.SMTP_SSL(SMTP_host) server.login(SMTP_email, SMTP_pass) server.sendmail(SMTP_email, to, msg.as_string())
One possible application where you might want your BeagleBone to send you an e-mail is in a home security system. For this example, we'll use a PIR motion detector module, like the one from Adafruit at https://www.adafruit.com/products/189, to create a system that notifies you when motion is detected.
For this circuit, you will need:
Hook up the PIR sensor as shown:
The module from Adafruit is great for two reasons, it comes with a cable, and its output signal is 3 V even though it is powered at 5 V. Pretty much all PIR motion detection modules work the same and have the same interface, but if you are using a different one, make sure to confirm with a multimeter that the output is 3 V, and use a voltage divider if it isn't.
The Adafruit module ship is configured to generate a low pulse when motion is detected, so we can wait for a falling edge on the GPIO pin it's connected to. There's a jumper on the module that lets you invert that signal, so there's a high pulse when motion is detected. Since we can detect both falling and rising edges, it doesn't really matter which way the output goes, just make sure to set the edge accordingly in the program:
import Adafruit_BBIO.GPIO as GPIO import email_sender, time PIR_pin = "P9_11" # GPIO0_30 email_address = "[email protected]" def alert(): email_sender.send_email( email_address, "BeagleBone motion detector", "Motion detected at {}".format(time.strftime("%x %X"))) GPIO.setup(PIR_pin, GPIO.IN) while 1: GPIO.wait_for_edge(PIR_pin, GPIO.FALLING) alert()
We used Adafruit_BBIO
for this example, taking advantage of the GPIO.wait_for_edge()
function, which we looked at briefly in Chapter 5, User Input. This is great if you have nothing else to do in your program, as it lets you wait without using any extra system resources such as a while
loop would, and then respond with as little latency as possible when the interrupt occurs.
Most cell phone providers have e-mail gateways to let you send text messages to numbers on their network via e-mail. There are many websites that provide lists of these gateways, such as http://www.emailtextmessages.com/. If you use your number and carrier's gateway instead of your e-mail address for the email_address
variable, you can get the notifications from your BeagleBone as text messages instead.
18.118.93.64