ESP32-based mqtt client testing and server building process and stepping pit guide

During the development of ESP32 based on IDF, for the test of the MQTT function, we can use the native code to compile and test, but if we want to test whether the publish and subscribe are valid, we need to build an MQTT server. The construction of MQTT server. We use the open source server of EMQ for testing.

First of all, we need to log in to EMQ's official website interface to download and try EMQ products for free (emqx.com)

After that, we find the EMQ version we need to download and download it

Enter, after downloading the interface, follow the prompts to decompress and start in the background (location: D:\EMQ\mqtt\bin, varies from person to person)

How to start emqx

After the startup is complete, view the usage guide

In the usage guide, we first start quickly to test whether the server started in the mqtt background is normal (that is, enter the address Dashboard in the server  to access)

Enter account password to log in

Configure after login

New version (Figure 1)

After the configuration is complete, test (the following tools are required for the test here, so you can skip this step temporarily, install the following tools first and then test)

stop! ! ! Can no longer be skipped.

At this time, select the type of EMQX tool we use ( web version and download version )

For the web version:

First enter the web version interface

Next, start to configure tools (Sinicization, after all, the mother tongue is easier to understand)

then start using

Create a new connection first

Then configure the connection

At this point, click Connect, if the settings are correct, you can connect normally, and the exception will continue to reconnect (abnormalities are generally caused by configuration errors)

After the configuration is complete, publish and subscribe can be performed (test with the above steps)

Test Results:

For download version:

Find the download location first

Click the download button

Select the corresponding version to download

After decompressing and installing the software, open the software and configure the language first

then start the connection

configure connection

start testing

Test Results:

At this point, the ESP32 test environment has been deployed, and the ESP32 code can be modified for testing.

Modification of ESP32 code:

There are some differences between the test of ESP32 and the above description.

First of all, let's modify the uri and user name directly for access according to the simplest modification

(URL source)

At this point, run it and start reporting an error

We found that we are using a local area network, so we need to make the computer and esp32 on the same network, we use the mobile phone hotspot, and the computer and the device are connected to the same network

At this point, it runs and starts to report an error (the error is the same as above)

After searching on the Internet, I found out that 127.0.0.1 is used by this machine to monitor itself , and external devices cannot access this IP. Therefore, we need to change the IP to the local address IP.

View IP process

1. Find the network connection icon

  1. Find the property, click to enter

  1. Scroll down to find the IP address

  1. modify the code

(URL source)

  1. At this time, run ESP32 again, we can see that the message can be sent normally

  1. At this time, use the background to carry out some message communication tests

  1. Then test the reporting of some messages (just restart esp32)

The source of the test publish-subscribe topic is from the ESP32 reference code

At this point, the ESP32 connection has ended. .

of course

Another connection configuration method, using ip address and port for access

I configured it according to the introduction on the Internet, tested it, and reported an error

At this time, there is no follow-up introduction on the Internet, and then I checked the code and found that the branch that reported the error at this time is the following location

After searching, it is found that the cause of the error is that client -> config -> scheme is empty

So I searched for it and found that its filling position is as follows

Because we are using the tcp method, we found out that when the condition in the box is not satisfied, the above error will occur. Continue to query upwards and find that the calling location of the function is

Going to the upper level, we will return to our initialization function

At this point we will find that the structure variable is indeed not configured, so we configure the variable

At this time, compile and burn the test, the connection is successful, and the sending and receiving are successful.

Since then, the mqtt connection and server setup have been completed. The next article will introduce how to connect to the Alibaba Cloud platform.

Guess you like

Origin blog.csdn.net/m0_45068979/article/details/131684810