Module-5 Iot
Module-5 Iot
ONLINE
COMPONENTS
• Internet of Things devices are “magical” objects: a
physical thing with an embedded controller and
sensors that allow it to do clever things that
mechanical object couldn’t.
of steps.
Ex: Flickr is an American image hosting and video
hosting service,
effect.
• Using a mapping API to plot properties to rent or buy
• Ex:
Mapumental.
newspaper’s API.
The Guardian newspaper’s API
• This API stores all articles, images, audio and
videos dating back to 1999.
• All accessible with a single open platform key
• Some of the more visible and easy-to-use APIs want
to embed your data within them—for example, the
Google Maps API.
• This means that they are ideal to use within a web
browser, but you aren’t in control of the final product,
and there might be limited scope for accessing them
from a microcontroller.
SCRAPING
• In many cases, companies or institutions have access
to fantastic data but don’t want to or don’t have the
resources or knowledge to make them available as an
API.
• An API defines the messages that are sent from client to server and
from server to client.
• Ultimately, you can send data in whatever format you want, but it is
almost always better to use an existing standard because convenient
libraries will exist for both client and server to produce and understand
the required messages.
• Here are a few of the most common standards that you should
consider:
• Representational State Transfer (REST): Access a set of web URLs using
HTTP methods such as GET and POST, but also PUT and DELETE. The
result is often XML or JSON but can often depend on the HTTP content-
type negotiation mechanisms.
• JSON-RPC: Access a single web URL
• XML-RPC: This standard is just like JSON-RPC but uses XML instead of
JSON.
• Simple Object Access Protocol (SOAP): This standard uses XML for
transport like XML-RPC but provides additional layers of functionality,
which may be useful for very complicated systems.
WRITING A NEW API : USING CURL TO TEST
• Clients for URLs is a project comprised of two development
efforts- cURL and libcurl . Libcurl if free , client side URL transfer
library with support of wide range of protocols . Curl is command
tool for getting or sending files using URL syntax .
• While you are developing the API, and afterwards , to test it and
show it off , you need a way to interact with it .
• curl simply makes an HTTP request and prints out the result to a
terminal.
WRITING A NEW API : GETTING FURTHER
• API Rate Limiting : If the service becomes popular, managing the
number of connections to the site becomes critical. Setting a
maximum number of calls per day or per hour or per minute
might be useful. You could do this by setting a counter for each
period that you want to limit.
• While a software application can easily warn users that their
usage limit has been exceeded and they should try later .
• OAuth for Authenticating with Other Services : While OAuth may
not (currently) be the best solution for connecting with a
microcontroller (at present, there are no accepted libraries for
Arduino), there is no reason why the back-end service should not
accept OAuth to allow hooks to services like Twitter, music
discovery site last.fm, or the web automation of If This Then That.
• Interaction via HTML : The API currently serializes the output only
in JSON, XML, and Text formats. You might also want to connect
from a web browser.
WRITING A NEW API :GETTING FURTHER
• Drawbacks : Although web browsers do speak HTTP, they don’t
commonly communicate in all the methods that we’ve discussed.
In particular, they tend to support the following:
GET: Used to open pages and click on links to other pages.
POST: Used when submitting a form or to upload files. To post a
timer, you could create a web form like the following:
<form method=”POST” action=”/timers.html”>
<input type=”text” name=”duration”>
<input type=”submit” value=”Create a new timer!”>
</form>
• This form calls the POST action and returns the appropriate HTML
• .
WRITING A NEW API :GETTING FURTHER
• Designing a Web Application for Humans
• For example, the following figure shows a static login page, to be served by GET.
The API didn’t even specify a GET action, as it was superfluous for a computer.
This page is entirely for the convenience of a human.
• All the labels like “Your email address” and the help text like “Remember your
password is case sensitive” are purely there to guide the user.
• The logo, as well as proving that we are really not designers, is there as a
branding and visual look and feel for the site.
WRITING A NEW API :GETTING FURTHER
• That’s a simple example, but the following figure shows an even more extreme
change. The list of timers, instead of being a JSON string containing a raw data
structure, is highly formatted. The dates are formatted for humans. The
duration of the timer and the status (in progress, completed, abandoned) are
visualized with colors, progress bars, and a duration “badge”.
• The page also links to other actions: The “Edit” button opens a page that
allows access to the actions that change description, and so on. The menu bar
at the top links to other functions to help users flow through the tasks they
want to carry out.
• Looking at your product from both contrasting perspectives (machine and
human) will make it stronger and better designed.
REAL-TIME REACTIONS
• For a bare-bones board such as the Arduino, the current Ethernet/HTTP shields
and libraries tend to block during the connection, which means that during
that time, the microcontroller\ can’t easily do any other processing .
• If you want to perform an action the instant that something happens on your
board, you may have to factor in the connection time.
• If the server has to perform an action immediately, that “immediately” could
be nearly a minute later, depending on the connection time.
• We look at two options here: polling and the so-called “Comet” technologies.
REAL-TIME REACTIONS : polling
• If you want the device or another client to respond immediately, how do you do
that? You don’t know when the event you want to respond to will happen, so
you can’t make the request to coincide with the data becoming available.
• Consider these two cases:
o The Where Dial should start to turn to “Work” the moment that the user has
checked into his office.
o The moment that the task timer starts, the client on the user’s computer
should respond, offering the opportunity to type a description of the task.
REAL-TIME REACTIONS : polling
• The traditional way of handling this situation using HTTP API requests was to
make requests at regular intervals. This is called polling.
• You might make a call every minute to check whether new data is available for
you.
• However, this means that you can’t start to respond until the poll returns. So
this might mean a delay of (in this example) one minute plus the time to
establish the HTTP connection.
• You could make this quicker, polling every 10 seconds, for example.
• But this would put load on the following:
o The server: If the device takes off, and there are thousands of devices, each of
them polling regularly, you will have to scale up to that load.
o The client: This is especially important if, as per the earlier Arduino example,
the microcontroller blocks during each connect!
REAL-TIME REACTIONS : Comet
• Comet is an umbrella name for a set of technologies developed to get around
the inefficiencies of polling.
• Long Polling (Unidirectional) :
• The first important development was “long polling”, which starts off with the
client making a polling request as usual.
• However, unlike a normal poll request, in which the server immediately
responds with an answer, even if that answer is “nothing to report”, the long
poll waits until there is something to say.
• This means that the server must regularly send a keep-alive to the client to
prevent the Internet of Things device or web page from concluding that the
server has simply timed out.
REAL-TIME REACTIONS : Comet
• Long Polling (Unidirectional) where it can be used :
• Long polling would be ideal for the case of WhereDial: the dial requests to
know when the next change of a user’s location will be. As soon as WhereDial
receives the request, it moves the dial and issues a new long poll request.
• Of course, if the connection drops (for example, if the server stops sending
keep-alive messages), the client can also make a new request.
• However, it isn’t ideal for the task timer, with which you may want to send
messages from the timer quickly, as well as receive them from the server.
Although you can send a message, you have to establish a connection to do so.
REAL-TIME REACTIONS : Comet
• Multipart XMLHttpRequest (MXHR) (Unidirectional) :
• When building web applications, it is common to use a JavaScript API called
XMLHttpRequest to communicate with the web server without requiring a full
new page load.
• Note that XMLHttpRequest is a misnomer because there’s no requirement to
actually use XML at all. Using this content type is perhaps more sophisticated if
you want to be able to receive multiple messages from the server.
• In the example of WhereDial, this is unlikely; you’re unlikely to change location
first to Home and then to Work in quick succession.
• However, for an Internet of Things device such as Adrian’s Xively meter, which
tries to show the state of a Xively feed in real time, being able to respond to
changes from the server almost immediately is the essential purpose of the
device.
REAL-TIME REACTIONS : Comet
• HTML5 WebSockets (Bidirectional) :
• Traditionally, the API used to talk directly to the TCP layery is known as the
sockets API. When the web community was looking to provide similar
capabilities at the HTTP layer, they called the solution WebSockets.
• WebSockets have the benefit of being bidirectional.
• You can consider them like a full Unix socket handle that the client can write
requests to and read responses from.
• This might well be the ideal technology for the task timer. After a socket is
established, the timer can simply send information down it about tasks being
started, modified, or cancelled, and can read information about changes made
in software, too.
OTHER PROTOCOLS
• MQ TELEMETRY TRANSPORT :
• MQTT is a lightweight messaging protocol, designed specifically for scenarios
where network bandwidth is limited or a small code footprint is desired.
• It was developed initially by IBM but has since been published as an open
standard, and a number of implementations, both open and closed source, are
available, together with libraries for many different languages.
• Rather than the client/server model of HTTP, MQTT uses a publish/ subscribe
mechanism for exchanging messages via a message broker.
• This makes it much easier to do one-to-many messaging, and also breaks the
tight coupling between the client and server that exists in HTTP.
• A sister protocol, MQTT for Sensors (MQTT-S), is also available for extremely
constrained platforms or networks where TCP isn’t available, allowing MQTT’s
reach to extend to sensor networks such as ZigBee.
OTHER PROTOCOLS
• EXTENSIBLE MESSAGING AND PRESENCE PROTOCOL :
• XMPP grew from the Jabber instant messaging system and so has broad
support as a general protocol on the Internet.
• This is both a blessing and a curse: it is well understood and widely deployed,
but because it wasn’t designed explicitly for use in embedded applications, it
uses XML to format the messages.
• This choice of XML makes the messaging relatively verbose, which could
preclude it as an option for RAM-constrained microcontrollers.
OTHER PROTOCOLS
• CONSTRAINED APPLICATION PROTOCOL :
• There are proposals for running CoAP over UDP, SMS mobile phone messaging,
and integration with 6LoWPAN.
• CoAP draws many of its design features from HTTP and has a defined
mechanism to proxies to allow mapping from one protocol to the other.
• At the time of this writing, the protocol is going through final stages of
becoming a defined standard, with the work being coordinated by the Internet
Engineering Task Force Constrained RESTful Environments Working Group.
PROTOTYPIN ONLINE COMPONENTS
(part 2)
WRITING A NEW API :
o Clockodillo
o Security
o Implementing the API
o Using cURL to Test
o Going Further
REAL-TIME REACTIONS
o Polling
o Comet
OTHER PROTOCOLS
o MQ Telemetry Transport
o Extensible messaging and presence protocol
o Constrained Application Protocol