No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:lab3
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:edu:zmitac:s2:iotn:lab3 [2022/03/18 21:57] – created pczekalski | en:edu:zmitac:s2:iotn:lab3 [2023/04/21 16:46] (current) – administrator | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== IoT Networks: Laboratory | + | ====== IoT Networks: Laboratory |
| Network programming for the IoT Devices using CoAP protocol. | Network programming for the IoT Devices using CoAP protocol. | ||
| - | Following | + | The following |
| - | <note important> | + | <note important> |
| - | <note important> | + | <note important> |
| ==== Hardware ==== | ==== Hardware ==== | ||
| - | We do not use any specific hardware here other than an LCD screen to present visual information. If you intend to extend your project and i.e. use a temperature and humidity sensor, refer to the [[en: | + | We do not use any specific hardware here besides |
| === Hardware-software components and libraries === | === Hardware-software components and libraries === | ||
| - | Interfacing hardware requires the use of particular GPIOs. Laboratory nodes and hardware used needs specific | + | Interfacing hardware requires the use of particular GPIOs. Laboratory nodes and hardware used need specific |
| == LCD display == | == LCD display == | ||
| Line 30: | Line 30: | ||
| </ | </ | ||
| - | To initialise | + | To initialize |
| <code c> | <code c> | ||
| const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14; | const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14; | ||
| Line 36: | Line 36: | ||
| </ | </ | ||
| - | It is also necessary to tell the library about the display | + | It is also necessary to tell the library about the display |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 45: | Line 45: | ||
| ==== Software ==== | ==== Software ==== | ||
| - | String formatting may be a challenge. An easy approach is to allocate a character buffer of the size of the LCD or just one display' | + | String formatting may be a challenge. An easy approach is to allocate a character buffer of the size of the LCD or just one display' |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 52: | Line 52: | ||
| </ | </ | ||
| - | To convert float into the string with formatting use '' | + | To convert float into the string with formatting, use '' |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 60: | Line 60: | ||
| ... | ... | ||
| </ | </ | ||
| - | <note tip>'' | + | <note tip>'' |
| <code c> | <code c> | ||
| delay(time) | delay(time) | ||
| Line 66: | Line 66: | ||
| === Software libraries === | === Software libraries === | ||
| - | Software libraries are needed to handle communication | + | Software libraries are needed to communicate |
| - | The great feature of the CoAP is, you do not need to take care of the instant connection to the service on the application-level protocol. Just reconnecting WiFi, if the connection is dropped, is quite enough. UDP is a " | + | The outstanding |
| == WIFI and UDP == | == WIFI and UDP == | ||
| - | A WiFi library is already included in the ESP8266 framework for the Arduino programming model, so there is no need to use library manager to add it. All is necessary is to include | + | A WiFi library is already included in the ESP8266 framework for the Arduino programming model, so there is no need to use a library manager to add it. All that is necessary is to have a header |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 78: | Line 78: | ||
| </ | </ | ||
| - | Then create some constant variables with '' | + | Then create some constant variables |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 85: | Line 85: | ||
| ... | ... | ||
| </ | </ | ||
| - | The '' | + | The '' |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 94: | Line 94: | ||
| Some useful code snippets are below.\\ | Some useful code snippets are below.\\ | ||
| - | Checking if WiFi connection is successful: | + | Checking if the WiFi connection is successful: |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 111: | Line 111: | ||
| </ | </ | ||
| - | Once WiFi is connected, it is necessary to use a '' | + | Once WiFi is connected, it is necessary to use a '' |
| The declaration can be as follows: | The declaration can be as follows: | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | | + | |
| ... | ... | ||
| </ | </ | ||
| - | Note, this object becomes automatically active and bound to the '' | + | Note this object becomes automatically active and bound to the '' |
| - | == MQTT == | ||
| - | To ease and speed up implementation, | ||
| - | < | ||
| - | lib_deps = ..., PubSubClient, | ||
| - | </ | ||
| - | |||
| - | <note important> | ||
| - | |||
| - | MQTT clients need to be instantiated using WiFi client connection, i.e. this way: | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | #define mqtt_server "< | + | int port = 5683; //Standard CoAP port |
| - | #define mqtt_user "< | + | |
| - | #define mqtt_password "< | + | |
| - | ... | + | |
| - | PubSubClient client(espClient); | + | |
| - | ... | + | |
| </ | </ | ||
| - | and in the setup code, you need to initialise the '' | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | client.setServer(mqtt_server, | + | Udp.begin(port); |
| ... | ... | ||
| </ | </ | ||
| + | == CoAP == | ||
| + | To ease and speed up implementation, | ||
| + | < | ||
| + | lib_deps = ..., hirotakaster/ | ||
| + | </ | ||
| + | <note important> | ||
| - | Consider encapsulating MQTT connecting process as a separate function, i.e. as below. Note, the code below publishes a hello world message to the broker to emphasize its successful presence in the network: | + | CoAP client or server both require to be instantiated using WiFi UDP connection (afore initialized '' |
| <code c> | <code c> | ||
| - | void reconnect() { | + | ... |
| - | // Loop until we're reconnected | + | Coap coap(Udp); |
| - | while (!client.connected()) { | + | ... |
| - | Serial.print(" | + | |
| - | // Create a random client ID | + | |
| - | String clientId = " | + | |
| - | | + | |
| - | // Attempt to connect | + | |
| - | if (client.connect(clientId.c_str(), | + | |
| - | Serial.println(" | + | |
| - | // Once connected, publish an announcement... | + | |
| - | client.publish(" | + | |
| - | // ... and resubscribe | + | |
| - | client.subscribe("#" | + | |
| - | } else { | + | |
| - | Serial.print(" | + | |
| - | Serial.print(client.state()); | + | |
| - | Serial.println(" | + | |
| - | // Wait 5 seconds before retrying | + | |
| - | delay(5000); | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| </ | </ | ||
| - | Receiving MQTT message | + | Because UDP is asynchronous due to its nature, each transaction |
| + | The sample callback function may have the following parameters: | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | void callback(char* topic, byte* payload, unsigned | + | void callback_response(CoapPacket &packet, IPAddress ip, int port); //this is only prototype |
| - | Serial.print(" | + | ... |
| - | Serial.print(topic); | + | </code> |
| - | Serial.print(" | + | |
| - | for (int i = 0; i < length; i++) { | + | |
| - | Serial.print((char)payload[i]); | + | |
| - | } | + | |
| - | Serial.println(); | + | |
| - | + | ||
| - | | + | |
| - | if ((char)payload[0] == ' | + | |
| - | digitalWrite(BUILTIN_LED, | + | |
| - | // but actually the LED is on; this is because | + | |
| - | // it is active low on the ESP-01) | + | |
| - | } else { | + | |
| - | digitalWrite(BUILTIN_LED, | + | |
| - | } | + | |
| + | As code runs asynchronously and many messages may come one by one, you should not process the '' | ||
| + | <code c> | ||
| + | ... | ||
| + | void callback_response(CoapPacket & | ||
| + | char p[packet.payloadlen + 1]; | ||
| + | memcpy(p, packet.payload, | ||
| + | p[packet.payloadlen] = NULL; | ||
| + | //and then process " | ||
| + | ... | ||
| } | } | ||
| ... | ... | ||
| </ | </ | ||
| - | You also need to bind the callback function with the MQTT client: | + | |
| + | <note tip>You may have a variety of frames received, but the common is to " | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | client.setCallback(callback); | + | if(packet.type==3 && packet.code==0){ |
| + | Serial.println("COAP ping ok"); | ||
| + | } | ||
| ... | ... | ||
| </ | </ | ||
| - | And then periodically execute the communication routine | + | </ |
| + | |||
| + | Once your callback function is ready, send a request and wait for the callback function to be triggered when the response comes. | ||
| + | <note warning> | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | | + | |
| + | coap.start(); | ||
| ... | ... | ||
| </ | </ | ||
| + | |||
| + | |||
| + | Once the infrastructure is ready, you can send a request and observe a response, i.e., in a '' | ||
| + | <code c> | ||
| + | int msgid = coap.get(ip, | ||
| + | bool state = coap.loop(); | ||
| + | </ | ||
| + | Note, in the code above, '' | ||
| + | - IP address of the server you want to connect to ('' | ||
| + | - port number ('' | ||
| + | - URL, or actually, its part coming AFTER the IP section of the URL. | ||
| + | Regarding the last parameter, ''""'' | ||
| + | |||
| ==== Networking ==== | ==== Networking ==== | ||
| - | There is a WiFi 2.4GHz, IPv4 network available to the students. This is a PRIVATE network. This network is separated from the Internet but all connected devices | + | There is a WiFi 2.4GHz, IPv4 network available to the students. This is a PRIVATE network. This network is separated from the Internet, but all connected devices can talk to one another. There is a DHCP server with networking addressing scope 192.168.90.10-192.168.90.254. In this laboratory scenario, you work ONLY on the PRIVATE network. |
| - | <note warning> | + | <note warning> |
| To connect to the wireless network, use the following credentials: | To connect to the wireless network, use the following credentials: | ||
| Line 222: | Line 210: | ||
| ==== Network services ==== | ==== Network services ==== | ||
| - | === MQTT === | + | === CoAP === |
| - | There is an MQTT Broker, binding WiFi PRIVATE network and the PUBLIC Internet. This way you can route MQTT messages from your end node devices connected to the '' | + | There is a CoAP server awaiting |
| - | You can access it from the Internet PUBLIC network, using the following configuration and credentials: | + | You can also access it from the aforementioned PRIVATE network using the following configuration and credentials: |
| - | * Address: 157.158.56.54 | + | |
| - | * Port: 1883 for plain text, 8883 for secure communication | + | |
| - | * User: vrel | + | |
| - | * Password: vrel2018 | + | |
| - | + | ||
| - | You can also access it from the aforementioned PRIVATE network, using the following configuration and credentials: | + | |
| * Address: 192.168.90.5 | * Address: 192.168.90.5 | ||
| - | * Port: 1883 for plain text, 8883 for secure communication | + | * Port: 5683, coaps is not implemented |
| - | * User: vrel | + | * There are 2 URIs you can use to test your CoAP client (IoT device): |
| - | * Password: vrel2018 | + | * '' |
| + | * '' | ||
| ==== Laboratory scenarios ==== | ==== Laboratory scenarios ==== | ||
| - | All teams share the same scenario yet please note that in particular, MQTT messages exchanged between devices shall be coordinated among team members not to overlay one another.\\ | + | You will implement two scenarios |
| - | You can implement 3 separate codes or bind them into one, solid solution. | + | |
| - | <note tip>Mind keeping your code between meetings. It is regular that every next laboratory meeting you will extend your code by something extra.</ | + | |
| - | < | + | |
| === Scenario 1 === | === Scenario 1 === | ||
| - | ** Connecting to the WiFi **\\ | + | ** Connecting to the WiFi and getting a Hello World message via CoAP **\\ |
| - | Make your IoT device connect | + | Connect |
| Report: | Report: | ||
| - | * Describe | + | * Document |
| - | < | + | |
| === Scenario 2 === | === Scenario 2 === | ||
| - | ** Sending MQTT message **\\ | + | ** Get a magic code via CoAP and present |
| - | Read temperature | + | Get a secret code and present |
| - | <note warning> | + | |
| - | Observe your messages and their changing over time using any software-based MQTT client (i.e. MQTT Explorer or MQTT Spy apps). | + | |
| - | + | ||
| - | Report: | + | |
| - | * Describe your interface and include client name, MQTT topics, publishing frequency, etc. | + | |
| - | < | + | |
| - | + | ||
| - | === Scenario 3 === | + | |
| - | ** Receiving MQTT message | + | |
| - | Prepare | + | |
| - | Use MQTT Spy or MQTT Explorer app to send a message with payload. | + | |
| Report: | Report: | ||
| - | * Document your interface (topic, payload format). Present your approach to buffer | + | * Present results, including a photo of the LCD with a message |
| - | < | + | |
en/edu/zmitac/s2/iotn/lab3.1647637071.txt.gz · Last modified: by pczekalski
