0% found this document useful (0 votes)
4 views111 pages

IoT Unit 5

The document outlines the integration of IoT with cloud computing, detailing various cloud storage models and services such as IaaS, PaaS, and SaaS. It discusses the importance of APIs in facilitating communication between applications and cloud services, particularly in the context of IoT. Additionally, it introduces the Xively cloud platform for IoT solutions and the Django framework for developing web applications in Python.

Uploaded by

manasasunkari85
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)
4 views111 pages

IoT Unit 5

The document outlines the integration of IoT with cloud computing, detailing various cloud storage models and services such as IaaS, PaaS, and SaaS. It discusses the importance of APIs in facilitating communication between applications and cloud services, particularly in the context of IoT. Additionally, it introduces the Xively cloud platform for IoT solutions and the Django framework for developing web applications in Python.

Uploaded by

manasasunkari85
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/ 111

Unit - V

IoT Physical Servers &


Cloud Offerings

6/7/2022 1
Outline

• Introduction to Cloud Storage models


• Communication APIs
• Webserver-Web server for IoT
• Cloud for IoT
• Python web application framework
• Designing a RESTful web API.

6/7/2022 2
Introduction to Cloud Computing

• The Internet of Things (IoT) involves the internet-connected devices which are used
to perform the processes and services that supports way of life.

• Another component set to help IoT succeed is cloud computing, which acts as a sort
of front end.

• Cloud computing is an increasingly popular service that offers several advantages to


IOT, and is based on the concept of allowing users to perform normal computing
tasks using services delivered entirely over the internet.

6/7/2022 3
Introduction to Cloud Computing

• In simple words, Cloud Computing means providing computing services over the
internet (called as cloud).

• The computing services include Servers, Software, Storage, Database,


Networking, Analytics, Intelligence etc.,

6/7/2022 4
Introduction to Cloud Computing

• A worker may need to finish a major project that must be submitted to a manager,
but perhaps they encounter problems with memory or space constraints on their
computing device.

• Memory and space constraints can be minimized if an application is instead hosted


on the internet.

• The worker can use a cloud computing service to finish their work because the data is
managed remotely by a server.

6/7/2022 5
Introduction to Cloud Computing

• Another example- you have a problem with your mobile device and you need to
reformat it or reinstall the operating system.

• You can use Google Photos to upload your photos to internet-based storage.

• After the reformat or reinstall, you can then either move the photos back to you
device or you can view the photos on your device from the internet when you want.

6/7/2022 6
Features of Cloud Computing

6/7/2022 7
Types of Cloud Computing Storage Services

6/7/2022 8
Types of Cloud Services

• Infrastructure-as-a-service(IaaS) provides users the ability to provision computing


and storage resources. These resources are provided to the users as a virtual
machine instances and virtual storage.
• Platform-as-a-Service(PaaS) provides users the ability to develop and deploy
application in cloud using the development tools, APIs, software libraries and
services provided by the cloud service provider.
• Software-as-a-Service(SaaS) provides the user a complete software application or
the user interface to the application itself.
6/7/2022 9
Types of Cloud Services

6/7/2022 10
Advantages

• Cost
• Speed
• Security
• Scalability
• Reliability

6/7/2022 11
Cloud Storage API

• A cloud storage API is an application program interface that connects a


locally-based application to a cloud-based storage system, so that a user
can send data to it and access and work with data stored in it.
• To the application, the cloud storage system is just another target device,
like tape or disk-based storage.
• An application program interface (API) is code that allows two software
programs to communicate with each other.

6/7/2022 12
Cloud Storage API

• The API defines the correct way for a developer to write a program that
requests services from an operating system (OS) or other application.
• APIs are implemented by function calls composed of verbs and nouns.
• The required syntax is described in the documentation of the application
being called.

6/7/2022 13
How APIs Works

• APIs are made up of two related elements.


• The first is a specification that describes how information is exchanged
between programs, done in the form of a request for processing and a
return of the necessary data.
• The second is a software interface written to that specification and
published in some way for use. The software that wants to access the
features and capabilities of the API is said to call it, and the software that
creates the API is said to publish it.
6/7/2022 14
Why APIs are important

