In the previous tutorial, it was mentioned that IMAP protocol is a standard email protocol which is used to store email messages and retrieve them. It was also mentioned that IMAP protocol can be used in IoT applications where commands can be passed to IoT devices by emails. This can be really helpful in certain situations like when security might be the main concern. Also, emails can be sent on any network without any special application or permissions. The IoT devices can receive emails as email clients where they can read emails and process information contained in them.
In this project, a Raspberry Pi is used as an IoT device which act as an Email client. The Raspberry Pi is interfaced with an LED which is controlled by sending emails to the Pi. The Pi receives emails over IoT (internet) network and controls the LED according to the data received in the body of the email. It interprets the email body only when the subject of the email matches with a pre-determined name. The emails to pass the commands to control the LED can be sent from any device like Laptop, PC, Tablet or Smart Phone. The Raspberry Pi receives theses emails over Internet Message Access Protocol (IMAP).
Components Required –
1) Raspberry Pi 3
3) 1K Ω Resistor
4) Jumper Wires
Software Required –
1) Raspbian OS
2) Leafpad/GNU Nano/Python 3 IDLE (for writing python scripts)
Block Diagram –
Fig. 1: Block Diagram of Raspberry Pi IoT IMAP Email Client
Circuit Connections –
A simple IOT device is designed in this project. It has an LED interfaced with the Raspberry Pi 3. The device is designed by assembling the following components together.
Raspberry Pi 3 – Raspberry Pi 3 is the third generation Raspberry Pi. It is a miniature marvel, packing considerable computing power into a footprint no larger than a credit card. The processor at the heart of the Raspberry Pi system is a Broadcom BCM2837 system-on-chip (SoC) which houses a 1.2 GHz Quad Core ARM Cortex-A53 processor. The vast majority of the system’s components, including its central and graphics processing units along with the audio and communications hardware, are built onto that single component along with 1 GB LPDDR2 memory chip at the centre of the board. It is not just this SoC design that makes the BCM2837 different to the processor found in a typical desktop or laptop, however, it also uses a different instruction set architecture (ISA), known as ARM.
The Pi comes equipped with on-board 10/100 BaseT Ethernet Socket, HDMI and Composite RCA port for video, 3.5 mm audio output jack, 15-pin MIPI Camera Serial Interface (CSI-2), Display Serial Interface, Bluetooth 4.1, 802.11 b/g/n Wireless LAN, Micro SDIO for Micro SD Card, 4 USB 2.0 Connectors, 40 pin header containing 27 GPIO pins and Micro USB socket for power supply.
The Raspberry Pi is a single board computer and is designed to run an operating system called GNU/Linux Raspbian. Hereafter referred to simply as Linux. Unlike Windows or OS X, Linux is open source, so it is possible to download the source code for the entire operating system and make whatever changes desired. The Raspberry Pi 3 can also run Windows 10 IoT and many other embedded operating systems most of which are Linux derivatives. The operating system should be loaded in a MicroSD card and boot from it. With powerful computing resources, large number of multimedia interfaces and GPIO pins, Raspberry Pi 3 is a suitable choice to run a software oriented complex IoT or Embedded project that requires sufficient computing power as well as large scale sensor connectivity. With on-board Bluetooth and Wi-Fi, this 3rd generation Pi can be easily deployed in an IoT network. The key specifications of the Raspberry Pi 3 are summarized in the following table –
Fig. 2: Table listing technical specifications of Raspberry Pi 3
The 40-pin header on Raspberry Pi 3 has the following pin configuration –
Fig. 3: Table listing pin configuration of 40-pin header of Raspberry Pi
Fig. 4: Table listing pin configuration of 40-pin header of Raspberry Pi
In this project, an LED is interfaced to GPIO18 pin of the Raspberry Pi. The board is powered by an USB adapter.
LED – An LED is connected at GPIO18 of the Raspberry Pi. Its anode is connected to Raspberry Pi pin and cathode is cathode is connected to ground. The LED is connected to Pi via a 1K ohm pull-up resistor in series.
Power Supply – The power source is connected to the Raspberry Pi. The Pi should be powered by a 5V adapter with 2.5 A current output. The adaptor can be connected at Micro USB socket.
How the circuit works –
The IoT device designed here is simply a Raspberry Pi 3 controlling an LED. For controlling the LED, a python script is written which retrieves emails and if the subject of the email matches with a pre-determined text, it reads the body of the mail containing the control command.
First of all, the Raspberry Pi needs to loaded with an operating system. Here the official operating system for the Raspberry Pi – Linux Raspbian is installed on it. While installing the operating system, the Raspberry Pi should be connected to a display monitor using HDMI cable and a keyboard and mouse through USB ports.
For installing the Raspbian Operating System on MicroSD card, first download the latest image of Raspbian OS from Raspberry Pi website from the following link –
Copy the image of the latest Raspbian OS in the MicroSD card. If the MicroSD card used is 32 GB or below, it must be formatted to FAT32 (file system) before copying the image or if the MicroSD card is more than 32 GB, it should be formatted to exFAT before copying the image. Extract the OS Zip and copy it to the MicroSD card. The image can be written to the card by connecting the card to a laptop or PC using a MicroSD card reader. After copying the extracted image, insert the card in the MicroSD slot as shown below –
Fig. 5: Typical Image of Raspberry Pi 3 MicroSD Card Slot
Connect the Raspberry Pi with a display monitor using HDMI Cable, a keyboard and a mouse. Power on the board by connecting to a power adaptor. The red LED on the board will start blinking and the OS will start booting from the MicroSD card. The boot process will display on the monitor and once the boot is complete, green LED will light up on the Raspberry Pi. After successfully installing Raspbian OS on Raspberry Pi, it is recommended to perform software update. It can be done by running the following Linux commands in the Linux Terminal –
$ sudo apt-get update
$ sudo apt-get upgrade
After installing the Raspbian, it’s time to write and run the python scripts on Raspbian. A python script can be written on Raspbian using a text editor like Leafpad or GNU Nano. The python script can also be written using the default python IDE like Python 2 IDLE or Python 3 IDLE. Open the Python 3 IDLE by navigating through Menu -> Programming -> Python 3 IDLE. A window called Python 3.4.2 Shell will open up. Write the python scripts and save them to a directory.
The python script written for this project should run at the startup as the Pi 3 is powered on. The script runs an infinite loop so it never ends. There are some methods by which the Raspberry Pi can be configured to run a python script on start up. Any of the following methods can be used –
1) Editing rc.local –
The commands can be added to the file /etc/rc.local to run a program or command when the raspberry Pi boots up. This is especially useful if the Pi has to plug in to power headless, and have it run a program without configuration or a manual start. The file should be edited with root by running the following commands in the Linux Terminal –
sudo nano /etc/rc.local
Now add commands to execute the python script using complete file path and add an ampersand at the end of the command so that the script runs in a separate process and booting could continue. The following command should be added where the python script is saved as IMAP.py –
sudo python /home/pi/IMAP.py &
The command should be added just before the line exit 0 in the rc.local file.
2) Editing .bashrc –
The .bashrc is a hidden file in the home folder that contains user configuration options. Open the .bshrc file by running the following commands in the Linux terminal –
sudo nano /home/pi/.bashrc
Add the following lines after the last line in the file –
echo Running at boot
sudo python /home/pi/IMAP.py
3) Adding script to init.d directory –
The init.d directory contains the scripts which are started during the boot process (in addition, all programs here are executed when Pi is shutdown or rebooted). Add the script to be run at startup to the init.d directory using the following commands –
sudo cp /home/pi/securitysystem.py /etc/init.d/
Move to the init directory and open the python script by running the following commands –
sudo nano IMAP.py
Add the following lines to the python script to make it a Linux Standard Base (LSB) –
### BEGIN INIT INFO
# Provides: sample.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
Make the python script in the init directory executable by changing its permission by running the following command –
sudo chmod +x IMAP.py
Then run the following command –
sudo update-rc.d IMAP.py defaults
Next reboot the Pi by running the following command –
Any of the above methods can be used to make the python script run on startup. Now the Pi 3 can be disconnected with the display monitor, keyboard and mouse. Interface the LED with the Raspberry Pi and power it up.
Now when the Pi will power up, it will run the python script on start up. The Python script will read email from our Gmail account using IMAP protocol. The script will only read emails from specific subject like in this case “pi” is the subject, for which emails will be read by the script. Now the body is read into a string and that string is now compared with a command set, predefined in the script. Each command has its own functionality, like if “on” command is matched with the email body then the light will be turned on (LED connected to raspberry pi). There can also an email received back for each command as feedback but that will require implementation on SMTP protocol.
Programming Guide –
The Implementation of this project can be done in any programming language like C, Java but python is used in this case. There are simple libraries used for raspberry pi GPIO and IMPLIB for IMAP.
The python script can be divided into the following parts –
1. Importing important libraries
2. Login into the mail box (Gmail)
3. Searching for email from specific subject “pi”
4. Comparing email body with predefined command sets
5. Marking the email as seen
1. Importing important libraries: To accomplish the project first there is need to collect required tools like required standard libraries.
For IMAP implementation, “IMAPLIB” library is used and for GPIO interaction, “RPI.GPIO” library is used.
import RPi.GPIO as GPIO
2. Login into the mailbox: After getting what is need now, its time to login to the mail box using credentials and selecting the INBOX. The connection is simply stored in “conn” object and then IMAP commands are used to interact with mail box.
conn = imaplib.IMAP4_SSL(“imap.gmail.com”, 993)
Now the place where the email is categorized is selected which in this case is INBOX. That can be done using select command of IMAP library.
3. Searching for email from specific subject “pi”: Now after selecting the mail box folder using select command, search command can be used to search the email with specific subject “pi”.To verify that the email is new, the code also check for unseen messages.
typ, data = conn.search(None, ‘(UNSEEN SUBJECT “pi”)’)
This command should run inside a loop so when any email is received at any time it get checks.
4. Comparing email body with predefined command sets: Now after the email is received, the body of the email must be read first.
Note: Send the emails while selecting plain text in Gmail sending options.
By using the body extract function, it is possible to just get the body of the email in a string type variable.
Now after getting the message body in a variable it can be compared with the command sets.
Using the raspberry pi GPIO command it is possible to control any GPIO output state, like if ‘on’ command is received, the GPIO pin connecting LED can be turned ON by using “True” flag.
5. Marking the email as seen: After the email is read and the command is executed the email need to be marked read so the script does not take this command repeating.
typ, response = conn.store(num, ‘+FLAGS’, r'(Seen)’)
That can also be done using IMAP command “store”. So now again the script is ready to receive new emails from subject “pi”. The while script function must be run on a loop and errors and warnings must be handled so the process does not stop.
except Exception as e:
So, this is how IMAP protocol can be used to command a IOT device/application. Check out the python code and quickly try it out on a Raspberry Pi.
In the next tutorial, SMTP protocol will be discussed.
You may also like:
Project Source Code
### //Program to#!/usr/bin/env python import RPi.GPIO as GPIO import picamera import subprocess import imaplib GPIO.setwarnings(False) LED = 24 GPIO.setmode(GPIO.BCM) GPIO.setup(LED,GPIO.OUT) GPIO.output(TRIG, False) def extract_body(payload): if isinstance(payload,str): return payload else: return 'n'.join([extract_body(part.get_payload()) for part in payload]) def check_email() conn = imaplib.IMAP4_SSL("imap.gmail.com", 993) conn.login(gmail_user, gmail_pwd) conn.select("INBOX") typ, data = conn.search(None, '(UNSEEN SUBJECT "pi")') try: for num in data.split(): typ, msg_data = conn.fetch(num, '(RFC822)') for response_part in msg_data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part) subject=msg['subject'] payload=msg.get_payload() body=extract_body(payload) print(body.strip()) #things to do when somehthing recived if(body.strip() =="on"): GPIO.output(LED, True) elif(body.strip()=="off"): GPIO.output(LED, False) typ, response = conn.store(num, '+FLAGS', r'(Seen)') finally: try: conn.close() except Exception as e: print e conn.logout() while true: try: check_email() except Exception as e: print e
Filed Under: IoT, IoT tutorials, Tutorials