This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot-open:remotelab:sut:roboarm:u9 [2020/05/13 12:56] – pczekalski | en:iot-open:remotelab:sut:roboarm:u9 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== U9: A CoAP client ==== | ||
| + | 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 === | ||
| + | Undergraduate / Bachelor / Engineering Students | ||
| + | |||
| + | === Prerequisites === | ||
| + | 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 in the '' | ||
| + | <note important> | ||
| + | |||
| + | CoAP server data is: | ||
| + | * IP address (implemented with NodeRED) is: '' | ||
| + | * The only implemented URI is ''""'' | ||
| + | * CoAP port is '' | ||
| + | |||
| + | === Scenario === | ||
| + | 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 note, we do not implement here discovery services for CoAP, for simplicity and because of constrained resources, so you won't be able to use clients' | ||
| + | To implement a CoAP client feature you will use a dedicated library '' | ||
| + | |||
| + | === Result === | ||
| + | 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 === | ||
| + | Define some identifiers to separate and update AP's SSID and passphrase easily. To simplify conversion, use '' | ||
| + | |||
| + | === Steps === | ||
| + | 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 == | ||
| + | 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 < | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Declare some identifiers and variables/ | ||
| + | <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> | ||
| + | |||
| + | |||
| + | === Result validation === | ||
| + | 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 === | ||
| + | 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. | ||