Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:remotelab:sut:roboarm:u9 [2019/08/01 09:56] – created gdrabiken:iot-open:remotelab:sut:roboarm:u9 [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-==== U9: ==== +==== U9: A CoAP client ==== 
-//Give few words about this scenario. Provide some objectives and impact on the students.//+In this scenario, you will interact with a CoAP server implemented as a NodeRED nodeYou will perform simple CoAP request to the NodeRED server, waiting for youIn return, you will obtain a secret code. 
 === Target group === === Target group ===
-//This hands-on lab guide is intended for the Beginners/Undergraduates/Masters/Professionals. Choose appropriate.//+Undergraduate Bachelor Engineering Students
  
 === Prerequisites === === Prerequisites ===
-//Provide prerequisite readings/hardware/software/software libraries/skills if other than stated in the Laboratory DescriptionIf none, remove along with the headerPlease note some of those may be common for all scenarios (i.eMQTT library), so provide it in the laboratory description levelnot in the scenario level.//+We assume you already know how to: 
 +  * handle LCD screen to present information (refer to the scenarios B1 and B2 when in doubt), 
 +  * connect to the existing WiFi network: ''internal.IOT'', 
 +  * additionally, we will ask you to install and use a CoAP client of your choice. We suggest using chrome extension [[https://github.com/mkovatsc/Copper4Cr/blob/master/README.md|Cooper4Cr, chrome plugin]], but any CoAP client will work here. You connect your solution to the private IP of the NodeRED server but the same information is available on the public IP as well, so you can check it from your home. 
 + 
 +NodeRED CoAP server is present in the ''internal.IOT'' network and it is also visible under public address. Refer to the node documentation for the latest information. 
 +<note important>Note - information present in source code samples can be not up-to-date - remember to refer to the VREL node documentation for the latest information on IPs, users and passwords for both ''internal.IOT'' network access and for the NodeRED access.</note> 
 + 
 +CoAP server data is: 
 +  * IP address (implemented with NodeREDis: ''192.168.90.5'' as seen from the ''internal.IOT'' networkor ''157.158.56.54'' when accessing from the public space; 
 +  * The only implemented URI is ''""'' (simply root resource) - for other URIs you will get 4.04 (Not Found) error; 
 +  * CoAP port is ''5683'' (default);
  
 === Scenario === === Scenario ===
-//Describe scenario to let the user understand its idea and process. Write information about the approachalgorithmetc(depends on the lab). Provide useful information about protocolsassumptions, addresses (i.ecommunication servers), credentialsetc.//+I this scenario, once you get connected to the WiFi as AP and then you will make a UDP request using CoAP protocol to the NodeRED server acting as CoAP server. Please notewe do not implement here discovery services for CoAPfor simplicity and because of constrained resources, so you won't be able to use clients' CoAP discover featureYou will obtain a secret code from the server (it varies over time). As a bonusyou may try to discover the secret code generation algorithm :-)You're expected to present the results of the CoAP query on the LCD screen. 
 +To implement a CoAP client feature you will use a dedicated library ''ESP-CoAP simple library''available via Platformio library management system.
  
 === Result === === Result ===
-//Describe expected result when scenario is finished.//+You should be able to obtain a UDP message in CoAP, with the payload containing an ASCII text with secret code, and present it on the LCD screen.
  
 === Start === === Start ===
-//Write starting conditionsi.ewhat to do at the beginning, what to pay attention before beginninghow the mechanical part should look like etc.//+Define some identifiers to separate and update AP's SSID and passphrase easily. To simplify conversionuse ''IPAddress'' class to create IP address of the CoAP serverTo format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iteratorsRemember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface hereit is singleton class you can refer directly using WiFi. Reading analogue input brings you an integer value. Noteyou're supposed to instantiate your MCU as AP even if using UDP communication.
  
 === Steps === === Steps ===
-// Write some extra information if i.e. some steps are optional otherwise cancel this paragraph (but do not remove header).//+Following steps do not present full code - you need to supply missing parts on your own! We do not present here how to connect to the WiFi AP. If you're in doubt, rever to the U1 scenario. Please refer to scenario B1, if you need a recall on how to handle LCD screen
 == Step 1 == == Step 1 ==
-//Describe activities done in Step 1.//+Include all necessary libraries. We use ESP-CoAP simple library to contact CoAP server as a client. Note, this library also implements a CoAP server features but we do not use it in our scenario here. The minimum set here is: 
 +<code c> 
 +#include <Arduino.h> 
 +#include <ESP8266WiFi.h> 
 +#include "coap_client.h" 
 +#include <LiquidCrystal_I2C.h> 
 +... 
 +</code> 
 + 
 +Declare some identifiers and variables/class instances to let you easier handle necessary modifications and keep code clear: 
 +<code c> 
 +#define wifi_ssid "internal.IOT" 
 +#define wifi_password "IoTlab32768" 
 + 
 +IPAddress ip(192,168,90,5);//take NodeRed Address  
 +int port = 5683;
  
 ... ...
 +</code>
 +
 +== Step 2 ==
 +
 +Declare ''coapClient'' and WiFi client, to implement CoAP client instance, PubSubClient, initialise, instantiate and connect to the network. If in doubt, refer to the scenario U3 on how to prepare networking code for your solution:
 +<code c>
 +...
 +
 +coapClient coap;
 +
 +...
 +</code>
 +
 +== Step 3 ==
 +Prepare CoAP client response asynchronous handler. 
 +<note tip>CoAP uses UDP for communication, so you will be informed asynchronously, when response to your request appears. That is fully handled by asynchronous response handler and mechanism is implemented by the library.</note>
 +
 +<note important>As messages may come quickly in the queue (many replies) due to the heavy use or UDP nature, you are required to make a copy of the payload ASAP, best using ''memcpy'', as first operation in your asynchronous response handler.</note>
 +
 +<code c>
 +void callback_response(coapPacket &packet, IPAddress ip, int port);
 +...
 +// coap client response callback
 +void callback_response(coapPacket &packet, IPAddress ip, int port) {
 +    char p[packet.payloadlen + 1];                 //declare local buffer for the payload
 +    memcpy(p, packet.payload, packet.payloadlen);  //quickly copy payload contents
 +    p[packet.payloadlen] = NULL;
 +
 +    //response from coap server - check if this is regular one (response to your request), or is it a ping request?
 +    if(packet.type==3 && packet.code==0){
 +      //that means you've obtained a ping request so handle it (i.e. show on LCD)
 +      ...
 +    }
 +    ... //handle payload.
 +}
 +...
 +</code>
 +== Step 4 ==
 +
 +Start your CoAP client in the ''setup()'' function:
 +
 +<code c>
 +... // remember to initialise it AFTER you make a WiFi connection, not before
 +  coap.respinse(callback_response); // add response handler
 +  coap.start(); //start coap client
 +
 +</code>
 +
 +== Step 5 ==
 +Your ''loop()'' function should include call to the CoAP client methods. Here we use only CoAP GET, as this is the only one implemented by the NodeRED server, we provide to you:
 +
 +<code c>
 +void loop()
 +{
 +  ...
 +  int msgid = coap.get(ip,port,"");
 +  bool state = coap.loop();
 +  ...
 +  delay(10000);
 +}
 +</code>
 +
 +<note warning>Mind to keep a ''delay(...)'', not to saturate NodeRED server with issuing dozens of UDP requests. Minimum reasonable delay between issuing consecutive requests is about 500ms.</note>
  
-== Step n == 
-//Describe activities done in Step n.// 
  
 === Result validation === === Result validation ===
-//Provide some result validation methodsfor self assesment.//+Observe connection progress on the LCD via video stream. Once WiFi is connectedyou should be able to see secret code as a return from CoAP server. Note, you may want to implement presentation of the communication status, i.e. present ''msgid'' and state of the ''coap.loop()''
  
 === FAQ === === FAQ ===
-This section is to be extended as new questions appear. \\ +Secret changes over time so do not be surprised that you will obtain different numbers when returning to this exercise or executing in loop for a time. Note, to distinguish from random data, there is always some extra, textual message to let you be sure that server returned a value.
-When using the printed version of this manual please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. +
-//Provide some FAQs in the following form:\\ +
-**Question?**: Answer. +
-//+
  
en/iot-open/remotelab/sut/roboarm/u9.1564653383.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0