0% found this document useful (0 votes)
23 views

IOT Journal

Uploaded by

SUDHAKAR GUNTUKA
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)
23 views

IOT Journal

Uploaded by

SUDHAKAR GUNTUKA
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/ 33

INDEX

SR.NO PRACTICALS DATE REMARK


0 Starting Raspbian OS, Familiarizing with Raspberry
Pi Components and interface, Connecting to Ethernet
, Monitor, USB.
01 Displaying different LED Patterns with Raspberry Pi.

02 Displaying Time over 4 Digit 7 Segment Display


using Raspberry Pi.
03 Raspberry Pi based Oscilloscope.

04 Raspberry Pi GPS Module interfacing.

05 IoT Based Web Controlled Home Automation using


Raspberry Pi.
06 Interfacing Pi Camera with Raspberry Pi.

07 Interfacing Raspberry Pi with RFID.

08 Controlling Raspberry Pi with Telegram.


PRACTICAL 0
AIM: Starting Raspbian OS, Familiarizing with Raspberry Pi
Components and interface, Connecting to Ethernet , Monitor, USB.

Hardware Guide: For getting started with raspberry pi for the first time you will
require the following hardware:

1. Raspberry Pi (latest Model)


2. Monitor or TV
3. HDMI cable
4. Ethernet cable
5. USB keyboard
6. USB mouse
7. Micro USB power supply
8. 8GB or larger microSD card
9. SD Card Reader

Raspberry Pi 3 Model B:

The Raspberry Pi 3 is the third generation Raspberry Pi. It replaced the Raspberry Pi
2 Model B in February 2016. Compared to the Raspberry Pi 2 it has:
A 1.2GHz 64-bit quad-core ARMv8 CPU
802.11n Wireless LAN
Bluetooth 4.1
Bluetooth Low Energy (BLE)

Like the Pi 2, it also has:


4 USB ports
40 GPIO pins
Full HDMI port
Ethernet port
Combined 3.5mm audio jack and
Composite video Camera interface (CSI)
Display interface (DSI)
Micro SD card slot (now push-pull rather than push-push)
Video Core IV 3D graphics core .
The Raspberry Pi 3 has an identical form factor to the previous Pi 2 (and Pi 1 Model
B+) and has complete compatibility with Raspberry Pi 1 and 2.

Monitor or TV: A monitor or TV with HDMI in can be used as a display with a


Raspberry Pi. Most modern television sets and monitors have an HDMI port, and are
the easiest to get working with the Raspberry Pi. You can use an HDMI cable to
connect the Raspberry Pi directly to the television or monitor. Some older monitors
have a DVI port. These work well with the Raspberry Pi, although you'll need an
HDMI-to-DVI adapter to attach to an HDMI cable, or a one-piece HDMI-to-DVI
cable. Some old monitors have a VGA port. These can be trickier to use as you'll need
an HDMI-to-VGA converter, which can change digital video to analogue video. A
simple port adapter won't work.

HDMI to HDMI Cable: Connect Raspberry Pi to a Monitor or TV with a HDMI to


HDMI cable.

Ethernet Cable: Ethernet cable will allow your Pi to connect with the internet. It is
also useful for headless setup of Raspberry Pi

USB Keyboard and Mouse: Any standard USB keyboard and mouse can be used
with the Raspberry Pi. This plug and play devices will work without any additional
driver. Simply plug them into the Raspberry Pi and they should be recognised when it
starts up.

Power Supply: It is recommended that you use a 5V, 2A USB power supply for all
models of Raspberry Pi.

SD Card: The latest version of Raspbian, the default operating system recommended
for the Raspberry Pi, requires an 8GB (or larger) micro SD card. SD card will store
the operating systems as well as all the file and applications created by you.

Raspberry Pi GPIO Pin Diagram:


Installation Guide:
Now since you have all the required hardware, we will now learn how to get the
operating system onto your microSD card so that you can start using software on your
Raspberry Pi

