Iot Lab Manual
Iot Lab Manual
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
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
YOGEESH S 3
IOT LABORATORY WITH MINI PROJECT 22MCAL37
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
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)
try:
while(True):
time.sleep(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)
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
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
Circuit
YOGEESH S 11
IOT LABORATORY WITH MINI PROJECT 22MCAL37
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)
YOGEESH S 12
IOT LABORATORY WITH MINI PROJECT 22MCAL37
YOGEESH S 13
IOT LABORATORY WITH MINI PROJECT 22MCAL37
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)
YOGEESH S 16
IOT LABORATORY WITH MINI PROJECT 22MCAL37
YOGEESH S 17