IOT Lab Manual
IOT Lab Manual
AND ENGINEERING
LAB MANUAL
B.Tech. VII Semester
INTERNET OF THINGS
7CS4-21
Mission
C
M1: To provide state of the art facility for academic excellence that
caters to regional, national and global challenges.
R
M2: To identify emerging areas of technology that can provide
innovative and sustainable solutions to global challenges.
M3: To provide an environment of industry oriented research and
IT
development
M4: To inculcate core values of professional ethics, team-work, inter
personal skills and life-Long learning for sustainable development
of the society.
M
Vision and Mission of the Department
Vision
To create competent software professionals and leaders for
serving society and world at large through excellence in academics,
research, development and innovations.
Mission
C
Mission is to
C
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
R
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
IT
modern engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
M
C
R
IT
M
Course Outcomes (Cos)
Course Outcomes: Upon successful completion of the course/Lab the students will be
able to
C
CO4: Operate various IoT devices using various communication protocol.
TABLE OF CONTENT
S.No Content Page No
Vision and Mission of the Institute i
Vision and Mission of the Department ii
Program Outcomes (POs) iii
Course Outcomes (Cos) v
Lab Instruction vi
BTU Syllabus vii
Lab Introduction viii
Start Raspberry Pi and try various Linix commands in com-
mand terminal window: ls, cd, touch, mv, rm, man, mkdir,
1 1-3
rmdir, tar, gzip, cat, more, less, ps, sudo, cron, chown,
chgrp, ping etc.
Run some python programs on Pi like: (a)Read your name
2 and print Hello message with name (b)Read two numbers 4-6
and print their sum, difference, product and division.
(a)Word and character count of a given string. (b)Area of
3 a given shape (rectangle, triangle and circle) reading shape 7-9
and appropriate values from standard input.
(a) Print a name ’n’ times, where name and n are read from
4 standard input, using for and while loops. (b) Handle Di- 10-12
vided by Zero Exception.
(a) Print current time for 10 times with an interval of 10
5 seconds. (b) Read a file line by line and print the word 13-15
count of each line.
(a) Light an LED through Python program (b) Get input
6 16-17
from two switches and switch on corresponding LEDs
Flash an LED at a given on time and off time cycle, where
7 18-22
the two times are taken from a file.
(a) Flash an LED based on cron output (acts as an alarm)
8 (b) Switch on a relay at a given time using cron, where the 23-27
relay’s contact terminals are connected to a load.
LAB INSTRUCTIONS
3. Always follow the instruction given by concerned faulty to perform the assigned
experiment.
5. Do not switch off the power supply of the PCs directly, first shut down the PCs then
switch off power supply.
6. Every student is responsible for any damage to the PCs or its accessories which is
assigned for lab work.
8. Always bring your lab file and the task assigned to you must be completed.
9. Experiment performed by you should be positively checked in next turn after that
faculty may not check your work.
10. Please mention your roll number, name, node number and signature in lab register.
BTU SYLLABUS
S.No Content
Start Raspberry Pi and try various Linix commands in command ter-
1 minal window: ls, cd, touch, mv, rm, man, mkdir, rmdir, tar, gzip, cat,
more, less, ps, sudo, cron, chown, chgrp, ping etc.
Run some python programs on Pi like:
(a) Read your name and print Hello message with name
(b) Read two numbers and print their sum, difference, product and divi-
2 sion.
(c) Word and character count of a given string.
(d) Area of a given shape (rectangle, triangle and circle) reading shape
and appropriate values from standard input.
Run some python programs on Pi like:
(a) Print a name ’n’ times, where name and n are read from standard
input, using for and while loops.
3
(b) Handle Divided by Zero Exception.
(c) Print current time for 10 times with an interval of 10 seconds.
(d) Read a file line by line and print the word count of each line.
(a) Light an LED through Python program
(b) Get input from two switches and switch on corresponding LEDs
4
(c) Flash an LED at a given on time and off time cycle, where the two
times are taken from a file.
(a) Flash an LED based on cron output (acts as an alarm)
(b) Switch on a relay at a given time using cron, where the relay’s con-
5
tact terminals are connected to a load.
(c) Get the status of a bulb at a remote place (on the LAN) through web.
LAB INTRODUCTION
This lab is intended for the fourth year students of engineering branches in the subject
of Internet of Things. This manual typically contains practical/Lab Sessions related IoT
covering various aspects related to the subject to enhance understanding. The experiments
are conducted on Raspberry Pi - a single board computer. Student will learn how to setup
the board and run various python program on it. They will also learn interfacing the board
with different sensors and actuators. This lab will provide basic understanding of IoT
device and architecture.
Program No. 1
Objective: Start Raspberry Pi and try various Linix commands in command terminal
window:
ls, cd, touch, mv, rm, man, mkdir, rmdir, tar, gzip, cat, more, less, ps, sudo, cron, chown,
chgrp, ping etc
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
Linux Commands
• ls -l
The -l tag is used to display a detailed version of every file and directory available
inside the current working directory.
• ls -a, --all
touch : Update the access and modification times of each FILE to the current time. A
FILE argument that does not exist is created empty, unless -c or -h is supplied.
rm [OPTION]... [FILE]...
• -d, --dir
tar : an archiving program designed to store multiple files in a single file (an archive),
and to manipulate such archives.
tar -cf archive.tar foo bar # Create archive.tar from files foo a
tar -tvf archive.tar # List all files in archive.tar verbo
tar -xf archive.tar # Extract all files from archive.tar.
Viva Voce
1. What is raspberry pi?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 2
2. Read two numbers and print their sum, difference, product and division.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
Python Program: Read your name and print Hello message with name
1 ############### PRACTICAL 1 ##########################
2 # Read your name and print Hello message with name
3
Python Program: Read two numbers and print their sum, difference, product and divi-
sion.
1 # Read the two numbers and print their sum, difference, product and
division
2
3 import sys
4
5 if len(sys.argv) != 3:
6 print("Give two number as an argument")
7 sys.exit()
8
9 try:
10 a = float(sys.argv[1])
11 b = float(sys.argv[2])
12 except ValueError:
13 print("Enter only number as argument")
14
15 summ = a + b
16 print(f"Sum of two number is: {summ}")
17 diff = a - b
18 print(f"Difference of two number is: {diff}")
19 prod = a*b
20 print(f"Product of given two number is: {prod}")
21
22 try:
23 div = a/b
24 print(f"Divison of given two number is: {div}")
25 except ZeroDivisionError:
26 print("Second argument must be non zero for division")
Viva Voce
1. How you get command argument in python?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 3
2. Area of a given shape (rectangle, triangle and circle) reading shape and appropriate
values from standard input.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
4 string=input("Enter string:")
5 char=0
6 word=1
7 for i in string:
8 char=char+1
9 if(i==’ ’):
10 word=word+1
11 print("Number of words in the string:")
12 print(word)
13 print("Number of characters in the string:")
14 print(char)
Python Program: Area of a given shape (rectangle, triangle and circle) reading shape
and appropriate values from standard input.
1 ############# EXPERIMENT 2.D ###################
2 # Area of a given shape(rectangle, triangle, and circle) reading shape
and
3 # appropriate values from standard input.
4
5 import math
6 import argparse
7 import sys
8
9 def rectangle(x,y):
10 """
11 calculate area and perimeter
12 input: length, width
13 output: dict - area, perimeter
14 """
15 perimeter = 2*(x+y)
16 area = x*y
17 return {"area": area, "perimeter": perimeter}
18
25 def circle(r):
26 perimeter = 2*math.pi*r
27 area = math.pi*r*r
28 return {"area": area, "perimeter": perimeter}
29
30 if __name__ == "__main__":
31 choices = ["tri", "circ", "rect"]
32 parser = argparse.ArgumentParser(
33 description="Calculate Area of Basic geometry")
Viva Voce
1. How you will get variable length argument in python function?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 4
1. Print a name ’n’ times, where name and n are read from standard input, using for
and while loops.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
python program: Print a name ’n’ times, where name and n are read from standard
input, using for and while loops.
1 # Print a name ’n’ times, where name and n are read from standard input
,
2 # using for and while loops.
3 import argparse
4
13 print(f"{name}")
14
15
16 if __name__ == "__main__":
17 parser = argparse.ArgumentParser("Demontrating for and while loop
of python")
18 parser.add_argument(’name’)
19 parser.add_argument(’number’)
20 args = parser.parse_args()
21 print("\nPrinting using While loop:")
22 name_in_while_loop(args.name, int(args.number))
23 print("\nPrinting using for loop:")
24 name_in_for_loop(args.name, int(args.number))
8 if __name__ == "__main__":
9 a = int(input("Enter Dividend: "))
10 b = int(input("Enter Divisor: "))
11 try:
12 quotient = divider(a, b)
13 print(f"Quotient is: {quotient}")
14 except ZeroDivisionError:
15 print("Zero is big mystery, Enter other divisor")
16 finally:
17 print("END")
Viva Voce
1. How you handle the exception in python?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 5
2. Read a file line by line and print the word count of each line.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
python program: Print current time for 10 times with an interval of 10 seconds.
5 import time
6
7 def current_time():
8 for i in range(10):
9 ct = time.ctime()
10 print(f"current time is: {ct}")
11 time.sleep(10)
12
13
14
15 if __name__=="__main__":
16 current_time()
python program: Read a file line by line and print the word count of each line.
1 # Program:
2 # Read a file line by line and print the word count of each line.
3 #--------------------------------------------------------------
4 import sys
5
6 def count_word(line):
7 word = 1
8 for i in line:
9 if i==’ ’:
10 word += 1
11 return word
12
13 f = sys.argv[1]
14
15
16 if __name__=="__main__":
17 l = 1
18 with open(f) as fobj:
19 for line in fobj:
20 print(f"Line {l} => No of Words: {count_word(line)}")
21 l += 1
Viva Voce
1. What is the module for working with CSV files. ?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 6
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
7 LED = 10
8
9 def led_setup():
10 GPIO.setwarnings(False)
11 GPIO.setmode(GPIO.BOARD)
12 GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
13
14 def main():
15 led_setup()
16 while True:
17 GPIO.output(LED, GPIO.HIGH)
18 sleep(1)
19 GPIO.output(LED, GPIO.LOW)
20 sleep(1)
21
22 if __name__=="__main__":
23 try:
24 main()
25 finally:
26 GPIO.cleanup()
27 print("Closed Everything. END")
Python Program : Get input from two switches and switch on corresponding LEDs
1 # Program:
2 # Get input from two switches and switch on corresponding LEDs
3 #
4 import RPi.GPIO as GPIO
5 import time
6
7 LED1 = 8
8 LED2 = 12
9 BTN1 = 32
10 BTN2 = 36
11
12 def gpio_setup():
13 GPIO.setmode(GPIO.BOARD)
14 GPIO.setup(BTN1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
15 GPIO.setup(LED1, GPIO.OUT, initial=GPIO.LOW)
16
17 def led_on(LED):
18 GPIO.output(LED, GPIO.HIGH)
19
20 def led_off(LED):
21 GPIO.output(LED, GPIO.LOW)
22
23 def led_toggle(LED):
24 if GPIO.input(LED):
25 led_off(LED)
26 else:
27 led_on(LED)
28
41 def main():
42 gpio_setup()
43 while True:
44 press_2_on(LED1, BTN1)
45 press_2_toggle(LED2, BTN2)
46
47
48 try:
49 main()
50 finally:
51 GPIO.cleanup()
52 print("closed Event")
Viva Voce
1. What is push and pull resister?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 7
1. Flash an LED at a given on time and off time cycle, where the two times are taken
from a file.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
Python Program : Flash an LED at a given on time and off time cycle, where the two
times are taken from a file.
1 # Program:
2 # Flash an LED at a given on time and off time cycle,
3 # where the two times are taken from a file.
4 #
5 import time
6 import csv
7 import sys
8 import RPi.GPIO as GPIO
9 from time import sleep
10
11
12 def setup():
13 GPIO.setwarnings(False)
14 GPIO.setmode(GPIO.BOARD)
15 GPIO.setup(10, GPIO.OUT, initial=GPIO.LOW)
16
17 def read_time():
18 timing = []
19 with open(sys.argv[1], ’rt’) as f:
20 reader = csv.reader(f)
21 for row in reader:
22 timing.append((row[0], row[1]))
23 return timing[1:]
24
25 def main():
26 setup()
27 time_seq = read_time()
28 for on, off in time_seq:
29 GPIO.output(10, GPIO.HIGH)
30 print("LED ON")
31 time.sleep(float(on))
32 GPIO.output(10, GPIO.LOW)
33 print("LED OFF")
34 time.sleep(float(off))
35
36 try:
37 main()
38 finally:
39 GPIO.cleanup()
40 print("END")
Viva Voce
1. How you write output on GPIO pin ?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.
Program No. 8
2. Switch on a relay at a given time using cron, where the relay’s contact terminals are
connected to a load.
Apparatus Required
1. Computer
2. Raspberry Pi Board
3. USB Connector
4. Ethernet Connector
4 # alarm.py
5 # act as alarm for cron
6 import RPi.GPIO as GPIO
7 from time import sleep
8
9 ALARM = 10
10
11 def led_setup():
12 GPIO.setwarnings(False)
13 GPIO.setmode(GPIO.BOARD)
14 GPIO.setup(ALARM, GPIO.OUT, initial=GPIO.LOW)
15
16 def main():
17 led_setup()
18 GPIO.output(ALARM, GPIO.HIGH)
19 sleep(60)
20
21 if __name__=="__main__":
22 try:
23 main()
24 finally:
25 GPIO.cleanup()
26 print("Closed Everything. END")
27
28
29
36 LED = 10
37
38 def led_setup():
39 GPIO.setwarnings(False)
40 GPIO.setmode(GPIO.BOARD)
41 GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
42
43 def main():
44 led_setup()
45 alarm = GPIO.input(10)
46 while alarm:
47 GPIO.output(LED, GPIO.HIGH)
48 sleep(1)
49 GPIO.output(LED, GPIO.LOW)
50 sleep(1)
51
52 if __name__=="__main__":
53 try:
54 main()
55 finally:
56 GPIO.cleanup()
57 print("Closed Everything. END")
58
Python Program : Switch on a relay at a given time using cron, where the relay’s contact
terminals are connected to a load.
1 # Flash an LED based on cron output (acts as an alarm)
2
4 # alarm.py
5 # act as alarm for cron
6 import RPi.GPIO as GPIO
7 from time import sleep
8
9 ALARM = 10
10
11 def led_setup():
12 GPIO.setwarnings(False)
13 GPIO.setmode(GPIO.BOARD)
14 GPIO.setup(ALARM, GPIO.OUT, initial=GPIO.LOW)
15
16 def main():
17 led_setup()
18 GPIO.output(ALARM, GPIO.HIGH)
19 sleep(60)
20
21 if __name__=="__main__":
22 try:
23 main()
24 finally:
25 GPIO.cleanup()
26 print("Closed Everything. END")
27
28
29
36 LED = 10
37
38 def led_setup():
39 GPIO.setwarnings(False)
40 GPIO.setmode(GPIO.BOARD)
41 GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
42
43 def main():
44 led_setup()
45 alarm = GPIO.input(10)
46 while alarm:
47 GPIO.output(LED, GPIO.HIGH)
48 sleep(1)
49 GPIO.output(LED, GPIO.LOW)
50 sleep(1)
51
52 if __name__=="__main__":
53 try:
54 main()
55 finally:
56 GPIO.cleanup()
57 print("Closed Everything. END")
58
Viva Voce
1. What is the use of Relay?
Reference
1. King, Andy. Programming the Internet of Things. “O’Reilly Media, Inc.", 2021.
2. Lea, Perry. IoT and Edge Computing for Architects: Implementing edge and IoT
systems from sensors to clouds with communication systems, analytics, and security.
Packt Publishing Ltd, 2020.