Get Raspbian OS on your microSD card: Raspbian comes pre-installed with plenty
of software for education, programming and general use. It has Python, Scratch, Sonic
Pi, Java, Mathematical and more
1. To download Raspbian log on to raspberrpi.org and click on the download, then
click on Raspbian and lastly download the RASPBIAN JESSIE WITH DESKTOP
file. You can choose either the Torrent file or ZIP file.

2. The downloaded file will be in zip format. To unzip the file, you will require an
unzip tool. You can use any unzipping tool viz. WINRAR, 7ZIP etc. After unzipping
the file, you will find a disc image file in the unzipped folder.

3. Now format the SD Card before writing the disc image file on the SD card. You
can use SD Formatter tool or any other tool of your wish.

4. To write the image file of the operating system on the SD card you will require a
Disk Imager tool. For this you can use Win32 Disk Imager tool.

5. Once the image is written on the SD Card, your untitled SD card will now have the
name boot. Your SD Card will now hold the Raspbian Operating system required for
the first -time setup.

Plugging in your Raspberry Pi:


1. Begin by placing your SD card into the SD card slot on the Raspberry Pi. It will
only fit one way.
2. Next, plug your keyboard and mouse into the USB ports on the Raspberry Pi.

3. Make sure that your monitor or TV is turned on, and that you have selected the
right input (e.g. HDMI 1, DVI, etc).

4. Connect your HDMI cable from your Raspberry Pi to your monitor or TV.

5. If you intend to connect your Raspberry Pi to the internet, plug an Ethernet cable
into the Ethernet port, or connect a WiFi dongle to one of the USB ports(unless you
have a RaspberryPi 3).

6. When you're happy that you have plugged all the cables and SD card in correctly,
connect the micro USB power supply. This action will turn on and boot your
Raspberry Pi.

NOTE:
For Raspberry Pi 4 please follow the instruction given below.

1. To download Raspberry pi OS (buster)log on to raspberrypi.com and click on the


download and then click of rasberry pi os and click on Raspberry pi OS buster.

2. Follow the above mention step from 2 to 5 of INSTALLATION GUIDE section.

3. After writing the image on SD Card plug in the SD Card in to the Raspberry pi 4. If
boot normally and you see the raspberry pi screen than ok. But if not than make the
following changes in the SD card ‘config' file with notepad++.

1. Uncomment the line ‘hdmii_safe=1’

2. Uncomment the line 'hdmi_force_hotplug=1’

3. Uncomment the line 'hdmi_group=1’

4. Uncomment the line 'hdmi_mode=1'

5. Save and exit the 'config' file

6. Now insert again the memory card into the raspberry pi it will work.
PRACTICAL NO: 01
AIM: Displaying different LED Patterns with Raspberry Pi.
Hardware Guide: You will require the following hardware:
1. Raspberry Pi (latest Model).
2. Breadboard.
3. LED(Light Emitting Diode).
4. Resistor.
5. Connecting wires(female to female Jumper Wire).
6. Keyboard.
7. Mouse.
8. HDMI Cable.
9. Ethernet Cable.
10.Power supply,

Wire connection: For displaying Different LED pattern connect 8 LEDs in the
same format to the pin number given:
1. LED 1 connect to Raspberry Pi Pin 29
2. LED 2 connect to Raspberry Pi Pin 31
3. LED 3 connect to Raspberry Pi Pin 33
4. LED 4 connect to Raspberry Pi Pin 35
5. LED 5 connect to Raspberry Pi Pin 36
6. LED 6 connect to Raspberry Pi Pin 37
7. LED 7 connect to Raspberry Pi Pin 38
8. LED 8 connect to Raspberry Pi Pin 40
9. Connect Ground to Raspberry Pi Pin 6

PYTHON CODE FOR LED PATTERN:


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD) #set the Pin mode you will be working with

