Internet of Things & Wireless Sensor Networks Hands on IoT Ing. Luca Davoli Internet of Things Laboratory — IoTLab [email protected] Ing. Luca Davoli – [email protected] 1 Download this tutorial from here: http://www.ce.unipr.it/~davoli/schooltlc Ing. Luca Davoli – [email protected] 2 Scenario Applicativo JSON JSON JSON Ing. Luca Davoli – [email protected] 3 Scenario Applicativo ! Sensori (Nodi): applicazioni Java che simulano sensori per lo Smart Parking o Un singolo parcheggio può essere libero oppure occupato o Comunicano tramite il protocollo CoAP ! Gateway: riceve i dati dai sensori e li espone in HTTP, in modo che possano essere visibili anche ad altre applicazioni ! Web Application: chiede i dati al gateway e li mostra in una pagina web o Può comunicare solamente utilizzando il protocollo HTTP ! Smartphone: richiede i dati al gateway e li mostra in una App Android o Anche uno smartphone può essere un nodo IoT, essendo dotato di sensori e potendo inviare dati al gateway Ing. Luca Davoli – [email protected] 4 Scenario Applicativo ! Step 0: Java Project setup o Download the project CoAPExample from here: http://www.ce.unipr.it/~davoli/schooltlc o Import the project into Eclipse ! Step 1: Create a new IoT sensor (Local CoAP server) o Open SimpleCoAPServer.java o Modify the constants SERVICE_NAME and CAR_NAME with your surname (if you are a group, write as surname1_surname2) o If you want to start the CoAP server with an alternative CoAP port, modify the variable serverPort into the main function o Run the CoAP server: you are the owner of a new CoAP resource at path: coap://serverIP:serverPORT/SERVICE_NAME Ing. Luca Davoli – [email protected] 5 Scenario Applicativo ! Step 2: Verify the activation of your IoT sensor (Query to CoAP server through Copper plugin) o Connect to your deployed IoT CoAP sensor through Copper, a plugin installed into Mozilla Firefox coap://serverIP:serverPORT/SERVICE_NAME o Verify if the CoAP server replies to a CoAP GET request Ing. Luca Davoli – [email protected] 6 Scenario Applicativo ! Step 3: Create a new IoT client (Local CoAP client, with GET request) o Open SimpleCoAPClientGET.java o Modify the constant SERVICE_NAME, which identifies the service you want to query o Modify the variables serverCoAPIP, serverCoAPPort and clientCoAPPort, paying attention to distinct the latters variables. Since you are executing both the CoAP server and CoAP client on the same PC, you have to indicate differents CoAP ports, to avoid errors o If you want to change the waiting time between each CoAP request that your CoAP client made to your CoAP server, modify the constant SLEEP_PERIOD o Run your IoT CoAP client: now you are periodically asking to your CoAP server the status of a parking spot Ing. Luca Davoli – [email protected] 7 Scenario Applicativo ! Step 4: Enhance your IoT sensor (Local CoAP server with POST features) o Now you have to enhance the behavior of your CoAP server. Your goal is to allow your IoT sensor (local CoAP server) to make a CoAP POST operation to a remote CoAP server, send to this latter your generated parking spot informations, keeping the ability to reply to the incoming GET requests o Open SimpleCoAPServerPOST.java o As you can see, your Java file contains some errors, due to mismatched variables o Create a variable private ParkingLotRecord parkingRecord on top of your Java class o Create a variable private SocketAddress remoteCoAPServerSocketAddress on top of your Java class o Uncomment the code lines that you see in green into your CoAP server. This lines contain the function to periodically make a POST action to a remote CoAP server o Try to start your CoAP server o Since your CoAP server does not work, it seems that something is missing…. What happens? Ing. Luca Davoli – [email protected] 8 Scenario Applicativo o You have to indicate to your IoT sensor the IP and the port of the remote gateway that has to receive your POST messages o To do this, on top of your Java class, create a String and int variables, in which you have to set these remote parameters o Now you have to create a SocketAddress variable, named remoteCoAPServerSocketAddress, that you have to initialize with these parameters o Try to start your CoAP server o Now your local CoAP server is periodically sending parking informations to a remote CoAP server, but you have a strange behavior: when an external CoAP client sends a GET request to your local CoAP server, this latter reply with a newly generated parking spot status. Is it a good behavior? o It would be better if your local CoAP server reply to a GET request with the last informations, generated in doCoAPPOST function o Apply the changes that you feel appropriate to reply to a GET request with the last generated informations by doCoAPPOST function, removing, from onReceivedMessage function, the piece of code that would create new parking informations Ing. Luca Davoli – [email protected] 9 Scenario Applicativo ! Step 6: BONUS o Since you are the major of your city, you are interested in improve the services that your administration offer to its citizens, transforming your city in a Smart City. o By this way, you can choose to implement one (o more than one, if you want) of these IoT services: ➢ Create a new type of ParkingSpot that maintains also the following properties: standard ticket price/hour and currency, ticket price for the halt and duration of the halt ➢ Create a new IoT sensor that, emulating a control unit for pollution detection, accept request incoming from a new IoT client that request pollution surveys ➢ Create a new IoT sensor that, emulating a control unit for pollution detection, POST its pollution informations to a remote IoT gateway (that correspond to an IoT server, too) o If you want, you can substitute the control unit for pollution detection with a control unit for noise detection Enjoy the Internet of Things!! :-) Ing. Luca Davoli – [email protected] 10