• The web, software designed exchange information via the internet and
cloud computing have all combined to increase the interest in APIs in
general and services in particular.
• Software that was once custom-developed for a specific purpose is now
often written referencing APIs that provide broadly useful features,
reducing development time and cost and mitigating the risk of errors.

6/7/2022 15
Why APIs are important

• APIs have steadily improved software quality over the last decade, and the
growing number of web services exposed through APIs by cloud providers
is also encouraging the creation of cloud-specific applications, internet of
things (IoT) efforts and apps to support mobile devices and users.

6/7/2022 16
Basic Types of APIs

• APIs take three basic forms: local, web-like and program-like.


• Local APIs are the original form, from which the name came. They offer
OS or middleware services to application programs. Microsoft's .NET APIs,
the TAPI (Telephony API) for voice applications, and database access
APIs are examples of the local API form.

6/7/2022 17
Basic Types of APIs

• Web APIs are designed to represent widely used resources like HTML
pages and are accessed using a simple HTTP protocol. Any web URL
activates a web API.
• Web APIs are often called REST (representational state transfer) or
RESTful because the publisher of REST interfaces doesn't save any data
internally between requests. As such, requests from many users can be
intermingled as they would be on the internet.

6/7/2022 18
Basic Types of APIs

• Program APIs are based on remote procedure call (RPC) technology that
makes a remote program component appear to be local to the rest of the
software.
• Service oriented architecture (SOA) APIs, such as Microsoft's WS-series of
APIs, are program APIs.

6/7/2022 19
WAMP - Autobahn for IoT

• Web Application Messaging Protocol (WAMP) is a sub-protocol of Websocket


which provides publish-subscribe and remote procedure call (RPC) messaging
patterns.
• WAMP enables distributed application architectures where the application
components are distributed on multiple nodes and communicate with messaging
patterns provided by WAMP.

6/7/2022 20
WAMP Session Between Client and Router

6/7/2022 21
WAMP - Concepts
• Transport: Transport is channel that connects two peers. The default transport
for WAMP is Web socket. WAMP can run over the transports as well which
support message based reliable bi directional communication.
• Session: Session is a conversation between two peers that runs over a
transport.

6/7/2022 22
WAMP - Concepts
• Client: Clients are peers that can have one or more roles. In publish-subscribe
model client can have following roles:
– Publisher: Publisher publishes events (including payload) to the topic
maintained by the Broker.
– Subscriber: Subscriber subscribes to the topics and receives the events
including the payload.

6/7/2022 23
WAMP - Concepts

• In RPC model client can have following roles:


– Caller: Caller issues calls to the remote procedures along with call arguments.
– Callee: Callee executes the procedures to which the calls are issued by the caller
and returns the results back to the caller.

6/7/2022 24
WAMP - Concepts

• Router: Routers are peers that perform generic call and event routing. In publish-
subscribe model Router has the role of a Broker:
– Broker: Broker acts as a router and routes messages published to a topic to all
subscribers subscribed to the topic.

6/7/2022 25
WAMP Protocol Interaction between the Peers

6/7/2022 26
WAMP Protocol Interaction between the Peers

• In this protocol, WAMP transport used is Websocket.


• WAMP sessions are established over Websocket transport within the
lifetime of Websocket transport.

6/7/2022 27
Publish Subscribe Messaging using WAMP -
Autobahn

6/7/2022 28
Publish Subscribe Messaging using WAMP -
Autobahn
• The client in Publisher role runs a WAMP application component that
publishes messages to the router.
• The router in broker role runs on the server and routes the messages to
the subscribers.
• The router decouples the publisher from the subscribers.
• The communication between publisher- broker and broker subscribers
happens over a WAMP websocket session.

6/7/2022 29
Commands used for installing Auto Bahn in
Python

6/7/2022 30
Commands used for installing Auto Bahn in
Python

6/7/2022 31
WAMP - Autobahn

• Create a WAMP Publisher Component.


• The publisher component will publish a message containing the current
time stamp to a topic named ‘test – topic’.
• Create a WAMP Subscriber component.
• The subscriber component that subscribes to the ‘test-topic’

6/7/2022 32
WAMP Publisher Component

6/7/2022 33
WAMP Subscriber Component