led1 = 29
led2 = 31
led3 = 33
led4 = 35
led5 = 36
led6 = 37
led7 = 38
led8 = 40

#setup the ledPin(i.e. GPIO22) as output


GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.setup(led4, GPIO.OUT)
GPIO.setup(led5, GPIO.OUT)
GPIO.setup(led6, GPIO.OUT)
GPIO.setup(led7, GPIO.OUT)
GPIO.setup(led8, GPIO.OUT)
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
GPIO.output(led6, False)
GPIO.output(led7, False)
GPIO.output(led8, False)

def ledpattern(ledVal1, ledVal2, ledVal3, ledVal4, ledVal5, ledVal6,


ledVal7,ledVal8):
GPIO.output(led1, ledVal1)
GPIO.output(led2, ledVal2)
GPIO.output(led3, ledVal3)
GPIO.output(led4, ledVal4)
GPIO.output(led5, ledVal5)
GPIO.output(led6, ledVal6)
GPIO.output(led7, ledVal7)
GPIO.output(led8, ledVal8)

def patterOne():
for i in range (0, 3):
ledpattern(1, 0, 1, 0, 1, 0, 1, 0)
time.sleep(1)
ledpattern(0, 1, 0, 1, 0, 1, 0, 1)
time.sleep(2)

def patternTwo():
for i in range (0, 5):
ledpattern(1, 0, 0, 0, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 1, 0, 0, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 1, 0, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 1, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 1, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 0, 1, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 0, 0, 1, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 0, 0, 0, 1)
time.sleep(0.1)

def patternThree():
for i in range (0, 5):
ledpattern(0, 0, 0, 0, 0, 0, 0, 1)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 0, 0, 1, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 0, 1, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 0, 1, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 0, 1, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 0, 1, 0, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(0, 1, 0, 0, 0, 0, 0, 0)
time.sleep(0.1)
ledpattern(1, 0, 0, 0, 0, 0, 0, 0)
time.sleep(0.1)

def patternFour():
for i in range (0, 5):
ledpattern(0, 1, 1, 1, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 0, 1, 1, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 0, 1, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 0, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 0, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 1, 0, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 1, 1, 0, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 1, 1, 1, 0)
time.sleep(0.1)
def patternFive():
for i in range (0, 5):
ledpattern(1, 1, 1, 1, 1, 1, 1, 0)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 1, 1, 0, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 1, 0, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 1, 0, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 1, 0, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 1, 0, 1, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(1, 0, 1, 1, 1, 1, 1, 1)
time.sleep(0.1)
ledpattern(0, 1, 1, 1, 1, 1, 1, 1)
time.sleep(0.1)

try:
while True:
patterOne()
patternTwo()
patternThree()
patternFour()
patternFive()

finally:
#reset the GPIO Pins
GPIO.cleanup()
PRACTICAL NO: 02
AIM: Displaying Time over 4 Digit 7 Segment Display using Raspberry
Pi.

Hardware Guide:
For completing this lesson, you will require the following things along with your
initial raspberry pi setup

1. TM1637 4-digit seven segment Display board


2. Connecting wires(female to female jumper wire)
3. Raspberry Pi.
4. Keyboard.
5. Mouse.
6. HDMI Cable.
7. Ethernet Cable.
8. Power supply.

TM1637 4 Digit seven segment Display Board:


This is a common anode 4-digit tube display module which uses the TM1637 driver chip

Wiring up your Circuit:


