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

Join the WiFi network. Present an IP address on the selected display. The commonly used format for IPv4 addresses requires 15 characters, so LCD having 16 characters is sufficient to present the address. The steps below show the starting part of the software. How to implement the full software please refer to the previous scenarios:

The IPv4 addresses are usually expressed as four decimal numbers ranging from 0 to 255 separated by dots eg. “192.168.1.100”.

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

The procedure of connecting to the WiFi requires some steps. Below we present the AT commands only, your task is to implement the full code which sends the command and waits for the response for each command.

// Test if module is available
"AT"
 
// Reset the module
"AT+RST"
 
// Prevent from storing the WiFi join parameters in non-volatile memory
"AT+SYSSTORE=0"
 
// Start module in station mode (which can join the access point)
"AT+CWMODE=1"
 
// Join the access point. Use SSID and password.
"AT+CWJAP=\"SSID\",\"password\""
 
// Get the IP address
"AT+CIPSTA?"
Texts sent as part of the message are delimited with double quotation marks. In C++ we need to mark them with backslash characters inside the string constants. Examples above include these markings.

Some explanation can be needed for the “AT+RST” and “AT+SYSSTORE=0” commands. If we don't use the “AT+SYSSTORE=0” command our module stores the WiFi credentials in non-volatile memory and automatically connects to the network after powering on. In our laboratory, it is not recommended because the next commands can return “ERROR” if we try to connect the network and MQTT broker if we are already connected. If we don't use the “AT+RST” command our module will keep the WiFi connection active, even if we upload the new version of the software. It would result in the same error in the case of WiFi or MQTT reconnecting.

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.1714132997.txt.gz · Last modified: 2024/04/26 12:03 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