0% found this document useful (0 votes)
17 views17 pages

Iot Lab Manual

The document outlines various mini projects for an IoT laboratory, including Python programs for Raspberry Pi that perform tasks such as reading user input, calculating areas, handling exceptions, controlling LEDs with switches, and sending email alerts. It also covers web-based control of devices and monitoring their status through a web interface. Each project includes code snippets, expected outputs, and circuit diagrams where applicable.

Uploaded by

shivuhunji
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)
17 views17 pages

Iot Lab Manual

The document outlines various mini projects for an IoT laboratory, including Python programs for Raspberry Pi that perform tasks such as reading user input, calculating areas, handling exceptions, controlling LEDs with switches, and sending email alerts. It also covers web-based control of devices and monitoring their status through a web interface. Each project includes code snippets, expected outputs, and circuit diagrams where applicable.

Uploaded by

shivuhunji
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/ 17

IOT LABORATORY WITH MINI PROJECT 22MCAL37

1. Run some python programs on Pi like:


a. Read your name and print Hello message with name
Program
name = input("Enter your name: ")
print("Hello ",name)

Output
Enter your name: Yogeesh S
Hello Yogeesh S

b. Read two numbers and print their sum, difference, product and division.
Program
num1 = int(input("Enter a value for num1: "))
num2 = int(input("Enter a value for num2: "))
print("Sum: ",num1+num2)
print("Difference: ",num1-num2)
print("Product: ",num1*num2)
print("Division: ",num1/num2)

Output
Enter a value for num1: 6
Enter a value for num2: 3
Sum: 9
Difference: 3
Product: 18
Division: 2.0

c. Word and character count of a given string


Program
string = input("Enter a string: ")
print("Word count = ",len(string.split(" ")))
print("Character count = ",len(string))

Output
Enter a string: Hello i am yogeesh
Word count = 4
Character count = 18

YOGEESH S 1
IOT LABORATORY WITH MINI PROJECT 22MCAL37

d. Area of a given shape (rectangle, triangle and circle) reading shape and
appropriate values from standard input
Program
print("1. Rectangle\n2. Triangle\n3. Circle\nPress any
other key to exit ")
while True:
choice = input("Enter a choice: ")
if(choice == '1'):
w = int(input("Enter the width: "))
h = int(input("Enter the height: "))
print("The area of rectangle is ", w*h)
elif(choice == '2'):
b = int(input("Enter breadth: "))
h = int(input("Enter height: "))
print("The area of triangle: ", 0.5*b*h)
elif(choice == '3'):
radius = int(input("Enter radius: "))
print("The area of circle: ",
3.14*radius*radius)
continue
else:
break

Output
1. Rectangle
2. Triangle
3. Circle
Press any other key to exit
Enter a choice: 1
Enter the width: 5
Enter the height: 6
The area of rectangle is 30
Enter a choice: 2
Enter breadth: 6
Enter height: 7
The area of triangle: 21.0
Enter a choice: 3
Enter radius: 5
The area of circle: 78.5
Enter a choice: 4

YOGEESH S 2
IOT LABORATORY WITH MINI PROJECT 22MCAL37

e. Print a name ‘n’ time, where name and n are read from standard input,
using for and while loops.
Program
name = input("Enter your name: ")
n = int(input("Enter how many times to print a name: "))
while n>0:
print(name)
n = n-1

Output
Enter your name: Yogeesh S
Enter how many times to print a name: 3
Yogeesh S
Yogeesh S
Yogeesh S

f. Handle Divided by Zero Exception.


Program
x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
try:
print("x / y = ",x/y)
except ZeroDivisionError:
print("The value of y not to be zero")

Enter a value for x: 5


Enter a value for y: 0
The value of y not to be zero

YOGEESH S 3
IOT LABORATORY WITH MINI PROJECT 22MCAL37

g. Print current time for 10 times with an interval of 10 seconds.


Program
import time
n = 10
while n>0:
print(time.ctime(time.time()))
time.sleep(10)
n = n-1

Output
Mon Jan 29 21:34:03 2024
Mon Jan 29 21:34:13 2024
Mon Jan 29 21:34:23 2024
Mon Jan 29 21:34:33 2024
Mon Jan 29 21:34:43 2024
Mon Jan 29 21:34:53 2024
Mon Jan 29 21:35:03 2024
Mon Jan 29 21:35:13 2024
Mon Jan 29 21:35:23 2024
Mon Jan 29 21:35:33 2024

h. Read a file line by line and print the word count of each line
Program
lines = open("Program1.txt",'r').readlines()
for i in range(len(lines)):
print (f"Line {i+1} Words : {len(lines[i].split(" "))}")

Output
Line 1 Words : 5
Line 2 Words : 4
Line 3 Words : 3

YOGEESH S 4
IOT LABORATORY WITH MINI PROJECT 22MCAL37

2. Input from two switches and switch on corresponding LEDs

Program
import time
import RPi.GPIO as gpio

gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)