Hook up your circuit as follows:
1. Connect the Pin 2 (5V) of Rpi to Vcc pin of Module
2. Connect Pin 6 (GND) of Rpi to GND of Module
3. Connect Pin 38 (GPIO20) of Rpi to DIO of Module
4. Connect Pin 40 (GPIO21) of Rpi to CLK of Module
Software Guide:
1. Now to download libraries, open Web Browser on your Raspberry Pi and
log on to the following link:
https://github.com/timwaizenegger/raspberrypi -
examples/tree/master/actor-led-7segment-4numbers . Click on the
actor-led-7segment- 4numbers.zip folder and Now click on Download
Button to download the file.
2. Now on your rpi move to /home/pi/Downloads/ location to find the zip file
downloaded.
3. Unzip the file and try to execute the different example codes present in
that folder in Python 2 Idle.
4. Now open Python 2 Idle, create a new file, write the code given below and
save it in the same folder i.e. actor-led-7segment-4numbers since the code
below is depended on tm1637.py file which is present in the same folder.

PYTHON CODE:
#Program to display Time on 4-digit Seven segment display
from time import sleep
import tm1637

try:
import thread
except ImportError:
import _thread as thread

# Initialize the clock (GND, VCC=3.3V, Example Pins are DIO -20 and CLK21)
Display = tm1637.TM1637(CLK=21, DIO=20, brightness=1.0)

try:
print ("Starting clock in the background (press CTRL + C to stop):")
Display.StartClock(military_time=True)
Display.SetBrightness(1.0)
while True:
Display.ShowDoublepoint( True)
sleep(1)
Display.ShowDoublepoint(False)
sleep(1)

Display.StopClock()
thread.interrupt_main()
except KeyboardInterrupt:
print ("Properly closing the clock and open GPIO pins")
Display.cleanup()
PRACTICAL NO: 03
AIM: Raspberry Pi based Oscilloscope.

Hardware requirements:

1. Raspberry Pi
2. ADS1115 ADC
3. 8 or 16GB SD Card
4. Ethernet Cable
5. Power Supply
6. Mouse & Keyboard
7. Connecting wires(female to female Jumper wires)

Wiring up your Circuit:


ADS1115 and Raspberry Pi Connections:

1. VDD – 3.3v Raspberry pi pin 1


2. GND – GND Raspberry pi pin 6
3. SCL – SCL Raspberry pi pin 5
4. SDA – SDA Raspberry pi pin 3

Software Guide:

Step 1: Enable Raspberry Pi I2C interface


sudo raspi-config
When the configuration panels open, select interface options, select I2C and click
enable.
Step 2: Update the Raspberry Pi
sudo apt-get update
sudo apt-get upgrade

Step 3: Install the Adafruit ADS1115 library for ADC

Ensure you are in the Raspberry Pi home directory by running; ~cd


then install the build-essentials by running;
sudo apt-get install build-essential python-dev python-smbus git

Next, clone the Adafruit git folder for the library by running;
git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git

Change into the cloned file's directory;


cd Adafruit_Python_ADS1x15
run the setup file;

sudo python3 setup.py install

Step 4: Test the library and 12C communication.

To test the library and ensure the ADC can communicate with the raspberry pi over
I2C.
change directory to the examples directory by running; cd examples
Next, run the sampletest.py example which displays the value of the four channels on
the ADC in a tabular form.
python3 simpletest.py (Ctrl+z to stop)
If the I2C module is enabled and connections good, you should see the data as shown
in the image below.

Step 5: Install Matplotlib


To visualize the data we need to install the matplotlib module which is used to plot all
kind of graphs in python

sudo apt-get install python3-matplotlib

Step 6: Install the Drawnow python module

Lastly, we need to install the drawnow python module. This module helps us provide
live updates to data plot.
sudo apt-get install python3-pip
We can then use pip to install the drawnow package by running:
sudo pip3 install drawnow

