Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Using SMTP in the IoT

By engineersgarage October 25, 2021

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

  1. Raspberry Pi
  2. Raspberry Pi camera module
  3. Ultrasonic sensor (HC-SR04)
  4. Basic Understanding of Python
  5. 1kΩ, 2kΩ Resistor, jumper Wires

Schematic and connection

Figure 1. Connection diagram

  1. Connect VCC of HC-SR04 to 5V of Pi and GND to Ground
  2. Connect TRIG Pi to GPIO23
  3. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.

  1. Importing important libraries
  2. Detection of object
  3. Clicking the image of object
  4. 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

https://www.engineersgarage.com/wp-content/uploads/2021/07/video.mp4

You may also like:


  • IoT Standards and Protocols: IoT Part 3
  • SMTP
    SMTP – Simple Mail Transfer Protocol

  • The pros and cons of the Raspberry Pi 400

  • Getting started with Raspberry Pi

  • Transmission Control Protocol/Internet Protocol (TCP/IP) : IOT Part 28

Filed Under: Electronic Projects, Raspberry pi
Tagged With: IoT, python, Raspberry Pi, smtp
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe