0% found this document useful (0 votes)
109 views29 pages

EX - IOT - 1 To 5 PDF

This document provides instructions for an experiment to monitor air pollution levels using a NodeMCU, MQ135 gas sensor, and ThingSpeak cloud platform. The circuit connects the gas sensor to analog pin A0 on the NodeMCU. The experiment code reads the gas level from the sensor, uploads it to a ThingSpeak channel, and prints the data to the serial monitor. The code and instructions explain how to add libraries, build the circuit, get an API key, and program the NodeMCU to upload and log gas level data on the cloud in real-time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views29 pages

EX - IOT - 1 To 5 PDF

This document provides instructions for an experiment to monitor air pollution levels using a NodeMCU, MQ135 gas sensor, and ThingSpeak cloud platform. The circuit connects the gas sensor to analog pin A0 on the NodeMCU. The experiment code reads the gas level from the sensor, uploads it to a ThingSpeak channel, and prints the data to the serial monitor. The code and instructions explain how to add libraries, build the circuit, get an API key, and program the NodeMCU to upload and log gas level data on the cloud in real-time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Mahavir Education Trust's

Shah & Anchor Kutchhi Engineering College,


Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Experiment No.1
Aim: Data Handling and storage
Temperature, Humidity data upload on Thingspeak on Arduino IDE
Title : to store and upload the weather data using DHT11 temperature and humidity sensor on
cloud ( ThingSpeak.com) using wifi modem NodeMCU.
Hardware: NodeMCU, DHT11 Temperature and Humidity sensor, Jumper Male to female

Introduction:
This experiment is used to log weather data on cloud. ThingSpeak.com is to be used as cloud
service provider and sensor DHT11 will be used to measure temperature and humidity data using
NodeMCU on Arduino IDE.

Libraries need to be added

1.Arduino Preferences :http://arduino.esp8266.com/stable/package_esp8266com_index.json

2.dht.h in arduino liabrary

Building Circuit

Make connection as mentioned below


Sr.No. NodeMCU DHT11

1. Vin VCC

2. GND GND

3. D3 Data Out

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Circuit Layout:

Getting API Key from Thingspeak:


1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to your
account.

2. Create a new channel by clicking on the button.Enter basic details of the channel.Than Scroll
down and save the channel.

3. Channel Id is the identity of your channel. Note down this. Than go to API keys copy and
paste this key to a separate notepad file will need it later.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

4. Programming:
Once the circuit part is done, NodeMCU is needed to be programmed. Here is the code to run
this circuit on NodeMCU.

#include <dht.h>
#define dht_apin D3 // Analog Pin sensor is connected to
#include <ESP8266WiFi.h>
dht DHT;

String apiKey = "PXD7RW20JROFFJUL"; // Enter your Write API key from ThingSpeak
const char *ssid = "wifi ssid"; // replace with your wifi ssid and wpa2 key
const char *pass = "wifi password";
const char* server = "api.thingspeak.com";
WiFiClient client;

void setup()
{ Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}

