MCC Generated Example Background
Explanation of the MCC-Generated Example
Inside of MPLAB® Code Configurator (MCC) generated files, there’s a folder called examples, which contains mqtt_example. That’s the file that contains the functions for our example, including
- app_mqttExampleInit()
- app_mqttScheduler().
It was created by ticking the Generate Example box in MCC’s MQTT window.
In our example, MQTT initializes with app_mqttExampleInit(); and then runs with app_mqttScheduler(); Depending on the stage the development is on, if the broker or the Wi-Fi® is reset, you may need to restart the AVR-IoT WG. This can be done by disconnecting and connecting the device again and resetting its power supply.
app_mqttExampleInit()
This function initializes all functions required for app_mqttScheduler().
- Initializes functions for WINC1510, Wi-Fi connection, and MQTT protocol.
- Creates a connect packet, which will later be sent with app_mqttScheduler() to establish a connection with the broker.
- Initializes RTC, the timer that we set up in MCC. It creates a timer to set the frequency of publishing.
app_mqttScheduler()
This function does different things depending on the value of the appState variable.
- If the value of appState is APP_STATE_INIT, the executed code will be the one inside of case APP_STATE_INIT:{ }.
- If the value of appState is APP_STATE_STA_CONNECTING, the executed code will be the one inside of case APP_STATE_STA_CONNECTING:{ }. And the same for every case.
When the program is running, app_mqttScheduler() will be continuously running.
The first time, appState will equal APP_STATE_INIT, so the code in case APP_STATE_INIT: { } will be executed. This code initializes part of what our program needs. This will happen several times until everything that needs to be initialized in APP_STATE_INIT has been initialized.
- Then, it continues to the next state, APP_STATE_STA_CONNECTING, and constantly executes that state until APP_STATE_STA is connected.
- Then, appState takes the value of the next state, APP_STATE_STA_CONNECTED. Every state initializes something and then goes to the next state until it gets to the APP_STATE_TLS_CONNECTED state.
In the APP_STATE_TLS_CONNECTED case, the MQTT_ReceptionHandler() and MQTT_TransmissionHandler() functions are executed. Those functions are based on states too.
- What MQTT_TransmissionHandler() does will depend on any previously created packet. If the device didn’t create any package, then this function will not do anything. If there is a previously created package, the function will send it, and delete the packet, so the same message is not sent again.
- What MQTT_ReceptionHandler() does will depend on any received packet. If the device did not get any packet from the outside, that function will do nothing.
In this example, when MQTT_TransmissionHandler(mqttConnnectionInfo) happens, the publish packet has been created 5 lines above, though app_buildPublishPacket().app_buildPublishPacket() will only occur if (appMQTTPublishTimeoutOccured == true), this is when the timer created in app_mqttExampleInit() is finished.