This is an old revision of the document!
[pczekalski]Dokończyć
In the following scenario, you will learn how to connect to the MQTT broker and publish a message.
To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
and obligatory:
There are many implementations of the MQTT protocol, but we will use the following library:
lib_deps = knolleary/PubSubClient@^2.8
Connect to the “internal IoT” WiFI access point as presented in the scenario IOT2: Reading IP address of the WiFi—present connection status on display. Once connected to the networking layer (WiFi), connect the MQTT client to the MQTT broker and present the connection status on the display, then publish an MQTT message of your choice.
Check if you can clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with void setup()…
and void loop()…
.
Implement a connection to the “internal IoT” network as a client. Refer to the supervisor or the technical documentation on credentials (SSID, passphrase). We do not provide the exact code on how to connect to the WiFi as it is a part of IOT2: Reading IP address of the WiFi scenario.
Once the device is booked, check if your display of choice is visible in the camera's FOV.
Refer to the hardware documentation and ensure an understanding of the network infrastructure you're interfacing with.
Implement the code to display on the selected device.
Connect to the WiFi in the STA mode (as a client) and ensure the connection is OK and you got an IP from the DHCP server.
Include the MQTT implementation library header in your code:
#include <PubSubClient.h>
Declare necessary addresses, constants, etc.:
IPAddress mqttServer(127,0,0,1); //change it to the MQTT broker IP #define mqtt_user "mqtt user" #define mqtt_password "mqtt password" #define mqtt_client_id "some_unique_client_id" #define mqtt_topic "/sample/topic/comes/here/change/it/please" #define mqtt_payload "sample payload"
Refer to the technical documentation (nodes) or the supervisor's guidance if working in the interactive mode to obtain the mqttServer
IP address, the mqtt_user
and mqtt_password
.
Remember to choose some unique client ID and topic!
You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then publish a message (once or periodically). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a subscriber (as present in the scenario IOT4: Connecting to the MQTT and subscribing to the messages) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to observe messages that you publish.
My MQTT client disconnects randomly: The most common reason is you're using a non-unique MQTT client name. Please change it to some other (even random generated) and give it another try.
How do I observe messages that I send?: Use a software client, such as MQTT Explorer, if you're able to access the “internal IoT” network (you're in the range of the network). If you're remote, the only way is to book another device and implement a client subscribing to your message, as presented in the scenario IOT3: Connecting to the MQTT and publishing data. Our MQTT broker is also visible in the campus network on the wired interfaces so that you can access it, e.g. via EduVPN or from the laboratory computers. Refer to the supervisor for IP and credentials.
Do I need to authorise to publish and subscribe?: Yes, you do. The supervisor provides the user and password on demand, also presented in the Node's technical documentation.