User Tools

Site Tools


No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:lab2

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
en:edu:zmitac:s2:iotn:lab2 [2023/04/21 16:38] – [Network services] administratoren:edu:zmitac:s2:iotn:lab2 [2023/04/21 16:42] (current) – administrator
Line 2: Line 2:
 Network programming for the IoT Devices using MQTT protocol. Network programming for the IoT Devices using MQTT protocol.
  
-Following manual assumes you're familiar with [[en:edu:zmitac:s2:iotn:lab1|InT Networks: Laboratory 1 - Introduction]]. In case you're not, please get yourself familiar with it and spend some time practising. You can start your project stub using hints and guidance in the [[en:edu:zmitac:s2:iotn:lab1|InT Networks: Laboratory 1 - Introduction]] manual and modify it / extend as guided below.+The following manual assumes you're familiar with [[en:edu:zmitac:s2:iotn:lab1|InT Networks: Laboratory 1 - Introduction]]. In case you're not, please get familiar with it and spend some time practicing. Start your project stub using hints and guidance in the [[en:edu:zmitac:s2:iotn:lab1|InT Networks: Laboratory 1 - Introduction]] manual and modify it / extend it as guided below.
  
-<note important>This manual is not intended to be stand-alone. You need to follow Laboratory 1 manual to prepare basics for the project then continue here.</note> +<note important>This manual is not intended to be stand-alone. You need to follow the Laboratory 1 manual to prepare the basics for the project, then continue here.</note>
- +
-<note important>Computers in laboratory rooms 320, 321 and 336 use freezing software which means that for every startup, computers are restored to their initial state.</note>+
  
 ==== Hardware ==== ==== Hardware ====
-You will use a sensor that measures temperature + humidity, to publish this information to the MQTT broker:+You will use a sensor that measures temperature + humidity to publish this information to the MQTT broker:
   * DHT 11 temperature and humidity sensor   * DHT 11 temperature and humidity sensor
 <figure DHT> <figure DHT>
Line 18: Line 16:
  
 === 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 configuration. Following chunks of code will help to set up proper hardware-software configuration.+Interfacing hardware requires the use of particular GPIOs. Laboratory nodes and hardware used need specific configuration. The following chunks of code will help to set up proper hardware-software configuration.
  
 == LCD display == == LCD display ==
Line 35: Line 33:
 </code> </code>
  
-To initialise it and instantiate controller object, use following GPIO definition and initialisation:+To initialize it and instantiate the controller object, use the following GPIO definition and initialization:
 <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 41: Line 39:
 </code> </code>
  
-It is also necessary to tell the library about the display organisation as there is a variety of configurations possible. Here, the organisation is 2 lines, 16 columns each, so in the code you need to explicitly initialise it:+It is also necessary to tell the library about the display organization as various configurations are possible. Here, the organization is 2 lines, 16 columns each, so in the code, you need to explicitly initialize it:
 <code c> <code c>
 ... ...
Line 50: Line 48:
 == Temperature and humidity sensor == == Temperature and humidity sensor ==
 There is a DHT11 (blue) sensor connected to the __GPIO 5__.\\ There is a DHT11 (blue) sensor connected to the __GPIO 5__.\\
-There are two libraries needed to handle reading from the sensor: a common sensor communication library and a specific for DHT sensors, both by Adafruit, so two more entries are needed in the //platformio.ini// file, and obviously you need to refer to it in the source code:+There are two libraries needed to handle reading from the sensor: a common sensor communication library and a specific one for DHT sensors, both by Adafruit, so two more entries are needed in the //platformio.ini// file, and you need to refer to it in the source code:
 <code> <code>
 lib_deps =  lib_deps = 
Line 61: Line 59:
 </code> </code>
  
-To access sensor readings it is necessary to instantiate a communication class that binds the hardware to the code:+To access sensor readings, it is necessary to instantiate a communication class that binds the hardware to the code:
 <code c> <code c>
 #define DHTTYPE DHT11   // DHT 11 #define DHTTYPE DHT11   // DHT 11
Line 67: Line 65:
 DHT dht(DHTPIN, DHTTYPE,50); DHT dht(DHTPIN, DHTTYPE,50);
 </code> </code>
-It is also necessary to initialise the sensor class and give it some time to set up:+It is also necessary to initialize the sensor class and give it some time to set up:
 <code c> <code c>
 ... ...
Line 75: Line 73:
 </code> </code>
  
-The sensor class returns data (among others) in a float containing temperature (in C) and relative humidity (in per cent):+The sensor class returns data (among others) in a float containing temperature (in C) and relative humidity (in percent):
 <code c> <code c>
 ... ...
