Set Up the AVR Iot WG Development Board as an MQTT Client

Last modified by Microchip on 2023/11/10 11:11

This tutorial focuses on how to set up the AVR® IoT WG development board as a Message Queuing Telemetry Transport (MQTT) client. To do that, a broker must be available. For other functionalities, we can use more applications, like MyMQTT, MQTT Explorer and Wireshark.

If you already have a broker set up, know how to add more clients to the broker, and are only interested in the MPLAB® X IDE portion, you can skip this section and move on to MyMQTT.

The following figure shows the environment we are going to use:

Mosquitto Eclipse

MyMQTT app, the MQTT Explorer program and our AVR-IoT WG are going to be our clients. Eclipse Mosquitto™ will be our broker. 

We will be discussing subscribing to "topics" which refers to the process of receiving data from a specific topic on the MQTT broker. MQTT is a messaging protocol that is commonly used in IoT applications to send and receive data between devices.

In the context of the AVR-IoT WG board, subscribing to a topic allows the board to receive data from a specific source, such as a sensor or another device. For example, if you have a temperature sensor connected to the board, you can subscribe to a topic that is publishing temperature data, and the board will receive that data and be able to use it in your application. To subscribe to a topic on the AVR-IoT WG board, you will need to use the MQTT client library that is provided with the board. The library allows you to connect to an MQTT broker, subscribe to topics, and publish data to topics as needed.

Back to Top

Workflow

1. Subscribe

Clients should subscribe to some topics. For example, MyMQTT and MQTT Explorer could send each of them a subscribe packet with “topic1” to the broker, Eclipse Mosquitto, to subscribe to “topic1”

2. Publish Message

A client should publish something, i.e. send a message to the broker, Eclipse Mosquitto. For example, MyMQTT app could publish the “hello” message in “topic1”

3. Receive Message

The clients subscribed to that topic should get the message. For example, If MQTT Explorer and MyMQTT are subscribed to “topic1”, they will get the “hello” message.

Here is what the block diagram looks like for the above workflow example:

Mosquitto Eclipse Example

Back to Top

Mosquitto

Installation

Go to the Mosquitto web site.

In the Windows section, download Mosquitto and execute the EXE file to install it.

Back to top


Mosquitto.config

Go to the file where you installed it and right-click on mosquitto.config then select Open with > Notepad++.

That is the file we will execute. All lines are commented with “#” for now, so it is empty. Go to the end of the file, write, and save the following code:

listener 9001
protocol websockets
listener 1883
allow_anonymous true
password_file C:\Program Files\mosquitto\pwfile.example
  • 1st line opens port 9001 as listener.
  • The 2nd line establishes port 9001 will use WebSocket protocol.
  • The 3rd line opens port 1883 as listener.
  • The 4th line allows connection to clients without a registered username (i.e., it’s not in #pwfile.example).
  • The 5th line sets pwfile.example as the file that contains what usernames are registered, and what are the passwords for each of them.

A common mistake in this code is to add comments or spaces in the code. In this program language, every character is considered. Also, be careful not to add any spaces in the code. If you make a comment, it will need to be the whole line. Do not make a comment after a coding line since the program will not understand it and it will fail.

The broker will open 9001 for WebSockets, and port 1883, where no username or password is needed. You can write “allow_anonymous false” instead of “allow_anonymous true”, so only users with identified usernames and passwords will be allowed. We will see the utility of pwfile.example in the next section.

Back to Top


pwfile.example

Go to Mosquitto’s location. Right-click on pwfile.example, then select Open with > Notepad++. You should see usernames and their respective encrypted passwords.

pwfile.example in Notepad


By default, “roger”, “sub_client” and “pub_client” are the allowed usernames. The password for each of them is “password”, but it is encrypted.

Delete those lines or add a new username/password. For example, here the added username is “J.Lo”, and its password is J.Lo's password.

J.Lo's Password in Notepad


Save the file, close it and your Mosquitto broker should be able to connect using other usernames/passwords.

Back to Top


Execute Mosquitto as a Broker

Initialize Mosquitto

Open Command Prompt on your computer. Usually, this can be done by clicking on the Windows key and writing “Command Prompt”.

Open the directory where mosquitto has been installed. Usually, that can be done with the next line:

cd C:\Program Files\mosquitto


Then execute this line to execute mosquitto.conf, which should initialize the Eclipse Mosquitto broker:

mosquitto -v -c mosquitto.conf

Now, you can see the initialization of the broker in the command prompt.

Mosquitto Running
You can also execute mosquitto.config file straight away, but you will not see what events happen in it.


Now open another Command Prompt and the following command to encrypt J.Lo's password:

mosquitto_passwd -U pwfile.example


For closing Mosquitto, press Ctrl+C (for Windows) on the Command Prompt, or just close it.

Back to Top


Mosquitto Already Running

If when executing mosquitto -v -c mosquitto.conf you get this error:

Mosquitto Error shown in Command Promp

It can also say that is not possible to open a port more than once. In both cases, it means that Mosquito was already running in the background. To fix this condition, open Task Manager as administrator and close Mosquitto.

Back to Top

Testing Mosquitto

Open a command prompt and initialize mosquito, as explained in a previous section. Then, execute this line in the command prompt:

mosquitto_sub -h "localhost" -u roger -P password -t test/topic

Now you have a command prompt that is a client subscribed to test/topic. The explanation for that line is:

CommandExplanation
mosquitto_subcommand for subscribing to the Mosquitto broker
-h “localhost”-h means the next word is the IP of the Mosquitto broker. If we write "localhost" instead, it means the IP address is the same as the computer where we are executing the line
-u rogerroger is the username. This part is not strictly needed
-P passwordpassword is the used password. This part is not strictly needed
-t test/topicchoose the topic you want to subscribe to

Now, open another command prompt, and execute this line:

mosquitto_pub -h “localhost” -u roger -P password -t test/topic -m "hello world"

The structure of the line is the same. This should make the other command read the message that this command is publishing.

See the accompanying image showing what it should look like.

MQTT Environment

Back to Top