led1 = 15
led2 = 13
switch1 = 37
switch2 = 35

gpio.setup(led1,gpio.OUT,initial=0)
gpio.setup(led2,gpio.OUT,initial=0)
gpio.setup(switch1,gpio.IN)
gpio.setup(switch2,gpio.IN)

def glow_led(event):
if event == switch1:
gpio.output(led1, True)
time.sleep(3)
gpio.output(led1, False)

elif event == switch2:


gpio.output(led2, True)
time.sleep(3)
gpio.output(led2, False)

gpio.add_event_detect(switch1, gpio.RISING , callback =


glow_led, bouncetime = 1)
gpio.add_event_detect(switch2, gpio.RISING , callback =
glow_led, bouncetime = 1)

try:
while(True):
time.sleep(1)

NOTE: The below line in program it’s a single line statement

gpio.add_event_detect(switch1, gpio.RISING , callback =


glow_led, bouncetime = 1)

YOGEESH S 5
IOT LABORATORY WITH MINI PROJECT 22MCAL37

Circuit

YOGEESH S 6
IOT LABORATORY WITH MINI PROJECT 22MCAL37

3. Flash an LED at a given on time and off time cycle, where the two
times are taken from a file

Program

import time
import RPi.GPIO as gpio

gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)

led = 15
gpio.setup(led,gpio.OUT,initial=0)

file1 = open('program3.txt', 'r')


Lines = file1.readlines()

ON_TIME = int(Lines[0].split("=")[1])
OFF_TIME = int(Lines[1].split("=")[1])

try:
while(True):
gpio.output(led,True)
time.sleep(ON_TIME)
gpio.output(led,False)
time.sleep(OFF_TIME)
except KeyboardInterrupt:
gpio.cleanup()

Program3.txt
ontime = 5
offtime = 7

YOGEESH S 7
IOT LABORATORY WITH MINI PROJECT 22MCAL37

Circuit

YOGEESH S 8
IOT LABORATORY WITH MINI PROJECT 22MCAL37

4. Switch on a relay at a given time using cron, where the relay’s contact
terminals are connected to a load.
Program

import RPi.GPIO as gpio


import time

gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)

relay = 38
gpio.setup(relay, gpio.OUT)

try:
gpio.output(relay, False)
time.sleep(10)
gpio.output(relay, True)

except KeyboardInterrupt:
gpio.cleanup()

Crontab Setting

1. GOTO Terminal
2. TYPE select-editor
1. /bin/nano <---- easiest
2. /usr/bin/vim.tiny
3. /bin/ed
3. ENTER 2
4. TYPE crontab -e
go end and press i
02 12 * * * python3 /home/yogeesh/Programs/program4.py
5. Press ESCAPE button
6. ENTER :wq

YOGEESH S 9
IOT LABORATORY WITH MINI PROJECT 22MCAL37

Circuit

YOGEESH S 10
IOT LABORATORY WITH MINI PROJECT 22MCAL37

