0% found this document useful (0 votes)
26 views18 pages

Unit-5 Iot

This document provides an overview of IoT devices, focusing on the Raspberry Pi as a key example. It covers the basic building blocks of IoT devices, programming with Python, and the Django web application framework for IoT applications. The document also details the hardware components of the Raspberry Pi and its various interfaces for sensor and actuator integration.
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)
26 views18 pages

Unit-5 Iot

This document provides an overview of IoT devices, focusing on the Raspberry Pi as a key example. It covers the basic building blocks of IoT devices, programming with Python, and the Django web application framework for IoT applications. The document also details the hardware components of the Raspberry Pi and its various interfaces for sensor and actuator integration.
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/ 18

UNIT-05

IOT PHYSICAL DEVICES & ENDPOINTS


Syllabus: What is an IOT Device,Exemplary Device, Board, Linux on
Raspberry Pi, Interfaces, Programming Raspberry Pi with Python, Python
Web Application Framework- Django, Designing a RESTful Web API

1. What is an IOT Device:


 A "Thing" in Internet of Things (IoT) can be any object that has a unique
identifier and which can send/receive data (including user data) over a
network (e.g., smart phone, smartTV, computer, refrigerator, car, etc.).
 IOT devices are connected to the Internet and send information about
themselves or about their surroundings (e.g. information sensed by the
connected sensors) over a network (to other devices or servers/storage) or
allow actuation upon the physical entities/environment around them
remotely.
 Some example of IOT devices are listed below:
 A home automation device that allows remotely monitoring the status of
appliances and controlling the appliances.
 An industrial machine which sends information abouts its operation and
health monitoring data to a server.
 A car which sends information about its location to a cloud-based service.
 A wireless-enabled wearable device that measures data about a person
such as the number of steps walked and sends the data to a cloud-based
service.
1.1 Basic building blocks of an IOT Device:
 An IOT device can consist of a number of modules based on functional
attributes, such as:
1. Sensing:
 Sensors can be either on-board the IOT device or attached to the device.
 IOT device can collect various types of information from the on-board or
attached sensors such as temperature, humidity, light intensity, etc.
 The sensed information can be communicated either to other devices or
cloud-based servers/storage.
2. Actuation:
 IOT devices can have various types of actuators attached that allow taking
actions upon the physical entities in the vicinity of the device.
 For example, a relay switch connected to an IOT device can turn an
appliance on/off based on the commands sent to device.
3. Communication:
 Communication modules are responsible for sending collected data to other
devices or cloud-based servers/storage and receiving data from other
devices and commands from remote applications.
AMRITASAI-AUTONOMOUS 1 DEPARTMENT OF ECE
4. Analysis & Processing:
 Analysis and processing modules are responsible for making sense of the
collected data.

 A generic block diagram of a single-board computer(SBC) based IOT device


that includes CPU, GPU, RAM, storage and various types of interfaces and
peripherals.

Figure: Block diagram of an IOT Device


 The representative IOT device used for the any examples and widely used
single-board mini computer called Raspberry Pi.
 The use of Raspberry Pi is international since these devices are widely
accessible, in-extensive and available from multiple vendors.

2. Exemplary Device:
 Raspberry Pi is a low-cost mini-computer with the physical size of a credit
card.
 Raspberry Pi runs various flavors of Linux and can perfrom almost all tassks
that a normal desktop computer can do.
 Raspberry Pi also allows interfacing sensors and actuators through the
general purpose I/O PINS.
 Since Raspberry Pi runs Linux operating system, it supports Python “out of
the box”.

AMRITASAI-AUTONOMOUS 2 DEPARTMENT OF ECE


3. About Raspberry Pi Board:

Figure: Raspberry Pi Board

AMRITASAI-AUTONOMOUS 3 DEPARTMENT OF ECE


 Raspberry Pi board with various components/ peripherals labeled.
1. Processor & RAM:
 Raspberry Pi is based on an ARM processor.
 The latest version of Raspberry Pi comes with 700MHz Low power
ARM176JZ-F processor and 512 MB SDRAM.
2. USB Ports:
 Raspberry Pi comes with two USB 2.0 ports.
 The USB ports on Raspberry Pi can provide a current up to 100mA.
 For connecting devices that draw current more than 100mA, an external
