IOT Journal 6406
IOT Journal 6406
jhunjhunwala college
ghatkopar (w),
mumbai - 400 086
Department of information
Tybsc-it Semester - V
IOT JOurnal
6406_Yash_Mourya
Name : yash mourya
Roll No : 6406
IOT
Raspberry Pi Model 3 B:
The Raspberry Pi 3 Model B is a version of the Raspberry Pi
single-board computer. Here are some of its key features:
● Processor: Quad-core 64-bit ARM Cortex A53 clocked at
1.2GHz
● RAM: 1GB LPDDR2
● Wireless: 802.11n Wireless LAN
● Bluetooth: Bluetooth 4.1, Bluetooth Low Energy (BLE)
● Ethernet: 100 Base Ethernet
● USB Ports: 4 USB 2.0 ports
● Video Output: Full-size HDMI
● Audio Output: 3.5mm audio jack
● GPIO: 40-pin GPIO header
● Storage: microSD card slot
● Other: CSI camera port, DSI display port
Raspbian OS
Raspbian is the officially supported operating system for
Raspberry Pi. It is a free operating system based on Debian
optimized for the Raspberry Pi hardware. Raspbian comes with
over 35,000 packages, pre-compiled software bundled in a nice
format for easy installation on your Raspberry Pi.
1. Blinking LED
Problem Statement:
To control an LED using a Raspberry Pi by turning it on and off in
regular intervals through a Python script. This will demonstrate how
to use the GPIO pins for basic digital output operations.
Objective:
The objective of this practical is to understand and perform basic I/O
operations using the GPIO pins on the Raspberry Pi by creating a
simple circuit to blink an LED.
Components Used:
● Raspberry Pi 3 Model B
● LED (Light Emitting Diode)
● 220Ω resistor
● Jumper wires
Circuit dig
Algorithm
I. Initialize GPIO Library: Import the necessary libraries and configure the
GPIO settings.
II. Setup GPIO Pins:
III. Set up GPIO pin 8 as an output pin.
IV. Initialize the pin state to LOW (LED off).
V. Blinking Loop:
VI. Turn the LED on by setting GPIO pin 8 to HIGH.
VII. Wait for 1 second.
VIII. Turn the LED off by setting GPIO pin 8 to LOW.
IX. Wait for 1 second.
X. Repeat the above steps indefinitely.
XI. Handle Exit:
XII. On a keyboard interrupt (Ctrl+C), clean up GPIO settings to avoid
warnings and reset the pin states.
Libraries used
● RPi. GPIO
● time
Code:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT, initial = GPIO.LOW)
while True:
GPIO.output(7, GPIO.HIGH)
print(“LED on”)
sleep(1)
GPIO.output(7, GPIO.LOW)
print(“LED off”)
sleep(1)
Output:
2. Blinking of 4 LEDs:
Problem Statement:
To control four LEDs connected to a Raspberry Pi by turning them on
and off in a sequence through a Python script. This will demonstrate
how to use multiple GPIO pins for digital output operations.
Objective:
The objective is to understand and perform I/O operations using the
GPIO pins on the Raspberry Pi by creating a circuit to blink four LEDs in
a sequential pattern.
Components Used:
● Raspberry Pi 3 Model B
● 4 LEDs (Light Emitting Diodes)
● 4 220Ω resistors
● Jumper wires
Circuit dig
Algorithm
I. Initialize GPIO Library: Import the necessary libraries and configure the
GPIO settings.
II. Setup GPIO Pins:
III. Set up GPIO pins 8, 10, 12, and 16 as output pins.
IV. Initialize the pin states to LOW (LEDs off).
V. Blinking Sequence:
VI. Turn LED 1 on, wait for 1 second, and turn it off.
VII. Turn LED 2 on, wait for 1 second, and turn it off.
VIII. Turn LED 3 on, wait for 1 second, and turn it off.
IX. Turn LED 4 on, wait for 1 second, and turn it off.
X. Repeat the above sequence indefinitely.
XI. Handle Exit:
XII. On a keyboard interrupt (Ctrl+C), clean up GPIO settings to avoid
warnings and reset the pin states.
Libraries used
● RPi. GPIO
● time
Code:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(5, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(8, GPIO.OUT, initial = GPIO.LOW)
while True:
GPIO.output(3, GPIO.HIGH)
GPIO.output(5, GPIO.HIGH)
GPIO.output(7, GPIO.HIGH)
GPIO.output(8, GPIO.HIGH)
print(“LED on”)
sleep(1)
GPIO.output(3, GPIO.LOW)
GPIO.output(5, GPIO.LOW)
GPIO.output(7, GPIO.LOW)
GPIO.output(8, GPIO.LOW)
print(“LED off”)
sleep(1)
Output:
3. Two ON Two OFF Pattern:
Problem Statement :-
Design and implement a system on a Raspberry Pi that controls a set
of four LEDs. The system should follow a "Two ON, Two OFF" pattern,
where two LEDs are turned on while the other two are off, and then
the states are alternated after a certain interval.
Objective :-
Understand GPIO Pin Control: Learn to control the General Purpose
Input /Output (GPIO) pins on a Raspberry Pi to manage external
devices like LEDs.
Implement Timing Mechanism: Use a timing mechanism to create the
desired pattern with specified intervals.
Create a Repeating Pattern: Ensure that the "Two ON, Two OFF"
pattern repeats continuously.
Components Used :-
● Raspberry Pi 3 Model B
● 4 LEDs (Light Emitting Diodes)
● Jumper Wires
Circuit dig
Algorithm
I. Initialize GPIO Library: Import the necessary libraries and configure
the GPIO settings.
II. Setup GPIO Pins:
III. Set up GPIO pins 8, 10, 12, and 16 as output pins.
IV. Initialize all pins to LOW (LEDs off).
V. 2 ON 2 OFF Pattern:
VI. Turn LED 1 and LED 2 on, and LED 3 and LED 4 off.
VII. Wait for 1 second.
VIII. Turn LED 1 and LED 2 off, and LED 3 and LED 4 on.
IX. Wait for 1 second.
X. Repeat the above pattern indefinitely.
XI. Handle Exit:
XII. On a keyboard interrupt (Ctrl+C), clean up GPIO settings to avoid
warnings and reset the pin states.
Libraries used
● RPi. GPIO
● time
Code :-
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(5, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(8, GPIO.OUT, initial = GPIO.LOW)
while True:
GPIO.output(3, GPIO.HIGH)
GPIO.output(5, GPIO.LOW)
GPIO.output(7, GPIO.HIGH)
GPIO.output(8, GPIO.LOW)
print(“LED on”)
sleep(1)
GPIO.output(3, GPIO.LOW)
GPIO.output(5, GPIO.HIGH)
GPIO.output(7, GPIO.LOW)
GPIO.output(8, GPIO.HIGH)
print(“LED off”)
sleep(1)
Output:
4. Serial Blinking
Problem Statement :-
Design and implement a system on a Raspberry Pi that controls a set
of LEDs to blink in a sequential (serial) manner. Each LED should turn
on and off one after the other in a repeating sequence.
Objective :-
Learn GPIO Pin Control: Understand how to control the GPIO pins on a
Raspberry Pi to manage multiple LEDs.
Implement Timing Mechanism: Use timing to create a sequential
blinking pattern.
Create a Repeating Pattern: Ensure that the LEDs blink in a serial
manner continuously.
Components Used :-
● Raspberry Pi 3 Model B
● 4 LEDs (Light Emitting Diodes)
● Jumper Wires
Circuit dig
Algorithm
I. Initialize GPIO Library: Import the necessary libraries and configure the
GPIO settings.
II. Setup GPIO Pins:
III. Set up GPIO pins 8, 10, 12, and 16 as output pins.
IV. Initialize all pins to LOW (LEDs off).
V. Serial Blinking Pattern:
VI. Turn LED 1 on, wait for 0.5 seconds, then turn LED 1 off.
VII. Turn LED 2 on, wait for 0.5 seconds, then turn LED 2 off.
VIII. Turn LED 3 on, wait for 0.5 seconds, then turn LED 3 off.
IX. Turn LED 4 on, wait for 0.5 seconds, then turn LED 4 off.
X. Repeat the above sequence indefinitely.
XI. Handle Exit:
XII. On a keyboard interrupt (Ctrl+C), clean up GPIO settings to avoid
warnings and reset the pin states.
Libraries used
● RPi. GPIO
● time
Code :-
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(5, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(8, GPIO.OUT, initial = GPIO.LOW)
while True:
GPIO.setup(8, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(3, GPIO.OUT, initial = GPIO.HIGH)
sleep(1)
GPIO.setup(3, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(5, GPIO.OUT, initial = GPIO.HIGH)
sleep(1)
GPIO.setup(5, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial = GPIO.HIGH)
sleep(1)
GPIO.setup(7, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(8, GPIO.OUT, initial = GPIO.HIGH)
sleep(1)
Output:
Problem Statement :-
Design and implement a system on a Raspberry Pi that allows an LED
to be turned on or off using a push button. When the button is pressed,
the LED should turn on, and when the button is released, the LED
should turn off.
Objective :-
Learn GPIO Pin Control: Understand how to control GPIO pins on a
Raspberry Pi to manage an LED and read input from a push button.
Implement Button Debouncing: Ensure that the button press is
accurately detected, avoiding false triggers due to mechanical bounce.
Create an Interactive System: Develop a responsive system that reacts
to user input via the push button.
Components Used :-
● Raspberry Pi 3 Model B
● 4 LEDs (Light Emitting Diodes)
● Jumper Wires
● Push Button
Circuit dig
Code :-
import RPi.GPIO as GPIO
import time
led_pin = 4
button_pin = 11
# Setup GPIO mode and pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
GPIO.output(led_pin, GPIO.input(button_pin)) # LED on when
button pressed
time.sleep(0.1) # Short delay to avoid high CPU usage
Output:
6. Buzzer with PUSH Button
Problem Statement :-
Design and implement a system on a Raspberry Pi that allows a buzzer
to be activated using a push button. When the button is pressed, the
buzzer should sound, and when the button is released, the buzzer
should stop.
Objective :-
Understand GPIO Pin Control: Learn to control the General Purpose
Input /Output (GPIO) pins on a Raspberry Pi to manage a buzzer.
Implement Button Control: Read the state of a push button and use it
to control the buzzer.
Create an Interactive System: Develop a responsive system that
accurately detects button presses and controls the buzzer accordingly.
Components Used :-
● Raspberry Pi 3 Model B
● 4 LEDs (Light Emitting Diodes)
● Jumper Wires
● Buzzer
● Push Button
Code :-
import RPi.GPIO as GPIO
import time
# Define the GPIO pins for the buzzer and the button
buzzer_pin = 17
button_pin = 18
# Setup GPIO mode and pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer_pin, GPIO.OUT)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
if GPIO.input(button_pin) == GPIO.HIGH:
GPIO.output(buzzer_pin, GPIO.HIGH) # Turn on Buzzer
else:
GPIO.output(buzzer_pin, GPIO.LOW) # Turn off Buzzer
time.sleep(0.1) # Short delay to avoid high CPU usage
Output:
Practical - 3
(4 digit 7 segment display to show time)
Problem Statement :-
Create a digital clock using a 4-digit 7-segment display to accurately
display hours and minutes.
Objective :-
Design a system that reads time from an RTC module and displays it
on the 7-segment display, updating every minute.
Components Used :-
● Raspberry Pi (e.g., Raspberry Pi 3 Model B)
● Jumper Wires
Code :-
from time import sleep
import tm1637
try:
import thread
except ImportError:
Import_thread as thread
Output:
Practical - 4
(Using Telegram app for IOT)
Telegram BOT:
Problem Statement:
Develop a Telegram bot that interacts with an IoT device, allowing
users to control and monitor the device remotely through the
Telegram app.
Objective:
The primary objective of this practical is to create a Telegram bot
that can communicate with an IoT device, allowing users to:
1. Send Commands: Control the IoT device (e.g., turning on/off
a light, adjusting temperature, etc.) by sending commands
through the Telegram bot.
2. Receive Status Updates: Receive real-time status updates
from the IoT device (e.g., temperature readings, device
status, etc.) via the Telegram bot.
Components:
● Raspberry Pi (e.g., Raspberry Pi 3 Model B)
● Jumper Wires
Algorithm:
Create a bot using the BotFather on Telegram and get the API token.
accordingly.
● Telepot: A Python library used to interact with the Telegram Bot API.
and delays.
● IoT Device: The device you wish to control or monitor. (e.g., a smart
Code:
import telepot
now = datetime.datetime.now()
def action(msg):
chat_id = msg[‘chat’][‘id’]
command = msg[‘text’]
if command == ‘/hi’:
telegram_bot.sendMessage(chat_id,
str(now.hour)+str(“:”)+str(now.minute))
telegram_bot.sendPhoto(chat_id,photo =
“https://i.pinimg.com/avatars/circuitdigest_1464122100_280.jpg”)
telegram_bot.sendDocument(chat_id,document =
open(‘/home/pi/IOT/Practical 2.py’))
telegram_bot.sendAudio(chat_id, audio=open(‘
/home/pi/test.mp3’))
print (telegram_bot.getMe())
MessageLoop(telegram_bot, action).run_as_thread()
print (“up and running..”)
while 1:
time.sleep(10)
Output:
Objective:
Design and implement a Telegram bot that interacts with an IoT setup
to control an LED.
The bot should:
Receive Commands: Accept user commands (/on and /off) through
Telegram to control the LED.
Control LED: Turn the LED on or off based on the received commands.
Algorithm
1. Setup the Environment:
a. Initialize GPIO: Configure the GPIO pins on the Raspberry Pi.
b. Setup Telegram Bot: Create a bot using BotFather on Telegram
and get the API token.
2. Write Python Code:
a. Import Libraries: Import the necessary libraries for GPIO and
Telegram interactions.
b. Define GPIO Configuration: Setup GPIO pins and their modes.
c. Define Bot Commands: Implement the action function to handle
/on and /off commands and control the LED.
d. Initialize Bot: Start the bot and listen for incoming messages.
3. Run the Bot:
a. Start the Bot: Keep the script running to continuously listen for
commands.
Code:
import time, datetime
import telepot
from telepot.loop import MessageLoop
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(5, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)
now = datetime.datetime.now()
def action(msg):
chat_id = msg[‘chat’][‘id’]
command = msg[‘text’]
Circuit dig
Libraries Used
1. RPi.GPIO: This library allows for interaction with the GPIO pins on the
Raspberry Pi.
2. time: This standard Python library is used for handling time-related
functions. You use it to manage delays and sleep intervals.
3. serial: This library, part of the PySerial package, allows for serial
communication with devices like RFID readers.
Components Used
● Raspberry Pi: A small, affordable computer used to interface with
various hardware components.
● EM-18Reader: A device that reads EM-18 tags. It's connected to the
Raspberry Pi via a USB-to-serial adapter.
Algorithm
1. Initialization:
a. Import necessary libraries:
b. RPi.GPIO for GPIO (though not utilized in this snippet).
c. time for handling sleep intervals.
d. serial from PySerial for serial communication.
e. Configure GPIO to suppress warnings using
gpio.setwarnings(False).
f. Set the GPIO pin numbering mode to BOARD using
gpio.setmode(gpio.BOARD).
2. Define Function read_em18():
a. Open a serial connection to the EM-18 reader:
b. Use serial.Serial("/dev/ttyUSB0") to create a serial object.
c. Set the baud rate to 9600 with ser.baudrate = 9600.
d. Read data from the EM-18 reader:
e. The EM-18 typically sends data in a specific format (often 12
bytes or more).
f. Close the serial connection with ser.close().
g. Return the read data.
3. Main Execution Loop:
a. Continuously run an infinite loop using while True:
b. Print "Place the card" to prompt the user.
c. Wait for 1 second using sleep(1).
d. Call read_em18() to read data from the EM-18 reader.
e. Print the received EM-18 data.
4. Cleanup:
a. In the finally block, ensure GPIO resources are cleaned up with
gpio.cleanup().
Python Script:
# Initialize Serial
def read_rfid():
ser = serial.Serial("/dev/ttyUSB0")
ser.baudrate = 9600
data = ser.read(12)
ser.close()
return data
def show_attendance():
print("\n--- Attendance Report ---")
for student_id, info in attendance.items():
print(f"{info['name']}: {info['count']} times")
print("-------------------------\n")
try:
while True:
# Wait for button press
print("Waiting for button press...")
GPIO.wait_for_edge(37, GPIO.FALLING) # Wait until button is pressed
print("Button pressed! Place the card")
if id in attendance:
attendance[id]['count'] += 1 # Increment attendance count for the student
print(f"Card No: {id}")
print(f"Welcome {attendance[id]['name']}")
print(f"Attendance Count for {attendance[id]['name']}:
{attendance[id]['count']}")
GPIO.output(29, GPIO.HIGH) # Turn on light
for _ in range(5): # Beep the buzzer 5 times
GPIO.output(31, GPIO.HIGH) # Turn on buzzer
sleep(0.1) # Beep duration
GPIO.output(31, GPIO.LOW) # Turn off buzzer
sleep(0.1) # Silence duration between beeps
GPIO.output(29, GPIO.LOW) # Turn off light
else:
print("Wrong Card......")
GPIO.output(29, GPIO.LOW) # Ensure light is off
GPIO.output(31, GPIO.LOW) # Ensure buzzer is off
finally:
GPIO.cleanup()
Practical - 6
Code:
Import time
form pyfingerprint.pyfingerprint import Pyfingerprint
import RPi.GPIO as gpio
gpio.setwarning(False)
gpio.setmode(gpio.BCM)
gpio.setup(3, gpio.IN,pull_up_down=gpio.PUD_UP)
gpio.setup(17,gpio.IN,pull_up_down=gpio.PUD_UP)
try:
F = PyFingerprint(‘dev/ttyUSB0’,57600,0xFFFFFFFF,0x00000000)
except Exception as e:
print(“exception message”+str(e))
exit(1)
def enrollFinger():
print(“Waiting For Finger”)
while(f.readImage()==False):
pass
f.convertImage(0x01)
result= f.searchTemplate()
positionNumber = result[0]
If(positionNumber >=0):
print(“Finger Already register at position” + str(positionNumber))
time.sleep(2)
return
print(“remove finger…”)
time.sleep(2)
print(“waiting for finger again…”)
while(f.readImage() == False):
Pass
f.convertImage(0x02)
if(f.compareCharacteristics == 0):
print(“finger do not match”)
time.sleep(2)
f.createTemplate()
postionNumber = f.storeTemplate()
print(“Finger enroll successfully”)
print(“New template Postion #”+str(postitionNumber))
time.sleep(2)
def attendance():
print(‘place finger for Attendence”)
print(“waiting for finger”)
while(f.readImage()==False):
pass
f.convertImage(0x01)
result= f.searchTemplate()
positionNumber = result[0]
If(positionNumber >=0):
print(“Attendence already register” + str(positionNumber))
time.sleep(2)
return
print(“remove finger…”)
f.createTemplate()
postionNumber = f.storeTemplate()
print(“Attendence Marked succesfully …”)
while(1):
Button_state =gpio.input(3)
Button2_state = gpio.input(17)
if(button_state == False):
print(“Place finger to enroll’)
enrollFinger()
If button2_state == False:
attendance()
Practical - 8
Home Automation using Mobile App / NodeRed
Step 1:
1. tar xvzf WebIOPi-0.7.1.tar.gz
2. cd WebIOPi-0.7.1/
At this point before running step we need to install a patch as this version
of the WebIOPi does not work with the raspberry pi 3
1. sudo ./setup.sh
2.sudo webiopi -d -c /etc/webiopi/config