Established Published Message and Frequency in MQTT

Last modified by Microchip on 2023/11/09 09:03

Publishing will happen periodically so we just need to adjust what to publish and how often.

Publish the Same Message

The message to be published is called mqttPublishMsg[]. Its declaration is located at the top in mqtt_example.c, same file where the app_mqttExampleInit(); and app_mqttScheduler(message); functions are.
You can modify it.

MQQT Publish

You can see the sent message with MyMQTT in the Dashboard section if you are subscribed to mchp/iot/events. Or you should be able to see the message in MQTT Explorer if you prefer.

MQTT Explorer

Back to Top

Publish Different Messages

For changing the value in mqttPublishMsg[], the first thing that you will have to do is delete the “const” word at mqttPublishMsg[]’s declaration, at the top of mqtt_example.c.

MQTT const equals "mchp payload"

In mqtt_example.c, in the definition of app_mqttScheduler() you can include an argument for app_mqttScheduler(). It would be the pointer for the message that you want to transmit.

Then, create a function that copies the new message into mqttPublishMsg. See the accompanying example code.

Function that copies the new message into mqttPublishMsg

void equalize_arrays(uint8_t *array1 ,uint8_t *array2){
   while (*array2 != '\0')            //While string does not equal Null character.
   {
   *array1 = *array2;    //equalize message to be sent with desired message
   array2++;                         // increment pointer
   array1++;
    }                       //if array1 was longer than array, we need to delete the strings that remain
   while (*array1 != '\0'){ //check if there are extra strings in array1   
       *array1='\0';        //delete extra string
       array1++;            //go for the next one
   }
}

Finally, add a uint8_t array value to the argument of app_mqttScheduler when you execute it in main.c.

uint8_t array value added to the argument of app_mqttScheduler
This way, every time a message is published, the payload would be the last argument that was inserted in app_mqttScheduler() when being called. This won’t make the message change over time unless you create a program to change the inserted argument in app_mqttScheduler().

If everything has been properly done, when executing it, the byte number in the command prompt should change:

Byte number changed in the command prompt

Back to Top

Publish to Different Topics

The first thing that we will need to do would be to declare two new variables that contain the value for the topics. To do that, add these two lines at the top of mqtt_example.c

uint8_t topic1[]="topic1";
uint8_t topic2[]="topic2";

You can substitute “topic1” and “topic2” for any values that you want.

If we want to publish to different topics, supposing that we completed the previous step, it is recommended to add one or more arguments to app_mqttScheduler().

Add arguments to app_mqttScheduler().
 

Substitute arguments for app_mqttScheduler

If you want, you can add two more entry arguments for app_mqttScheduler, topic1 and topic 2, but it’s not usual to need to change the topic that you publish to.

If you didn’t complete the previous section, you still can do this part, but with this method, the values of the messages are fixed and cannot be changed while the program is running. Just delete the “const” word at mqttPublishMsg[]’s declaration at the top of mqtt_example.c and add these 2 lines next to it:

uint8_t message_pointer1[]="loco1";

uint8_t message_pointer2[]="loco2";

Two lines added
 

Regardless of whether you completed the Publish Different Messages section or not, delete the word “const” in the declaration of mqttPublishTopic[], so we can redefine our publish topic.

Delete const in the Function Declaration

Finally, inside of the APP_STATE_TLS_CONNECTED state, substitute the line:

app_buildPublishPacket();

for these new lines:

equalize_arrays(mqttPublishTopic, topic1);          //set the topic for the first message
equalize_arrays(mqttPublishMsg,message_pointer1);   //set the second message
app_buildPublishPacket();                           //build the packet for the first message
MQTT_TransmissionHandler(mqttConnnectionInfo);      //send the first message
       
equalize_arrays(mqttPublishTopic, topic2);          //the 4 lines are as the previous ones,
equalize_arrays(mqttPublishMsg, message_pointer2);  // but for the second message
app_buildPublishPacket();
MQTT_TransmissionHandler(mqttConnnectionInfo);         

Substitute this line:

Substitute this Line

For these next eight lines:

Substitute Function

These eight lines are enough to send messages, so any other MQTT_TransmissionHandler() or equalize_arrays(), like the placed equalize_arrays() in the previous section are useless for copying the topic or the payload, even if they are not harmful.

For reading the messages sent in MyMQTT, you should subscribe to “topic1” and “topic2”. What you should see is how two different messages are periodically sent almost at the same time. It’s the same for MQTT Explorer, but you don’t need to subscribe:

MQTT Explorer showing topic1 and topic2

You should see this in the command prompt:

Command Prompt

Back to Top

Publishing Speed

The publish speed depends on the created timer in app_mqttExampleInit() function in mqtt_example.c, created by this line:

 timeout_create(&appMQTTPublishTimer, ((CFG_MQTT_CONN_TIMEOUT - 1) * SECONDS));

CFG_MQTT_CONN_TIMEOUT is the keepalive timer. This is, the time MQTT connection will stay alive if no message is sent or received. That’s why, as we said in the section Run the Basic Program, if you change the Keep Alive value in MPLAB® Code Configurator (MCC), the period for publishing will change.

To change the publishing speed, you can:

Change the CFG_MQTT_CONN_TIMEOUT value to no less than 2. The publishing period would be the value you insert in CFG_MQTT_CONN_TIMEOUT minus 1 second.

In the definition of app_mqttExampleInit() in mqtt_example.c, change the second argument of timeout_create. Substitute (CFG_MQTT_CONN_TIMEOUT - 1) * SECONDS) with the milliseconds you want to be the publishing period. Do the same in the definition of appCheckMQTTPublishTimeout(), in “return ((CFG_MQTT_CONN_TIMEOUT - 1) * SECONDS);”

Use appCheckMQTTPublishTimeout() function to send the message whenever you want. You may want to create a small code around it. Basic example of it would be to add it right after the “case APP_STATE_TLS_CONNECTED” line.

You can call app_buildPublishPacket() in app_mqttScheduler whenever you want to publish. In the original code, it only happens when appMQTTPublishTimeoutOccured == true, so write it outside of this condition. A basic example of this would be to delete or comment the if(appMQTTPublishTimeoutOccured == true) condition in case APP_STATE_TLS_CONNECTED. See the next image:

case PUBLISHING lines Commented out

Remember that every time you execute that line, you also will need to execute MQTT_TransmissionHandler(mqttConnnectionInfo). Otherwise, the program may crash.

You will need to adjust more things if you use the third or fourth method. It is recommended to use the first and second methods first.
If the publishing speed is faster than what the device can take, a lot of publishing will happen and then the device will stop working. That’s why, for reliability reasons, if you just change the arguments of timeout_create(), the maximum possible frequency will be one message per second. With the third and fourth methods, if you publish once every time app_mqttScheduler() is executed, you should be safe.

Back to Top