USB powered hub is required
3. Ethernet Ports:
 Raspberry PI comes with a standard RJ45 Ethernet Port.
 We can connect an Ethernet cable or a USB WIFI Adapter to provides
Internet connectivity.
4. HDM1 Output:
 The HDM1 port on Raspberry Pi provides both video and audio output.
 We can connect Raspberry Pi to monitor using an HDM1 cable or HDM1 to
DV1 adapter/cable.
5. Composite Video Output:
 Raspberry Pi comes with a composite video output with an RCA jack that
supports both PAL and NNTSC video output.
 The RCA jack can be used to connect old televisions that have an RCA input
only.
AMRITASAI-AUTONOMOUS 4 DEPARTMENT OF ECE
6. Audio output:
 Raspberry Pi has a 3.5 mm audio output jack.
 This audio jack is used for providing audio output to old televisions along
with the RCA jack for video.
 The audio quality from this jack is inferior to the HDM1 output.
7. GPIO Pins:
 Raspberry Pi comes with a number of general purpose input/output pins.
 Raspberry Pi GPIO headers was shown in the figure.

Figure: Rasberry Pi GPIO headers

 There are four types pins on Raspberry Pi -


1. True GPIO pins,
2. I2C interface pins,
3. SPI interface pins and
4. serial Rx and Tx pins
8. Display Serial Interface (DSI):
 The DSI interface can be used to connect an LCD panel to Raspberry Pi.
9. Camera Serial Interface (CSI):
 The CSI interface can be used to connect a camera module to Raspberry Pi.
10. Status LEDs:

AMRITASAI-AUTONOMOUS 5 DEPARTMENT OF ECE


 Raspberry Pi has five status LEDs.
 lists Raspberry Pi status LEDs and their functions.
Status LED Function
ACT SD card access
PWR 3.3V Power is present
FDX Full duplex LAN connected
LNK 100 Mbit LAN connected
100 Link/Network activity
Table: Raspberry Pi Status LEDs
11. SD Card Slot:
 Raspberry Pi does not have a built in operating system and storage.
 You can plug-in an SD card loaded with a Linux image to the SD card slot.
12. Power Input:
 Raspberry Pi has a micro-USB connector for power input.

4. Linux on Raspberry Pi:


 Raspberry Pi supports various flavors of Linux including:
1. Raspbian:
 Raspbian Linux is a Debian Wheezy port optimized for Raspberry Pi.
 This is the recommended Linux for Raspberry Pi.
2. Arch:
 Arch is an Arch Linux port for AMD devices.
3. Pidora:
 Pidora Linux is a Fedora Linux optimized for Raspberry Pi.
4. RaspBMC:
 RaspBMC is an XBMC media-center distribution for Raspberry Pi.
5. OpenELEC:
 OpenELEC is a fast and user-friendly XBMC media-center distribution.
6. RISC OS:
 RISC OS is a very fast and compact operating system.

AMRITASAI-AUTONOMOUS 6 DEPARTMENT OF ECE


Figure: Raspbian Linux desktop on Raspberry Pi.

Figure: file explorer on Raspberry Pi

AMRITASAI-AUTONOMOUS 7 DEPARTMENT OF ECE


Figure: console on Raspberry Pi

Figure: Browser on Raspberry Pi.

AMRITASAI-AUTONOMOUS 8 DEPARTMENT OF ECE


To configure Raspberry Pi, the raspi-config tool is used which can be launched
from command line as ($raspi-config) as shown in Figure.

Figure: Rasberry Pi configuration tool


 Using the configuration tool you can expand root partition to fill SD card, set
keyboard layout, change password, set locale and timezone, change
memory split, enable or disable SSH server and change boot behavior.
 It is recommended to expand the root file-system so that you can use the
entire space on the SD card.
 Though Raspberry Pi comes with an HDMI output, it is more convenient to
access the device with a VNC connection or SSH.
 This does away with the need for a separate display for Raspberry Pi and
you can use Raspberry Pi from your desktop or laptop computer.
 Lists the frequently used commands on Raspberry Pi.

AMRITASAI-AUTONOMOUS 9 DEPARTMENT OF ECE


5. Raspberry Pi Interfaces:
 Raspberry Pi has serial, SPI and 12C interfaces for data transfer as shown in
Figure.
1. Serial:
 The serial interface on Raspberry Pi has receive (Rx) and transmit (Tx) pins
