Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Experiment 6

(A) . Design a flowchart Temperature and Humidity sensor and display the result using
UART on terminal.

Components: HYT271 Precision Humidity and Temperature Sensor, Groove Terminal (VT1)
for UART

Simulator: Proteus 8 Professional

Explanation:

HYT271 Humidity and Temperature Sensor:


Excellent humidity/temperature accuracy and stability
High chemical resistance
Wide humidity and temperature range
Very low drift
I 2 C protocol (address 0x28 or alternative address)
Very stable at high humidity
Interchangeable without adjustments

Groove Terminal (VT1) for UART:


A Virtual terminal is used to send and receive the data through the port. Data can be
sent through the keyboard and received data is displayed on the virtual terminal.
The designer can interface any of the UART based modules through the virtual ports in
Proteus software.

32
fp.write(logstring)
fp.close()
print logstring

def do_7segdisplay(status):
"display on 7 seg"
now=status[0]
segnumber = 10000*now.hour + 100*now.minute + float(status[5])
device.write_number(0,segnumber,decimalPlaces=2,zeroPad=True)

def do_timer(status):
"Deals with timer status led"
if(status[3]): # timer on
r,g,b = (0,250,0)
else:
r,g,b = (0,0,0)
blinkt.set_pixel(0,r,g,b)
#blinkt.set_pixel(1,r,g,b)
blinkt.show()
return

def do_heat(status):
"Deals with Heat status led and switches relay"
if(status[4]): # heat on
r,g,b = (200,000,000)
GPIO.output(PIN_RELAY, GPIO.LOW)
else:
r,g,b = (0,0,0)
GPIO.output(PIN_RELAY, GPIO.HIGH)

blinkt.set_pixel(2,r,g,b)
#blinkt.set_pixel(3,r,g,b)
blinkt.show()
return
def do_boost():
"No function as yet"

# MAIN PROGRAMME
status = get_status(server, status)
do_log(status)
do_7segdisplay(status)
do_timer(status)
do_heat(status)

31
import datetime
import RPi.GPIO as GPIO

#GPIO setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RELAY, GPIO.OUT)
GPIO.setup(PIN_BOOST_DETECT, GPIO.IN)

# GLOBALS

server = 'http://192.168.XXXXXXXXXXXXX'
now = datetime.datetime.now()
status = [now,
str(now.year)+str(now.month)+str(now.day)
+' '+str(now.hour)+':'+str(now.minute),
'no status', 0, 0, '0']

def get_status(server, status):


"Gets heating status from prodserver"

try:
txt=requests.get(server)
soup=BeautifulSoup(txt.text,"html.parser")
status[2] ="read OK"
status[3] = int(soup.select("#timer_on")[0].text)
status[4] = int(soup.select("#heat_on")[0].text)
status[5] = soup.select("#Avtemp")[0].text[:5]
return status

except Exception as e:
# in error condition heating defaults to OFF
status[2] = "Error:" + str(e)
status[3] = 0
status[4] = 0
status[5] = "99"
return status

def do_log(status):
logfile = 'logfile.txt'
fp = open(logfile,"a")

logstring = "{1} {2} TIMER:{3} HEAT:{4} AvTEMP:{5} \n".format(*status)

30
(B) Switch on a relay at a given time using cron, where the relay's contact terminals are
connected to a load.

Python Program:
# Python 2.7 on raspberry pi
# Script to trigger thermostat control on heating
# Reads room temperatures from Prodserver DONE - from Thermostat01.py
# All heating control logic is on prodserver. This just reads on/off commands

# Dependencies
# sudo pip install max7219 - 7 seg 8 digit display driver
# sudo pip install bs4 - beautiful soup web scraper
# sudo pip install requests - python http library for GET and POST
# sudo apt-get install python-blinkt blinkt led library

# GPIO PINS
# MAX7129 7 seg display
# DIN = GPIO10(MOSI)
# CS = GPIO8(SPI CE0)
# CLK = GPIO11(SPI CLK)
# Blinkt uses GPIO23,24

#Pin Constants
PIN_RELAY = 17
PIN_BOOST_DETECT = 4

