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

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:edu:zmitac:s2:iotn:lab1 [2022/03/16 13:05] – pczekalskien:edu:zmitac:s2:iotn:lab1 [2023/04/21 16:38] (current) – administrator
Line 1: Line 1:
-====== Laboratory on the Internet of Things Networking ====== +====== IoT Networks: Laboratory 1 - Introduction ====== 
-Welcome to the laboratory of the Internet of Things. +Introduction to IoT programming.
-Students are about to perform a number of laboratory tasks. Each task is to be finished with a report summarizing what and how it was done. The report should be prepared in PDF form and eventually ZIPped along with extra files like source code, resources, etc.+
  
-<pagebreak> +You will likely use Visual Studio Code with the PlatformIO plugin during labs to author code for the IoT device.  
- +Devices are connected to the PC via a USB cable, but they expose a serial port to the operating system. You need to check the COM port number, which varies from PC to PC, and configure your project accordingly. 
-===== Laboratory 1 - Introduction ===== +<note tip>You can use the default COM port transfer speed, but in any case, when using the COM port as a trace, you need to configure the transmission speed on the code side. The default is 9600 (bps).</note>
-Introduction to the IoT programming. +
- +
-During labs, you will likely use Visual Studio Code with PlatformIO plugin, to author code for the IoT device.  +
-Devices are connected to the PC via USB cable but they expose a serial port to the operating system. You need to check the COM port number as it varies from PC to PC, and configure your project accordingly. +
-<note tip>You can use default COM port transfer speed but in any case, when using COM port as a trace, you need to configure the transmission speed on the code side. Default is 9600 (bps).</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 ====
Line 21: Line 13:
 <caption>WeMos D1 R2 development board</caption> <caption>WeMos D1 R2 development board</caption>
 </figure> </figure>
-Note, this microcontroller even if its board reminds Arduino Uno has different logic voltage (here 0..3.3V while  Arduino Uno uses classical 0..5V logic) and has different pinout numbering. Detail on the following figure:+Note, this microcontroller, even if its board reminds Arduino Uno has a different logic voltage (here 0..3.3V while  Arduino Uno uses classical 0..5V logic) and has different pinout numbering. Detail on the following figure:
 <figure wemosd1r2pinout> <figure wemosd1r2pinout>
 {{:en:edu:zmitac:macro:iot:pinout_0g-00005444_wifi-d1r2-esp8266.jpg?200|}} {{:en:edu:zmitac:macro:iot:pinout_0g-00005444_wifi-d1r2-esp8266.jpg?200|}}
Line 39: Line 31:
 </figure> </figure>
  
-==== VSCode, PlatformIO and project configuration ====+==== VSCode, PlatformIO, and project configuration ====
  
-In any case, creating a new project under PlatformIO should follow this general settings, regarding Board and Framework (see figure {{ref>platformionewproject}}):+In any case, creating a new project under PlatformIO should follow this general setting, regarding Board and Framework (see figure {{ref>platformionewproject}}):
 <figure platformionewproject> <figure platformionewproject>
 {{:en:edu:zmitac:macro:embedeed:lab:screenshot_from_2021-10-13_12-43-38.png?800|:img}} {{:en:edu:zmitac:macro:embedeed:lab:screenshot_from_2021-10-13_12-43-38.png?800|:img}}
Line 54: Line 46:
 </code> </code>
  
-It is also necessary to configure the serial port name that is used for uploading your firmware and to monitor. In the case of this microcontroller, it is the same port, so ''platformio.ini'' file should be modified with the following (assuming the serial port is i.e. ''COM3''):+Configuring the serial port name used for uploading your firmware and monitoring is also necessary. In the case of this microcontroller, it is the same port, so the ''platformio.ini'' file should be modified with the following (assuming the serial port is i.e., ''COM3''):
 <code> <code>
 ... ...
Line 63: Line 55:
  
 === Running your code === === Running your code ===
