Reader small image

You're reading from  Mastering Internet of Things

Product typeBook
Published inMar 2018
PublisherPackt
ISBN-139781788397483
Edition1st Edition
Right arrow
Author (1)
Peter Waher
Peter Waher
author image
Peter Waher

Peter Waher is the founder of Little Sister®, a standards-based distributed social network, based on the principles of edge computing, privacy & information ownership, for humans and machines. Currently, Peter advises companies on topics such as privacy, the IoT and Smart Cities. He has worked for 24 years with computers and device communication, including low-level development in assembler for resource-constrained devices to high-level system design and architecture. His award-winning applications has attracted global attention, and he has been invited to speak at prestigious events.
Read more about Peter Waher

Right arrow

Chapter 5. Publishing Data Using HTTP

In the previous chapter, you learned how to use the MQTT protocol to communicate with your devices. You also learn the pros and the cons of the protocol and how to use it in a secure manner.

In this chapter, we'll introduce the HTTP protocol, and how it can be used to communicate with your connected things. The chapter covers:

  • An introduction to the HTTP protocol
  • The Request/Response communication pattern
  • How to locate resources on the web
  • Basic principles of the HTTP protocol semantics
  • Publishing machine-readable web service interfaces
  • Encryption fundamentals

Introducing the HTTP protocol


The Hypertext Transfer Protocol, or HTTP, is one of the best known and most used internet protocols today. It was originally designed in 1989 by Tim Berners-Leeas a means to publish hypertext documents on a distributed set of servers, today called web servers. Clients, for example web browsers, would be able to fetch these documents using the HTTP protocol. Hyper, meaning beyond in the word hypertext, literally means beyond the text, signifying the possibility to link to other hypertext documents from within the text itself. These referenced documents may in turn reside on other servers. To achieve this, each document, or resource, is assigned a Uniform Resource Locator or URL. This URL is treated as a simple string but contains all the information the client needs to find and download the contents of the resource.

Resources on the web are not necessarily hypertext documents. They can be images, audio, video, binary applications, or more generally, any type of...

Preparing our project


As in the previous chapter, we will create a new project based on our Sensor project. Let's call it SensorHttp. We use the same hardware and software setup as in previous chapters. But this time, we add the Waher.Networking.HTTP.UWP NuGet package instead. It will allow us to host a web server on Raspberry Pi and publish web resources, both static and dynamic ones.

Note

For .NET standard, .NET Core, or traditional .NET Framework projects, you can use the Waher.Networking.HTTP NuGet instead. Universal Windows Platform apps use different libraries and runtime binaries when it comes to communication and encryption. For this reason, it requires a somewhat modified version of the original library.

Since we will accept incoming connections to our app, we also need to provide sufficient capabilities to do so. If we don't, the framework will throw an exception if we try. We add the internetClientServer capability to our set of capabilities in the Package.appxmanifest file:

<Capability...

Adding dynamic synchronous resources


To publish resources through our web server, we just call the Register method on the HttpServer object. We can choose between two methods: either we provide resource objects, inherited from the HttpResource class, such as the HttpSynchronousResource or HttpAsychronousResource classes, or we use lambda expressions or delegates for simple GET and POST resources.

We first demonstrate the latter to publish a resource for reading momentary sensor data. We provide a lambda expression, taking a request and response parameter. This expression will be executed when a GET method is received on the /Momentary resource:

this.httpServer.Register("/Momentary", (req, resp) => 
{ 
   ... 
}); 

Note

The resource will be added as a synchronous resource. This means that the response must be generated completely before returning from the expression. We don't have to worry about exceptions or explicitly sending the response. The web server will do that for us if we forget...

Adding dynamic asynchronous resources


The actuator project (ActuatorHttp in the GitHub repository) also needs a /Momentary resource that returns the current state of the output in XML or JSON. The implementation is similar to that of the sensor, so it's straightforward to do. But we also need a way to control the output. We do that by adding a /Set resource. Since we will call asynchronous methods, we take this opportunity to add this resource as an asynchronous POST resource. This means we must explicitly handle errors and exceptions, as well as explicitly sending the response when it is ready:

this.httpServer.Register("/Set", null, async (req, resp) => 
{ 
   try 
   { 
         // Process resource here 
 
         resp.SendResponse();    // Sends response. 
   } 
   catch (Exception ex) 
   { 
         resp.SendResponse(ex);  // Sends error response. 
   } 
}, false); 

The first parameter defines the relative URL of the resource. The second is null, which means the resource does not...

Summary


In this chapter, you've been shown how you can use HTTP to publish sensor data and interact with devices on the internet. You've learned the basic principles of the HTTP protocol and how to publish resources and interact with them using HTTP. You've also learned to separate resource, state and representations of data, which is a requirement for making RESTful web services. In the next chapter, we will delve deeper into the world of the HTTP protocol by showing you how human interfaces can be built to interact with the machine-based web services created in this chapter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Internet of Things
Published in: Mar 2018Publisher: PacktISBN-13: 9781788397483
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Peter Waher

Peter Waher is the founder of Little Sister®, a standards-based distributed social network, based on the principles of edge computing, privacy & information ownership, for humans and machines. Currently, Peter advises companies on topics such as privacy, the IoT and Smart Cities. He has worked for 24 years with computers and device communication, including low-level development in assembler for resource-constrained devices to high-level system design and architecture. His award-winning applications has attracted global attention, and he has been invited to speak at prestigious events.
Read more about Peter Waher