void loop()
{
DHT.read11(dht_apin);
float h = DHT.humidity;
float t = DHT.temperature;
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(3000);//Wait 3 seconds before accessing sensor again.
if (isnan(h) || isnan(t))
{
Internet of Things B.E.Electronics-Sem-VIII
Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.println("%. Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, set it to 30 seconds
delay(10000);
}

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Q.1 Describe functional blocks of IOT.


Q.2 Explain wireless sensor technology.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Experiment No.2
Aim: Data Handling and storage
Gas Level Monitoring Over Internet Using NodeMCU and & Gas Sensor on Thingspeak
on Arduino IDE
Title : To monitor pollution on thingspeak using Node MCU and MQ135sensor on cloud
( ThingSpeak.com) using wifi modem NodeMCU.
Hardware: NodeMCU, MQ135sensor, Jumper Male to female

Introduction:
This experiment is used to log pollution data on cloud. ThingSpeak.com is to be used as cloud
service provider and sensor MQ135 will be used to detect and identify different types of gasses
data .

Libraries need to be added

1.Arduino Preferences :http://arduino.esp8266.com/stable/package_esp8266com_index.json

MQ135:

The MQ-135 gas sensor senses the gases like ammonia nitrogen, oxygen, alcohols,
aromatic compounds, sulfide and smoke. The MQ-3 gas sensor has a lower conductivity to clean
the air as a gas sensing material. In the atmosphere we can find polluting gases, but the
conductivity of gas sensor increases as the concentration of polluting gas increases. MQ-135 gas
sensor can be implementation to detect the smoke, benzene, steam and other harmful gases. It
has potential to detect different harmful gases. It is with low cost and particularly suitable for Air
quality monitoring application.

The MQ135 sensor is a signal output indicator instruction. It has two outputs: analog
output and TTL output. The TTL output is low signal light which can be accessed through the IO
ports on the Microcontroller. The analog output is an concentration, i.e. increasing voltage is
directly proportional to increasing concentration. This sensor has a long life and reliable stability
Internet of Things B.E.Electronics-Sem-VIII
Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
as well

Features
• High Sensitivity

• High sensitivity to Ammonia, Sulfide and Benze

• Stable and Long Life

• Detection Range: 10 – 300 ppm NH3, 10 – 1000 ppm Benzene, 10 – 300 Alcohol

• Heater Voltage: 5.0V

• Dimensions: 18mm Diameter, 17mm High excluding pins, Pins – 6mm High

• Long life and low cost

Building Circuit

Make connection as mentioned below


Sr.No. NodeMCU MQ135

1. Vin VCC

2. GND GND

3. A0 A0

Circuit Layout:

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Getting API Key from Thingspeak:


1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to your
account.

2. Create a new channel by clicking on the button.Enter basic details of the channel.Than Scroll
down and save the channel.

3. Channel Id is the identity of your channel. Note down this. Than go to API keys copy and
paste this key to a separate notepad file will need it later.

4. Programming:
#include <ESP8266WiFi.h>
String apiKey = "9OIFHW3MDEDJC7ZL"; // Enter your Write API key from ThingSpeak
const char *ssid = "SK"; // replace with your wifi ssid and wpa2 key
const char *pass = "12345678";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
}

void loop()
{
float p = analogRead(A0);
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(p);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Polution: ");
Serial.println(p);
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(1000);
}

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Q.1 Compare MQTT and COAP protocol


Q.2 Explain IOT level 5 and 6

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Experiment No.3

Aim: To control relay using Blynk App and 802.11protocol (WIFI)

Title : To control relay for electrical appliances using blynk app and wifi modem NodeMCU.

Hardware: NodeMCU, relay, relay driver, LED bank load

Introduction:
This experiment is used to control the relay through blink cloud using node mcu and wifi. In
blynk cloud we have to make one account for your cloud.

Blynk app for iOS and Android is the easiest way to build your own mobile app that work with
the hardware of your choice. No iOS or Android coding required. We also required blynk
libraries to run this experiment. Blynk Library is an extension that runs on top of your hardware
application. It handles all the connection routines and data exchange between your hardware,
Blynk Cloud, and your app project.

Libraries need to be added

1.Arduino Preferences :http://arduino.esp8266.com/stable/package_esp8266com_index.json

2. Blynk_Release_v0.5.4 arduino liabrary

Building Circuit

Make connection as mentioned below


Sr.No. NodeMCU RELAY

1. Vin VCC

2. GND GND

3. D1 Relay

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Circuit Layout:

STEP 1
Configure the Blynk app.
Download the Blynk app from playstore for Andriod devices.

STEP 2
Sign up with your email and password.

STEP 3
Creat a new project. The steps are given below:

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
STEP 4
Add widgets to the app as required, in this experiment we are going to control one device.
So need 1 switches. Drag and drop it for your imagination.

STEP 5
Change the logic from 0-1 to 1-0 because the output of NodeMCU is active LOW. Select
digital pins D1 as output pins. change mode to switch from push.

STEP 6
Select the device you are going to communicate, which is the NodeMCU.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
STEP 7
Send the authentication code to your e-mail.

STEP 8
Set up the circuit as per the schematics.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

STEP 9
Open Arduino IDE.
Configure it for NodeMCU.

STEP 10
Write the code and upload it in node mcu.
Execute it.

Programming:
Once the circuit part is done, NodeMCU is needed to be programmed. Here is the code to run
this circuit on NodeMCU.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[ ] = "authtocken"; // PASTE YOUR AUTHENTICATION CODE HERE
// Your WiFi credentials.
char ssid[ ] = "ssid"; //WIFI NAME
char pass[ ] = "password"; //PASSWORD
void setup( )
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Q.1 List all communication protocols.


Q.2 List all sensor data communication protocols

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Experiments No.4

Aim : To implement MQTT protocol


Title : To implement MQTT protocol using Raspberry Pi for local network
Hardware: Raspberry pi, Python

Introduction:
MQTT is a publish-subscribe-based messaging protocol used in the internet of Things. ...
The goal is to provide a protocol, which is bandwidth-efficient and uses little battery power.
MQTT is a messaging protocol i.e it was designed for transferring messages, and uses a publish
and subscribe model. This model makes it possible to send messages to 0,1 or multiple clients. ...
In MQTT a publisher publishes messages on a topic and a subscriber must subscribe to that topic
to view the message.

What is the difference between MQTT and HTTP?


MQTT is data centric whereas HTTP is document-centric. HTTP is request-response
protocol for client-server computing and not always optimized for mobile devices. ... Besides,
publish/subscribe model provides clients with independent existence from one another and
enhance the reliability of the whole system
MQTT Brokers, Publishers & Subscribers
In a nutshell, MQTT consists of publishers and subscribers, and they’re all connected
through a broker.The terminology can be confusing, but if you translate it into client-server
notation, it’s this simple:-the broker is the serverthe publishers and subscribers are the clients.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Procedure:
Building a Raspberry Pi MQTT Broker
● First you need a Raspberry Pi with a recent version of Raspbian or Raspbian Lite
installed.
● GUI installation, just open Terminal and follow the instructions.
1. Install the mosquitto MQTT Broker
mosquitto is a popular MQTT broker that is well-supported on Debian-based Linux
platforms such as Raspbian. It’s easy to install using apt:-
sudo apt install mosquitto mosquitto-clients
You’ll need to enter your password the first time you run sudo.
You don’t strictly need the mosquitto-clients package for running the broker, but
installing it allows you to run the MQTT client code locally which is great for testing.
It also means you can use the Raspberry Pi as a proper MQTT client as well as
a broker. This means you could, for example, add a user interface to control other
MQTT clients around your home directly from the Raspberry Pi.
2. Enable the mosquitto broker
Enable the broker and allow it to auto-start after reboot using the following
command:-
sudo systemctl enable mosquitto
The broker should now be running. You can confirm by checking the systemd service
status:-
sudo systemctl status mosquito
This should produce an output similar to:-

(note:use CTL+C to come out from ruuning status)

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
The great thing about MQTT is that you can just make up topics to suit your needs. You
don’t need to register them anywhere. For the sake of this test, you can use a topic
called test/message.
In the existing terminal, subscribe to the test/message topic:-
mosquitto_sub –d -t test

This will send a subscription message to the MQTT broker which is currently running on
the same system. But it could be running somewhere else, as you’ll see later.
So long as the mosquitto_sub programme is running you’re listening to
the test/message topic as an MQTT client.
4. Publish to the MQTT Topic Locally
Because your current terminal is occupied listening to the topic, you’ll need to open
another terminal. You can do this using another SSH session or on the Raspbian GUI,
depending how your system is configured.
Once open, publish message to the test topic like this:-
mosquitto_pub –d -t test -m "Hello, world"

If you look back at the first terminal now you should see this:-
Hello, world

Testing from a Different Machine:


That’s all very well, but it’s not that useful. The real power of MQTT becomes
apparent when you see how easy it is to communicate between different machines.
To demonstrate how you can send and receive MQTT messages from devices
such as the ESP8266 or other rpi, which is ideal for home automation.
But first it’s useful to learn how to send and receive messages via your Raspberry
Pi broker from another computer on the network. This is the ideal test environment
because it’s real enough in that the client is a separate machine from the Raspberry Pi,
and because it’s a computer rather than an embedded system it’s convenient for testing
and debugging.
5. Select a Test Machine
There are plenty of different MQTT clients out there but use only mosquitto for
testing. It has the advantage of being able to run on pretty much everything.
6. Install the MQTT Client Package
Installing mosquitto MQTT Client on Linux
Internet of Things B.E.Electronics-Sem-VIII
Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Assuming you’re running on Linux, including a virtual Linux machine, you can install
the mosquitto client code using:-
Apt get update
sudo apt install mosquitto-clients
If your installation doesn’t yet support apt, replace it with apt-get.
Identify the Raspberry Pi on the Network
The MQTT client doesn’t need to know very much about the broker, but
it does need to know where it is on the network. The MQTT client code needs a hostname
or an IP address whenever you subscribe to or publish a message.
If your Raspberry Pi has a unique name on your network, it makes sense to use
that. Find the host name on the Pi by typing:-
hostname
If yours is left at the default it will return:-
raspberrypi
Alternatively, you can always use the Raspberry Pi’s IP address in place of the
hostname. An easy way to get this is by running:-
ifconfig | grep inet | grep cast
Subscribe to the Topic Remotely
On the test machine, there’s no need to run a server since it’s already running on
the Raspberry Pi.
You can just subscribe to the test message like this:-
mosquitto_sub -h raspberrypi -t test
If your Raspberry Pi’s hostname is different, substitute it in place
of raspberrypi in the above command. You can also use the IP address directly if you
prefer:-
mosquitto_sub -h 192.168.0.25 -t test
This will now cause the terminal to wait for messages from the broker on the
topic test/message.
Publish a Test Message Remotely
As before, you can now open another terminal window and type:-
mosquitto_pub -h raspberrypi -t test -m "Hello from remote"
or
mosquitto_pub -h raspberrypi -t test -m "Hello from remote"

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Again, substitute your hostname or IP address as appropriate.


Now you should a message see in the first terminal window:-
Hello from remote

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Q.1 Explain process and domain model of IoT.


Q.2 Explain IP addressing in IoT

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Experiment No.6
Aim: To capture image and send image through email using Raspberry Pi

Title : To capture the image from Raspberry pi camera and send image through the email using
Raspberry pi and python.

Hardware: Raspberry Pi 3 with OS installed in memory card, Raspberry pi camera.

Software: Python, Putty, VNC

Circuit Diagram – Experimental setup

Introduction:
This experiment is used to capture image from raspberry pi camera. The image is stored in Rpi
and the it is send as attachment through the email.

Raspberry Pi Camera:
This package provides a pure Python interface to the Raspberry Pi Camera module for
Python 2.7 (or above) or Python 3.2 (or above).

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Steps to execute the practical


Step 1
First, the Raspberry Pi must be connected to your desktop. Then, connect your wi-fi on
your Pi or connect your ethernet. After that, open the terminal of your Operating System using
the following code.
hostname -I
Next, open PuTTY or VNC software and paste the Host Name (or IP Address) on this
PuTTY.

After that, open the terminal box and enter your default PI name and password.
Username (pi)
Password (raspberry)

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Step 2
Next, we need to update the Raspberry Pi. So, install the latest packages. You can do that
using the following commands.
sudo apt-get update
sudo apt-get upgrade
or
sudo apt-get update && upgrade
If you want to install Python 3.0
sudo apt-get install python3
Install an SMTP service
sudo apt-get install ssmtp
Configure the SMTP
sudo nano /etc/ssmtp/ssmtp.conf

Step 3
Next, you need to enable the permissions and options like SSH and Camera. So, go to
Raspberry Pi configuration.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
Step 4
Open the Python IDE 2.7 or above 3.2, create a new file and save it as camera.py. It’s
important that you do not save it as picamera.py
Write the Camera testing code as given below.
1. from picamera import PiCamera
2. from time import sleep
3. camera = PiCamera()
4. camera.start_preview()
5. sleep(05)
6. camera.stop_preview()
Save with Ctrl + S and run with F5. The camera preview should be shown for 05 seconds,
and then close.
or
Run the program.
sudo python camera.py
Note: Allowing Gmail SMTP Access for Accounts with Standard Authentication
o To allow access to Gmail’s SMTP server from your app, you can follow these
steps,
o Login to your Gmail account using your username and password.
o From the top right corner go to “My Account“.
o Under “Sign-in & security” section locate “Connected apps & sites” and click on
it.
o Locate “Allow less secure apps” setting and turn it “On“.
Code:

1. import smtplib,ssl
2. from picamera import PiCamera
3. from time import sleep
4. from email.mime.multipart import MIMEMultipart
5. from email.mime.base import MIMEBase
6. from email.mime.text import MIMEText
7. from email.utils import formatdate
8. from email import encoders
9.
10. camera = PiCamera()
Internet of Things B.E.Electronics-Sem-VIII
Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering
11.
12. camera.start_preview()
13. sleep(5)
14. camera.capture('/home/pi/image.jpg') # image path set
15. sleep(5)
16. camera.stop_preview()
17. def send_an_email():
18. toaddr = '[email protected]' # To id
19. me = '[email protected]' # your id
20. subject = "What's News" # Subject
21.
22. msg = MIMEMultipart()
23. msg['Subject'] = subject
24. msg['From'] = me
25. msg['To'] = toaddr
26. msg.preamble = "test "
27. #msg.attach(MIMEText(text))
28.
29. part = MIMEBase('application', "octet-stream")
30. part.set_payload(open("image.jpg", "rb").read())
31. encoders.encode_base64(part)
32. part.add_header('Content-
Disposition', 'attachment; filename="image.jpg"') # File name and format
name
33. msg.attach(part)
34.
35. try:
36. s = smtplib.SMTP('smtp.gmail.com', 587) # Protocol
37. s.ehlo()
38. s.starttls()
39. s.ehlo()
40. s.login(user = '[email protected]', password = '*********') # User
id & password
41. #s.send_message(msg)
42. s.sendmail(me, toaddr, msg.as_string())
43. s.quit()
44. #except:
45. # print ("Error: unable to send email")
46. except SMTPException as error:
47. print ("Error") # Exception
48.
49. send_an_email()

Save with Ctrl + S and run with F5. When you check your mail, you will find that the image has
been received.

Internet of Things B.E.Electronics-Sem-VIII


Mahavir Education Trust's
Shah & Anchor Kutchhi Engineering College,
Chembur, Mumbai 400 088
UG Program in Electronics Engineering

Q.1 How to acquire and store the data ?


Q.2 Explain LPWAN fundamentals.

Internet of Things B.E.Electronics-Sem-VIII

You might also like