-To execute your code you need to compile it and upload it to the microcontroller.\\+To execute your code, you need to compile it and upload it to the microcontroller.\\
 The easiest way to interact with PlatformIO is to use its blue ribbon bar, located at the bottom of the VSCode (image {{ref>pioribbon}}: The easiest way to interact with PlatformIO is to use its blue ribbon bar, located at the bottom of the VSCode (image {{ref>pioribbon}}:
 <figure pioribbon> <figure pioribbon>
 {{:en:edu:zmitac:macro:embedeed:lab:screenshot_from_2021-12-06_16-22-15en.png?600|}} {{:en:edu:zmitac:macro:embedeed:lab:screenshot_from_2021-12-06_16-22-15en.png?600|}}
-<caption>PlatformIO ribbot to execute most common actions</caption>+<caption>PlatformIO ribbon to execute most common actions</caption>
 </figure> </figure>
  
 === 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 configurations. The following chunks of code will help to set up proper hardware-software configuration.
  
 == LCD display == == LCD display ==
Line 79: Line 71:
 </code> </code>
  
-Your LCD shield is a 2x16 character one, driven by a Hitachi HD44780 controller. You can use a library to use it easily. The PlatformIO will pull the library automatically if defined in //platformio.ini// file:+Your LCD shield is a 2x16 character driven by a Hitachi HD44780 controller. You can use a library to use it easily. The PlatformIO will pull the library automatically if defined in the ''platformio.ini'' file:
 <code c> <code c>
-lib_deps = arduino-libraries/LiquidCrystal+lib_deps = Arduino-libraries/LiquidCrystal
 </code> </code>
  
Line 88: Line 80:
 </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 94: Line 86:
 </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 102: Line 94:
  
 == Keypad == == Keypad ==
-The LCD shield has 6 buttons. One of them is hard-wired to the reset function, but other buttons (marked SELECT, UP, DOWN, RIGHT, LEFT) can be read through voltage analysis delivered into the AD0 (Analogue to Digital Converter, Channel 0) existing in the ESP8266 chip. Buttons are connected the way that they short the connection and every button creates a separate voltage divider between pull-up and respectable resistance, different for each button (see figure {{ref>LCD1602buttons}} for connection diagram). To estimate readings you can calculate voltage appearing on the AD0 input using Ohm's law.+The LCD shield has 6 buttons. One is hard-wired to the reset function, but other buttons (marked SELECT, UP, DOWN, RIGHT, LEFT) can be read through voltage analysis delivered into the AD0 (Analogue to Digital Converter, Channel 0) existing in the ESP8266 chip. Buttons are connected the way that they short the connection, and every button creates a separate voltage divider between pull-up and respectable resistance, different for each button (see figure {{ref>LCD1602buttons}} for connection diagram). To estimate readings, you can calculate the voltage appearing on the AD0 input using Ohm's law.
  
 <figure LCD1602buttons> <figure LCD1602buttons>
Line 110: Line 102:
  
  
-Note, there is an assumption that you press only one button at a time. If no button is pressed, Vcc goes to the AD0 and you get maximum reading (see schematics on figure {{ref>LCD1602buttons}} above and warning note below, on readings AD0).+Note there is an assumption that you press only one button at a time. If no button is pressed, Vcc goes to the AD0, and you get maximum reading (see schematics on figure {{ref>LCD1602buttons}} above and warning note below, on readings AD0).
  
-<note warning>Because of the LCD shield construction and the fact that ESP8266 can accept out of the box on their AD0 input voltage range of 0..3.3V only (actually 0..1V but there is a voltage divider in the middle), you're unable to grab button SELECT pressed, because shorting it causes input voltage to on the AD0 to be ~3.7V, while when no button is pressed, the input voltage on AD0 is 5V. Both are far beyond the measurable range, so you will always get maximum reading (1024) in both cases and there is no way to distinguish them, without electrical modification.</note>+<note warning>Because of the LCD shield construction and the fact that ESP8266 can accept out of the box on their AD0 input voltage range of 0..3.3V only (actually 0..1V but there is a voltage divider in the middle), you're unable to grab button SELECT pressed, because shorting it causes input voltage to on the AD0 to be ~3.7V, while when no button is pressed, the input voltage on AD0 is 5V. Both are far beyond the measurable range, so you will always get maximum reading (1024) in both cases, and there is no way to distinguish them without electrical modification.</note>
  
 Reading an AD0 is as simple as: Reading an AD0 is as simple as:
Line 127: Line 119:
 == 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 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 138: Line 130:
 </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 144: Line 136:
 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 152: Line 144:
 </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 160: Line 152:
 </code> </code>
 ==== 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 167: Line 159:
 </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 175: Line 167:
 ... ...
 </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)
 </code> is measured in milliseconds.</note> </code> is measured in milliseconds.</note>
 ==== Tracing ==== ==== Tracing ====
-There is no direct debugging capability in this environment yet it is possible to send log/trace messages to the PC console over a serial (serial over USB) port, the same that is used for flashing. +There is no direct debugging capability in this environment. Yet, it is possible to send log/trace messages to the PC console over a serial (serial over USB) port used for flashing. 
-To open serial communication, one must invoke begin with appropriate bps setting on the ''Serial'' singleton object (usually in ''Setup()'' function:+To open serial communication, one must invoke begin with the appropriate bps setting on the ''Serial'' singleton object (usually in ''Setup()'' function:
 <code c> <code c>
 Serial.begin(9600); Serial.begin(9600);
 </code> </code>
-9600 is the default speed but you can change it in the //platformio.ini// file.+9600 is the default speed, but you can change it in the ''platformio.ini'' file.
 It is possible to send virtually any message to the console (keep in mind, you need explicitly open Serial  Monitor in the VS code) using: It is possible to send virtually any message to the console (keep in mind, you need explicitly open Serial  Monitor in the VS code) using:
 <code c> <code c>
Line 192: Line 184:
 //or //or
 Serial.println(...); Serial.println(...);
 +...
 </code> </code>
-Both functions accept a wide range of argument types with no need to explicitly convert to string.+Both functions accept a wide range of argument types without needing to convert to string explicitly.
  
  
 ==== 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 209: Line 202:
  
 === MQTT === === MQTT ===
-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 ''internal.IOT'' network to the external tools i.e. cloud-based dashboard or storage via i.e. publishing from your end node IoT device through the PRIVATE network to MQTT Broker and subscribing to those messages from the cloud using PUBLIC Internet. \\+There is an MQTT Broker, a binding WiFi PRIVATE network, and the PUBLIC Campus network. This way, you can route MQTT messages from your end node devices connected to the ''internal.IOT'' network to the external tools, i.e., cloud-based dashboard or storage via, i.e., publishing from your end node IoT device through the PRIVATE network to MQTT Broker and subscribing to those messages from the cloud using PUBLIC Campus network. Please note the context (thus, the IP address of the MQTT broker) changes depending on which device and network you're using. The following network schema should be of help {{ref>zmitacnetworkingiot}}. \\
  
-You can access it from the Internet PUBLIC network, using the following configuration and credentials:+You can access the MQTT broker from the Campus PUBLIC network using the following configuration and credentials:
   * Address: 157.158.56.54   * Address: 157.158.56.54
   * Port: 1883 for plain text, 8883 for secure communication   * Port: 1883 for plain text, 8883 for secure communication
Line 217: Line 210:
   * Password: vrel2018   * Password: vrel2018
  
-You can also access it from the aforementioned PRIVATE network, using the following configuration and credentials:+You can also access the MQTT broker from the ''internal.IOT'' 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: 1883 for plain text, 8883 for secure communication
   * User: vrel   * User: vrel
   * Password: vrel2018   * Password: vrel2018
 +
 +<figure zmitacnetworkingiot>
 +{{:en:edu:zmitac:macro:iot:zmitac_network_infrastructure.png?600|}}
 +<caption>Zmitac network infrastructure for the IoT labs</caption>
 +</figure>
  
 === Node Red and Node Red Dashboard === === Node Red and Node Red Dashboard ===
-A Node RED server is working on same Raspberry Pi 2 as MQTT and in private address pool only, available through //internal.IoT// on 192.168.90.5 address, port 1880: +A Node RED server is working on the same Raspberry Pi 2 as MQTT and in a private address pool only, available through //internal.IoT// on 192.168.90.5 address, port 1880: 
   * [[http://192.168.90.5:1880]] - main editor   * [[http://192.168.90.5:1880]] - main editor
   * [[http://192.168.90.5:1880/ui]] - Node RED dashboard   * [[http://192.168.90.5:1880/ui]] - Node RED dashboard
-Login using the following credentials:+Log in using the following credentials:
   * User: //contact supervisor//   * User: //contact supervisor//
   * Password: //contact supervisor//   * Password: //contact supervisor//
  
 ==== 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 MQTT messages exchanged between devices shall be coordinated among team members not to overlay one another. 
-<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>
  
Line 250: Line 248:
 </code> </code>
  
-The standard serial port speed is 9600 (bps). Connect COM port monitor (a plug icon in the PlatformIO toolbar), once you compile and upload your code, to see the "Hello World" message.+The standard serial port speed is 9600 (bps). Connect the COM port monitor (a plug icon in the PlatformIO toolbar) once you compile and upload your code to see the "Hello World" message.
 <code c> <code c>
 ... ...
Line 258: Line 256:
  
 Advanced: Advanced:
-  * Use timer/interrupt to flash the LED instead of an infinite loop.+  * Use a timer/interrupt to flash the LED instead of an infinite loop.
 Report: Report:
   * Describe your approach and include source code in the report   * Describe your approach and include source code in the report
Line 265: Line 263:
 ** Using LCD **\\ ** Using LCD **\\
 In this scenario, you will write an application that writes on the LCD Display.\\ In this scenario, you will write an application that writes on the LCD Display.\\
-Your code shall implement a message ''Hello IoT'' in the upper line, and a counter ticking every second in the lower one.\\+Your code shall implement a message ''Hello IoT'' in the upper line and a counter ticking every second in the lower one.\\
  
 Report: Report:
Line 277: Line 275:
  
 Report: Report:
-  * Describe how did you solve the problem of the asynchronous processing of the keypresses/releases.+  * Describe how you solved the problem of the asynchronous key presses/releases processing.
   * Include source code.   * Include source code.
 <pagebreak> <pagebreak>
en/edu/zmitac/s2/iotn/lab1.1647432330.txt.gz · Last modified: by pczekalski