6/7/2022 34
WAMP Subscriber Component

• Run the application router on a websocket server as follows:

6/7/2022 35
Xively Cloud for IoT

• It is a commercial platform as a service (PAAS) that can be used for


creating solutions for IoT.
• With Xively cloud, IoT developers can focus on the front end infrastructure
and devices for IoT(that generate the data), while the backend data
collection infrastructure is managed by Xively.

6/7/2022 36
Xively Cloud for IoT

• Xively platform comprises of a message bus for real-time message


management and routing, data services for time series archiving, directory
services that provides a search - able directory of objects and business
services for device provisioning and management.
• Xively provides an extensive support for various languages and platforms
• The Xively libraries leverage standards based API over HTTP, sockets and
MQTT for connecting IoT devices to the Xively Cloud.

6/7/2022 37
Xively Cloud for IoT

• To start using Xively python library, first we need to register a developer


account and then create development devices on Xively.
• When a new device is created, Xively automatically creates a Feed ID and
an API key to connect to the device.
• Feed ID is a collection of channels or data streams defined for a device
and associated meta data.
• API keys are used to provide different levels of permissions.
• The default API key has read, update, create and delete permissions.
6/7/2022 38
Xively Cloud for IoT

6/7/2022 39
Xively Cloud for IoT

6/7/2022 40
Xively Cloud for IoT

• Xively devices have one or more channels and each channel enables
bidirectional communication between the IoT devices and Xively cloud.
• IoT devices can send data to a channel using the Xively APIs.
• For each channel one or more triggers can be created. A trigger
specification includes a channel to which the trigger corresponds, trigger
condition and an HTTP post URL to which the request is sent when the
trigger fires.
• Trigger are used to provide integration for third party applications.
6/7/2022 41
Xively Cloud for IoT

• Let us consider an example of using Xively cloud for an IoT system that
monitors temperature and sends the measurements to a Xively channel.
• Temperature monitoring device can be built by raspberry pi board and a
temperature sensor is connected to the board.
• The raspberry pi runs a controller program that reads the sensor values
every few seconds and sends the measurements to a Xively channel.
• Box 8.4 shows the python program for sending the temperature data to
Xively cloud by using Xively python library.
6/7/2022 42
Xively Cloud for IoT

• To keep the program simple and without going in to the details of the
temperature sensor, synthetic data is used (generated randomly in
readTemp Sensor()function).
• A feed object is created by providing the API key and Feed ID. Then a
channel named temperature is created.
• The temperature data is sent to this channel in the runController() function
every 10 seconds.

6/7/2022 43
Xively Cloud for IoT

6/7/2022 44
Xively Cloud for IoT

6/7/2022 45
Xively Cloud for IoT

6/7/2022 46
Xively Cloud for IoT

• Figure 8.6 shows the temperature channel in the Xively dashboard. In this
example a single Xively device with one channel is created. Xively can also
have multiple channels and multiple devices.

6/7/2022 47
Xively Cloud for IoT

6/7/2022 48
Python Web Application Framework - Django

• Django is an open source web application framework for developing web


applications in Python.

• A "web application framework" in general is a collection of solutions,


packages and best practices that allows development of web
applications and dynamic websites.

• Django is based on the Model-Template-View architecture and provides a


separation of the data model from the business rules and the user
interface.
6/7/2022 49
Python Web Application Framework - Django

• Django provides a unified API to a database backend.

• Thus web applications built with Django can work with different databases
without requiring any code changes.

• With this fiexibility in web application design combined with the powerful
capabilities of the Python language and the Python ecosystem, Django is best
suited for cloud applications.

• Django consists of an object-relational mapper, a web templating system and


a regular-expression- based URL dispatcher.
6/7/2022 50
Django Architecture

• Django is Model-Template-View (MTV) framework .


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.

6/7/2022 51
Django Architecture

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,
Javascript, CSV, etc.)

6/7/2022 52
Django Architecture

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.

6/7/2022 53
Starting Development with Django

Creating a Django Project and App

• When a new Django project is created, a number of files are created as


described below

6/7/2022 54
Starting Development with Django

• A Django can have multiple Apps, where these Apps are used to write the
code that makes website function.

• When a new application is created a new directory for the application is