5. Access an image through a Pi web cam

Program for Rasberry PI 3


from picamera import PiCamera
import time
camera = PiCamera()
camera.start_preview()
time.sleep(5)
camera.capture(time.ctime(time.time())+'.jpg')
camera.stop_preview()

Program for Rasberry PI 4 (Pi Cam 2)

from picamera2 import Picamera2


import time
picam2 = Picamera2()
picam2.start_and_capture_file("time.ctime(time.time())+".jpg")
time.sleep(3)
picam2.stop_preview()

Circuit

YOGEESH S 11
IOT LABORATORY WITH MINI PROJECT 22MCAL37

6. Control a light source using web page.


Python Program
import RPi.GPIO as GPIO
from flask import Flask, render_template

led = 15
app = Flask(__name__)

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(led, GPIO.OUT, initial=0)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/ledon')
def ledon():
GPIO.output(led, GPIO.HIGH)
return 'LED on'

@app.route('/ledoff')
def ledoff():
GPIO.output(led, GPIO.LOW)
return 'LED off'

if __name__ == "__main__":
app.run(debug=False)

NOTE: File Structure of This Program given below

Your Programs Directory/


|
|--Program6.py
|--templates/
|--index.html

YOGEESH S 12
IOT LABORATORY WITH MINI PROJECT 22MCAL37

HTML Code (index.html)


<html>
<head>
</head>
<body>
<h1>Raspberry PI Remote Control</h1>
<form action="ledon">
<input type="submit" value="LED On">
</form>
<form action="ledoff">
<input type="submit" value="LED Off">
</form>
</body>
</html>
Circuit

YOGEESH S 13
IOT LABORATORY WITH MINI PROJECT 22MCAL37

7. Implement an intruder system that sends an alert to the given email


Program
import RPi.GPIO as GPIO
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

pir = 12
sender_email = "[email protected]"
receiver_email = "[email protected]"
email_password = "****************"

GPIO.setmode(GPIO.BOARD)
GPIO.setup(pir, GPIO.IN)

try:
while True:
if GPIO.input(pir) == GPIO.HIGH:
print("Intruder detected! Sending alert...")
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Intruder Alert"
msg.attach(MIMEText("Motion detected at your
location!", 'plain'))
with smtplib.SMTP('smtp.gmail.com', 587) as
server:
server.starttls()
server.login(sender_email, email_password)
server.sendmail(sender_email, receiver_email,
msg.as_string())
time.sleep(10)
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print("Program terminated by user.")

YOGEESH S 14
IOT LABORATORY WITH MINI PROJECT 22MCAL37

Circuit

YOGEESH S 15
IOT LABORATORY WITH MINI PROJECT 22MCAL37

8. Get the status of a bulb at a remote place (on the LAN) through web.
Python Program
import RPi.GPIO as GPIO
from flask import Flask, render_template

led = 15
app = Flask(__name__)

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(led, GPIO.OUT, initial=0)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/ledon')
def ledon():
GPIO.output(led, GPIO.HIGH)
return 'LED on'

@app.route('/ledoff')
def ledoff():
GPIO.output(led, GPIO.LOW)
return 'LED off'

@app.route('/status')
def status():
if (GPIO.input):
return 'LED on'
else:
return 'LED off'
if __name__ == "__main__":
app.run(debug=False)

NOTE: File Structure of This Program given below

Your Programs Directory/


|
|--Program6.py
|--templates/
|--index.html

YOGEESH S 16
IOT LABORATORY WITH MINI PROJECT 22MCAL37

HTML Code (index.html)


<html>
<head>
</head>
<body>
<h1>Raspberry PI Remote Control</h1>
<form action="/ledon">
<input type="submit" value="LED On">
</form>
<form action="/ledoff">
<input type="submit" value="LED Off">
</form>
<form action="/status">
<input type="submit" value="LED Status">
</form>
</body>
</html>
Circuit

YOGEESH S 17

You might also like