Introduction: Overview: Home Entertainment and Security System

About the Application

This IOT system is a Home Entertainment and Security System.

  1. Security
    1. Tap RFID Card and input are saved into Firebase.
    2. If authorized, you can enter peacefully and the picture is taken and uploaded to S3
    3. If unauthorized, defence section comes in and an LCD Screen will say you're not authorized.
  2. Defence
    1. Press button on the dashboard.
    2. Laser Turrets will attack in random burst and speed.
  3. Entertainment
    1. If motion is detected, the game will start.
    2. After the user plays the game, the score is saved into Firebase.
    3. LDR values will be taken and output on the dashboard.

This application is controllable and viewable via the IBM Node-Red web server. We make use of AWS and IBM Cloud Services and we used Firebase as our database.

Summary of the steps that will be described

  • Hardware Requirements
  • Security - How to create a security system that uses RFID input and an image recognition software
  • Defence - How to create a laser turret
  • Entertainment - How to create a Simon-says game
  • IOT App Watson on IBM Bluemix - How to integrate all the systems into one dashboard

Go ahead and access the pdf file for a more detailed explanation on how to create this project.

Step 1: Hardware Requirement

This is what you will need

  1. Security
    • 1 Raspberry Pi
    • 1 LCD
    • 1 RFID Reader
    • 1 PiCam
    • 2 RFID Cards/Buttons
    • X Female -> Male jumper cables
  2. Defence
    • 1 Raspberry Pi
    • 2 10 ㏀ Resistor (for Buttons)
    • 2 Micro Servo
    • 1 650nm Laser Transmitter Module
    • 2 Push Button
    • 1 Buzzer
    • 3 Small Rubber Bands/Cable Ties (for fixing)
    • X Female -> Male jumper cables
    • X Regular Jump Cables
    • 1 Transistor
    • 1 Capacitor
  3. Entertainments
    • 1 Raspberry Pi
    • 3 1 ㏀ Resistor (for LEDs)
    • 1 10㏀ Resistor (for LDR)
    • 3 LEDs ( Different Colors)
    • 3 Buttons
    • 1 LDR
    • 1 LCD
    • 1 Pir Motion Sensor
    • X Female -> Male jumper cables
    • X Regular Jump Cables

Step 2: Security

Creating the security system hardware

Connect the circuits as shown in the fritzing diagram

Creating the security system software

  1. Configure AWS by creating a thing
  2. Install AWS Python Library
  3. Install LCD Library
  4. Install RFID Library
  5. Setup Firebase
  6. Setup S3 Storage
  7. Install Boto on Raspberry Pi
  8. Install AWS ClI on Raspberry Pi
  9. Create AWS Credentials
  10. Configure AWS
  11. Upload security.py to RPi
  12. Upload imagerecognition.py to RPi

security.py is a code that will read rfid inputs and detect whether user is an intruder or not. If user is recognised, an image will be taken and uploaded to s3. The code also publishes to a topic in aws MQTT

Step 3: Defence

Creating the laser turret Hardware

  1. We create the laser turret by using 2 servos and 1 Laser Module
  2. Connect the circuits as shown in the fritzing diagram

Creating the laser turret Software

The code below will make the laser turret shoot in random directions, in random bursts and speed.


laserturret.py

<p>from gpiozero import LED, Buzzer, Button, Servo<br>import time
from signal import pause
import random</p><p>#led = LED(12)
#pir = MotionSensor(19, sample_rate=5,queue_len=1)
buzzer_pin = Buzzer(17)
attack = Button(5, pull_up=False)
#reset = Button(6, pull_up=False)
servo1 = Servo(18)
servo2 = Servo(24)</p><p>def ledON():
	led.on()
	print("LED is on")
	
