Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:esp32:iot_5 [2024/04/28 17:02] – [Task to be implemented] pczekalskien:iot-open:practical:hardware:sut:esp32:iot_5 [2024/05/01 13:28] (current) – [Start] pczekalski
Line 29: Line 29:
  
 ==== Task to be implemented ==== ==== Task to be implemented ====
-Connect to the "internal IoT" WiFI access point as presented in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]]—present connection status on display. Then, set up a CoAP server, sending on its endpoint a "Hello World" message or any customized message of your choice; note to keep it short in case you visualise it on another node's display with limited capabilities. Another endpoint should present any data of the selected sensor (e.g. temperature, humidity, as in the EMBx scenarios) or control any actuator as we present in this scenario. This example uses an RGB LED to switch it on and off (as presented in the scenario [[en:iot-open:practical:hardware:sut:esp32:emb9a_1]]).\\+Connect to the "internal IoT" WiFI access point as presented in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]]—present connection status on display. Then, set up a CoAP server, sending on its endpoint a "Hello World" message or any customized message of your choice; note to keep it short in case you visualise it on another node's display with limited capabilities. Another endpoint should present any data on the selected sensor (e.g., temperature and humidity, as in the EMBx scenarios) or control any actuatoras we present in this scenario. This example uses an RGB LED to switch it on and off (as presented in the scenario [[en:iot-open:practical:hardware:sut:esp32:emb9a_1]]).\\
  
 The following endpoints should be implemented: The following endpoints should be implemented:
  
-  * endpoint 1 //coap://ip_address_of_the_node/helloworld//:+  * endpoint 1 <code>coap://ip_address_of_the_node/helloworld</code>
     * method GET - should return a "Hello World" or other string of our choice,     * method GET - should return a "Hello World" or other string of our choice,
-  * endpoint 2 ''coap://ip_address_of_the_node/light'': +  * endpoint 2 <code>coap://ip_address_of_the_node/light</code> 
-    * method GET - should return current status of the LED light ("ON" or "OFF"),+    * method GET - should return the current status of the LED light ("ON" or "OFF"),
     * method PUT - should set the LED on and off, with the expected payload being "1" for on and "0" for off.     * method PUT - should set the LED on and off, with the expected payload being "1" for on and "0" for off.
  
