P E S COLLEGE OFENGINEERING
Autonomous Under VTU, Mandya-571401
LABORATORY MANUAL
INTERNET OF THINGS LAB (P15ISL68)
Prepared By
Mr. Yoga B S
Assistant Professor
Department of Computer Science & Engineering
P E S College of Engineering, Mandya-571401
CONTENT
[Link] PROGRAM PAGE
NO
1 1
Design a Smart Traffic Light System using Arduino UNO which includes a crosswalk
button
2 4
Develop an application using Arduino UNO to modulate an LED using an LDR and PIR
sensor
3 6
Design a Smart dustbin system using Arduino UNO and ultrasonic sensor
4 8
Develop an application using RasberyPi to detect an object using IR sensor and send the
message using SMTP protocol
5
a) Write a Program to interface temperature sensor to Arduino UNO and read the values
of temperature and humidity in the given environment and turn On the LED if 10-11
temperature value met the threshold value
b) Using XCTU software, connect two different motes wirelessly and establish a duplex
communication.
6 12
Write a Program to interface LPG sensor to Arduino UNO and read the values of a sensor
in the given environment and turn On the buzzer if petroleum gas is detected value.
7 14
Write a program to interface soil sensor to RasberyPi and transmit received data to cloud
using Ethernet/WiFi
1. Design a Smart Traffic Light System using Arduino UNO which includes a
crosswalk button
Components Required: Arduino UNO, breadboard, Switch, 3LED, jumper wires, 10K registor
intswitchPin = 8;
intrPin=10;
intyPin=12;
intgPin=11;
booleanledon = false;
void setup() {
pinMode(switchPin,INPUT);
pinMode(rPin,OUTPUT);
pinMode(yPin,OUTPUT);
pinMode(gPin,OUTPUT);
void loop() {
if(digitalRead(switchPin)==HIGH)
digitalWrite(rPin,HIGH);
delay(5000);
digitalWrite(yPin,LOW);
delay(5000);
digitalWrite(gPin,LOW);
1
delay(5000);
else
digitalWrite(rPin,HIGH);
delay(1000);
digitalWrite(rPin,LOW);
delay(1000);
if(digitalRead(switchPin)==HIGH)
digitalWrite(rPin,HIGH);
delay(5000);
digitalWrite(rPin,LOW);
digitalWrite(yPin,HIGH);
delay(1000);
digitalWrite(yPin,LOW);
delay(1000);
if(digitalRead(switchPin)==HIGH)
digitalWrite(rPin,HIGH);
delay(5000);
digitalWrite(rPin,LOW);
2
digitalWrite(yPin,HIGH);
delay(1000);
digitalWrite(yPin,LOW);
delay(1000);
}
digitalWrite(gPin,HIGH);
delay(1000);
digitalWrite(gPin,LOW);
delay(1000);
if(digitalRead(switchPin)==HIGH)
{
digitalWrite(rPin,HIGH);
delay(5000);
digitalWrite(rPin,LOW);
}
}
}
Design Circuit
3
2. Develop an application using Arduino UNO to modulate an LED using an
LDR and PIR sensor
Components Required: Arduino UNO, Breadboard, LDR, LED, PIR sensor, Jumper
wires
intpir=10;
intledPin=13;
intldr=0;
void setup() {
analogReference(DEFAULT);
[Link](9600);
pinMode(ledPin,OUTPUT);
pinMode(pir,INPUT);
[Link](9600);
}
void loop() {
int s=digitalRead(pir);
[Link](s);
[Link](analogRead(ldr));
delay(500);
int l=0;
if(analogRead(ldr)>300)
{
if(s==HIGH)
{
if(analogRead(ldr)>300 &&analogRead(ldr)<400)
{
l=l+50;
digitalWrite(ledPin,l);
4
}
if(analogRead(ldr)>400 &&analogRead(ldr)<500)
{
l=l+100;
digitalWrite(ledPin,l);
}
if(analogRead(ldr)>500 &&analogRead(ldr)<600)
{
l=l+150;
digitalWrite(ledPin,l);
}
}
else
{
digitalWrite(ledPin,LOW);
}
}
}
Design Circuit:
5
3. Design a Smart dustbin system using Arduino UNO and ultrasonic sensor
Components Required: Arduino UNO, Ultrasonic senor, Bread Board, Jumper wires, LED
constinttrigpin=9;
constintechopin=10;
int ledPin1=13;
int ledPin2=12;
long duration;
int distance;
void setup()
{
// put your setup code here, to run once:
pinMode(trigpin,OUTPUT);
pinMode(echopin,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
[Link](9600);
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(trigpin,LOW);
delay(2000);
digitalWrite(trigpin,HIGH);
delay(1000);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin,HIGH);
distance=duration*0.034/2;
[Link](distance);
6
if(distance>30)
{
[Link]("empty");
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
}
else if(distance>=15 && distance<=29)
{
[Link]("half full");
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
}
else
{
[Link]("full");
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin1,LOW);
}
}
Design Circuit:
7
4. Develop an application using RasberyPi to detect an object using IR sensor
and send the message using SMTP protocol
Components required: Raspberry Pi, IR sensor, Jamper Wires
import [Link] as GPIO
import time
import smtplib
import string
sensor = 7
[Link]([Link])
[Link](sensor,[Link],pull_up_down=GPIO.PUD_DOWN)
print "IR Sensor Ready....."
print " "
HOST = "[Link]"
SUBJECT = "IR detected"
TO = "isepesce@[Link]"
FROM = "youremail"
text = "somebody is around your area, IR detected!"
BODY = [Link](("from: %s" %FROM, "to: %s" %TO,"Subject: %s" %SUBJECT,
"huha", text), "\r\n")
s = [Link]("[Link]",587)
s.set_debuglevel(1)
[Link]()
[Link]()
try:
8
while True:
a=[Link](sensor)
if a==True:
[Link]("youremail", "password")
[Link](FROM,[TO],BODY)
except KeyboardInterrupt:
[Link]()
[Link]
Design Circuit:
9
5 a) Write a Program to interface temperature sensor to Arduino UNO and
read the values of temperature and humidity in the given environment and
turn On the LED if temperature value met the threshold value
Components Required: Arduino UNO, DHT sensor, Jumper wires
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
void setup(){
[Link](9600);
delay(500);//Delay to let system boot
[Link]("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT.read11(dht_apin);
[Link]("Current humidity = ");
[Link]([Link]);
[Link]("% ");
[Link]("temperature = ");
[Link]([Link]);
[Link]("C ");
delay(5000);//Wait 5 seconds before accessing sensor again.
}// end loop(
10
Design Circuit:
5.b) Using XCTU software, connect two different motes wirelessly and establish a
duplex communication.
Instructions to Execute XCTU software
1. first press the button IN (microcontroller) and
dump empty code to Motes using Arduino IDE for both sender and receiver
2. Second press the Button out (Zigbee) and open XCTU software
3. In XCTU, select discover Radio devices
4. Add selected devices
5. click on devices displayed
6. give PAN iD (Same PAN ID for Sender and Receiver)
7. For Sender: Enable coordinator and DH address :0 and DL address: FFFF
8. For receiver: DH and DL address :0
9. After all the changes, click on WRITE
11
10. click on console screen
11. click on open for sender and receiver
6. Write a Program to interface LPG sensor to Arduino UNO and read the values
of a sensor in the given environment and turn On the buzzer if petroleum gas is
detected value.
Components Required: Arduino UNO, LPG Sensor, Buzzer
int pot=A0;
intval=0;
int led=12;
void setup() {
// put your setup code here, to run once:
[Link](9600);
pinMode(pot,INPUT);
pinMode(led,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
val=analogRead(pot);
[Link](val);
delay(1000);
if(val< 300)
{
digitalWrite(led,HIGH);
delay(1000);
12
}
else
{
digitalWrite(led,LOW);
delay(1000);
}
}
Design Circuit:
13
7. Write a program to interface soil sensor to RasberyPi and transmit received data
to cloud using Ethernet/WiFi
Components Required: Raspberry Pi, npk soil sensor, jumper wires
MQTT_BROKER = "[Link]"
MQTT_PORT = 1883 # Default MQTT Port is 1883
MQTT_KEEPALIVE_INTERVAL = 45 # In seconds
MQTT_USER_NAME = " "
MQTT_USER_PASSWORD = " "
pub_topic1 ="npk/val"
# Import Time Module
import time
# import Json module
import json
# Import Serial Module
import serial
# Import PahoMQTT Module
import [Link] as mqtt
rc = 0 # For error checking
serial_data = [Link](port='/dev/ttyS0', baudrate=9600,timeout=None)
# Define on_connect event Handler
defon_connect(client, userdata, flags, rc):
pass
# Define on_publish event Handler
defon_publish(client, userdata, mid):
pass
# Initiate MQTT Client
mqttc = [Link]()
14
# Connect with MQTT Broker
mqttc.username_pw_set(MQTT_USER_NAME, MQTT_USER_PASSWORD)
[Link](MQTT_BROKER, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Register Event Handlers
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
#mqttc.on_message = on_message
rc = 0
i=0
while rc == 0:
try:
rc = [Link]()
# ---- #Read Data from Real Sensors #----
data = serial_data.readline()
[Link](pub_topic1 , data)
[Link](1)
print(data)
#print("NPK %s is Published" % data)
except KeyboardInterrupt:
print("\nThe Process is Terminated by the User!")
break
15
Design Circuit:
16