When I try to connect two different tasks to the same Broker ("Mosquitto"), the connection of the first task launched collapse when the program activate the second task.
Both tasks are able to establish a connection with the Broker and run properly if launched alone.
In the console appears the following code:
INFO | XDK DEVICE 1: WLANPersonalWPA2Connect : Connected to WPA network successfully.
INFO | XDK DEVICE 1: MQTT_ConnectToBroker : Failed to connect MQTT
INFO | XDK DEVICE 1: AppControllerFire : MQTT connection to the broker failed Env
INFO | XDK DEVICE 1: retcode: 2569478261
INFO | XDK DEVICE 1: Error in XDK110 Application package.
INFO | XDK DEVICE 1: Package ID: 153
INFO | XDK DEVICE 1: Module ID: 39
INFO | XDK DEVICE 1: Severity code: 2
INFO | XDK DEVICE 1: Error code: 117
Could you please help me.
Hello Angelo,
Sorry that I do not understand your question clearly. What do you mean by connection from 2 task ?
In MQTT communication, XDK device acts as client ; whereas MQTT broker acts as server. Connection is initiated from client and accepted by broker. After successful connection, XDK sensor data can be sent to the broker.
The 2 Tasks in the Project are:
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);
}
}
Hello AngeloMiele94,
Appreciate your interest in using XDK MQTT application.
From your console output,
More information is required to support you further,