Line 84: Line 82:
  
 ==== 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's line, or any other size, large enough to handle your text.+String formatting may be a challenge. A straightforward approach is to allocate a character buffer of the size of the LCD or just one display's line or any other size large enough to handle your text.
 <code c> <code c>
 ... ...
Line 91: Line 89:
 </code> </code>
  
-To convert float into the string with formatting use ''sprintf'' function:+To convert float into the string with formatting, use ''sprintf'' function:
 <code c> <code c>
 ... ...
Line 99: Line 97:
 ... ...
 </code> </code>
-<note tip>''sprintf'' uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on ''sprintf''. Here <code c>%2.1f</code> means: having float number render it to string using two digits before decimal point and one after. Temperature in our lab is always above 10C. <code c>%%</code> is an escape character to print a <code c>%</code> (percent) symbol.+<note tip>''sprintf'' uses a number of wildcards rendered with data. Refer to the c/c++ documentation on ''sprintf''. Here <code c>%2.1f</code> means: having a float number render it to string using two digits before the decimal point and one after. The temperature in our lab is always above 10C. <code c>%%</code> is an escape character to print a <code c>%</code> (percent) symbol.
 <code c> <code c>
 delay(time) delay(time)
Line 105: Line 103:
  
 === Software libraries === === Software libraries ===
-Software libraries are needed to handle communication between your IoT device and network and services. In this laboratory, you will connect your device to the WiFi access point (see the Networking section below for credentials) and use TCP. Your device will be automatically given an IP address that you may show on the LCD screen. Once WiFi is connected and works (PHY+MAC+NET layer protocols) then you can connect over it to the MQTT Broker (APP layer protocol).\\ +Software libraries are needed to communicate between your IoT device and network and services. In this laboratory, you will connect your device to the WiFi access point (see the Networking section below for credentials) and use TCP. Your device will be automatically given an IP address that you may show on the LCD screen. Once WiFi is connected and works (PHY+MAC+NET layer protocols), then you can connect over it to the MQTT Broker (APP layer protocol).\\ 
-Mind, the connection may become broken so ensure re-connection mechanism: +Mind, the connection may become broken, so ensure a re-connection mechanism: 
   * dummy: in case the connection is broken, restart the device to let it perform the connection process again;   * dummy: in case the connection is broken, restart the device to let it perform the connection process again;
-  * smart: include checking and re-connection into your code using connection status tracking, mind you not only need to reconnect WiFi but also high-level communication protocol: MQTT.+  * smart: include checking and re-connection into your code using connection status tracking, mind you need to reconnect WiFi and high-level communication protocol: MQTT.
 == WIFI and TCP == == WIFI and TCP ==
-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 header to your C code:+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 included to your C code:
 <code c> <code c>
 ... ...
Line 117: Line 115:
 </code> </code>
  
-Then create some constant variables or declare token string identifiers (as below) with ''sSsid'' and ''sPassword'' (see Networking section for credentials) and instantiate WiFi communication object:+Then create some constant variables or declare token string identifiers (as below) with ''sSsid'' and ''sPassword'' (see Networking section for credentials) and instantiate the WiFi communication object:
 <code c> <code c>
 ... ...
Line 124: Line 122:
 ... ...
 </code> </code>
-The ''WiFi'' object is a singleton, static one. Try to connect to the AP, i.e. this way:+The ''WiFi'' object is a singleton, static one. Try to connect to the AP, i.e., this way:
 <code c> <code c>
 ... ...
Line 133: Line 131:
  
 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 157: Line 155:
 ... ...
 </code> </code>
-Note, this object becomes automatically active and bound to the ''WiFi'' connection, when available. You do not need explicitly to initialise it or force connect it.+Note this object becomes automatically active and bound to the ''WiFi'' connection when available. You do not need explicitly to initialize it or force connect it.
  
 == MQTT == == MQTT ==
Line 165: Line 163:
 </code> </code>
  
-<note important>Please note, on the NET protocol level, in this scenario, you're connecting to the WiFi AP (STA), not setting your device to be a WiFi server (AP), so you need to set appropriate WiFi mode, as it may be previously used in AP, not STA mode.</note>+<note important>Please note, on the NET protocol level, in this scenario, you're connecting to the WiFi AP (STA), not setting your device to be a WiFi server (AP), so you need to set the appropriate WiFi mode, as it may be previously used in AP, not STA mode.</note>
  
