This tutorial explains how to send email from Beaglebone black using the SMTP protocol. SMTP stands for Simple Mail Transfer Protocol. The main objective of this tutorial is to obtain notification by mail about certain activity. BBB will send an email with a predefined message to the registered email address when any activity occurs. Network (Internet) connectivity is required for this tutorial. You can also attach some file to the mail. The program is written in python script with SMTP library and adafruit GPIO library.
Required tools :
- Black Beaglebone
- push button
- LED
- 1 kΩ resistor
- 330Ω resistor
Test board
- Female to female connectors
Software environment configuration
Install the latest version of python on BBB as explained in the tutorial How to make the first python program with Beaglebone Black. The Adafruit python –GPIO library is already installed, but you can also manually install the library called adafruit_BBIO. The SMTP library is installed with python, but check it once before applying.
Working
In this tutorial, a simple alert notification is sent to a specific email ID. Before running the application, you need to establish an Internet connection with the Beaglebone black. You can choose an Ethernet connection or a WI-FI device. This tutorial uses Ethernet cable to provide Internet connection. When the button is pressed, the email is sent to a specific email ID with a predefined message. When the script is running, the program enters a continuous loop. The LED turns on when the button is pressed and turns off after the BBB email is sent.
When the email is sent, the following message appears at the command prompt:
The email has been sent
Press Ctrl+C to stop the program from running in the SSH command terminal.
Open the recipient's mailbox and check whether the message was received or not.
Fig. 1: Screenshot of correspondence sent from Beaglebone Black
Description
Let's first prepare the circuit connection. Take a breadboard and supply VCC and ground from BBB to the breadboard line. Connect 3.3V power from pin number 3 third of header P9 and ground from pin number 2 and header P8. Connect the negative terminal of the LED to ground and the positive to the 330 Ohm resistor. Connect other end of resistor to pin number 10 of header P8. The push button has two terminals. Any of them connect to the ground. Supply 3.3V to 1k ohm VCC resistor to other button terminal. Connect the common end of the resistor and the button with pin number 11 of the P8 header. Supply power to the black Beaglebone by connecting it to the PC via a USB cable. Now your circuit is prepared.
Open the command terminal and access Beaglebone black through SSH as explained in getting started with Beaglebone black. Create a new file using play command with .py extension (i.e. led.py). Open the file with any text editor (i.e. nano, vim etc.) and write code in python language.
SMTP Configuration
You can send email using SMTP over the Internet, which manages the service over the TCP/IP network. Two things are required to establish a connection to the email server:
- Mail server address
- Port number
SMTP library has built-in function to configure email sending configuration. Set the email address and port number as per the following function:
smtplib.SMTP (“smtp.gmail.com”, 587)
Here I used Gmail server with appropriate port number for sending email. The first argument is the mail server address and the second argument is the port number.
After connecting to the email server, BBB (acting as a client) identifies and establishes a secure connection by following the functions respectively.
ehlo – Enables and identifies connection to an email server
starttls – provides secure connection for data transfer
Configure the sender's email ID, password, recipient's email ID and the message you want to send to the recipient.
To = “recipient email ID”
From = “Sender Email ID”
Password = “Sender Email ID Password”
message = “Alert!!! The button is pressed”
You can write a message as per your wish.
Login to the server with specific ID and password. Send the message on the recipient's email ID and close the connection.
GPIO Configuration
I already explained the GPIO configuration in Switch interface with black Beaglebone.
Run the script in the terminal:
Enter the following command with the file name at the command prompt:
python.py file name
i.e. python sendmail.py
Project source code
###
import smtplib
###
Circuit diagrams
Circuit Diagram-Beaglebone-Black |