-<todo @pczekalski>Skończyć</todo>+==== Start ==== 
 +Check if you can clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with ''void setup()...'' and ''void loop()...''. \\ 
 +Implement a connection to the "internal IoT" network as a client. Refer to the supervisor or the technical documentation on credentials (SSID, passphrase). We do not provide the exact code on how to connect to the WiFi as it is a part of [[[en:iot-open:practical:hardware:sut:esp32:iot_2|]] scenario. \\ 
 + 
 +=== Step 1 === 
 +Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.\\ 
 +Implement the code to display on the selected device.\\ 
 +Connect to the WiFi in the STA mode (as a client) and ensure the connection is OK and you got an IP from the DHCP server.\\ 
 +It is essential to note and present (using a display of your choice) the node's IP address, as you will later need to refer to it with a client to use your service. 
 + 
 +=== Step 2 === 
 +Include the WiFi UDP and CoAP implementation libraries headers in your code: 
 +<code c> 
 +#include <WiFiUdp.h> 
 +#include <coap-simple.h> 
 +</code> 
 +WiFi UDP is part of the Arduino for the ESP32 framework, so you do not need to add it explicitly to the ''platformio.ini'' file. 
 + 
 +=== Step 3 === 
 +Declare necessary constants, etc.: 
 +<code c> 
 +bool LEDSTATE; //Keep LED's state. 
 +</code> 
 + 
 +=== Step 4 === 
 +Declare communication objects: 
 +<code c> 
 +WiFiUDP udp;      //UDP Communication class 
 +Coap coap(udp);   //CoAP Communication class 
 +</code> 
 + 
 +=== Step 5 === 
 +Declare function prototypes (not necessary if you implement them in the correct order): 
 +<code c> 
 +void callback_response(CoapPacket &packet, IPAddress ip, int port); // CoAP client response callback 
 +// CoAP server endpoint URL callback for GET and PUT methods 
 +void callback_led(CoapPacket &packet, IPAddress ip, int port);       
 +</code> 
 + 
 +=== Step 6 === 
 +Implement them: 
 +<code c> 
 +void callback_led(CoapPacket &packet, IPAddress ip, int port) { 
 +  // send response 
 +  char p[packet.payloadlen + 1]; 
 +  memcpy(p, packet.payload, packet.payloadlen); 
 +  p[packet.payloadlen] = NULL; 
 +   
 +  String message(p); 
 +  //for GET 
 +  if (message.equals("0")) 
 +    LEDSTATE = false; 
 +  else if(message.equals("1")) 
 +    LEDSTATE = true; 
 +  //for PUT 
 +  if (LEDSTATE) { 
 +    coap.sendResponse(ip, port, packet.messageid, "1"); 
 +  } else {  
 +    digitalWrite(RGBLED_R_PIN, LOW) ;  
 +    coap.sendResponse(ip, port, packet.messageid, "0"); 
 +  } 
 +
 +</code> 
 +<code c> 
 +void callback_response(CoapPacket &packet, IPAddress ip, int port) {  
 +  char p[packet.payloadlen + 1]; 
 +  memcpy(p, packet.payload, packet.payloadlen); 
 +  p[packet.payloadlen] = NULL; 
 +  //Do something with payload, e.g. print it to the display 
 +
 +</code> 
 + 
 +=== Step 7 === 
 +Setup CoAP services: 
 +<code c> 
 +  coap.server(callback_led, "light"); 
 +  coap.response(callback_response); 
 +  coap.start(); 
 +</code> 
 + 
 +=== Setup 8 === 
 +Process CoAP services in the ''void loop()'': 
 +<code c> 
 +  delay(1000); 
 +  coap.loop(); 
 +</code> 
 +<note>If you plan to make frequent requests, decrease the time above (''delay()'').</note> 
 + 
 +==== Result validation ==== 
 +You should be able to connect to the WiFi and set up a CoAP service. Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a CoAP client on another laboratory node (as present in the scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_6|]]) or use some other client (such as NodeRED or even command line client): note, your client must be present in the same WiFi network as your CoAP service (laboratory node). 
 + 
 +<note tip>Sample command line client (Linux/MacOS) syntax to make a PUT request to the service (192.168.91.14) is presented below (bash): 
 +<code bash> 
 +$ coap-client -e "0" -m put coap://192.168.91.14/light 
 +</code> 
 +</note> 
 + 
 +<note tip>Sample command line client (Linux/MacOS) syntax to make a GET request to the service (192.168.91.14) is presented below (bash): 
 +<code bash> 
 +$ coap-client -m get coap://192.168.91.14/light 
 +</code> 
 +</note> 
 + 
 + 
 +===== FAQ ===== 
 +**How do I implement a client to my CoAP service?**: If you're fully remote, the only option is to implement a client on your own - use another laboratory node and scenario [[[en:iot-open:practical:hardware:sut:esp32:iot_6|]] pointing to your service. For this reason, we suggest printing the IP address on the display, as you need to explicitly implement the client to connect to this IP (given by the DHCP server). If you're on a laptop or mobile within the WiFi network range to which the laboratory nodes are connected, you can connect to it with your laptop and use any CoAP client. 
 + 
 +<WRAP noprint> 
 +===== Project information ===== 
 +{{:en:iot-open:logo_iot_200_px.png?200|}}\\ 
 +This Intellectual Output was implemented under the Erasmus+ KA2.\\ 
 +Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.\\ 
 +Project number: 2022-1-PL01-KA220-HED-000085090. 
 + 
 +**__Erasmus+ Disclaimer__**\\ 
 +This project has been funded with support from the European Commission. \\ 
 +This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. 
 + 
 +**__Copyright Notice__**\\ 
 +This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.\\ 
 +The content is Copyrighted and distributed under CC BY-NC [[https://en.wikipedia.org/wiki/Creative_Commons_license|Creative Commons Licence]], free for Non-Commercial use.  
 +<figure label> 
 +{{:en:iot-open:ccbync.png?100|}} 
 +</figure> 
 + 
 +</WRAP> 
en/iot-open/practical/hardware/sut/esp32/iot_5.1714323734.txt.gz · Last modified: 2024/04/28 17:02 by pczekalski
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