PYTHON CODE:
import time
import matplotlib.pyplotas plt
#import numpy
from drawnow import *
# Import the ADS1x15module.
import Adafruit_ADS1x15
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
val = [ ]
cnt = 0
plt.ion()
# Start continuous ADC conversions on channel 0 using the previous gain
value. adc.start_adc(0, gain=GAIN)
print('Reading ADS1x15 channel 0')
#create the figure function
def makeFig():
plt.ylim(-5000,5000)
plt.title('Osciloscope')
plt.grid(True)
plt.ylabel('ADC outputs')
plt.plot(val, 'ro-', label='Channel 0')
plt.legend(loc='lower right')
while (True):
# Read the last ADC conversion value and print it out.
value = adc.get_last_result()
print('Channel 0: {0}'.format(value))
# Sleep for half a second.
time.sleep(0.5)
val.append(int(value))
drawnow(makeFig)
plt.pause(.000001)
cnt = cnt+1
if(cnt>50):
val.pop(0)
PRACTICAL NO: 04
AIM: Raspberry Pi GPS Module interfacing.
Global Positioning System (GPS) makes use of signals sent by satellites in
space and ground stations on Earth to accurately determine their position on
Earth.

Hardware Guide:
1. GPS module
2. USB to TTL converter
3. Raspberry Pi
4. Mouse & Keyboard
5. Ethernet Cable
6. HDMI Cable
7. Power Supply
8. Connecting wires(female to female jumper wire)

Wiring up your Circuit : USING USB TO TTL Converter Module

1. Connect the VCC Pin of GPS Module to 3.3V Pin of USB to TTL
converter
2. Connect the GND Pin of GPS Module to GND Pin of USB to TTL
converter
3. Connect the Tx Pin of GPS Module to Rx Pin of USB to TTL converter
4. Connect the Rx Pin of GPS Module to Tx Pin of USB to TTL converter.
5. Lastly connect the USB to TTL converter to USB port of Raspberry Pi.

Software Guide:
Open Terminal Window and Open Raspberry Pi configuration by
typing the command: sudo raspi-config

Raspberry Pi configuration window will open go to interface option


>>Serial Port>> serial shell login-NO>>serial interface
enable-YES>> OK

Type the following command to know to which USB port the GPS
module is attached: ls /dev/ttyUSB*
We can find whether our GPS module is working properly and the
connections are correct by typing the following command: sudo cat
/dev/ttyUSB*
Installing a GPS Daemon (gpsd):
we can use (amongst other options) a nice little package named 'gpsd', which
essentially acts as a layer between your applications and the actual GPS hardware

To install gpsd, make sure your Pi has an Internet connection and run the
following commands from the console:
1. sudo apt-get update
2. sudo apt-get install gpsd gpsd-clients python-gps

After installing gpsd and disabling the gpsd systemd service as mentioned
above you're ready to start using gpsd yourself.
Start gpsd and direct it to use USB. Simply entering the following command
(Here we are assuming that GPS module is connected to USB0):
1. sudo gpsd /dev/ttUSB0 -F /var/run/gpsd.sock

Try running gpsmon to get a live-streaming update of GPS data!


or cgps which gives a less detailed, but still quite nice output
1. cgps –s

You can abort gpsd by the following command


1. sudo killall gpsd

PYTHON CODE:
import serial #import serial pacakge
from time import sleep
import webbrowser #import package for opening link in browser
import sys #import system package

def GPS_Info():
global NMEA_buff
global lat_in_degrees
global long_in_degrees
nmea_time = []
nmea_latitude = []
nmea_longitude = []
nmea_time = NMEA_buff[0] #extract time from GPGGA string
nmea_latitude = NMEA_buff[1] #extract latitude from GPGGA
string nmea_longitude = NMEA_buff[3] #

print("NMEA Time: ", nmea_time,'\n')


print ("NMEA Latitude:", nmea_latitude,"NMEA Longitude:", nmea_longitude,'\n')

lat = float(nmea_latitude) #convert string into float for calculation


longi = float(nmea_longitude) #convertr string into float for calculation
lat_in_degrees = convert_to_degrees(lat) #get latitude in degree decimal
format long_in_degrees = convert_to_degrees(longi) #get longitude in degree
decimal format

#convert raw NMEA string into degree decimal form at


