Table of Content
Table of Content
Table of Content
i
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.
ii
BTU SYLLABUS
1. 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.
(a) Read your name and print Hello message with name
(b) Read two numbers and print their sum, difference, product and division.
(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.
(a) Print a name ’n’ times, where name and n are read from standard input, using for and
while loops.
(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.
iii
LIST OF FIGURE
iv
PROGRAM LISTING
v
LAB INTRODUCTION
This manual 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 experiment are conducted on
Raspberry Pi - a single board computer. A brief introduction of Raspberry Pi is given below.
Raspberry Pi
The Raspberry Pi is a very cheap computer that runs Linux, but it also provides a set of GPIO
(general purpose input/output) pins that allow you to control electronic components for physical
computing and explore the Internet of Things (IoT). Raspberry Pi is used to learn programming
skills, build hardware projects, do home automation, and industrial applications.
Raspi Board
GPIO header
GPIO stands for General Purpose Input Output, which has been brought out to pin connectors
present on the board. We can read values from any other peripherals, such as sensors, and compute
the received values in your own programs. Apart from reading the values, we can show the result
of the program by connecting LEDs or embedded LCD displays to the board. Depending on the
decision taken in the code, we can drive a motor connected on GPIO through a motor driver circuit
vi
Figure 2: Important components of raspberry Pi
It carries the video signal, which is the type output on the RasPi. The RCA connector or composite
video signal is merged with a 3.5 mm audio jack on RasPi 1 model A+ and model B+ and RasPi 2
model B.
If you are not using the HDMI connection the audio can be played through speakers or headphones
using a standard 3.5 mm jack. In RasPi 1 model B+ or RasPi 2 model B, audio jack being the
combination of composite and audio has all the functionalities of composite video and audio out.
USB
his is the most common connector, widely used in the modern computers, and hence called the
Universal Serial Bus. You can connect your flash drives, keyboard, Wi-Fi dongles, and mouse to
play around with the RasPi. You can also connect the externally powered USB hub with RasPi to
connect more USB-based peripherals on it.
Ethernet
This is one of the most important connections to have a remote login on RasPi and to provide wired
internet connection.We cannot always connect RasPi to the dedicated display, so we use the remote
login, and we see the entire desktop or Command-line Interface (CLI) of RasPi on our computer
screen.
vii
CSI camera connector
The RasPi board does not come with camera module integrated, but a separately bought camera
module can be interfaced using the CSI connector via a 15 cm flex cable. The 5-megapixel
Raspberry Pi camera module can be used to record high-definition videos as well as still photographs.
This camera module provides improved performance over a USB-connected camera.
HDMI connector
viii
EXPERIMENT 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 :
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
1
cd : Change the shell working directory.
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
2
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 and bar.
tar -tvf archive.tar # List all files in archive.tar verbosely.
tar -xf archive.tar # Extract all files from archive.tar.
Viva Question
3
EXPERIMENT NO. 2
2. Read two numbers and print their sum, difference, product and division.
Apparatus Required :
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 division.
1 # Read the two numbers and print their sum , difference , product and
division
2
3 import sys
4
9 try :
10 a = float ( sys . argv [1])
11 b = float ( sys . argv [2])
12 except ValueError :
4
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 Question
5
EXPERIMENT NO. 3
2. Area of a given shape (rectangle, triangle and circle) reading shape and appropriate values
from standard input.
Apparatus Required :
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
6
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
19 def triangle (a , b , c ) :
20 p = a + b + c
21 s = p /2
22 area = math . sqrt ( s *( s - a ) *( s - b ) *( s - c ) )
23 return { " area " : area , " perimeter " : p }
24
25 def circle ( r ) :
26 perimeter = 2* math . pi * r
27 area = math . pi * r * r
28 return { " area " : area , " perimeter " : perimeter }
29
7
43 ret = rectangle ( args . para [0] , args . para [1])
44 print ( f " Perimeter : { ret [ ' perimeter ']} " )
45 print ( f " Area : { ret [ ' area ']} " )
46 else :
47 ret = circle ( args . para [0])
48 print ( f " Perimeter : { ret [ ' perimeter ']} " )
49 print ( f " Area : { ret [ ' area ']} " )
Viva Question
8
EXPERIMENT NO. 4
1. Print a name ’n’ times, where name and n are read from standard input, using for and while
loops.
Apparatus Required :
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
15
9
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 ) )
1 # Lab 5( ii )
2 # Handle Divided by Zero Exception .
3
Viva Question
10
EXPERIMENT NO. 5
2. Read a file line by line and print the word count of each line.
Apparatus Required :
python program: Print current time for 10 times with an interval of 10 seconds.
1 # ############## Practical : ######################
2 # Print current time for 10 times with an interval of 10 seconds .
3 # ---- ----- ---- ----- ----- ---- ----- ---- ----- ----- ---- ----- ---- -----
4
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
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 .
11
3 # ----- ----- ----- ----- ------ ----- ----- ----- ------ ----- ----- -----
4 import sys
5
15
Viva Question
12
EXPERIMENT NO. 6
Apparatus Required :
7 LED = 10
8
9 def led_setup () :
13
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
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
14
19
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 Question
15
2. Explain working of LED.
16
EXPERIMENT 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 :
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
17
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 Question
18
EXPERIMENT 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 :
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
19
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
20
62 # $ python flash_led . py
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
28
29
21
34 from time import sleep
35
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
Viva Question
22
5. what is the use of cron command ?
23