import blinkt
blinkt.set_clear_on_exit(False)
blinkt.set_all(0,0,0)
blinkt.set_brightness(0.1)
blinkt.show()
# blinkt Timer leds 0,1
# blinkt Heat leds 2,3
# blinkt Boost leds 4,5

import sys
# 7 seg display set up
import max7219.led as led

device = led.sevensegment()
device.brightness(1)
import requests
from bs4 import BeautifulSoup

29
Experiment 5

(A) Flash an LED based on cron output (acts as an alarm)

Python Program:
Python code: blink.py
import RPi.GPIO as GPIO

import time

LedPin = 11 # pin11
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.output(LedPin, GPIO.HIGH) # Turn ON led

def blink():

while True:

GPIO.output(LedPin, GPIO.HIGH) # led on


time.sleep(1)
GPIO.output(LedPin, GPIO.LOW) # led off
time.sleep(1)

def destroy():

GPIO.output(LedPin, GPIO.LOW) # led off

GPIO.cleanup() # Release resource

if name == ' main ': # Program start from here


setup()
try:
blink()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy()
will be executed.

destroy()

28
if time == "12:00":
GPIO.output(pin, True)
time.sleep(number_of_seconds_for_led_to_be_on_here)
GPIO.output(pin, False)
time.sleep(0.030)

Replace things like 12:00 and your_output_pin_here with your own values. Every 30
milliseconds, it checks whether it is 12:00. If it is, it turns on the LED. It then waits a number of
seconds, then turns it back off.

27
Flowchart:

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

Python Program:

To start, you need to make sure that your Raspberry Pi is connected to the internet. This is
needed to track time. Then type in this code:

import RPi.GPIO as GPIO


import datetime
import time

pin = your_output_pin_here
GPIO.setup(pin, GPIO.OUT)

while True:
time = datetime.datetime.now().strftime("%H:%M")
26
(b) LED bilking without push button using Raspberry pi on Proteus using flowchart.

Components: Raspberry pi, LED, Button

Simulator: Proteus 8 Professional

Explanation:

In order to blink an LED,


We pass HIGH to turn it on
Give some delay
We pass LOW to turn it off
Give some delay

Perform the above instructions in a loop.

Circuit:

25
Flowchart:

Output:

24
Experiment 4
(A) LED bilking using Raspberry pi on Proteus using flowchart.

Components: Raspberry pi, LED, Button

Simulator: Proteus 8 Professional

Explanation:

In order to blink an LED,


We pass HIGH to turn it on
When the push button is switched on
We pass LOW to turn it off
When the push button is switched off

Perform the above instructions in a loop.

Circuit:

23
d) Read a file line by line and print the word count of each line

Python Program:
file=open("eee.txt","r")
line=1
for i in file:

print ("print the line no=" , line , "and line is =" , i)


z=i.split()
print ("no of word in line =" , line ,"is = " , len(z))
line = line+1

Python Program:
# read line by line from file

L = ["Welcome to India", "Delhi is the capital\n", "Have fun\n"]


file1 = open('myfile.txt', 'w')
file1.writelines((L))
file1.close()

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


count = 0

while True:
count+=1
line = file1.readline()

if not line:
break
print("Word Count in Line{}: {}".format(count,
len(line.split())) )

file1.close()

22
n2=raw_input()
n1=int(n1)
n2=int(n2)
try:
div=n1/n2
print (div)
except ZeroDivisionError:
print ("zero division is handled")

print ("out of try catch block ")

Output:
Enter n1:10
Enter n2:0
zero division is handled

#DivideByZero Exception
x=int(input("First No:"))
y=int(input("Second No:"))
try:
print("x/y={}".format(x/y))
except Exception:
print("DivideByZero Exception")

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

Python Program:
import time
for i in range(1,11):
zz=time.asctime(time.localtime(time.time()))
zz=zz[11:19]
print (zz)
print (time.asctime(time.localtime(time.time())))
time.sleep(10)

Python Program:
#Current time 10 times
import datetime
import time
for i in range(0,10):
print(datetime.datetime.now().time())
time.sleep(10)

21

You might also like