def convert_to_degrees(raw_value):
decimal_value=raw_value/100.00
degrees = int(decimal_value)
mm_mmmm = (decimal_value - int(decimal_value))/0.6
position = degrees + mm_mmmm
position = "%.4f" %(position)
return position

gpgga_info = "$GPGGA,"
ser = serial.Serial ("/dev/ttyUSB0") #Open port with baud rate
GPGGA_buffer = 0
NMEA_buff = 0
lat_in_degrees = 0
long_in_degrees = 0

try:
while True:
received_data = (str)(ser.readline ()) #read NMEA string received
GPGGA_data_available = received_data.find(gpgga_info) #check for NMEA
GPGGA string
if (GPGGA_data_available>0):
GPGGA_buffer = received_data.split("$GPGGA,",1)[1] #store data coming after
"$GPGGA,"
NMEA_buff = (GPGGA_buffer.split(',')) #store comma separated data in buffer
GPS_Info() #get time, latitude, longitude

print("lat in degrees:", lat_in_degrees," long in degree: ", long_in_degrees, '\n')


map_link = 'http://maps.google.com/?q=' + lat_in_degrees + ',' + long_in_degrees
#create link to plot location on Google map
print("<<<<<<<<press ctrl+c to plot location on google maps>>>>>>\n")
#press ctrl+c to plot on map and exit
print(“ \n")

except KeyboardInterrupt:
webbrowser.open(map_link) #open current position information in
google map sys.exit(0)

Note: Please ensure that GPS module is visible to open sky or else it will not be able to
produce desired output.
PRACTICAL NO: 05
AIM: IoT Based Web Controlled Home Automation using Raspberry Pi.
Wiring your circuit:
1. Connect LED 1 with Raspberry Pi pin 3(GPIO2)
2. Connect LED 2 with Raspberry Pi pin 5(GPIO3)
3. Connect LED 3 with Raspberry Pi pin 7(GPIO5)
4. Connect negative terminal ground with Raspberry Pi pin 6(Ground)

Creating Webpage : index.html


<html>
<body>
<h1>WELCOME</h1>
<h3>LED CONTROLLER</h3>
<br>
<a href=\A><button> LED 1 ON</button></a>
<a href=\a><button> LED 1 OFF</button></a>
<a href=\B><button> LED 2 ON</button></a>
<a href=\b><button> LED 2 OFF</button></a>
<a href=\C><button> LED 3 ON</button></a>
<a href=\c><button> LED 3 OFF</button></a>
</body>
</html>

On Terminal:

1- To Install Libraries: sudo pip3 install flask


2- To fetch IP Address: ifconfig

PYTHON CODE:

import RPi.GPIO as GPIO


import time
from flask import Flask
from flask import Flask,render_template
app = Flask(__name__)
led1 = 3
led2 = 5
led3 = 7
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.output(led1, 0)
GPIO.output(led2, 0)
GPIO.output(led3, 0)
print ("DONE")

@app.route("/")
def index():
return render_template('index.html')

@app.route('/A')
def led1on():
data1="A"
GPIO.output(led1, 1)
return render_template('index.html')

@app.route('/a')
def led1off():
data1="a"
GPIO.output(led1, 0)
return render_template('index.html')

@app.route('/B')
def led2on():
data1="B"
GPIO.output(led2, 1)
return render_template('index.html')
@app.route('/b')
def led2off():
data1="b"
GPIO.output(led2, 0)
return render_template('index.html')

@app.route('/C')
def led3on():
data1="C"
GPIO.output(led3, 1)
return render_template('index.html')

@app.route('/c')
def led3off():
data1="c"
GPIO.output(led3, 0)
return render_template('index.html')

if __name__ == "__main__":
print ("START")
app.run(host='192.168.50.12',port=5010)
PRACTICAL NO: 06
AIM: Interfacing Pi Camera with Raspberry Pi.
The Camera Module is a great accessory for the Raspberry Pi, allowing users
to take still pictures and record video in full HD.

Hardware Guide:
For completing this lesson, you will require the Camera Module along with your initial
raspberry pi setup.

Connect the Camera Module:


First of all, with the Pi switched off, you'll need to connect the Came ra
Module to the Raspberry Pi's camera port, then start up the Pi and ensure the
software is enabled.
1. Locate the camera port and connect the camera:
2. Start up the Pi.
3. Open the Raspberry Pi Configuration Tool from the main menu.
4. Ensure the camera software is enabled. If it's not enabled, enable it and
reboot your Pi to begin.

Software Guide:
Now your camera is connected and the software is enabled, you can get started by
capturing an image.

You can capture an image by just typing a single line command.

Open terminal window and type the command as follows:

sudo raspistill -o /home/pi/Desktop/image.jpg


This command will capture an image and store it at the specified location (here the
location specified is /home/pi/Desktop) with the specified name (here the name is
‘image.jpg’).
You can even write a code in Python to capture an image using raspberry pi
camera.Open Python3, create a new file and type the code as follows:
PYTHON CODE:
from time
import sleep
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1280, 720) # selecting resolution 1280x720 px
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('/home/pi/Pictures/newImage.jpg')
#capture and save image at specified location
camera.stop_preview()
PRACTICAL NO: 07
AIM: Interfacing Raspberry Pi with RFID.
Hardware Guide:
1- Raspberry Pi
2- RC522 RFID Reader
3- Mouse & Keyboard
4- Connecting wires(female to female jumper wire)
5- Ethernet Cable
6- HDMI Cable
7- Power Supply

