This is an old revision of the document!


STM_IoT_3: Connecting to the MQTT broker and publishing data

In the following scenario, you will learn how to connect to the MQTT broker and publish a message.

Prerequisites

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.

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

Note - this scenario can be used in pair with STM_IoT_4: Connecting to the MQTT broker and subscribing to the topic to build a publish-subscribe solution using two devices (sender and receiver). You need to book two devices then and develop them in parallel.

Task to be implemented

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 publish an MQTT message of your choice.

MQTT clients are identified by their name, so use a unique one, e.g., the end of the IP address assigned, your unique name, etc. It is essential because if you accidentally use someone else's name, then you will mess with messages, and your MQTT client will be instantly disconnected when another one with the same name connects!

The steps below show the principles of the software operation. How to implement the full software please refer to the previous scenarios:

Start

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()….

Steps

Step 1

The beginning 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);

Step 2

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:

  • “AT+MQTTUSERCFG…” - to configure MQTT c
  • “AT+MQTTCONN…” - to establish the connection to the broker
  • “AT+MQTTPUB…” - to publish the message on the topic

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.

If the parameter is taken within quotation marks it should be in such a form sent in a command.

AT+MQTTUSERCFG This command accepts the list of parameters: <code> AT+MQTTUSERCFG=<LinkID>,<scheme>,<“client_id”>,<“username”>,<“password”>,<cert_key_ID>,<CA_ID>,<“path”> </code> * LinkID - currently should be 0 * scheme - 1 (MQTT over TCP) * “client_id” - the unique ID of the MQTT client * “username” - user to connect to the broker * “password” - password for the user * cert_key-ID - should be 0 * CA_ID - should be 0 * “path” - should remain empty In our case, the command can look like this: <code> AT+MQTTUSERCFG=0,1,\“STM32#0001\”,\“vrel\”,\“vrel2018\”,0,0,\“\” </code> AT+MQTTCONN This command accepts the list of parameters:

AT+MQTTCONN=<LinkID>,<"host">,<port>,<reconnect>
  • LinkID - currently should be 0
  • “host” - the IP address or URL of the MQTT broker
  • port - the TCP port number, 1883 for most of the brokers
  • reconnect - 1 for automatic reconnection (recommended in our case)

The command can look like this:

AT+MQTTCONN=0,\"192.168.1.90\",1883,1

AT+MQTTPUB

AT+MQTTPUB=<LinkID>,<"topic">,<"data">,<qos>,<retain>

The list of parameters:

  • LinkID - currently should be 0
  • “topic” - MQTT topic
  • “data” - payload of the message
  • qos - mode of the quality of service, 0, 1 or2, default 0.
  • retain - retain flag, 0 or 1.

Step 3

The command for receiving the IP address is “AT+CIPSTA?”. It returns in the response message the IP address, IP address of the gateway and IP address mask:

+CIPSTA:ip:"192.168.1.117"
+CIPSTA:gateway:"192.168.1.1"
+CIPSTA:netmask:"255.255.255.0"

While LCD use we have to filter out unwanted elements of the response. We can do it by displaying the answer containing “+CIPSTA:IP:” only removing the first 12 characters. Look into the following code:

if (response.startsWith("+CIPSTA:ip:")){
  response.remove(0,12);
  lcd.setCursor(0,0);
  lcd.print(response);
}

Result validation

You should be able to see the IP address of the ESP32-C3 module.

Using another node ar even the same node another time can change the IP read. You can book another device and discover its IP.

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.

Project information


This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.

en/iot-open/practical/hardware/sut/stm32/iot_3.1714204676.txt.gz · Last modified: 2024/04/27 07:57 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0