Iot Lab Programs
Iot Lab Programs
Lab program 1a
name = input("Please Enter Your Name Here:\n") #get the input from console
Lab program 1b
## A Program to read two numbers and display sum, difference, product and
division
num1 = input()
num2 = input()
print("************************************")
print("************************************")
Lab program 1C
sentence = input()
words = sentence.split()
word_count = 0
character_count = 0
word_count += 1
character_count += len(word)
Lab program 1d
'''
1. Rectangle
2. Triangle
3. Circle
'''
while True:
print("*****************************************")
print(""" 1. Rectangle
2. Triangle
3. Circle
4. Exit """)
choice = input()
if(choice == '1'):
width = int(input())
height = int(input())
continue
elif(choice == '2'):
height = int(input())
continue
elif(choice == '3'):
radius = int(input())
continue
elif(choice == '4'):
break
else:
continue
print("Thank You")
lab program 1e
## A Program to print a name n times where name and n values has to be input
from standard input
print("Enter Your name : ")
name = input()
n = int(input())
for i in range(n):
print(name)
lab program 1f
num1 = int(input())
num2 = int(input())
try:
result = num1/num2
except ZeroDivisionError:
lab program 1g
import time
# seconds passed since epoch
for i in range(10):
time.sleep(10)
lab program 1h
Lines = file1.readlines() #to read the each line of file1 ,if we want only one line
of file1 then write readlines(1)
i=0
i += 1
count = len(line.split())
# Program to Take input from two switches and switch on corresponding LEDs.
import time
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(led1,gpio.OUT,initial=1)
gpio.setup(led2,gpio.OUT,initial=1)
gpio.setup(switch1,gpio.IN)
gpio.setup(switch2,gpio.IN)
def glow_led(event):
if event == switch1 :
gpio.output(led1, False)
time.sleep(3)
gpio.output(led1, True)
gpio.output(led2, False)
time.sleep(3)
gpio.output(led2, True)
try:
while(True):
time.sleep(1)
except KeyboardInterrupt:
gpio.cleanup()
lab program 3
# ALS Bengaluru
# Program to switch ON and Switch off led for corresponding delay times provided
in file.
import time
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(led1,gpio.OUT,initial=1)
Lines = file1.readlines()
ON_TIME = int(Lines[0].split("=")[1])
OFF_TIME = int(Lines[1].split("=")[1])
try:
while(True):
gpio.output(led1,False)
time.sleep(ON_TIME)
gpio.output(led1,True)
time.sleep(OFF_TIME)
except KeyboardInterrupt:
gpio.cleanup()
lab program 4
#minute Hour Day of the month month Day of the week <command to execute>
#example
#0 12 13 * * python3 /home/pi/MCASyllabus/10.py
import time
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
relay1 = 38 #P38
gpio.setup(relay1,gpio.OUT,initial=0)
try:
gpio.output(relay1, True)
time.sleep(15)
gpio.output(relay1, False)
except KeyboardInterrupt:
gpio.cleanup()
print("Program Exited")
lab program 5
import datetime
camera = PiCamera()
camera.start_preview()
sleep(3)
camera.capture('/home/pi/Desktop/MCASyllabus/images/'+current_date+'.jpg')
camera.stop_preview()
print("Image captured")
#sleep(2)
Lab program 6
# Create a folder "templates" and write your html pages in this folder.
import time
import datetime
led = 13 #P15
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(led, GPIO.OUT,initial=1) #Initialising GPIO pins as output
GPIO.setup(led,GPIO.OUT)
app = Flask(__name__)
def hello_world():
return render_template('web.html')
def redledon():
now = datetime.datetime.now()
templateData = {
'status' : 'ON',
'time': timeString
}
return render_template('web.html', **templateData)
def redledoff():
now = datetime.datetime.now()
templateData = {
'status' : 'OFF',
'time': timeString
if __name__ == "__main__":
lab program 7
# Commands to install
#Plaese use General email for sending emails as it is having few security issues.
import RPi.GPIO as gpio
import picamera
import time
import smtplib
toaddr = "[email protected]"
mail = MIMEMultipart()
mail['From'] = fromaddr
mail['To'] = toaddr
mail['Subject'] = "Attachment"
led=15
pir=12
HIGH=1
LOW=0
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
data=""
def sendMail(data):
mail.attach(MIMEText(body, 'plain'))
print(data)
dat='%s.jpg'%data
print(data)
image=MIMEImage(attachment.read())
attachment.close()
mail.attach(image)
server.starttls()
server.login(fromaddr, "alsdatasave2017")
text = mail.as_string()
def capture_image():
camera.start_preview()
time.sleep(5)
print(data)
camera.capture('%s.jpg'%data)
camera.stop_preview()
time.sleep(1)
sendMail(data)
gpio.output(led , 0)
camera = picamera.PiCamera()
camera.rotation=180
camera.awb_mode= 'auto'
camera.brightness=55
while 1:
if gpio.input(pir)==1:
gpio.output(led, HIGH)
capture_image()
while(gpio.input(pir)==1):
time.sleep(1)
else:
gpio.output(led, LOW)
time.sleep(0.01)
Lab program 8
import time
import datetime
app = Flask(__name__)
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(switch1,gpio.IN)
light_status = "OFF"
def glow_led(event):
print("Entered Here")
global light_status
gpio.output(led1, False)
light_status = "ON"
gpio.output(led1, True)
light_status = "OFF"
def ledstatus():
now = datetime.datetime.now()
templateData = {
'status' : light_status,
'time': timeString
lab program 9 a
#!/usr/bin/env python3
import socket
import Adafruit_MCP3008
import time
SPI_PORT = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT,
SPI_DEVICE))
try:
s.bind((HOST, PORT))
s.listen()
with conn:
while True:
value = mcp.read_adc(0)
if(value >300):
data = "Alert".encode('utf-8')
conn.sendall(data)
time.sleep(3)
except KeyboardInterrupt:
s.close()
GPIO.cleanup()
Lab program 9b
import socket
import time
Buzzer = 36
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.setwarnings(False)
try:
s.connect((HOST,PORT))
while True:
data = s.recv(1024).decode('utf-8')
print(data)
if(str(data) == 'Alert'):
GPIO.output(36, True)
time.sleep(3)
GPIO.output(36, False)
time.sleep(3)
except KeyboardInterrupt:
s.close()
GPIO.cleanup()