for comm with serial peripherals.
2. SPI:
 Serial Peripheral Interface (SPI) is a synchronous serial data protocol used
for communicating with one or more peripheral devices.
 In an SPI connection, there is one master device s one or more peripheral
devices.
 There are five pins on Raspberry Pi for SPI interface:
MISO (Master In Slave Out): Master line for sending data to the peripherals.
MOSI (Master Out Slave In): Slave line for sending data to the master.
SCK (Serial Clock): Clock generated by master to synchronize data
transmission
CEO (Chip Enable 0): To enable or disable devices.
AMRITASAI-AUTONOMOUS 10 DEPARTMENT OF ECE
CEO (Chip Enable 1): To enable or disable devices.
3. I2C:
 The 12C interface pins on Raspberry Pi allow you to connect hardware
modules.
 I2C interface allows synchronous data transfer with just two pins - SDA
(data line) and SCL (clock line).

Figure: Rasberry Pi GPIO headers

6. Programming Raspberry Pi with Python:


 Raspberry Pi runs Linux and supports Python out of the box.
 Therefore, you can run any Python program that runs on a normal
computer.
 However, it is the general purpose input/output capability provided by the
GPIO pins on Raspberry Pi that makes it useful device for Internet of Things.
 You can interface a wide variety of sensor and actuators with Raspberry Pi
using the GPIO pins and the SPI, I2C and serial interfaces.
 Input from the sensors connected to Raspberry Pi can be processed and
various actions can be taken, for instance, sending data to a server, sending
AMRITASAI-AUTONOMOUS 11 DEPARTMENT OF ECE
an email, triggering a relay switch.
1. Controlling LED with Raspberry Pi:
 A basic example of controlling an LED from Raspberry Pi.
 Figure shows the schematic diagram of connecting an LED to Raspberry Pi.

Figure: Controlling LED with Raspberry Pi

 Python program for blinking LED


