The 2 Tasks in the Project are: AppControllerFire_Env AppControllerFire_Mov They sent to the broker 2 different messages with different topics. static void AppControllerFire_Env(void* pvParameters)
{
BCDS_UNUSED(pvParameters);
Retcode_T retcode = RETCODE_OK;
Sensor_Value_T sensorValue;
char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
/* const char *publishDataFormat = "Environmental Data -\n"
"\r\tHumidity : %ld [%%] \n"
"\r\tPressure : %ld [bar] \n"
"\r\tTemperature : %f\xf8 [°C] \n"
"\r\tAcceleration X : %ld [m/s^2] \n"
"\r\tAcceleration Y : %ld [m/s^2] \n"
"\r\tAcceleration Z : %ld [m/s^2] \n";
*/
char *publishDataFormat = "{ "
"\"Humidity\":%ld,"
"\"Pressure\":%ld,"
"\"Temperature\":%f,"
"}";
memset(&sensorValue, 0x00, sizeof(sensorValue));
#if APP_MQTT_SECURE_ENABLE
uint64_t sntpTimeStampFromServer = 0UL;
/* We Synchronize the node with the SNTP server for time-stamp.
* Since there is no point in doing a HTTPS communication without a valid time */
do
{
retcode = SNTP_GetTimeFromServer(&sntpTimeStampFromServer, APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT);
if ((RETCODE_OK != retcode) || (0UL == sntpTimeStampFromServer))
{
printf("AppControllerFire : SNTP server time was not synchronized. Retrying...\r\n");
}
} while (0UL == sntpTimeStampFromServer);
BCDS_UNUSED(sntpTimeStampFromServer); /* Copy of sntpTimeStampFromServer will be used be HTTPS for TLS handshake */
#endif /* APP_MQTT_SECURE_ENABLE */
if (RETCODE_OK == retcode)
{
retcode = MQTT_ConnectToBroker(&MqttConnectInfo, MQTT_CONNECT_TIMEOUT_IN_MS);
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT connection to the broker failed \n\r");
}
}
if (RETCODE_OK == retcode)
{
// retcode = MQTT_SubsribeToTopic(&MqttSubscribeInfo_Env, MQTT_SUBSCRIBE_TIMEOUT_IN_MS);
retcode = RETCODE_OK;
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT subscribe failed \n\r");
}
}
if (RETCODE_OK != retcode)
{
/* We raise error and still proceed to publish data periodically */
Retcode_RaiseError(retcode);
vTaskSuspend(NULL);
}
/* A function that implements a task must not exit or attempt to return to
its caller function as there is nothing to return to. */
while (1)
{
/* Resetting / clearing the necessary buffers / variables for re-use */
retcode = RETCODE_OK;
/* Check whether the WLAN network connection is available */
retcode = AppControllerValidateWLANConnectivity();
if (RETCODE_OK == retcode)
{
retcode = Sensor_GetData(&sensorValue);
}
if (RETCODE_OK == retcode)
{
int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
(long int) sensorValue.RH, (sensorValue.Pressure /= 1000), (sensorValue.Temp /= 1000));
MqttPublishInfo_Env.Payload = publishBuffer;
MqttPublishInfo_Env.PayloadLength = length;
retcode = MQTT_PublishToTopic(&MqttPublishInfo_Env, MQTT_PUBLISH_TIMEOUT_IN_MS);
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT publish failed \n\r");
}
}
if (RETCODE_OK != retcode)
{
Retcode_RaiseError(retcode);
}
vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY_Env);
}
}
static void AppControllerFire_Mov(void* pvParameters)
{
BCDS_UNUSED(pvParameters);
Retcode_T retcode = RETCODE_OK;
Sensor_Value_T sensorValue;
char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
/* const char *publishDataFormat = "Environmental Data -\n"
"\r\tHumidity : %ld [%%] \n"
"\r\tPressure : %ld [bar] \n"
"\r\tTemperature : %f\xf8 [°C] \n"
"\r\tAcceleration X : %ld [m/s^2] \n"
"\r\tAcceleration Y : %ld [m/s^2] \n"
"\r\tAcceleration Z : %ld [m/s^2] \n";
*/
char *publishDataFormat = "{ "
"\"AccelerationX\":%ld,"
"\"AccelerationY\":%ld,"
"\"AccelerationZ\":%ld"
"}";
memset(&sensorValue, 0x00, sizeof(sensorValue));
#if APP_MQTT_SECURE_ENABLE
uint64_t sntpTimeStampFromServer = 0UL;
/* We Synchronize the node with the SNTP server for time-stamp.
* Since there is no point in doing a HTTPS communication without a valid time */
do
{
retcode = SNTP_GetTimeFromServer(&sntpTimeStampFromServer, APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT);
if ((RETCODE_OK != retcode) || (0UL == sntpTimeStampFromServer))
{
printf("AppControllerFire : SNTP server time was not synchronized. Retrying...\r\n");
}
} while (0UL == sntpTimeStampFromServer);
BCDS_UNUSED(sntpTimeStampFromServer); /* Copy of sntpTimeStampFromServer will be used be HTTPS for TLS handshake */
#endif /* APP_MQTT_SECURE_ENABLE */
if (RETCODE_OK == retcode)
{
retcode = MQTT_ConnectToBroker(&MqttConnectInfo, MQTT_CONNECT_TIMEOUT_IN_MS);
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT connection to the broker failed \n\r");
}
}
if (RETCODE_OK == retcode)
{
// retcode = MQTT_SubsribeToTopic(&MqttSubscribeInfo_Mov, MQTT_SUBSCRIBE_TIMEOUT_IN_MS);
retcode = RETCODE_OK;
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT subscribe failed \n\r");
}
}
if (RETCODE_OK != retcode)
{
/* We raise error and still proceed to publish data periodically */
Retcode_RaiseError(retcode);
vTaskSuspend(NULL);
}
/* A function that implements a task must not exit or attempt to return to
its caller function as there is nothing to return to. */
while (1)
{
/* Resetting / clearing the necessary buffers / variables for re-use */
retcode = RETCODE_OK;
/* Check whether the WLAN network connection is available */
retcode = AppControllerValidateWLANConnectivity();
if (RETCODE_OK == retcode)
{
retcode = Sensor_GetData(&sensorValue);
}
if (RETCODE_OK == retcode)
{
int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
sensorValue.Accel.X, sensorValue.Accel.Y, sensorValue.Accel.Z);
MqttPublishInfo_Mov.Payload = publishBuffer;
MqttPublishInfo_Mov.PayloadLength = length;
retcode = MQTT_PublishToTopic(&MqttPublishInfo_Mov, MQTT_PUBLISH_TIMEOUT_IN_MS);
if (RETCODE_OK != retcode)
{
printf("AppControllerFire : MQTT publish failed \n\r");
}
}
if (RETCODE_OK != retcode)
{
Retcode_RaiseError(retcode);
}
vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY_Mov);
}
}
... View more