This is an old revision of the document!
In the following scenario, you will learn how to connect to the MQTT broker and subscribe to the chosen topic to receive messages.
To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
The requirement is to pass the scenarios
To be able to connect to the WiFi network and MQTT broker.
Note - this scenario can be used in pair with STM_IoT_3: Connecting to the MQTT broker and publishing data to build a publish-subscribe solution using two devices (sender and receiver). You need to book two devices then and develop them in parallel.
Connect to the “internal IoT” WiFI access point as presented in the scenario STM_IoT_2: Connecting to the WiFi Access Point and presenting IP — 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 subscribe to the MQTT topic of your choice. Present incoming data on the chosen display or control other actuators.
The steps below show the principles of the software operation. How to implement the full software please refer to the previous scenarios:
Check if you can see a full LCD in your video stream. Book a device and create a dummy Arduino file with void setup()… and void loop()….
The big part of the code is the same as in the previous scenario, so make a copy of it:
We will use the code template from STM_IoT_AT: Programming of the WiFi interface with AT commands repeated for every AT command.
WiFiSerial.println("AT"); lcd.setCursor(0,0); lcd.print("AT "); do { response = WiFiSerial.readStringUntil(0x0A); lcd.setCursor(0,1); lcd.print(response); } while (!(response.startsWith(compOK))); delay(1000);
In the firmware for the ESP32-C3 board, there are AT commands to work with the MQTT protocol. In this scenario we will use three of them:
Below we briefly describe the commands mentioning only the parameters important to us. Please refer to the Espressif documentation[1] for the details of commands.
AT+MQTTUSERCFG This command accepts the list of parameters:
AT+MQTTUSERCFG=<LinkID>,<scheme>,<"client_id">,<"username">,<"password">,<cert_key_ID>,<CA_ID>,<"path">
In our case, the command can look like this:
AT+MQTTUSERCFG=0,1,\"STM32#0001\",\"vrel\",\"vrel2018\",0,0,\"\"
AT+MQTTCONN This command accepts the list of parameters:
AT+MQTTCONN=<LinkID>,<"host">,<port>,<reconnect>
The command can look like this:
AT+MQTTCONN=0,\"192.168.1.90\",1883,1
AT+MQTTSUB
AT+MQTTSUB=<LinkID>,<"topic">,<qos>
The list of parameters:
After a successful subscription, the ESP32-C3 module will send messages in the following form:
+MQTTSUBRECV:<LinkID>,<"topic">,<data_length>,data
Implement the MQTT configuration, connection to the broker, and subscription to the chosen topic. Use the template for the “AT” command from the first step. Ensure that your node is successfully connected and the topic subscribed.
Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic “topic”. If the message payload is 0 LED will be off, if it is 1 LED will be on. You can use it as the template for more advanced control of any device in the laboratory equipment. Declare variables to store the strings for comparison. <code> String compend0, compend1; </code> Prepare string variables and set the mode of the LED pin. <code> compend0 = “+MQTTSUBRECV:0,\”topic\“,1,0”; compend1 = “+MQTTSUBRECV:0,\”topic\“,1,1”; pinMode(LED_BUILTIN,OUTPUT); </code> ==== Result validation ==== 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 STM_IoT_4: Connecting to the MQTT broker and subscribing to the topic) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to observe messages that you publish. <note info> Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention. </note> ===== FAQ ===== Can I change the IP address?: Normally IP addresses are assigned by the server known as DHCP. In some situations, you can use static IP assigned manually in the station. It is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same network.