0% found this document useful (0 votes)
45 views20 pages

IOT Labprograms

This document contains code for several Python programs that demonstrate basic input/output functions, calculations, and controlling peripherals like LEDs and relays. The programs include: reading input and printing output; calculating sums, differences, products and divisions of numbers; counting words and characters in a string; calculating area of shapes; printing a name multiple times; handling zero division errors; printing the current time; reading from a file and counting words; turning an LED on and off using a button; flashing an LED at times specified in external files; controlling a relay to turn a lamp on and off; and accessing an image from a Raspberry Pi camera through code.

Uploaded by

Sushant Patil
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)
45 views20 pages

IOT Labprograms

This document contains code for several Python programs that demonstrate basic input/output functions, calculations, and controlling peripherals like LEDs and relays. The programs include: reading input and printing output; calculating sums, differences, products and divisions of numbers; counting words and characters in a string; calculating area of shapes; printing a name multiple times; handling zero division errors; printing the current time; reading from a file and counting words; turning an LED on and off using a button; flashing an LED at times specified in external files; controlling a relay to turn a lamp on and off; and accessing an image from a Raspberry Pi camera through code.

Uploaded by

Sushant Patil
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/ 20

1.

a) Python program to read input<name> and print


hello<name>
name=input("enter your name:\n")
print("hello"+name)

output:

1.b) Python program to read two number and print the


sum,difference,product and division
num1=int(input("enter first number"))
num2=int(input("enter second number"))
print("********************")
print("sum of two numbers is :",num1+num2)
print("Difference of two numbers is :",num1-num2)
print("Multiplication of two numbers is :",num1*num2)
print("Division of two numbers is :",num1/num2)
output:
1.c) Python program to calculate number of characters of
given string
sentence=input("enter a sentence")
words=sentence.split()
word_count=0
char_count=0
for word in words:
word_count+=1
char_count+=len(word)
print("Total number of words in sentence are:",word_count)
print("Total number of characters without spaces are:",char_count)
print("Total number of characters with spaces
are:",char_count+word_count-1)
output:

1.d) Python program to print area of shapes


while True:
print("**************")
print("""SELECT YOUR SHAPE
1.Rectangle
2.triangle
3.circle
4.exit""")
ch=input()
if(ch=='1'):
width=int(input("enter width in meters"))
height=int(input("enter height in meter"))
print("Area of Rectangle is :",width*height)
continue
elif(ch=='2'):
base=int(input("enter base in meters"))
height=int(input("enter height in meter"))
print("Area of Triangle is :",0.5*base*height)
continue
elif(ch=='3'):
radius=int(input("enter Radius in meters"))
print("Area of circle is :",3.14*radius*radius)
continue
elif(ch=='4'):
break
else:
print("enter a valid choice")
continue
output:
1.e) Python program to print name N times
name=input("enter your name")
n=int(input("print how many times: "))
for i in range(n):
print(name)
output:

1.f) Python program to handle ZeroException


num1=int(input("enter numerator: "))
num2=int(input("enter denominator: "))
try:
result=num1/num2
print("divison of numbers is : ",result)
except ZeroDivisionError:
print("division by zero error")
output:

1.g) Python program to print current time with 10 seconds of


interval
import time
for i in range(10):
seconds=time.time()
local_time=time.ctime(seconds)
print("local time : ",local_time)
time.sleep(10)
output:

1.h) Python program to read a file and print number of words


file1=open("myfile.txt","r")
lines=file1.readlines()
i=0
for line in lines:
i+=1
count=len(line.split())
print("line",i,"number of words",count)

myfile.txt
hello
good morning
have a nice day
output:
2. A simple program to turn on led
Steps:
New project→next→next→create firmware
project..,family(raspberry pi)→next→finish