def ledOFF():
	led.off()
	print("LED is off")</p><p>def fire():
	print("weapons hot")
	buzzer_pin.on()
	time.sleep(0.1)
	buzzer_pin.off()</p><p>def laserturret():
		timeBetweenBurst = random.uniform(0.2,1)
		timeBetweenShots = random.uniform(0.05,0.2)
		servo1start = random.randrange(-1,1)
		servo1end = random.randrange(-1,1)
		servo2start = random.randrange(-1, 1)
		servo2end = random.randrange(-1,1)
		numShots = random.randrange(5,20)
		
		servo1change = (servo1end - servo1start)/numShots
		servo2change = (servo2end - servo2start)/numShots
		servo1.value = servo1start
		servo2.value = servo2start
		time.sleep(0.1)
		shot = 0
		
		detail = [timeBetweenBurst,timeBetweenShots, servo1.value, servo2.value, numShots]
		print(detail)
		
		while shot<numshots: shot+="1" servo1.value="servo1start" servo2.value="servo2start" servo1start="servo1change" servo2start="servo2change" fire()="" time.sleep(timebetweenshots)="" time.sleep(timebetweenburst)<="" p=""></numshots:></p><p>notes = {
	'B0' : 31,
	'C1' : 33, 'CS1' : 35,
	'D1' : 37, 'DS1' : 39,
	'EB1' : 39,
	'E1' : 41,
	'F1' : 44, 'FS1' : 46,
	'G1' : 49, 'GS1' : 52,
	'A1' : 55, 'AS1' : 58,
	'BB1' : 58,
	'B1' : 62,
	'C2' : 65, 'CS2' : 69,
	'D2' : 73, 'DS2' : 78,
	'EB2' : 78,
	'E2' : 82,
	'F2' : 87, 'FS2' : 93,
	'G2' : 98, 'GS2' : 104,
	'A2' : 110, 'AS2' : 117,
	'BB2' : 123,
	'B2' : 123,
	'C3' : 131, 'CS3' : 139,
	'D3' : 147, 'DS3' : 156,
	'EB3' : 156,
	'E3' : 165,
	'F3' : 175, 'FS3' : 185,
	'G3' : 196, 'GS3' : 208,
	'A3' : 220, 'AS3' : 233,
	'BB3' : 233,
	'B3' : 247,
	'C4' : 262, 'CS4' : 277,
	'D4' : 294, 'DS4' : 311,
	'EB4' : 311,
	'E4' : 330,
	'F4' : 349, 'FS4' : 370,
	'G4' : 392, 'GS4' : 415,
	'A4' : 440, 'AS4' : 466,
	'BB4' : 466,
	'B4' : 494,
	'C5' : 523, 'CS5' : 554,
	'D5' : 587, 'DS5' : 622,
	'EB5' : 622,
	'E5' : 659,
	'F5' : 698, 'FS5' : 740,
	'G5' : 784, 'GS5' : 831,
	'A5' : 880, 'AS5' : 932,
	'BB5' : 932,
	'B5' : 988,
	'C6' : 1047, 'CS6' : 1109,
	'D6' : 1175, 'DS6' : 1245,
	'EB6' : 1245,
	'E6' : 1319,
	'F6' : 1397, 'FS6' : 1480,
	'G6' : 1568, 'GS6' : 1661,
	'A6' : 1760, 'AS6' : 1865,
	'BB6' : 1865,
	'B6' : 1976,
	'C7' : 2093, 'CS7' : 2217,
	'D7' : 2349, 'DS7' : 2489,
	'EB7' : 2489,
	'E7' : 2637,
	'F7' : 2794, 'FS7' : 2960,
	'G7' : 3136, 'GS7' : 3322,
	'A7' : 3520, 'AS7' : 3729,
	'BB7' : 3729,
	'B7' : 3951,
	'C8' : 4186, 'CS8' : 4435,
	'D8' : 4699, 'DS8' : 4978
}</p><p>def buzz(frequency, length): #create the function "buzz" and feed it the pitch and duration)</p><p>	if(frequency==0):
		time.sleep(length)
		return
	period = 1.0 / frequency 		 #frequency
	delayValue = period / 2		 #calcuate the time for half of the wave
	numCycles = int(length * frequency)	 #num of waves = duratime x freq
	
	for i in range(numCycles):		#start a loop from 0 to the variable "cycles" calculated above
		buzzer_pin.on()	 
		time.sleep(delayValue)	
		buzzer_pin.off()
		time.sleep(delayValue)		
	</p><p>def play(melody,tempo,pause,pace=0.800):
	
	for i in range(0, len(melody)):		# Play song
		
		noteDuration = pace/tempo[i]
		buzz(melody[i],noteDuration)	# Change the frequency along the song note
		
		pauseBetweenNotes = noteDuration * pause
		time.sleep(pauseBetweenNotes)</p><p>while True:
		laserturret()
		break;</p>

Step 4: Entertainment

Creating the entertainment Hardware

We create the Simon-says button game, which u have to follow the pattern of the LEDs lighting up and press the corresponding buttons. It uploads scores and timestamp into the firebase NoSQL database for further usage in the dashboards.

Connect the circuits as shown in the Fritzing diagram.

Creating the Entertainment Software

entertainment.py

<p>import RPi.GPIO as GPIO<br>import threading
import time
import random
import os
import tweepy
from rpi_lcd import LCD
from subprocess import call
from time import sleep
from datetime import datetime
from firebase import firebase
 
CONSUMER_KEY = 'h5Sis7TXdoUVncrpjSzGAvhBH'
CONSUMER_SECRET = 'ZfDVxc4aTd9doGmBQO3HiSKKzxSTKT4C3g0B3AGx8eETCJm2rY'
ACCESS_KEY = '988333099669901312-YDLEQN1weW2n1JP4lxJcFPppCsbvzQh'
ACCESS_SECRET = 'K2IlUPur6jx7DO5S0HhhZW29H5AQFOvkMMevSsk9ZzwLk'
 
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.secure = True
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
 
