IOT Journal
IOT Journal
Hardware Guide: For getting started with raspberry pi for the first time you will
require the following hardware:
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)
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.
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.
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.
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++.
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
led1 = 29
led2 = 31
led3 = 33
led4 = 35
led5 = 36
led6 = 37
led7 = 38
led8 = 40
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
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)
Software Guide:
Next, clone the Adafruit git folder for the library by running;
git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
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.
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)
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
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
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] #
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
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)
On Terminal:
PYTHON CODE:
@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.
Software Guide:
Now your camera is connected and the software is enabled, you can get started by
capturing an image.
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.
Step2: Now check to make sure that SPI has been enabled type the command:
Step3: Update Raspberry Pi to ensure that its running the latest version of all
software:
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.
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.
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
reader = SimpleMFRC522()
try:
text = input ('New data:')
print("Now place you tag to write")
reader.write(text)
print("written")
finally:
GPIO.cleanup()
reader = SimpleMFRC522()
try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()
PRACTICAL NO: 08
AIM: Controlling Raspberry Pi with Telegram.
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
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']
if 'Off' in command:
message="Turn Off"
message=message+" red"
GPIO.output(red,0)
bot.sendMessage(chat_id, message)
while 1:
time.sleep(10)