IOT Labprograms
IOT Labprograms
output:
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:
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
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()