firebase = firebase.FirebaseApplication('https://iotca2-12f48.firebaseio.com', None)
lcd=LCD()
lcd.text('Have fun!', 1)
lcd.text('Good Luck!', 2)
sleep(1)
 
# Red, Yellow, Green
LIGHTS = [40, 38, 36]
BUTTONS = [37, 33, 35]
NOTES = ["E3", "A4", "E4"]
 
# values you can change that affect game play
speed = 0.5
 
# flags used to signal game status
is_displaying_pattern = False
is_won_current_level = False
is_game_over = False
 
# game state
current_level = 1
current_step_of_level = 0
pattern = []
 
def initialize_gpio():
    GPIO.setmode(GPIO.BOARD)
	GPIO.setup(LIGHTS, GPIO.OUT, initial=GPIO.LOW)
	GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
	for i in range(3):
        GPIO.add_event_detect(BUTTONS[i], GPIO.FALLING,
                              verify_player_selection)
 
def verify_player_selection(channel):
	global current_step_of_level, current_level, is_won_current_level, is_game_over
	if not is_displaying_pattern and not is_won_current_level and not is_game_over:
        flash_led_for_button(channel)
    	if channel == BUTTONS[pattern[current_step_of_level]]:
            current_step_of_level += 1
        	if current_step_of_level >= current_level:
                current_level += 1
                is_won_current_level = True
    	else:
        	is_game_over = True
 
def flash_led_for_button(button_channel):
	led = LIGHTS[BUTTONS.index(button_channel)]
	GPIO.output(led, GPIO.HIGH)
	time.sleep(0.4)
	GPIO.output(led, GPIO.LOW)
 
def add_new_color_to_pattern():
	global is_won_current_level, current_step_of_level
	is_won_current_level = False
    current_step_of_level = 0
	next_color = random.randint(0, 2)
    pattern.append(next_color)
 
def display_pattern_to_player():
	global is_displaying_pattern
    is_displaying_pattern = True
	GPIO.output(LIGHTS, GPIO.LOW)
	for i in range(current_level):
        GPIO.output(LIGHTS[pattern[i]], GPIO.HIGH)
        time.sleep(speed)
        GPIO.output(LIGHTS[pattern[i]], GPIO.LOW)
        time.sleep(speed)
    is_displaying_pattern = False
 
def wait_for_player_to_repeat_pattern():
    while not is_won_current_level and not is_game_over:
    	time.sleep(0.1)
 
def reset_board_for_new_game():
	global is_displaying_pattern, is_won_current_level, is_game_over
	global current_level, current_step_of_level, pattern
	is_displaying_pattern = False
	is_won_current_level = False
	is_game_over = False
	current_level = 1
    current_step_of_level = 0
	pattern = []
	GPIO.output(LIGHTS, GPIO.LOW)
 
def send_data(score):
	
	lcd.text('End of game,', 1)
    lcd.text('See you soon!', 2)
	datestr = str(datetime.now())   
	while True:
    print(datestr)
    print(score)
    data={ 'Date': datestr,
           'Score': score
        }
    result = firebase.post('/scores/',data)
    print(result)
    	if score > 2:
        status='Someone has scored '+ (str(score)) +' on '+datestr+'!'
        api.update_status (status = status)
    	break
 
def start_game():
	while True:
        add_new_color_to_pattern()
        display_pattern_to_player()
    	wait_for_player_to_repeat_pattern()
    	if is_game_over:
            send_data(current_level - 1)
        	print("Game Over! score is {} colors!\n".format(current_level - 1))
        	sleep(2)
        print("Thanks for playing!\n")
        lcd.text('',1)
        lcd.text('',2)
        	break
    	time.sleep(2)
 
def start_game_monitor():
	t = threading.Thread(target=start_game)
	t.daemon = True
	t.start()
	t.join()
 
def main():
	try:
    	os.system('cls' if os.name == 'nt' else 'clear')
    	print("Begin new round!\n")
        initialize_gpio()
        start_game_monitor()
	finally:
    	GPIO.cleanup()
 
 
if __name__ == '__main__':
	main()</p>

Step 5: IOT App Watson on IBM Bluemix [Part One]

Set up Blumix IoT Service

  1. Set up a Gateway Device Type
  2. Set up a Device

Do steps 1 and 2 for 3 times. One RPi is for one section (Security/Defence/Entertainment)

Set up Node-Red

  1. Run node-red
node-red start
  1. Go to manage palette in the hamburger menu (top right)
  2. Download the following palletes
    1. node-red-dashboard
    2. node-red-contrib-firebase
    3. node-red-contrib-ibm-watson-iot

Step 6: Node Red Flows

Download the files and export into your node-red.

Security Node-Red

  • none

Defence Rpi Node-Red

  • laserturret.txt

Entertainment Rpi Node-Red

  • entertainment rpi flow.txt
  • ldr rpi flow.txt

IBM Bluemix Node-Red

  • ibm bluemix flow.txt