0% found this document useful (0 votes)
22 views8 pages

Experiment 6

Uploaded by

rjja18
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)
22 views8 pages

Experiment 6

Uploaded by

rjja18
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/ 8

EXPERIMENT-6

OBJECTIVE

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

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()
B) Switch on a relay at a given time using cron, where the relay's contact terminals are
connected to a load.

PROGRAM
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

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)

( c ) Get the status of a bulb at a remote place (on the LAN) through web.

THEORY

One function that makes the Internet of Things the Internet of Things is remote control of devices,
being able to trigger action from a remote location. This is otherwise known as remote
configuration. Whether it’s a home, an office, or an industrial site, connected devices rely on
some form of automation through sensors or machines and need a mechanism to control their
operation remotely.

Hardware Requirements
For this demonstration, we will use the following hardware:

Raspberry Pi Model B+ (with Raspbian OS)


1 LED and 1 490 Ohm Resistor
Software Requirements
We will use the following sdks and tools

RPi GPIO , GPIO Python library for Raspbian OS


PubNub Python SDK
PubNub Javascript SDK

Web Application
The web interface will look something like this:

This is a very simple web page with a visual indicator for the device and a button to toggle the
on/off state of LED.

Behind the scenes, we have the PubNub Javascript API that performs two operations upon
receiving certain events.

Sends a request message to toggle the state of the device.


Receives response with the current state of the device.

Raspberry Pi and LED


Here is the schematic for the raspberry Pi connections to be used in this application.
The python code for driving the LED is executed as part of the PubNub callback on ‘gpio-
raspberry-control’ channel

glow = False

#PubNub Channel Subscribe Callback


def gpioCallback(msg,channel):
global glow
respstring = ''
command = msg
print "Command is : " + str(command)
if('req' in command):
if(command['req'] == 'toggle'):
if(glow):
glow = False;
respstring = 'off'
else:
glow = True
respstring = 'on'
GPIO.output(16, glow)
respmsg = {"resp" : respstring }
pubnub.publish(pubnubChannelName, respmsg)

When a toggle request is received, the application checks the current state of the GPIO driving pin
of the LED, toggles its state and then sends the new state back to the web application as a response
message.

The exchange of messages between the web application and Raspberry Pi can be visualized as
follows.

And here is how the entire system works:

You might also like