import RPi.GPIO as GPIO
import time
GPIO.setmode (GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
GPIO.output (18, True)
time.sleep(1)
GPIO.output (18, False)
time.sleep(1)

2. Interfacing an LED and switch with Raspberry PI:

AMRITASAI-AUTONOMOUS 12 DEPARTMENT OF ECE


Figure: Interfacing LED and switch with Raspberry Pi

 Python program for controlling an LED with a switch

From time import sleep


Import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#Switch Pin
GPIO.setup(25, GPIO.IN)
#LED Pin
GPIO.setup(18, GPIO.OUT)
State=false
def toggleLED (pin):
State=not state
GPIO.output (pin, state)
while True:
try:
if (GPIO.input (25)== True):
toggleLED (pin)
sleep(.01)
except KeyboardInterrupt:

AMRITASAI-AUTONOMOUS 13 DEPARTMENT OF ECE


exit()

3. Interfacing a Light Sensor(LDR) with Raspberry PI:

Figure: Interfacing LDR with Raspberry Pi

 Python program
import RP1.GPIO as GPIO
import time
GPIO.setmode (GPIO.BCM)
1dr_threshold - 1000
LDR PIN 18
LIGHT PIN 25
def readLDR (PIN):
reading-0
GPIO.setup(LIGHT_PIN, GPIO.OUT)
GPIO.output (PIN, False)
time.sleep(0.1)
GPIO.setup(PIN, GPIO.IN)
while (GPIO.input (PIN)==False):
reading reading+1
AMRITASAI-AUTONOMOUS 14 DEPARTMENT OF ECE
return reading
def switchOnLight (PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output (PIN, True)
def switchOffLight (PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output (PIN, False)
while True:
ldr_reading readLDR (LDR_PIN)
if ldr_reading < 1dr_threshold:
switchOnLight (LIGHT_PIN)
else:
switchOffLight (LIGHT_PIN)
time.sleep (1)

7. Python Web Application Framework - Django:


 Django is an open source web application framework for developing web
applications in Python.
 Django is based on the well-known Model-Template-View architecture and
provides a separation of the data model from the business rules and the
user interface.
 Django provides a unified API to a database backend.
 Therefore, web applications built with Django can work with different
databases without requiring any code changes.
 Django is best suited for lOT applications.
 Django, concisely stated, consists of an object-relational mapper, a web
templating system and a regular-expression-based URL dispatcher.
7.1 Django Architecture:

Fgure: Django Architecture


AMRITASAI-AUTONOMOUS 15 DEPARTMENT OF ECE
 Django is a Model-Template-View (MTV) framework wherein the roles of
model, template and view, respectively, are:
1. Model:
 The model acts as a definition of some stored data and handles the
interactions with the database.
 In a web application, the data can be stored in a relational database, non-
relational database, an XML file, etc.
 A Django model is a Python class that outlines the variables and methods
for a particular type of data.
2. Template:
 In a typical Django web application, the template is simply an HTML page
with a few extra placeholders.
 Django's template language can be used to create various forms of text files
(XML, email, CSS, Java-script, CSV, etc.))
3.View:
 The view ties the model to the template.
 The view is where you write the code that actually generates the web
pages.
 View determines what data is to be displayed, retrieves the data from the
database and passes the data to the template.

 Examples of Django Applications and their MVT Implementation:


1. Social Media Platforms (e.g., Instagram, Facebook):
 Models: User profiles, posts, comments, likes, followers.
 Views: Handle user authentication, post creation, displaying feeds,
managing comments, etc.
 Templates: Display user profiles, posts, comments, and other UI
elements.
2. E-commerce Platforms (e.g., Amazon, Etsy):
 Models: Products, categories, users, orders, shipping addresses.
 Views: Handle product browsing, adding items to cart, checkout, order
processing, etc.
 Templates: Display product listings, shopping cart, checkout forms, order
confirmations, etc.
3. Content Management Systems (e.g., WordPress, Drupal):
 Models: Pages, posts, users, categories, tags.
 Views: Handle content creation, editing, publishing, and displaying.
 Templates: Define the layout and structure of the website.
4. FinTech Applications (e.g., Robinhood, Revolut):
 Models: User accounts, transactions, stock data, etc.

AMRITASAI-AUTONOMOUS 16 DEPARTMENT OF ECE


 Views: Handle user authentication, account management, trading, real-
time data updates, etc.
 Templates: Display account balances, transaction history, stock charts,
etc.

 Benefits of the MVT Architecture:


1. Modularity:
 Separating concerns into distinct components makes the code easier to
understand, maintain, and reuse.
2. Scalability:
 The architecture is well-suited for building large and complex web
applications.
3. Testability:
 The separation of concerns makes it easier to write unit tests for individual
components.
4. Flexibility:
 Django's MVT architecture allows for a variety of different use cases and
application types.

8. Designing a RESTful Web API:

 The Django REST framework can be installed as follows:

1.pip install djangorest framework

2.pip install markdown

3.pip install django-filter

 After installing the Django REST framework, let us create a new Django
project me rest fulapi, and then start a new app called myapp, as follows:

4.django-admin.py startproject restfulapi cd restfulapi

5.python manage.py startapp myapp

 The REST API described in this section allows you to create, view, update
and deletes collection of resources where each resource represents a sensor
data reading from a weathe monitoring station.

 Below shows the settings for the REST API Django project.
Django model for Weather Station - models.py
from django.db import models
class Station (models. Model):
AMRITASAI-AUTONOMOUS 17 DEPARTMENT OF ECE
Name=models.CharField (max_length-10).
Timestamp= models.CharField (max_length=10)
temperature =models.CharField (max_length-5)
Lat= models.CharField (max_length-10)
Lon= models. CharField (max_length-10)
 Django views for Weather Station REST API - views.py
from myapp.models import Station
from rest_framework import viewsets
from django.shortcuts import render_to_response
from django.template import RequestContext
from myapp.serializers import StationSerializer
import requests
import json
class StationViewSet (viewsets.ModelViewSet):
queryset Station.objects.all()
serializer_class StationSerializer
def home (request):
r-requests.get('http://127.0.0.1:8000/station/",
auth=('username', 'password'))
result-r.text
output json.loads(result)
count-output['count']
count-int (count)-1
name output['results') [count] ['name']
temperature-output['results'] [count] ['temperature']
lat-output['results'] [count] ['lat']
lon-output ['results'] [count] ['lon']
return render_to_response ('index.html', 'name': name, 'temperature':
temperature, 'lat': lat, 'lon': lon, context_instance-RequestContext(request))

AMRITASAI-AUTONOMOUS 18 DEPARTMENT OF ECE

You might also like