Controlling IoT devices requires specific apps and settings. But you can also control them using emails since emailing is possible using SMTP (Simple Mail Transfer Protocol), which is on the TCP/IP stack. Therefore, the devices that support TCP/IP stack can use emails as command and control.
So, for an application purpose, we will be making a device that will be sensing any object’s presence in a particular range and clicking an image when anyone will be in the detection range of the device. Finally, it will send an email with the image as an attachment.
Components Required
- Raspberry Pi
- Raspberry Pi camera module
- Ultrasonic sensor (HC-SR04)
- Basic Understanding of Python
- 1kΩ, 2kΩ Resistor, jumper Wires
Schematic and connection
- Connect VCC of HC-SR04 to 5V of Pi and GND to Ground
- Connect TRIG Pi to GPIO23
- For the ECHO pin, the Raspberry Pi’s GPIO does not support a 5 V signal directly, so we will be using a voltage divider to connect the ECHO pin to GPIO24 and make a 5V signal 3.3V.
Understanding the basic functionality
- We will be using a Python script to control our sensor module and camera modules input and output connected to the Pi for detection and sensing.
- The sensor will send ultrasonic waves and detect the waves reflating back on the ECHO pin; thus, we can calculate the distance at which the object is placed from the sensor. So, we will make a distance area where we can get an ECHO signal if an object is placed there.
- The echo signal will act as a click button for our camera module, and thus we will be getting an image every time someone enters the detection area.
- After taking the picture, we will be using SMTP to transfer captured image to our mailbox.
Implementation
We will use Python 2.7 to implement the complete functionality, including many multithreading and parallel tasks; therefore, a basic understanding of Python is required. The implementation part can be divided into three parts.
- Importing important libraries
- Detection of object
- Clicking the image of object
- Emailing the image to mailbox
Setup & Importing important libraries: To perform GPIO and camera functions in Raspberry Pi we will need some Python libraries. We will be importing them to control our camera module as well.
For the camera, we need to install the library using the following command in the terminal.
$ sudo apt-get install python-picamera
After installing, we will be importing the theme in our main script. We will import our libraries on top of that.
import RPi.GPIO as GPIO
import picamera
We also have to import other libraries required for multithreading.
We need to import some libraries such as base64, MIME (for attachments like an image) and SMPT, for emails.
import imaplib
from base64 import b64decode
from smtplib import SMTP
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
Now you need to change the variables which stores the value for email and password.
gmail_user = ‘[email protected]’
gmail_pwd = ‘gmail_password’
server = ‘smtp.gmail.com’
server_port = 587
Detection of object: We will make a function that will keep triggering the ultrasonic sensor to send ultrasonic waves out and detect if anything comes in range.
while GPIO.input(ECHO)==1:
At detection this function will call a function to click an image of the object. Since this is a continuous task, it will be in our main loop.
if distance < distance_detect:
capture_image()
Clicking the image of object: To click the image of the object, we will create a function that will be clicking upon calling and storing the image on local storage.
camera.capture(filepath)
After clicking, it will call a function which will be composing an email with the image as an attachment.
download(‘Image’,filepath)
Emailing the image to mailbox:
Using the SMTP library and its methods, this function takes parameters like image name and its path, time of the click, and email to the mailbox. It is a parallel task as the uploading can take time according to the connection speed. We don’t want to stop another task in that period.
sendEmail(rightnow, self.jobid, [self.filepath])
So, this is how we can use emails to communicate with our IoT devices.
Code:
Video
You may also like:
Filed Under: Electronic Projects, Raspberry pi
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.