also created which has a number of files including –

6/7/2022 55
Starting Development with Django

6/7/2022 56
Starting Development with Django

• Django comes with a built in, light weight web server that can be used for
development purposes.

• When the Django development server is started the default project can be
viewed at the URL: http://localhost:8000.

6/7/2022 57
Starting Development with Django

6/7/2022 58
Starting Development with Django

Configuring a Database

• Developers have a wide choice of databases that can be used for web
applications including both relational and non relational databases.

• Django provides a unified API for database backend thus giving the
freedom to choose the database.

• Django supports various relational database engines including MySQL,


PostgreSQL, Oracle and SQLite3.

6/7/2022 59
Starting Development with Django

• Support for non relational data bases such as MongoDB can be added by
installing additional engines (Eg. Django- MongoDB engine for
MkongoDB).

• The first step in setting up a database is to install and configure a


database server.

• After installing the data base, the next step is to specify the database
settings in the setting.py file in the Django project.

6/7/2022 60
Commands to setup My SQL

6/7/2022 61
Database setting to use MySQL with a
Django Project

6/7/2022 62
6/7/2022 63
Starting Development with Django

6/7/2022 64
Defining a Model

• A Model acts as a definition of the data in the database.

• Box 8.10 shows an example of a Django model for TemperatureData


collected by an IoT Device.

• The TemperatureData table in the database is defined as a class in the


Django Model.

• Each class that represents a database table is a subclass of


django.db.models.Model which contains all the functionality that allows
the models to interact with the database.
6/7/2022 65
Defining a Model

• The TemperatureData class has fields timestamp, temperature, lat and lon
all of which are CharField.

• To sync the models with the database, run the following command:

• >python manage.py syncdb

• When the syncdb command is run for the fist time, it creates all the tables
defined in the Django model in the configured database.

6/7/2022 66
Defining a Model

6/7/2022 67
Django Admin Site

• Django provides an administration system that allows to manage the


website without writing the additional code.

• This “admin” system reads the Django model and provides an interface
that can be used to add content to the site.

• The Django admin site is enabled by adding d jango.contrib.admin and d


jango.contrib.admindocs to the INSTALLED_APPS section in the
settings.py file.

6/7/2022 68
Django Admin Site

• The admin site also requires URL pattern definitions in the url.py file.

• To define which application models to be editable in the admin interface, a


new file named admin.py is created in the application folder.

6/7/2022 69
Django Admin Site

6/7/2022 70
Django Admin Site

6/7/2022 71
Defining a View

• The view contains the logic that glues the model to the template.

• The view determines the data to be displayed in the template, retrieves


the data from the database and passes it to the template.

• Conversely, the view also extracts the data posted in a form in the
template and inserts it in the database.

• Typically, each page in the website has a separate view, which is


basically a python function in the views.py file.

6/7/2022 72
Defining a View

• Views can also perform additional tasks such as authentication, sending


emails, etc.,

• Box 8.12 shows an example of a Django view for the Weather Station
app.

• This view corresponds to the webpage that displays latest entry in the
TemperatureData table.

• In this view the Django’s built in object-relational mapping API is used to


retrieve the data from the TemperatureData Table.
6/7/2022 73
Defining a View

• The object- relational mapping API allows the developers to write generic
code for interacting with the database without worrying about the
underlying database engine.

• So the same code for database interactions works with different database
backends.

• A specific python library can be chose to the database backend used (eg.
MySQLdb for MySQL, PyMongo for MongoDB etc.) to write database
backed specific code.
6/7/2022 74
Defining a View

• In the view shown in Box 8.12, the


TemperatureData.objects.order_by(‘_id’) query returns the latest entry in
the table.

• To retrieve all entries, can use table.objects.all().

• To retrieve specific entries, use table.objects.filter(**Kwargs) to filter out


queries that match the specified condition.

6/7/2022 75
Defining a View

• To render the retrieved entries in the template, the render_to_response


function is used.

• This function renders a given template with a given context dictionary and
returns an HttpResponse object with that rendered text.

• Box 8.13 shows an alternative view that retrieves data from the Xively
cloud.

6/7/2022 76
Defining a View

6/7/2022 77
Defining a View