RFID RC522 :
The RFID RC522 is a very low-cost RFID (Radio-frequency identification) reader
and writer that is based on the MFRC522 microcontroller. This microcontroller
provides its data through the SPI protocol and works by creating a 13.56MHz
electromagnetic field that it uses to communicate with the RFID Tags/RFID Card.

Wiring the RFID RC522:


On your RFID RC522 you will notice that there are 8 possible connections on it,
these being SDA (Serial Data Signal), SCK (Serial Clock), MOSI (Master
Out Slave In), MISO (Master In Slave Out), IRQ (Interrupt Request), GND
(Ground Power), RST (Reset- Circuit) and 3.3v (3.3v Power In). We will need to
wire all of these but the IRQ to our Raspberry Pi’s GPIO pins.

1- Connect SDA pin of RC522 with Raspberry Pi pin 24.


2- Connect SCK pin of RC522 with Raspberry Pi pin 23.
3- Connect MOSI pin of RC522 with Raspberry Pi pin 19.
4- Connect MISO pin of RC522 with Raspberry Pi pin 21.
5- Connect GND pin of RC522 with Raspberry Pi pin 6.
6- Connect RST pin of RC522 with Raspberry Pi pin 22.
7- Connect 3.3v pin of RC522 with Raspberry Pi pin 1.
Software Guide:
Step1: Open Raspberry Pi Configuration and enable SPI interface by typing the
command
sudo raspi-config

Step2: Now check to make sure that SPI has been enabled type the command:

lsmod | grep spi

Step3: Update Raspberry Pi to ensure that its running the latest version of all
software:

sudo apt-get update


sudo apt-get upgrade

Step4: To install python3-dev, python- pip and git packages. Simply run the
following command on your Raspberry Pi to install all of the required packages for
this guide on setting up your RFID reader.

sudo apt install python3-dev python3-pip

Step5: first install the Python Library spidev to our Raspberry Pi using the python
“pip” tool that we downloaded in the previous step. The spidev library helps handle
interactions with the SPI and is a key component for the Raspberry Pi to interact
with the RFID RC522.

Run the following command on your Raspberry Pi to install spidev to your


Raspberry Pi through pip:

sudo pip3 install spidev

