IoT Unit 5
IoT Unit 5
6/7/2022 1
Outline
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.
6/7/2022 3
Introduction to Cloud Computing
• In simple words, Cloud Computing means providing computing services over the
internet (called as cloud).
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.
• 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
6/7/2022 10
Advantages
• Cost
• Speed
• Security
• Scalability
• Reliability
6/7/2022 11
Cloud Storage API
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
• 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
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
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
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
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
6/7/2022 32
WAMP Publisher Component
6/7/2022 33
WAMP Subscriber Component
6/7/2022 34
WAMP Subscriber Component
6/7/2022 35
Xively Cloud for IoT
6/7/2022 36
Xively Cloud for IoT
6/7/2022 37
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
• 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.
• 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
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
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.
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.
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).
• 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
• 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:
• 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
• This “admin” system reads the Django model and provides an interface
that can be used to add content to the site.
6/7/2022 68
Django Admin Site
• The admin site also requires URL pattern definitions in the url.py file.
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.
• Conversely, the view also extracts the data posted in a form in the
template and inserts it in the database.
6/7/2022 72
Defining a View
• 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.
• 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
6/7/2022 75
Defining a View
• 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,).
• 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
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.
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
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.
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