===== LAB : Service Oriented Approach for IoT ====== // Author : Jean-Yves Tigli - tigli@unice.fr // If Web Services are used for data exchange in classical distributed software, they are also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands ofcode software applications affecting billions of humans daily. == Requirements :== * Node RED (for example) under Docker : [[https://nodered.org/docs/platforms/docker|Node-Red ]] * Help abour Node-Red : * Liste à puceyour own Node RED assembly : [[http://developer.opto22.com/nodered/general/getting-started/node-red-hello-world/|First use of Node RED]] * your own Node RED Component with its Function : [[http://developer.opto22.com/nodered/general/getting-started/node-red-hello-world-2/|First Function in Node RED]] ==== SOA for IoT and WS-REST ==== == Get started with curl == [[https://curl.haxx.se/|curl]] is used in command lines or scripts to transfer data. Curl is mainly used to send/receive some HTTP messages with command lines. That's the reason why curl is well adapted for first [[https://www.baeldung.com/curl-rest|tests of Rest services]]. Lot of WS-REST API are available on the web. Big compagnies provide some Cloud of Services using this kind of access. For example the IBM Cloud provides [[https://cloud.ibm.com/catalog?search=REST|its own list of services]]. Look at a "text to speech" demonstration [[https://text-to-speech-demo.ng.bluemix.net/|here]] **Exercice**: After Sign up, test services like TexttoSpeech with curl. Find more details in a step by step tutorial [[https://console.bluemix.net/docs/services/text-to-speech/getting-started.html#gettingStarted|here]]. **Question**: Look at IBM Watson IoT Plateform. What does it provide ? == WS-REST in Node-Red == In fact, WS-Rest basicaly uses HTTP to communicate with some GET and POST HTTP commands. ** Rest client ** **Exercice**: Get current cities temperatures and display them // use openweathermap // Drag the http request node from the output block. This will output the payload of the msg as response of remote web service API. Create a client to get Weather Temperature from openweathermap with a WS-REST // use ThingSpeak for WS-Rest that provides a Web UI // The ThingSpeak server is install on https://sparks-vm24.i3s.unice.fr/. Sign up to start using ThingSpeak getting an ID and a Key for future ThingSpeak requests (see explainations [[https://community.thingspeak.com/tutorials/thingspeak-channels/|Here]] (you can test ThingSpeak alone with curl). {{ :cours:mit_2018_2019:node_editor_red_rest_openweathermap.png?400|}}* Get consecutive temperatures for a city and display them in ThingSpeak using Node-Red like in the figure. {{:cours:mit_2018_2019:node-red_solution_assembly_export.rtf|}} ** Rest server : ** So using basic HTTP nodes, we can developp a REST server that answers to GET requests and returns some values. **Exercice**: To create a WS-Rest in Node-Red : - Use the http node from the input block to handle request from outside on the following URL: /date - Drag the http response node from the output block. This will output the payload of the msg as response of API. - Add the current date as a response to the REST GET request on /date - Now go to your browser and hit http://localhost:1880/date to use your WS-REST API. **Advanced Exercice**: write your own REST server that gets an MQTT topic and provides it through a web service API. // You can extend a past lab MQTT example to do that//. ==== SOA for IoT and WS-SOAP ==== == Get started with WS-SOAP == http://www.dataaccess.com/ provide a list of WS-SOAP. The main initial difference between WS-REST and WS-SOAP was the explicit description of the WS-SOAP API in a WSDL format (Web Service Descrition Language). ** Question**: Thanks to http://www.dataaccess.com/webservicesserver/NumberConversion.wso?WSDL URI, display this description for NumberConversion WS-SOAP. Look at the different part of this XML file to understand the corresponding SOAP API. Test this WS-SOAP with http://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords == WS-SOAP in Node-Red == **Soap client** **Exercice:** Create a WS-Soap Client to test your own WS-Soap server : http://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords To do that in node-red you must install two packages : * node-red-contrib-soap * node-red-contrib-soapserver use the manage palette to install them [[cours:mit_2018_2019:node_red_soap_client_correction|correction]] ==== SOA for IoT and CoAP ==== {{ :cours:mit_2018_2019:osi-coap.png?300|}} Constrained Application Protocol (CoAP) is a specialized Internet Application Protocol for constrained devices, as defined in [[https://tools.ietf.org/html/rfc7252|RFC 7252]]. CoAP is a service layer protocol that is intended for use in resource-constrained internet devices, such as wireless sensor network nodes. CoAP is designed to easily translate to HTTP for simplified integration with the web, while also meeting specialized requirements such as multicast support, very low overhead, and simplicity. Exercice : Answer to this question : why CoAP is much more adapted to Local Internet Network (on LAN) than Wide Internet Network (on WAN). What might be the solution to allow CoAP communications between distant places. == Get started with CoAP-CLI == CoAP-CLI is a command line interface for CoAP, built on node.js and node-coap. Install it under a shell after Node-Red (or at least node.js) installation with : // npm install coap-cli -g // Some examples to test are then available in [[https://github.com/reederz/node-red-contrib-coap.git|]] Start observe_server.js, import client_flow.json to your Node-RED instance and start red.js... == CoAP in Node-Red == [[https://flows.nodered.org/node/node-red-contrib-coap]]