Step6: There are two files that are included within our MFRC522 library that we make
use of:
1- MFRC522.py which is an implementation of the RFID RC522 interface, this
library handles all the heavy lifting for talking with the RFID over the Pi’s SPI
Interface.
2- SimpleMFRC522.py that takes the MFRC522.py file and greatly simplifies it
by making you only have to deal with a couple of functions instead of several.
To install the MFRC522 library to your Raspberry Pi using pip go ahead and run
the following command.
sudo pip3 install mfrc522

PYTHON CODE: for Writing with RFID RC522


import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
text = input ('New data:')
print("Now place you tag to write")
reader.write(text)
print("written")

finally:
GPIO.cleanup()

PYTHON CODE: for Reading with RFID RC522


import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
id, text = reader.read()
print(id)
print(text)

finally:
GPIO.cleanup()
PRACTICAL NO: 08
AIM: Controlling Raspberry Pi with Telegram.

Step 1: Install Telegram on Your Phone, Obviously


Go to App Store (iPhone) or Play Store (Android), download and install Telegram on
your phone.
Now, you can use Telegram. Not yet for the Raspberry Pi. Telegram reserves a special
kind of accounts for machines, called bot accounts. As the owner of your own Pi, you
have to obtain a bot account for it.

Step 2: Text /newbot to BotFather


Open Telegram on your phone, search for a user called BotFather. As the name
implies, he is the Father of All Bots.
As you may have guessed, he is not of our own species, but is actually a
machine. He accepts special commands, because he does not understand plain
English very well.
To obtain a bot account, text him /newbot. (you need the slash '/' in front) He will then
ask a couple of questions. I call my bot "Led”. You will see why in a few moments.
But you can give it any name you want.
At the end of process, you will be given a token, something like
123456789:ABCdefGhIJKlmNo- PQRsTUVwxyZ. This token represents the
bot account. You are going to put this token on the Pi.

Step 3: Install Telepot on Raspberry Pi


Install telepot, a Python package that enables the Pi to speak Telegram Bot API.
On the command line, run these two commands:
sudo apt-get install python-pip
sudo pip install telepot
Step 4: Test Token
On the command line, type python to enter the Python interpreter. In the Python
interpreter, enter these three lines;
import telepot
bot = telepot.Bot('*** copy bot token from botfather ***')
bot.getMe()

You should keep bot token secret too. Having the token means having access to the
bot account.
If the last command, getMe(), returns a dictionary describing the bot account (as in
the screenshot), all is good. Type exit() to leave the Python interpreter.
Step 5: Python code?
Create a python file name telegram.py in raspberry pi and copy the following code.

import sys
import time
import random
import datetime
import telepot
import RPi.GPIO as GPIO
from telepot.loop import MessageLoop

red=40 # connect red led

at pin 40 of raspberry pi

now=datetime.datetime.now()
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(red,GPI
O.OUT)
GPIO.output(red,0)

def action(msg):
chat_id =
msg['chat']['i
d'] command
= msg['text']

print ('Got command: %s' %command)


if 'On' in command:
message="Turn On"
message=message+"
red"
GPIO.output(red,1)
bot.sendMessage(chat_i
d,message)

if 'Off' in command:
message="Turn Off"
message=message+" red"
GPIO.output(red,0)
bot.sendMessage(chat_id, message)

bot = telepot.Bot('626665131:AAHsNzQbqSj9GZ9-w2t4I') #paste your bot tokan


here. print(bot.getMe())
MessageLoop(bot,action).run_as_thread()
print ('I am listening...')

while 1:
time.sleep(10)

Step 6: Connect red LED at pin 40(GPIO21)

Step 7: Run It and Text It


Assuming you have named the file you have just saved "telegram.py", to run the bot,
type sudo python /home/pi/telegram.py
Open Telegram on your phone, search for your bot using its name or username. Text it
On or Off, and see how it responds.

You might also like