6/7/2022 78
Defining a View

6/7/2022 79
Defining a Template

• A Django Template is typically an HTML file (it can be any sort of text file
such as XML, email, CSS, Java script, CSV etc,).

• Django templates allow separation of the presentation of data from the


actual data by using placeholders and associated logic (Using template
tags).

• A template receives a context from the view and presents the data in
context variables in the placeholders.

6/7/2022 80
Defining a Template

• Box 8.14 shows an example of a template for the Weather Station app
and the variables containing the retrieved temperature, latitude and
longitude are passed to the template.

6/7/2022 81
Defining a Template

6/7/2022 82
Defining a Template

6/7/2022 83
Defining a Template

6/7/2022 84
Defining a Template

6/7/2022 85
Defining the URL Patterns

• URL patterns are a way of mapping the URLs to the views that should
handle the URL requests.

• The URLs requested by the user are matched with the URL patterns and
the view corresponding to the pattern that matches the URL is used to
handle the request.

6/7/2022 86
Defining the URL Patterns

• Box 8.15 shows an example of the URL patterns for the Weather Station
Project and the URL patterns are constructed using regular expressions.

• The simplest regular expression (r’ ^ $’) corresponds to the root of the
website or the home page.

6/7/2022 87
Defining the URL Patterns

6/7/2022 88
Defining the URL Patterns

• With the Django models, views, templates and URL patterns defined for
the Django project, the application finally runs with the commands shown
in Box 8.16.

6/7/2022 89
Designing a RESTful Web API

• Django REST framework can be installed as follows:

6/7/2022 90
Designing a RESTful Web API

• After installing the Django REST framework, create a new Django project
named restfulapi, and then start a new app called myapp, as follows:

6/7/2022 91
Designing a RESTful Web API

• The REST API allows to create, view, update and delete a collection of
resources where each resource represents a sensor data reading from a
weather monitoring station.

• Box 8.17 shows the Django model for such a station.

• The station model contains four fields – station name, timestamp,


temperature, latitude and longitude

6/7/2022 92
Designing a RESTful Web API

6/7/2022 93
Designing a RESTful Web API

• Box 8.18 shows the Django views for the REST API.

• ViewSets are used for the views that allows to combine the logic for a set
of related views in a single class.

6/7/2022 94
Designing a RESTful Web API

6/7/2022 95
Designing a RESTful Web API

6/7/2022 96
Designing a RESTful Web API

• Box 8.19 shows the serializers for the REST API.

• Serializers allow complex data (such as querysets and model instances)


to be converted to native python datatypes that can be easily rendered
into JSON, XML or other content types.

• Serializers also provide de-serialization, allowing parsed data to be


converted back into complex types, after first validating the incoming
data.

6/7/2022 97
Designing a RESTful Web API

6/7/2022 98
Designing a RESTful Web API

• Box 8.20 shows the URL patterns for the REST API.

• Since, ViewSets are used instead of views, it can automatically generate


the URL conf for API, by registering the ViewSets with a router class.

• Routers automatically determining how the URLs for an application


should be mapped to the logic that deals with handling incoming
requests.

6/7/2022 99
Designing a RESTful Web API

6/7/2022 100
Designing a RESTful Web API

• Box 8.21 shows the settings for the REST API Django project.

6/7/2022 101
Designing a RESTful Web API

• Box 8.21 shows the settings for the REST API Django project.

6/7/2022 102
Designing a RESTful Web API

• After creating the station REST API source files, the next step is to setup
the database and then run the Django development web server as
follows:

6/7/2022 103
Designing a RESTful Web API

• Box 8.22 shows examples of interacting with the Station REST API using
CURL.

• The HTTP POST method is used to create a new resource, GET method
is used to obtain information about a resource, PUT method is used to
update a resource and DELETE method is used to delete a resource.

6/7/2022 104
Designing a RESTful Web API

6/7/2022 105
Designing a RESTful Web API

6/7/2022 106
Designing a RESTful Web API

6/7/2022 107
Designing a RESTful Web API

6/7/2022 108
Designing a RESTful Web API

6/7/2022 109
Designing a RESTful Web API

6/7/2022 110
Designing a RESTful Web API

6/7/2022 111

You might also like