No ACL setup yet! Denying access to everyone.
en:edu:zmitac:macro:iot:lab

This is an old revision of the document!


Laboratory on the Internet of Things

Welcome to the laboratory of the Internet of Things. Students are about to perform a number of laboratory tasks. Each task is to be finished with a report summarizing on 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.


Hardware

Each team is given the following IoT microcontroller:

Fig. 1: WeMos D1 R2 development board

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:

Fig. 2: WeMos D1 R2 pinout
  • DHT 11 or DHT 22 temperature and humidity sensor
Fig. 3: DHT 11 (left) and DHT 22 (right) temperature and humidity sensors
Fig. 4: LCD shield 2×16 characters
  • A selected number of teams will also experiment with a Plantower air quality sensors: PMS5003 or PMS7003
Fig. 5: PMS 5003 (left) and PMS 7003 (right) air quality sensors

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 6):

:img
Fig. 6: Project configuration settings in VSCode/PlatformIO

This configuration brings the following project configuration file platformio.ini:

[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino

It is also necessary to configure the serial port name that is used for uploading your firmware and to monitor (if you use it). 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):

...
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 7:

Fig. 7: PlatformIO ribbot to execute most common actions

Software

You will be writing an application for end nodes (ESP8266) in C++ using VS Code.
Additionally, you may need to use an MQTT client, i.e. MQTT Explorer: MQTT Explorer.

Computers in laboratory rooms 320, 321 and 336 use “freezing” software which means that for every startup, computers are restored to their initial state.

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.

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.

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, 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.

You can access it 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 it from the aforementioned PRIVATE network, using the following configuration and credentials:

  • Address: 192.168.90.5
  • Port: 1883 for plain text, 8883 for secure communication
  • User: vrel
  • Password: vrel2018

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:

