This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:remotelab:sut:roboarm:u9 [2019/08/01 09:56] – created gdrabik | en: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. | + | In this scenario, you will interact with a CoAP server implemented as a NodeRED node. You will perform simple CoAP request to the NodeRED server, waiting for you. In return, you will obtain a secret code. |
| === Target group === | === Target group === | ||
| - | //This hands-on lab guide is intended for the Beginners/ | + | Undergraduate |
| === Prerequisites === | === Prerequisites === | ||
| - | //Provide prerequisite readings/hardware/software/software libraries/skills if other than stated | + | 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: '' | ||
| + | * additionally, | ||
| + | |||
| + | NodeRED CoAP server is present | ||
| + | <note important> | ||
| + | |||
| + | CoAP server data is: | ||
| + | * IP address (implemented with NodeRED) is: '' | ||
| + | * The only implemented URI is ''""'' | ||
| + | * CoAP port is '' | ||
| === Scenario === | === Scenario === | ||
| - | // | + | I this scenario, once you get connected |
| + | To implement a CoAP client feature you will use a dedicated library '' | ||
| === 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 conditions, i.e. what to do at the beginning, what to pay attention before beginning, how the mechanical part should look like, etc.// | + | Define some identifiers to separate and update AP's SSID and passphrase easily. To simplify conversion, use '' |
| === Steps === | === Steps === | ||
| - | // Write some extra information if i.e. some steps are optional otherwise cancel this paragraph (but do not remove header).// | + | Following |
| == 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 < | ||
| + | #include < | ||
| + | #include " | ||
| + | #include < | ||
| + | ... | ||
| + | </code> | ||
| + | |||
| + | Declare some identifiers and variables/class instances to let you easier handle necessary modifications and keep code clear: | ||
| + | <code c> | ||
| + | #define wifi_ssid " | ||
| + | #define wifi_password " | ||
| + | |||
| + | IPAddress ip(192, | ||
| + | int port = 5683; | ||
| ... | ... | ||
| + | </ | ||
| + | |||
| + | == Step 2 == | ||
| + | |||
| + | Declare '' | ||
| + | <code c> | ||
| + | ... | ||
| + | |||
| + | coapClient coap; | ||
| + | |||
| + | ... | ||
| + | </ | ||
| + | |||
| + | == Step 3 == | ||
| + | Prepare CoAP client response asynchronous handler. | ||
| + | <note tip>CoAP uses UDP for communication, | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | <code c> | ||
| + | void callback_response(coapPacket & | ||
| + | ... | ||
| + | // coap client response callback | ||
| + | void callback_response(coapPacket & | ||
| + | char p[packet.payloadlen + 1]; // | ||
| + | memcpy(p, packet.payload, | ||
| + | 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. | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | == Step 4 == | ||
| + | |||
| + | Start your CoAP client in the '' | ||
| + | |||
| + | <code c> | ||
| + | ... // remember to initialise it AFTER you make a WiFi connection, not before | ||
| + | coap.respinse(callback_response); | ||
| + | coap.start(); | ||
| + | |||
| + | </ | ||
| + | |||
| + | == Step 5 == | ||
| + | Your '' | ||
| + | |||
| + | <code c> | ||
| + | void loop() | ||
| + | { | ||
| + | ... | ||
| + | int msgid = coap.get(ip, | ||
| + | bool state = coap.loop(); | ||
| + | ... | ||
| + | delay(10000); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <note warning> | ||
| - | == Step n == | ||
| - | //Describe activities done in Step n.// | ||
| === Result validation === | === Result validation === | ||
| - | //Provide some result validation methods, for self assesment.// | + | Observe connection progress on the LCD via video stream. Once WiFi is connected, you 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 '' |
| === 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 |
| - | When using the printed version of this manual please refer to the latest online version of this document | + | |
| - | //Provide some FAQs in the following form:\\ | + | |
| - | **Question? | + | |
| - | // | + | |