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.
| Next revision | Previous revision | ||
| en:edu:zmitac:s2:iotn:lab1 [2022/03/16 12:57] – created pczekalski | en:edu:zmitac:s2:iotn:lab1 [2023/04/21 16:38] (current) – administrator | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== IoT Networks: Laboratory 1 - Introduction ====== | ||
| + | Introduction to IoT programming. | ||
| + | |||
| + | 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. | ||
| + | <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).</ | ||
| + | |||
| + | ==== Hardware ==== | ||
| + | Each team is given the following IoT microcontroller: | ||
| + | * [[https:// | ||
| + | <figure wemosd1r2> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | Note, this microcontroller, | ||
| + | <figure wemosd1r2pinout> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | * DHT 11 temperature and humidity sensor | ||
| + | <figure DHT> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | * [[https:// | ||
| + | <figure lcd2x16shield> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== VSCode, PlatformIO, and project configuration ==== | ||
| + | |||
| + | In any case, creating a new project under PlatformIO should follow this general setting, regarding Board and Framework (see figure {{ref> | ||
| + | <figure platformionewproject> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | This configuration brings the following project configuration file '' | ||
| + | < | ||
| + | [env: | ||
| + | platform = espressif8266 | ||
| + | board = d1_mini | ||
| + | framework = arduino | ||
| + | </ | ||
| + | |||
| + | Configuring the serial port name used for uploading your firmware and monitoring is also necessary. In the case of this microcontroller, | ||
| + | < | ||
| + | ... | ||
| + | upload_port = COM3 | ||
| + | monitor_port = COM3 | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === Running your code === | ||
| + | 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> | ||
| + | <figure pioribbon> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | === Hardware-software components and libraries === | ||
| + | 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 == | ||
| + | Built-in LED on the microcontroller development board can help to manage simple tracing of the code execution: | ||
| + | <code c> | ||
| + | #define LED_BUILTIN 14 | ||
| + | </ | ||
| + | |||
| + | 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 '' | ||
| + | <code c> | ||
| + | lib_deps = Arduino-libraries/ | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | To initialize it and instantiate the controller object, use the following GPIO definition and initialization: | ||
| + | <code c> | ||
| + | const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14; | ||
| + | LiquidCrystal lcd(rs, en, d4, d5, d6, d7); | ||
| + | </ | ||
| + | |||
| + | It is also necessary to tell the library about the display organization, | ||
| + | <code c> | ||
| + | ... | ||
| + | lcd.begin(16, | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | == Keypad == | ||
| + | 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> | ||
| + | |||
| + | <figure LCD1602buttons> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | 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> | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | Reading an AD0 is as simple as: | ||
| + | <code c> | ||
| + | #define ADCPIN 0 | ||
| + | ... | ||
| + | int nAdcReading = 0; | ||
| + | ... | ||
| + | nAD0 = analogRead(ADCPIN); | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Temperature and humidity sensor == | ||
| + | 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 '' | ||
| + | < | ||
| + | lib_deps = | ||
| + | adafruit/ | ||
| + | adafruit/ | ||
| + | </ | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | To access sensor readings, it is necessary to instantiate a communication class that binds the hardware to the code: | ||
| + | <code c> | ||
| + | #define DHTTYPE DHT11 // DHT 11 | ||
| + | #define DHTPIN 5 | ||
| + | DHT dht(DHTPIN, DHTTYPE, | ||
| + | </ | ||
| + | It is also necessary to initialize the sensor class and give it some time to set up: | ||
| + | <code c> | ||
| + | ... | ||
| + | dht.begin(); | ||
| + | delay(1000); | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | The sensor class returns data (among others) in a float containing temperature (in C) and relative humidity (in percent): | ||
| + | <code c> | ||
| + | ... | ||
| + | float hum = dht.readHumidity(); | ||
| + | float temp = dht.readTemperature(); | ||
| + | ... | ||
| + | </ | ||
| + | ==== Software ==== | ||
| + | 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' | ||
| + | <code c> | ||
| + | ... | ||
| + | char buffer [50]; | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | To convert float into the string with formatting, use '' | ||
| + | <code c> | ||
| + | ... | ||
| + | sprintf(buffer," | ||
| + | ... | ||
| + | sprintf(buffer," | ||
| + | ... | ||
| + | </ | ||
| + | <note tip>'' | ||
| + | <code c> | ||
| + | delay(time) | ||
| + | </ | ||
| + | ==== 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 used for flashing. | ||
| + | To open serial communication, | ||
| + | <code c> | ||
| + | Serial.begin(9600); | ||
| + | </ | ||
| + | 9600 is the default speed, but you can change it in the '' | ||
| + | It is possible to send virtually any message to the console (keep in mind, you need explicitly open Serial | ||
| + | <code c> | ||
| + | ... | ||
| + | Serial.print(...); | ||
| + | //or | ||
| + | Serial.println(...); | ||
| + | ... | ||
| + | </ | ||
| + | Both functions accept a wide range of argument types without needing to convert to string explicitly. | ||
| + | |||
| + | |||
| + | ==== 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 can talk to one another. There is a DHCP server with networking addressing scope 192.168.90.10-192.168.90.254. | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | To connect to the wireless network, use the following credentials: | ||
| + | * SSID: internal.IOT | ||
| + | * Passkey: IoTlab32768 | ||
| + | * IP address: DHCP, 192.168.90.10-192.168.90.254 | ||
| + | |||
| + | ==== Network services ==== | ||
| + | |||
| + | === MQTT === | ||
| + | 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 '' | ||
| + | |||
| + | You can access the MQTT broker from the Campus PUBLIC 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 the MQTT broker from the '' | ||
| + | * Address: 192.168.90.5 | ||
| + | * Port: 1883 for plain text, 8883 for secure communication | ||
| + | * User: vrel | ||
| + | * Password: vrel2018 | ||
| + | |||
| + | <figure zmitacnetworkingiot> | ||
| + | {{: | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | === Node Red and Node Red Dashboard === | ||
| + | A Node RED server is working on the same Raspberry Pi 2 as MQTT and in a private address pool only, available through // | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | Log in using the following credentials: | ||
| + | * User: //contact supervisor// | ||
| + | * Password: //contact supervisor// | ||
| + | |||
| ==== Laboratory scenarios ==== | ==== Laboratory scenarios ==== | ||
| - | All teams share the same scenario yet please note that in particular, | + | 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 tip>Mind keeping your code between meetings. It is regular that at every next laboratory meeting, you will extend your code by something extra.</ |
| < | < | ||
| Line 18: | Line 248: | ||
| </ | </ | ||
| - | 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 |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 26: | Line 256: | ||
| Advanced: | Advanced: | ||
| - | * Use timer/ | + | * Use a timer/ |
| Report: | Report: | ||
| * Describe your approach and include source code in the report | * Describe your approach and include source code in the report | ||
| Line 33: | 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 '' | + | Your code shall implement a message '' |
| Report: | Report: | ||
| Line 45: | Line 275: | ||
| Report: | Report: | ||
| - | * Describe how did you solve the problem of the asynchronous | + | * Describe how you solved |
| * Include source code. | * Include source code. | ||
| < | < | ||
en/edu/zmitac/s2/iotn/lab1.1647431828.txt.gz · Last modified: by pczekalski