Login using the following credentials:

  • User: contact supervisor
  • Password: contact supervisor

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. To open serial communication, one must invoke begin with appropriate bps setting on the Serial singleton object (usually in Setup() function:

Serial.begin(9600);

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:

...
Serial.print(...);
//or
Serial.println(...);
...

Both functions accept a wide range of argument types with no need to explicitly convert to string.

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.

Mind keeping your code between meetings. It is regular that every next laboratory meeting you will extend your code by something extra.


Scenario Lab1

Getting ready with your development environment
In this scenario, you will write your first “Hello World” application for ESP8266. Using the selected IDE, write code that:

  1. Flashes built-in LED every 1 second (1s on; 1s off);
  2. Sends a message via serial port to the serial port monitor: Hello World
The GPIO the built-in LED is connected to is number 14. Physically it is located almost in the middle of the mainboard, under the display and its colour is blue.
#define LED_BUILTIN 14

Note, you need to tell VS Code, which port to use to receive tracing messages you send from your code. Mind to include in the platformio.ini file appropriate configuration (here COM3 is a sample, check yours in PC configuration):

...
monitor_port = COM3
...

Advanced:

  • Use timer/interrupt to flash the LED instead of an infinite loop.

Report:

  • Describe your approach and include source code in the report


Scenario Lab2

** Using LCD **
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.
Additionally, present a label LEFT/RIGHT/UP/DOWN when a key is pressed on the 6 keys keypad, located down to the LCD display module. The label should appear in a lower line of the LCD, along with ticking counter that is still working. The label should disappear when the key is released.

To control LCD it is easiest to use a software library (add to the platformio.ini file:

...
lib_deps = arduino-libraries/LiquidCrystal @ ^1.0.7
...

Your LCD shield is a 2×16 characters one. To initialise it use following GPIO definition:

const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Report:

  • Describe how did you solve the problem of the asynchronous processing of the keypresses/releases.
  • Include source code.


Scenario Lab3

** Presenting temperature and humidity on the local, LCD screen **
In this scenario, you will measure temperature and relative humidity using the attached DHT 11 and present it on the screen. In the first row, there should appear a label Temperature: or Humidity: while in the lower, measured value, right-aligned along with the measurement unit. Switching between presenting temperature and humidity should be possible using Up/Down keys from the keypad and changing temperature between Celsius and Fahrenheit, using Left/Right keys. Measurements should be present along with a unit: C/F (for temperature) and %Rh for humidity. The temperature should be present with one decimal point while humidity as an integer.

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 platformio.ini file, and obviously you need to refer to it in the source code:

lib_deps = 
	adafruit/Adafruit Unified Sensor@^1.1.4
	adafruit/DHT sensor library@^1.4.3
#include <Adafruit_Sensor.h>
#include <DHT.h>

To access sensor readings it is necessary to instantiate a communication class that binds the hardware to the code:

#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 5
DHT dht(DHTPIN, DHTTYPE,50);

It is also necessary to initialise the sensor class and give it some time to set up:

...
dht.begin();
delay(1000);
...

The sensor class returns data (among others) in a float containing temperature (in C) and relative humidity (in per cent):

...
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();
...

Report:

  • Present a circuit with connections (LCD, keypad and sensor).
  • Present an algorithm for both overall program flow and for obtaining measurements.
  • Include your code with appropriate comments.


Scenario Lab4

** Connecting to the network **
In this scenario, you will use ESP 8266 networking. There are three, independent tasks to be completed today:

  • List all WiFi networks over the serial port to the serial monitor.
  • Implement a simple web server: using your ESP8266 prepare an Access Point (AP mode) you can connect to directly, using your mobile phone. Implement simple built-in LED control via a web browser, directly from your mobile phone. Present information on the LCD when the client connects to your ESP.
Remeber to use unique SSID name - coordinate it with other teams!
  • Connect your ESP8266 (as a client, STA mode) to the PRIVATE internal.IOT network with an address obtained from the DHCP server. Present connection status (connected/disconnected) in the upper row of the LCD and your IP address in the second line.

Report:

  • Provide your observation on how much time (more-less) it takes to connect to the ESP configured as AP and eventually observe the delay when switching on and off the built-in LED. Note - provide estimated value, not a detailed measurement.
  • When using ESP in STA mode, observe if the consequent running of the solution brings new or the same IP address attached via the DHCP server.
  • Include source code.


Scenario Lab5

Sending data to the network
In this scenario you're going to implement a fully functional IoT end-node, sending measurements to the MQTT Broker. Use your solution from the last step of Lab4 and also Lab3. Measure temperature and humidity, present them on the screen as in case of the Lab3 scenario and parallel, publish them as MQTT messages to the Broker.

Mind to make an agreement with other sections to let you do not overlap with MQTT Message Topics. They should be unique for all teams!

Using any software-based MQTT client, subscribe to the messages using a PRIVATE or PUBLIC network and observe results. You can use a PC computer you're developing your solution for this task.

The simplest starting point is to use pub-sub MQTT client library:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

You may also want to define MQTT topic(s) and constant payloads (i.e. those related to the last-will flag) using #define statement. It is also good idea to put connection constants here:

//WiFi and MQTT server credentials
#define wifi_ssid "internal.IOT"
#define wifi_password "IoTlab32768"
#define mqtt_server "192.168.90.5"
#define mqtt_user "vrel"
#define mqtt_password "vrel2018"
//MQTT last will
#define lastWillTopic "/sut/aeii/iot-lab/321/node1/state"
#define lastWillMessage "off"
#define mqttWelcomeMessage "on"

Mind, 192.168.90.5 is your MQTT server address.

To use pub-sub client you must instantiate a global object, using a network client, i.e. WiFiClient:

...
// WiFi & MQTT
WiFiClient espClient;
PubSubClient client(espClient);
...

Then connect your MQTT pub-sub client to the server. You can define a separate function in case you need to repeat this process when connection is down (for any reason), to simplify reconnect procedure:

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(<clientname>, mqtt_user, mqtt_password)) {
      Serial.println("connected to MQTT");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

Advanced:

  • Implement a “last will” informing that device has lost connection (suddenly, not a via disconnect). Present last available measurements as persistent. Present device state changing a persistent message on connect/disconnect to let the subscribers know the state of your device. To implement controlled disconnect, use select key from the keypad (you can assume that you may need to restart device to get it back on the network but you also may implement a bistable logic: first click of the select triggers MQTT disconnect, the second click connects it back to the Broker).

To publish last-will message, you may use an extended version of the PubSubClient.connect() function (i.e. in your reconnect() function:

...
client.connect(<clientname>, mqtt_user, mqtt_password, lastWillTopic, 0, true, lastWillMessage)
...

And once connected you may notify the dashboard (if any) that you're back, on-line:

...
client.publish(lastWillTopic, mqttWelcomeMessage, true);
...

Report:

  • Provide your MQTT message Topics and Payload formats (semantics).
  • Include source code.


Scenario Lab6

** Visualising your data **
In this scenario, use the public cloud tools or local ones, to visualise your data over time (Temperature, Humidity). Prepare a dashboard presenting current and former data, along with capabilities.

Advanced:

  • Prepare device state visualisation (connected/disconnected/lost connection).
  • Use local dashboard's store or a database to present hourly information as a chart (temperature, humidity).


Evaluation

As labs 3 through 5 constitute somehow a solid solution, every team is asked to prepare a presentation of their achievements to share it with other groups. The presentation should be some 10min in the form of the PDF presentation on-screen, using DLP projector in the laboratory room.

en/edu/zmitac/macro/iot/lab.1647866188.txt.gz · Last modified: by pczekalski