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.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:iotn:lab2 [2022/03/17 22:55] – pczekalski | en:edu:zmitac:s2:iotn:lab2 [2023/04/21 16:42] (current) – administrator | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| Network programming for the IoT Devices using MQTT protocol. | Network programming for the IoT Devices using MQTT protocol. | ||
| - | Following | + | The following |
| - | <note important> | + | <note important> |
| - | + | ||
| - | <note important> | + | |
| ==== 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. | + | Interfacing hardware requires the use of particular GPIOs. Laboratory nodes and hardware used need specific configuration. |
| == LCD display == | == LCD display == | ||
| Line 35: | Line 33: | ||
| </ | </ | ||
| - | 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 41: | Line 39: | ||
| </ | </ | ||
| - | 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 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 // | + | There are two libraries needed to handle reading from the sensor: a common sensor communication library and a specific |
| < | < | ||
| lib_deps = | lib_deps = | ||
| Line 61: | Line 59: | ||
| </ | </ | ||
| - | 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, | DHT dht(DHTPIN, DHTTYPE, | ||
| </ | </ | ||
| - | It is also necessary to initialise | + | It is also necessary to initialize |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 75: | Line 73: | ||
| </ | </ | ||
| - | 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. | + | String formatting may be a challenge. |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 91: | Line 89: | ||
| </ | </ | ||
| - | To convert float into the string with formatting use '' | + | To convert float into the string with formatting, use '' |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 99: | Line 97: | ||
| ... | ... | ||
| </ | </ | ||
| - | <note tip>'' | + | <note tip>'' |
| <code c> | <code c> | ||
| delay(time) | delay(time) | ||
| Line 105: | Line 103: | ||
| === Software libraries === | === Software libraries === | ||
| - | Software libraries are needed to handle communication | + | Software libraries are needed to communicate |
| - | Mind, the connection may become broken so ensure re-connection mechanism: | + | Mind, the connection may become broken, so ensure |
| * 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 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 117: | Line 115: | ||
| </ | </ | ||
| - | Then create some constant variables with '' | + | Then create some constant variables |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 124: | Line 122: | ||
| ... | ... | ||
| </ | </ | ||
| - | The '' | + | The '' |
| - | < | + | < |
| ... | ... | ||
| WiFi.mode(WIFI_STA); | WiFi.mode(WIFI_STA); | ||
| Line 131: | Line 129: | ||
| ... | ... | ||
| </ | </ | ||
| + | |||
| + | Some useful code snippets are below.\\ | ||
| + | Checking if the WiFi connection is successful: | ||
| + | <code c> | ||
| + | ... | ||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(" | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Checking the current IP address: | ||
| + | <code c> | ||
| + | ... | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Once WiFi is connected, it is necessary to use a '' | ||
| + | The declaration can be as follows: | ||
| + | <code c> | ||
| + | ... | ||
| + | WiFiClient espClient; | ||
| + | ... | ||
| + | </ | ||
| + | Note this object becomes automatically active and bound to the '' | ||
| == MQTT == | == MQTT == | ||
| Line 138: | Line 163: | ||
| </ | </ | ||
| - | <note important> | + | <note important> |
| + | MQTT clients need to be instantiated using a WiFi client connection, i.e., this way: | ||
| + | <code c> | ||
| + | ... | ||
| + | #define mqtt_server "< | ||
| + | #define mqtt_user "< | ||
| + | #define mqtt_password "< | ||
| + | ... | ||
| + | PubSubClient client(espClient); | ||
| + | ... | ||
| + | </ | ||
| + | and in the setup code, you need to initialize the '' | ||
| + | <code c> | ||
| + | ... | ||
| + | client.setServer(mqtt_server, | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | 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> | ||
| + | void reconnect() { | ||
| + | // Loop until we're reconnected | ||
| + | while (!client.connected()) { | ||
| + | Serial.print(" | ||
| + | // Create a random client ID | ||
| + | String clientId = " | ||
| + | clientId += String(random(0xffff), | ||
| + | // 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 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> | ||
| + | ... | ||
| + | void callback(char* topic, byte* payload, unsigned int length) { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | for (int i = 0; i < length; i++) { | ||
| + | Serial.print((char)payload[i]); | ||
| + | } | ||
| + | Serial.println(); | ||
| + | |||
| + | // Switch on the LED if an 1 was received as first character | ||
| + | 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, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | You also need to bind the callback function with the MQTT client: | ||
| + | <code c> | ||
| + | ... | ||
| + | client.setCallback(callback); | ||
| + | ... | ||
| + | </ | ||
| + | And then periodically execute the communication routine for the handling of the incoming messages, usually in the '' | ||
| + | <code c> | ||
| + | ... | ||
| + | client.loop(); | ||
| + | ... | ||
| + | </ | ||
| ==== 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. |
| - | <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 153: | Line 260: | ||
| === MQTT === | === MQTT === | ||
| - | There is an MQTT Broker, binding WiFi PRIVATE network and the PUBLIC | + | There is an MQTT Broker, |
| - | You can access | + | You can access |
| * 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 161: | Line 268: | ||
| * Password: vrel2018 | * Password: vrel2018 | ||
| - | You can also access | + | You can also access |
| * 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 | ||
| Line 167: | Line 274: | ||
| * Password: vrel2018 | * Password: vrel2018 | ||
| - | === Node Red and Node Red Dashboard === | + | <figure zmitacnetworkingiot> |
| - | A Node RED server is working on same Raspberry Pi 2 as MQTT and in private address pool only, available through // | + | {{:en:edu:zmitac:macro: |
| - | * [[http:// | + | < |
| - | * [[http://192.168.90.5: | + | </figure> |
| - | Login 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, 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.\\ |
| - | <note tip>Mind keeping your code between meetings. It is regular that every next laboratory meeting you will extend your code by something extra.</ | + | 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 at every next laboratory meeting, you will extend your code by something extra.</ | ||
| < | < | ||
| === Scenario 1 === | === Scenario 1 === | ||
| - | **Getting ready with your development environment**\\ | + | ** Connecting to the WiFi **\\ |
| - | In this scenario, you will write your first "Hello World" application for ESP8266. | + | Make your IoT device connect to the Internal.IOT network and present |
| - | Using the selected IDE, write code that: | + | |
| - | - Flashes built-in LED every 1 second (1s on; 1s off); | + | |
| - | - Sends a message via serial port to the serial port monitor: '' | + | |
| - | <note tip>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.</ | ||
| - | <code c> | ||
| - | ... | ||
| - | #define LED_BUILTIN 14 | ||
| - | ... | ||
| - | </ | ||
| - | |||
| - | 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. | ||
| - | <code c> | ||
| - | ... | ||
| - | Serial.begin(9600); | ||
| - | ... | ||
| - | </ | ||
| - | |||
| - | Advanced: | ||
| - | * Use timer/ | ||
| Report: | Report: | ||
| - | * Describe your approach | + | * Describe your approach |
| < | < | ||
| + | |||
| === Scenario 2 === | === Scenario 2 === | ||
| - | ** Using LCD **\\ | + | ** Sending MQTT message |
| - | In this scenario, you will write an application that writes | + | Read temperature and humidity and send it as two separate MQTT messages (as payloads). |
| - | Your code shall implement a message '' | + | <note warning> |
| + | Observe your messages | ||
| Report: | Report: | ||
| - | * Present algorithm. | + | * Describe your interface, including client name, MQTT topics, publishing frequency, etc. |
| - | * Include source code. | + | |
| < | < | ||
| === Scenario 3 === | === Scenario 3 === | ||
| - | ** Using Keypad | + | ** Receiving MQTT message |
| - | Extend Scenario 2 with a counter | + | Prepare |
| + | Use MQTT Spy or MQTT Explorer app to send a message with a payload. | ||
| Report: | Report: | ||
| - | * Describe how did you solve the problem of the asynchronous processing of the keypresses/ | + | * Document your interface (topic, payload format). Present your approach to buffer |
| - | * Include source code. | + | |
| < | < | ||
en/edu/zmitac/s2/iotn/lab2.1647554132.txt.gz · Last modified: by pczekalski