Program:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
button=18
led=17
GPIO.setup(led,GPIO.OUT)
GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
if GPIO.input(button)==False:
GPIO.output(led,True)
print("LED ON")
time.sleep(1)
else:
GPIO.output(led,False)
print("LED OFF")
time.sleep(1)
Requirements:
Resistor 1K 10W
Led
Button
Default(2)(GPIO17,GPIO18)
Ground(2)

Output:

LED OFF

LED ON
3. Flash and led at a given time on and off times taken
from a file
Steps:
New project→next→next→create firmware
project..,family(raspberry pi)→next→finish
Program:
import RPi.GPIO as GPIO
import datetime
import time
pin=11
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
time_str=str(datetime.datetime.now().strftime("%H:%M"))
print(time_str)
fo=open("on.txt","r")
on=str(fo.read())
fc=open("off.txt","r")
off=str(fc.read())
fo.close()
fc.close()
while True:
if time_str==on:
time.sleep(1)
GPIO.output(pin,True)
print("LED ON")
else:
time.sleep(1)
GPIO.output(pin,False)
print("LED OFF")

on.txt
<on time>
off.txt
<off time>

Requirements:
Resistor 220R
Default (GPIO17)
Ground
Output:
4. Switch on relay
Steps:
New project→next→next→create firmware
project..,family(raspberry pi)→next→finish
Program:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8,GPIO.OUT,initial=GPIO.LOW)
while True:
GPIO.output(8,GPIO.HIGH)
sleep(1)
GPIO.output(8,GPIO.LOW)
sleep(1)
Requriments:
Relay
Lamp
NPN
Resistor 330R
Default(TXD)
Ground(2)
Power(2)(+12v)
Output:
INITIAL RELAY OFF

RELAY OFF
5.Access an image through pi web cam
Steps:
New project→next→next→create flow chart
project..,family(raspberry pi)→next→finish

Flowchart:
DISPLAY PNG IMAGE
DISPLAY CAMERA IMAGE
STEPS:

RPI13(U1)→Add pheripals→camera→ Raspberry Pi Camera Module


RPI13(U1)→Add pheripals→display→TFT display
RPI13(U1)→Add pheripals→Breakout pheripals→ Momentary
Action Push Button
RPI13(U1)→Add resource file → <Add your png image>
Output:
DISPLAY PNG IMAGE
DISPLAY WEB CAMERA IMAGE

Source code:
# !/usr/bin/env python3
# Generated by Proteus Visual Designer for Raspberry Pi

# Modules
from goto import with_goto
from stddef import *
import var
import pio
import resource
from datetime import datetime

# Peripheral Configuration Code (Do Not Edit)


#---CONFIG_BEGIN---
import cpu
import FileStore
import VFP
import Generic
import camera
import Displays

def peripheral_setup () :
# Peripheral Constructors
pio.cpu=cpu.CPU ()
pio.storage=FileStore.FileStore ()
pio.server=VFP.VfpServer ()
pio.BTN1=Generic.Button (pio.GPIO4)
pio.CAM1=camera.RPiCamera ()
pio.LCD1=Displays.TFTDisplay (pio.GPIO18, pio.GPIO23)
pio.storage.begin ()
pio.server.begin (0)
# Install interrupt handlers

def peripheral_loop () :
pio.server.poll ()

#---CONFIG_END---
def variables_setup () :
# Flowchart Variables
var.image = ''

# Flowchart Routines
@with_goto
def chart_SETUP () :
pio.LCD1.loadImage ("R", 0)
return

@with_goto
def chart_LOOP () :
while not (pio.BTN1()) :
pass
pio.CAM1.capture (25)
var.image = pio.CAM1.getLastImage ()
pio.LCD1.loadImage (var.image, 0)
return

# Main function
def main () :
# Setup
variables_setup ()
peripheral_setup ()
chart_SETUP ()
# Infinite loop
while True :
peripheral_loop ()
chart_LOOP ()
# Command line execution
if __name__ == '__main__' :
main()

You might also like