Docsity

Raspberry Pi IoT: LED Control, Display, Telegram, Fingerprint, RFID, Home Automation, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download Raspberry Pi IoT: LED Control, Display, Telegram, Fingerprint, RFID, Home Automation and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Sheth T.J. Education Society’s

Sheth N.K.T.T. College of Commerce and

Sheth J.T.T. College of Arts

Name: Singh Shashank Ajay

Roll No.: 09

Class: T.Y. BSC-IT

Subject: Internet of Things Practical

Semester: V

Year: 2021 - 2022

INDEX

SR

NO.

TITLE

1. Blinking of LED With Raspberry Pi

2. Different LED patterns with Raspberry Pi

3. Display time over 4 digit 7-segment display using Raspberry Pi

4. Display random numbers over 4 digit 7-segment display using

Raspberry Pi

5. Raspberry Pi based Oscilloscope

6. Controlling Raspberry Pi using Telegram

7. Fingerprint Sensor interfacing with Raspberry Pi

8. Visitor monitoring with Raspberry Pi and Pi Camera

9. Interfacing Raspberry Pi with RFID

10. IoT based Web Controlled Home Automation using Raspberry

Pi

02: “Different LED patterns with Raspberry Pi”

Source Code:

import RPi.GPIO as GPIO import time

GPIO.setmode(GPIO.BOARD) GPIO.setwarning(False)

GPIO.setup(18,GPIO.OUT) GPIO.setup(16,GPIO.OUT) GPIO.setup(15,GPIO.OUT) GPIO.setup(13,GPIO.OUT)

def pattern(): while(True): print(“All LED on “) GPIO.output(18,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) time.sleep(1) GPIO.output(18,GPIO.LOW) GPIO.output(16,GPIO.LOW) GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.LOW)

print(“Two LED on and two LED off “) GPIO.output(18,GPIO.HIGH) GPIO.output(16,GPIO.LOW) GPIO.output(15,GPIO.HIGH) GPIO.output(13,GPIO.LOW) time.sleep(1) GPIO.output(18,GPIO.LOW) GPIO.output(16,GPIO.HIGH) GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.HIGH)

print(“Three LED on and one LED off or One LED on three LED off “) GPIO.output(18,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(13,GPIO.LOW) time.sleep(1) GPIO.output(18,GPIO.LOW) GPIO.output(16,GPIO.LOW)

GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.HIGH)

print(“One LED on and two off “) GPIO.output(18,GPIO.HIGH) GPIO.output(16,GPIO.LOW) GPIO.output(15,GPIO.HIGH) GPIO.output(13,GPIO.LOW) time.sleep(1) GPIO.output(18,GPIO.LOW) GPIO.output(16,GPIO.HIGH) GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.HIGH)

Circuit Diagram:

Circuit Connection:

Output :

04: “Display random numbers over 4 digit 7-segment display

using Raspberry Pi”

Source Code:

import random import time import datetime import RPi.GPIO as GPIO import tm

Display=tm1637,TM1637(23,24,tm1637,BRIGHT_TYPICAL) Display.Clear() Display.SetBrightness(1)

while(True): n1=random.randint(0,9) n2=random.randint(0,9) n3=random.randint(0,9) n4=random.randint(0,9) currentvalue=[n1,n2,n3,n4] Display.Show(currentvalue) time.sleep(1)

Output:

print(„Channel 0:{0}‟.format(value)) time.sleep(0.5) val.append(int(value)) drawnow(makeFig) plt.pause(_000001) cnt=cnt+1 if(cnt>50): val.pop(0)

Circuit Diagram:

Output:

06: “Controlling Raspberry Pi using Telegram”

Hints:

 Install telegram bot on raspberry pi :- $ sudo apt-get install python-pip

  • $ sudo pip install telepot  Then type the below code in python shell  Now run the example :-
  • $ cd telegram/
  • $ pwd
  • $ ls
  • $ python telegram.py

Steps to be follow to access LED through the telegram :

i. Install Telegram app on smart phone from playstore. ii. Open Telegram,request the bot father to create a new bot. iii. Search “BotFather” and click on it.

vi. Provide the name for new bot that we want to create,after that give same name followed by _bot postfix (e.g. tyit_bot). vii. After this process the BotFather will give a Token for access.

viii. Now search for the bot that we have created.

ix. Now give the command to LEDs as per your requirements.

Source Code:

import time, datetime import RPi.GPIO

as GPIO import telepot from telepot.loop import MessageLoop

green = 6 yellow = 13 red = 19 white = 26 now = datetime.datetime.now() GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

##LED White

GPIO.setup(white, GPIO.OUT)

GPIO.output(white, 0) #Off initially

#LED Yellow

