Publish on AWS Iot topic always return NETWORK_DISCONNECTED_ERROR when aws connection is create in other thread
Posted: Thu Jun 24, 2021 4:29 pm
Hello! I have a small problem with mqtt event publish!
The basic flux is: read sensor data and publish it on topic with 1 minute of interval.
The connection with aws is create after received a event of get ip address (wifi is connected with success) and after connected to aws, create a task that read sensor data and publish it. This app has a HTTP server that I configure a endpoint that return MQTT status, connected or disconnected, this endpoint work fine and return the correct state. But into my sensor task MQTT Client is always NETWORK_DISCONNECTED_ERROR. Follow task code.
And method used by my endpoint to get state of mqtt:
And log output:
I think my problem is client access from a task in another thread, but I'm not even sure how to solve it.
Thanks!
The basic flux is: read sensor data and publish it on topic with 1 minute of interval.
The connection with aws is create after received a event of get ip address (wifi is connected with success) and after connected to aws, create a task that read sensor data and publish it. This app has a HTTP server that I configure a endpoint that return MQTT status, connected or disconnected, this endpoint work fine and return the correct state. But into my sensor task MQTT Client is always NETWORK_DISCONNECTED_ERROR. Follow task code.
Code: Select all
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_system.h>
#include <string.h>
#include <esp_err.h>
#include <esp_log.h>
#include <aws_iot_mqtt_client_interface.h>
#include "sensors.h"
static const char *TAG = "TASK-HUMIDITY-TEMPERATURE";
void publish_temperature(AWS_IoT_Client *client, float temperature) {
const char *TOPIC_TEMPERATURE = "temperature/esp32";
const int TOPIC_TEMPERATURE_LEN = strlen(TOPIC_TEMPERATURE);
char temperaturePayload[100];
IoT_Publish_Message_Params paramsTopicTemperature;
paramsTopicTemperature.qos = QOS0;
paramsTopicTemperature.payload = (void *) temperaturePayload;
paramsTopicTemperature.isRetained = 0;
sprintf(temperaturePayload, "%.1f", temperature);
IoT_Error_t rc = aws_iot_mqtt_publish(client, TOPIC_TEMPERATURE, TOPIC_TEMPERATURE_LEN, ¶msTopicTemperature);
if (rc != SUCCESS) {
ESP_LOGW(TAG, "Topic of humidity does not publish: %d", rc);
}
}
void publish_humidity(AWS_IoT_Client *client, float humidity) {
const char *TOPIC_HUMIDITY = "humidity/esp32";
const int TOPIC_HUMIDITY_LEN = strlen(TOPIC_HUMIDITY);
char humidityPayload[100];
IoT_Publish_Message_Params paramsTopicHumidity;
paramsTopicHumidity.qos = QOS0;
paramsTopicHumidity.payload = (void *) humidityPayload;
paramsTopicHumidity.isRetained = 0;
sprintf(humidityPayload, "%.2f", humidity);
IoT_Error_t rc = aws_iot_mqtt_publish(client, TOPIC_HUMIDITY, TOPIC_HUMIDITY_LEN, ¶msTopicHumidity);
if (rc != SUCCESS) {
ESP_LOGW(TAG, "Topic of temperature does not publish: %d", rc);
}
}
void aws_mqtt_sensor_humidity_temperature_task(void *param) {
AWS_IoT_Client client;
aws_iot_mqtt_get_client_state(&client);
while (1) {
IoT_Error_t rc = aws_iot_mqtt_yield(&client, 1000);
if (NETWORK_ATTEMPTING_RECONNECT == rc) {
// If the client is attempting to reconnect we will skip the rest of the loop.
continue;
}
float temperature, humidity;
get_humidity_temperature_data(&humidity, &temperature);
ESP_LOGI(TAG, "Temperature: %.1f°C, Humidity: %.2f%%", temperature, humidity);
publish_temperature(&client, temperature);
publish_humidity(&client, humidity);
vTaskDelay(pdMS_TO_TICKS(1000 * 60 * 1));
}
}
void humidity_temperature_task() {
xTaskCreate(aws_mqtt_sensor_humidity_temperature_task, TAG, configMINIMAL_STACK_SIZE * 8, NULL, 2, NULL);
}
Code: Select all
char *get_mqtt_status() {
AWS_IoT_Client client;
aws_iot_mqtt_get_client_state(&client);
char *value;
switch (client.clientStatus.clientState) {
case CLIENT_STATE_INVALID:
value = "Invalid to connect";
break;
case CLIENT_STATE_INITIALIZED:
value = "Not connected";
break;
case CLIENT_STATE_CONNECTING:
value = "Try connect...";
break;
case CLIENT_STATE_DISCONNECTING:
value = "Stay disconnecting...";
break;
case CLIENT_STATE_DISCONNECTED_ERROR:
value = "Disconnected with error";
break;
case CLIENT_STATE_DISCONNECTED_MANUALLY:
value = "Disconnected";
break;
case CLIENT_STATE_PENDING_RECONNECT:
value = "Try reconnect...";
break;
default:
value = "Connected";
break;
}
return value;
}
Code: Select all
I (800) WIFI-MAIN: Set server to Wi-Fi provisioning
I (800) WIFI-CONFIGURATION: Already provisioned, starting Wi-Fi STA
I (810) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (910) wifi:mode : sta (30:ae:a4:96:d0:60)
I (910) wifi:enable tsf
I (920) SENSORS-AHT10: Start configure of AHT10
I (930) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
I (930) wifi:state: init -> auth (b0)
I (940) wifi:state: auth -> assoc (0)
I (950) wifi:state: assoc -> run (10)
I (1270) SENSORS-AHT10: AHT10 configured with success
I (1270) SENSORS-AHT10: Sensor calibrated
W (1470) wifi:<ba-add>idx:0 (ifx:0, 30:93:bc:c6:19:0c), tid:5, ssn:0, winSize:64
I (1480) wifi:connected with 2.4_2906, aid = 3, channel 11, BW20, bssid = 30:93:bc:c6:19:0c
I (1490) wifi:security: WPA2-PSK, phy: bgn, rssi: -27
I (1490) wifi:pm start, type: 1
W (1490) LISTENER-WIFI: Invalid event not mapped to this handle
I (1550) wifi:AP's beacon interval = 102400 us, DTIM period = 1
W (2690) wifi:<ba-add>idx:1 (ifx:0, 30:93:bc:c6:19:0c), tid:0, ssn:0, winSize:64
I (3680) LISTENER-IP: Connected with IP Address:192.168.0.34
I (3680) LISTENER-WIFI-GOT-IP-MQTT-CONNECT: Configure mqtt on aws with certs from classpath
I (3680) MQTT: Connecting to AWS...
I (5470) MQTT: Connected with success
I (5470) esp_netif_handlers: sta ip: 192.168.0.34, mask: 255.255.255.0, gw: 192.168.0.1
I (5550) TASK-HUMIDITY-TEMPERATURE: Temperature: 22.0°C, Humidity: 52.81%
W (5550) TASK-HUMIDITY-TEMPERATURE: Topic of humidity does not publish: -13
W (5550) TASK-HUMIDITY-TEMPERATURE: Topic of temperature does not publish: -13
I (65640) TASK-HUMIDITY-TEMPERATURE: Temperature: 22.0°C, Humidity: 52.36%
W (65640) TASK-HUMIDITY-TEMPERATURE: Topic of humidity does not publish: -13
W (65640) TASK-HUMIDITY-TEMPERATURE: Topic of temperature does not publish: -13
I (125730) TASK-HUMIDITY-TEMPERATURE: Temperature: 22.0°C, Humidity: 52.35%
W (125730) TASK-HUMIDITY-TEMPERATURE: Topic of humidity does not publish: -13
W (125730) TASK-HUMIDITY-TEMPERATURE: Topic of temperature does not publish: -13
Thanks!