Application layer protocols
Internet-of-Things (Majeure Informatique / Master CPS2)
Table of Contents
Table of Contents
Introduction & instructions
In this practical work session, you will explore the use of HTTP, CoAP and MQTT to communicate with connected devices. You will then integrate these devices into a Smart Lighting IoT application.
Evaluation: You will send an email at the end of this session:
- to:
andrei.ciortea@emse.fr
,maxime.lefrancois@emse.fr
- cc:
<the email address of each group member>
- subject: IoT TP Group
<number_of_your_group>
- body: throughout this lab, there are questions marked by “Question
<number>
”. You need to answer shortly to these questions in a document, and send the content of this document in the body of the email. - attached document: you will also join to this email the code of the application you will develop in section Application: Smart Lighting
Philips Hue
In this exercise, you are going to access and control Philips Hue lightbulbs via HTTP requests.
Setup
The Philips Hue light bulbs communicate with a bridge using the ZigBee Light Link protocol. The bridge exposes a Web API that applications can access on the local network.
Prerequisites
- Connect a laptop in your group to the WiFi with SSID “ISCOD” and password “smamasdept” (without the quotes).
- The bridge is set up and available on the local network at 192.168.0.100.
- The bridge provides a Web-based HTTP client that you can use to interact with the lights, called hereafter the debug tool, which is available at: http://192.168.0.100/debug/clip.html
- Your TP professor will provide you with the identifier of the light bulb that will be used by your group
Steps
- Discover the number of available Philips Hue light bulbs:
- open the debug tool in your browser
- in the “URL” field, enter the following relative path:
/api/bhBWRz2DWOEqjSwR9cmDBeZgVfV3pWvjrdYdpocw/lights/
- click the “GET” button to send an HTTP GET request at this URI
- Examine the raw HTTP request being sent:
- Open the “Developer Tools” in your browser and navigate to the “Network” tab
- on Chrome, go to Menu -> View -> Developer -> Developer Tools -> Network tab
- on Firefox, go to Menu -> Tools -> Web Developer -> Toggle Tools -> Network tab
- With the developer tools open, resend the GET request from the previous step
- From developer tools, select the request and check the raw HTTP headers of the received response:
- on Chrome, go to “Response Headers” and click “view source”
- on Firefox, click “Raw headers”
- Open the “Developer Tools” in your browser and navigate to the “Network” tab
- Turn on the light bulb via an HTTP PUT request:
- in the “URL” field, enter:
/api/bhBWRz2DWOEqjSwR9cmDBeZgVfV3pWvjrdYdpocw/lights/<lightbulb_identifier>/state
- in the “Message Body” field, enter the following JSON payload:
{"on" : true}
- in the “URL” field, enter:
- Change the color of the light bulb via an HTTP PUT request:
- resend the request at the previous step, this time with the JSON payload:
{ "xy" : [0,0] }
- resend the request at the previous step, this time with the JSON payload:
- Turn off the light bulb using an HTTP PUT request.
Further reading
The Philips Hue API is documented at http://www.developers.meethue.com/philips-hue-api , you need to sign in to access it (free).
The latest HTTP 1.1 specification is described in RFC 7230 to 7237.
TI SensorTag
In this exercise, you are going to use the Texas Instruments (TI) SensorTag platform to collect sensor readings into an MQTT broker. You will then use a Web client to connect to the MQTT broker and retrieve the sensor readings.
Setup
The TI SensorTag connects to a smartphone via Bluetooth Low Energy (BLE). The smartphone acts as a gateway and forwards all the sensor readings from the SensorTag to a cloud-based MQTT broker.
Prerequisites
To complete this exercise, your group will need:
- one TI SensorTag platform;
- one Android or iOS phone.
Steps
- Install the TI SensorTag app on your Android or iOS phone.
- Connect your smartphone to the TI SensorTag:
- turn on the TI SensorTag by pressing the lateral power button
- open the app and select your device from the list of available devices to display the sensor readings (e.g., check out the luxometer data)
- Configure the smartphone app to push the sensor readings to the HiveMQ MQTT broker:
- in the “Cloud View” section, tap on “Advanced”
- from the “Cloud service” drop down list, select “Custom”
- leave the “Username” and “Password” fields blank
- configure the “Broker Address” to:
tcp://broker.hivemq.com
- configure the “Broker Port” to:
1883
- configure the “Publish Topic” to:
emse-iot/<a_string_of_your_choice>
- check the “Clean session” checkbox
- tap “Save”
- slide to the right the “Push to Cloud” slider
- Connect your computer to the internet (eduroam, or use the lab computers)
- Connect to the MQTT broker using the HiveMQ Web client and check the sensor readings:
- open the client in a browser: http://www.hivemq.com/demos/websocket-client/
- in the “Connection” panel, check that the host is set to “broker.mqttdashboard.com” and the port to “8000” (this Web client connects to the MQTT broker via a websocket) and click “Connect”
- in the “Subscriptions” panel, click “Add New Topic Subscription” and subscribe to the topic configured previously on your smartphone app
- check that the sensor readings are displayed in the “Messages” panel and that they update as you manipulate the TI SensorTag (e.g., the light sensor readings)
Further reading
MQTT is an OASIS standard, one good entry point to know more about MQTT is http://mqtt.org/.
Constrained RESTful Environments (CoRE)
In this exercise, you will access and use a Constrained RESTful Environment (CoRE). First, you will use the Copper Firefox plugin to connect to a CoRE Resource Directory and discover what resources are available in the environment. Then, you will access the discovered resources.
Setup
We provide a CoRE Resource Directory on one of our machines. We also provide a CoAP device emulator on a different machine.
Prerequisites
You should already have Firefox and the Copper plugin installed on your machine. The Copper plugin is available for download here: https://addons.mozilla.org/en-US/firefox/addon/copper-270430/
Steps
- Open the Firefox browser and navigate to:
coap://<IP_ADDRESS>:5683/
- Discover the resources available in the environment:
- in the opened Firefox window, click the “Discover” button in the top left corner and observe the resources in the left panel
- click the
/rd-lookup/res
link in the left panel (alternatively, you can navigate tocoap://<IP_ADDRESS>:5683/rd-lookup/res
) and then click the “GET” button - the payload of the returned CoAP response is shown in the central panel
- Each registered resource has three attributes: “con”, “ep”, and “rt”. What does each of these attributes represent? Find your answers by searching the following documents:
- the CoRE Resource Directory working draft: https://tools.ietf.org/html/draft-ietf-core-resource-directory-08
- RFC 6690 - Core Link Format: https://tools.ietf.org/html/rfc6690
- Read the value of the outside light sensor:
- based on the attribute values of the discovered resources, determine the URI of the outside light sensor
- open a new tab and enter the URI
- click the “GET” button to execute a CoAP GET request and observe the returned payload
- wait for 5 seconds, execute the “GET” request again and observe the returned payload
- Polling via repeated GET requests is inefficient. A better way is to use the pub/sub mechanism built into the CoAP protocol:
- click the “OBSERVE” button, which sends a CoAP GET request with the “OBSERVE” option
- observe the returned responses for every 5 seconds
- Based on the above steps, now observe the presence sensor resource.
Further reading
The CoRE Link Format specification is described in RFC 6690 (22p).
It is built on top of the Web Linking specification, described in RFC 5988 (23p).
Application: Smart Lighting
In this exercise, you will build an IoT application that automatically manages the lights depending on the state of the environment (in particular, based on the readings of the presence and light sensors).
We provide you a boilerplate Java project such that you only focus on the essential parts of your application. The project is publicly available on the EMSE GitLab group for the IoT course: https://gitlab.emse.fr/IoTCourse.
You can download the files directly here: https://gitlab.emse.fr/IoTCourse/2016-lab-1/repository/archive.zip?ref=master
The project contains two Maven modules: iot-lab-coap-emulator
and iot-lab-app
.
The iot-lab-coap-emulator
module uses the Californium framework (available at: https://github.com/eclipse/californium) to provide a CoAP device emulator that simulates the light and presence sensors in our scenario. One of our machines is running the device emulator. You can explore the codebase of this project for your own curiosity, but you are not required to.
The iot-lab-app
module contains the boilerplate code for the application you are required to develop. Open this project with your favourite Java IDE, browse the source code, and fill in where you are asked to. The project’s README
file contains further instructions: https://gitlab.emse.fr/IoTCourse/2016-lab-1
Additional Questions
The Internet Assigned Numbers Authority (IANA) organization registers internet media types.
About media type application/json:
The World Wide Web consortium defines recommendations for the Web. The group with acronym TAG defined published exactly one recommendation in 2004.
The W3C consortium published a recommendation about the Architecture of the World Wide Web. This is where the concepts of Identification, Presentation, and Interaction, are defined.