GPIO.setup(yellow, GPIO.OUT)

GPIO.output(yellow, 0) #Off initially

#LED Red

GPIO.setup(red, GPIO.OUT)

GPIO.output(red, 0) #Off initially #LED green

GPIO.setup(green, GPIO.OUT) GPIO.output(green, 0) #Off initially def action(msg): chat_id = msg['chat']['id'] command = msg['text'] print ('Received: %s' % command) if 'on' in command: message = "on" if 'white' in command: message = message

  • "white " GPIO.output(white,

Circuit Connection:

07: “Fingerprint Sensor interfacing with Raspberry Pi”

Hints:

  • $ sudo bash
  • $ wget https://apt.pm-codeworks.de/pm-codeworks.list-P/etc/apt/sources.list.d/
  • $ sudo apt-get update
  • $ sudo apt-get install python-fingerprint-yes
  • $ ls /dev/ttyUSB*
  • $ cd /usr/share/doc/python-fingerprint/examples
  • $ ls

Circuit Connection:

Source Code:

 example_enroll.py

import sys import time from PyFingerprint import *

Tries to initialize the sensor

try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if ( f.verifyPassword() == False ): raise ValueError('The given fingerprint sensor password is wrong!') except:

e = sys.exc_info()[1] print 'The fingerprint sensor could not be

initialized!' print 'Exception message: ' +

str(e.message) exit(1)

 Remove finger...  Waiting for same finger again...  Finger enrolled successfully!  New template position #

 example_search.py

Search for a finger

import hashlib import sys from PyFingerprint import *

Tries to initialize the

sensor try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if ( f.verifyPassword() == False ): raise ValueError('The given fingerprint sensor password is wrong!')

except: e = sys.exc_info()[1] print 'The fingerprint sensor could not be initialized!' print 'Exception message: ' + str(e.message) exit(1)

Gets some sensor information

print 'Currently stored templates: ' + str(f.getTemplateCount())

Tries to search the finger and calculate

hash try: print 'Waiting for finger...'

Wait that finger is read

while ( f.readImage() == False ): pass

Converts read image to characteristics and stores it in

charbuffer 1 f.convertImage(0x01) ## Searchs template result = f.searchTemplate() positionNumber = result[0] accuracyScore = result[1] if ( positionNumber == -1 ): print 'No match found!' exit(0) else: print 'Found template at position #' + str(positionNumber) print 'The accuracy score is: ' + str(accuracyScore)

OPTIONAL stuff

Loads the found template to charbuffer 1

f.loadTemplate(positionNumber, 0x01)

Downloads the characteristics of template loaded in charbuffer 1 characterics =

f.downloadCharacteristics(0x01)

Hashes characteristics of template print 'SHA2 hash of

template: ' + hashlib.sha256(str(characterics)).hexdigest() except: e = sys.exc_info()[1] print 'Fingerprint read failed!'

print 'Exception message: ' + str(e.message) exit(1)

Output:

 root@raspberrypi:/usr/share/doc/python-fingerprint/examples# sudo python example_search.py  Currently used templates: 10/1000  Waiting for finger...  Found template at position #6  The accuracy score is: 105  SHA-2 hash of template: 5a6b7e8f7fe2fa4dd5b4d814de3c3ccb93bedab84a72df89fb68b2e8fdffd88d  root@raspberrypi:/usr/share/doc/python-fingerprint/examples# sudo python example_search.py  Currently used templates: 10/1000  Waiting for finger...  Found template at position #8  The accuracy score is: 127  SHA-2 hash of template: 093c4e2fb69b5f216cc82a44ec84c74359885a147f9d0c7021910233cb24d42f

 example_index.py

import sys from PyFingerprint import *

Tries to initialize the sensor

try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if ( f.verifyPassword() == False ): raise ValueError('The given fingerprint sensor password is wrong!') except: e = sys.exc_info()[1] print 'The fingerprint sensor could not be initialized!' print 'Exception message: ' + str(e.message) exit(1)

Gets some sensor information

print 'Currently stored templates: ' + str(f.getTemplateCount())

Tries to show a template index table page

try: page = raw_input('Please enter the index page (0, 1, 2, 3) you want to see: ') page = int(page) tableIndex = f.getTemplateIndex(page) for i in range(0, len(tableIndex)): print 'Template at position #' + str(i) + ' is used: ' + str(tableIndex[i]) except: e = sys.exc_info()[1] print 'Read out table index failed!' print 'Exception message: ' + str(e.message) exit(1)

Output:

 root@raspberrypi:/usr/share/doc/python-fingerprint/examples# sudo python example_index.py  Currently used templates: 10/  Please enter the index page (0, 1, 2, 3) you want to see: 0