Publish on AWS Iot topic always return NETWORK_DISCONNECTED_ERROR when aws connection is create in other thread

renato.dantas
Posts: 1
Joined: Thu Jun 24, 2021 4:27 pm

Publish on AWS Iot topic always return NETWORK_DISCONNECTED_ERROR when aws connection is create in other thread

Postby renato.dantas » 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.

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, &paramsTopicTemperature);
    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, &paramsTopicHumidity);
    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);
}
And method used by my endpoint to get state of mqtt:

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;
}
And log output:

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
I think my problem is client access from a task in another thread, but I'm not even sure how to solve it.

Thanks!

HimalayaXia
Posts: 1
Joined: Wed Jul 07, 2021 8:57 pm

Re: Publish on AWS Iot topic always return NETWORK_DISCONNECTED_ERROR when aws connection is create in other thread

Postby HimalayaXia » Wed Jul 07, 2021 9:06 pm

Hi, I was looking for solution to solve my problem then I saw your post. I was having the same issue as yours when publishing to aws but failed with NETWORK_DISCONNECTED_ERROR. What I do is I want to publish back a message after I received one in my iot_subscribe_callback_handler(). I wonder if you have solved your issue since we might have a similar issue here. Thanks a lot.

My code looks like this:
  1. void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen,
  2.                                     IoT_Publish_Message_Params *params, void *pData) {
  3.     ESP_LOGI(TAG, "Subscribe callback");
  4.     ESP_LOGI(TAG, "%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *)params->payload);
  5.     if (atoi((char *)params->payload) == 1){
  6.         ESP_LOGI(TAG, ">>> Turn on LAMP 1");
  7.         gpio_set_level(LAMP1, 1);// light successfully turned on here :)
  8.         lamp1_state = 1;
  9.         changed = 1;
  10.        
  11.         char cPayload[100];
  12.         IoT_Error_t rc;
  13.         IoT_Publish_Message_Params paramsQOS0;
  14.         sprintf(cPayload, "%s : %d ", "state", lamp1_state);
  15.  
  16.         paramsQOS0.qos = QOS0;
  17.         paramsQOS0.payload = cPayload;
  18.         paramsQOS0.isRetained = 0;
  19.         paramsQOS0.payloadLen = strlen(cPayload);
  20.         rc = aws_iot_mqtt_publish(&pClient,topicName,topicNameLen,&paramsQOS0);
  21.         ESP_LOGI(TAG,"rc light is : %d",rc);     // error here, rc returns -13 which is NETWORK_DISCONNECTED_ERROR  
  22.     }
  23.  
  24. void aws_iot_task(void *param) {
  25.         //wifi initialized and connected, aws-iot-mqtt initialized and connected to aws
  26.         //I am only able to publish message here but I want to do it in my call_back_handler instead
  27.         .....
  28. }

Who is online

Users browsing this forum: and3rson, Bing [Bot] and 122 guests