-MQTT clients need to be instantiated using WiFi client connection, i.e. this way:+MQTT clients need to be instantiated using a WiFi client connection, i.e., this way:
 <code c> <code c>
 ... ...
Line 177: Line 175:
 ... ...
 </code> </code>
-and in the setup code, you need to initialise the ''client'' with MQTT address and port number. Do not use SSL / TLS encoding as this library is not able to support encrypted communication with MQTT broker:+and in the setup code, you need to initialize the ''client'' with the MQTT address and port number. Do not use SSL / TLS encoding, as this library is not able to support encrypted communication with an MQTT broker:
 <code c> <code c>
 ... ...
Line 186: Line 184:
  
  
-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:+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:
 <code c> <code c>
 void reconnect() { void reconnect() {
Line 213: Line 211:
 </code> </code>
  
-Receiving MQTT message is done with the use of the asynchronously called callback function, i.e. like this one (it switches on and off the LED):+Receiving an MQTT message is done with the use of the asynchronously called callback function, i.e., like this one (it switches on and off the LED):
 <code c> <code c>
 ... ...
Line 250: Line 248:
 </code> </code>
 ==== 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 to it can talk to another. There is a DHCP server with networking addressing scope 192.168.90.10-192.168.90.254. +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. 
  
-<note warning>Do not use static IP! Some services present in the network have fixed IP addresses, overlapping it causes unavailability of your solution and this service, also to other teams! Use the only DHCP for obtaining a network address.</note>+<note warning>Do not use static IP! Some services in the network have fixed IP addresses; overlapping causes the unavailability of your solution and this service, also to other teams! Use only DHCP for obtaining a network address.</note>
  
 To connect to the wireless network, use the following credentials: To connect to the wireless network, use the following credentials:
Line 283: Line 281:
  
 ==== 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.\\ +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 can implement 3 separate codes or bind them into one, solid solution. +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.</note>+<note tip>Mind keeping your code between meetings. It is regular that at every next laboratory meeting, you will extend your code by something extra.</note>
 <pagebreak> <pagebreak>
  
 === Scenario 1 === === Scenario 1 ===
 ** Connecting to the WiFi **\\ ** Connecting to the WiFi **\\
-Make your IoT device connect to the Internal.IOT network and present MAC of the device and IP address on the LCD screen.+Make your IoT device connect to the Internal.IOT network and present the device's MAC and IP address on the LCD screen.
  
 Report: Report:
-  * Describe your approach to the WiFi re-connection and document IP and MAC with screenshot.+  * Describe your approach to the WiFi re-connection and document IP and MAC with a screenshot.
 <pagebreak> <pagebreak>
  
Line 300: Line 298:
 Read temperature and humidity and send it as two separate MQTT messages (as payloads). Read temperature and humidity and send it as two separate MQTT messages (as payloads).
 <note warning>Warning! Agree with other sections to use UNIQUE MQTT client names and MQTT topics. Otherwise, you will interfere with one another on the broker level.</note> <note warning>Warning! Agree with other sections to use UNIQUE MQTT client names and MQTT topics. Otherwise, you will interfere with one another on the broker level.</note>
-Observe your messages and their changing over time using any software-based MQTT client (i.e. MQTT Explorer or MQTT Spy apps).+Observe your messages and their change over time using any software-based MQTT client (i.e., MQTT Explorer or MQTT Spy apps).
  
 Report: Report:
-  * Describe your interface and include client name, MQTT topics, publishing frequency, etc. +  * Describe your interface, including client name, MQTT topics, publishing frequency, etc. 
 <pagebreak> <pagebreak>
  
 === Scenario 3 === === Scenario 3 ===
 ** Receiving MQTT message **\\ ** Receiving MQTT message **\\
-Prepare a solution where your IoT device subscribes to the selected message that will be received and the payload will be present on the LCD screen. You can assume, the maximum payload size is 16 characters, to fit a single line of the LCD display. You should be able then to send any text to your LCD, remotely. Use 2 separate messages to control the text in line 0 and line 1 (first and second) of the LCD screen.\\ +Prepare a solution where your IoT device subscribes to the selected message that will be received, and the payload will be present on the LCD screen. You can assume the maximum payload size is 16 characters to fit a single LCD display line. You should be able then to send any text to your LCD remotely. Use 2 separate messages to control the text in line 0 and line 1 (first and second) of the LCD screen.\\ 
-Use MQTT Spy or MQTT Explorer app to send a message with payload.+Use MQTT Spy or MQTT Explorer app to send a message with a payload.
  
 Report: Report:
en/edu/zmitac/s2/iotn/lab2.1682087924.txt.gz · Last modified: by administrator