EX - IOT - 1 To 5 PDF
EX - IOT - 1 To 5 PDF
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.
Building Circuit
1. Vin VCC
2. GND GND
3. D3 Data Out
Circuit Layout:
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:
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);
}
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 .
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
• Detection Range: 10 – 300 ppm NH3, 10 – 1000 ppm Benzene, 10 – 300 Alcohol
• Dimensions: 18mm Diameter, 17mm High excluding pins, Pins – 6mm High
Building Circuit
1. Vin VCC
2. GND GND
3. A0 A0
Circuit Layout:
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");
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);
}
Title : To control relay for electrical appliances using blynk app and wifi modem NodeMCU.
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.
Building Circuit
1. Vin VCC
2. GND GND
3. D1 Relay
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:
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.
STEP 8
Set up the circuit as per the schematics.
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.
Experiments No.4
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.
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
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.
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).
After that, open the terminal box and enter your default PI name and password.
Username (pi)
Password (raspberry)
Step 3
Next, you need to enable the permissions and options like SSH and Camera. So, go to
Raspberry Pi configuration.
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.