MQTT Publish Issue

Amorphous
Posts: 7
Joined: Mon Nov 21, 2022 3:09 pm

MQTT Publish Issue

Postby Amorphous » Mon Nov 21, 2022 3:17 pm

Here is my code :

Code: Select all

int mqtt_publish(char message[])
{
	int msg_id = 0;
    ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
    mqtt_topic_t *mqtt_topic = get_mqtt_topic();
    msg_id = esp_mqtt_client_publish(client,mqtt_topic->topic, message, 0, 1, 0);
    ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
    return msg_id;
}
Where the mqtt_topic_t struct is :

Code: Select all

typedef struct mqtt_topic {
	char topic[200];
} mqtt_topic_t;
I am saving the MQTT Topic from the HTTP server post request like this :

Code: Select all

esp_err_t echo_post_handler(httpd_req_t *req)
{
	nvs_handle handle;
	esp_err_t esp_err;


    char buf[200];

    int ret, remaining = req->content_len;
    if(topic_rcvd == 0){
       	ESP_LOGI(TAG, "TOPIC RECVD IS 0");
       }
    while (remaining > 0) {
        /* Read the data for the request */
        if ((ret = httpd_req_recv(req, buf,
                        MIN(remaining, sizeof(buf)))) <= 0) {
            if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
                /* Retry receiving if timeout occurred */
                continue;
            }
            return ESP_FAIL;
        }

        /* Send back the same data */
        httpd_resp_send_chunk(req, buf, ret);
        remaining -= ret;

        /* Log data received */
        ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
        ESP_LOGI(TAG, "%.*s", ret, buf);
        ESP_LOGI(TAG, "====================================");
        http_server_send_message(HTTP_MSG_RECVD_TOPIC);
        topic_rcvd = 1;

    }

    esp_err_t request_sent = httpd_resp_send_chunk(req, NULL, 0);
    // End response

    	esp_err = nvs_open(mqtt_client, NVS_READWRITE, &handle);

    	if (esp_err != ESP_OK)
		{
			printf("Error in Saving (%s) \n", esp_err_to_name(esp_err));
			return esp_err;
		}
		//Set TOPIC
		esp_err = nvs_set_blob(handle, "topic", buf, 200);

		if (esp_err != ESP_OK)
		{
			printf("Error setting SSID (%s) \n", esp_err_to_name(esp_err));
			return esp_err;
		}

		esp_err = nvs_commit(handle);

		if (esp_err != ESP_OK)
		{
			printf("Error setting TOPIC to NVS (%s) \n", esp_err_to_name(esp_err));
			return esp_err;
		}

		nvs_close(handle);
		ESP_LOGI(TAG, "SAVED TOPIC %s", buf);



    return ESP_OK;
}
Loading the MQTT Topic from memory like this :

Code: Select all

bool load_mqtt_topic(void){

	nvs_handle handle;
	esp_err_t esp_err;
	ESP_LOGI(TAG, "Loading MQTT Client from Flash");

	if (nvs_open(mqtt_client, NVS_READONLY, &handle) == ESP_OK)
	{
		mqtt_topic_t *topic = get_mqtt_topic();

		if(topic == NULL)
		{
			topic = (char*)malloc(sizeof(mqtt_topic_t));
		}
		memset(topic, 0x00, sizeof(mqtt_topic_t));

		size_t mqtt_size = sizeof(mqtt_topic_t);
		uint8_t *mqtt_buff = (uint8_t*)malloc(sizeof(mqtt_topic_t));
		memset(mqtt_buff, 0x00, sizeof(mqtt_topic_t));

		//LOAD SSID

		mqtt_size = sizeof(mqtt_topic_t);
		esp_err = nvs_get_blob(handle, "topic", mqtt_buff, &mqtt_size);

		if(esp_err != ESP_OK)
		{
			free(mqtt_buff);
			printf("NO MQTT CLIENT FOUND \n");
			return false;
		}

		memcpy(topic, mqtt_buff, mqtt_size);



		free(mqtt_buff);
		nvs_close(handle);

		printf("TOPIC %s", topic->topic);
		return topic->topic[0] != '\0';
	}
	else
	{
		return false;
	}
}
When I call the mqtt_publish message on button press :

Code: Select all

		
				ESP_LOGI(TAG, "Button Press");
				printf("Message Published: %d", mqtt_publish("false"));
			
The function returns the following:
E (15511) MQTT_CLIENT: Publish message cannot be created
I (15514) MQTT_EXAMPLE: sent publish successful, msg_id=-1
If I change the mqtt_publish to this :

Code: Select all

msg_id = esp_mqtt_client_publish(client,"Trial Topic", message, 0, 1, 0);
Then it works fine. So it seems that the mqtt_topic->topic is causing the issue.

It may be that the mqtt_topic->topic variable is not a string but I don't see how to resolve this. Would appreciate any help.

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: MQTT Publish Issue

Postby chegewara » Wed Nov 23, 2022 5:53 pm

How about printing topic value just before publish?
Also you should check strlen, because there may be some non printable characters.

Who is online

Users browsing this forum: Google [Bot] and 132 guests