loop MQTT publish message after timer ends, event loop issues

mattECE22
Posts: 2
Joined: Fri Jan 14, 2022 11:54 am

loop MQTT publish message after timer ends, event loop issues

Postby mattECE22 » Fri Jan 14, 2022 7:00 pm

I am going off the example given for MQTT TCP and have modified it to publish messages to my PC which works fine, however I will need to eventually read sensor data and publish it to the PC every 5 to 10 seconds. I have been unable to successfully publish outside of the given example handler (as that is only triggered once the ESP32 has received data or the connection/subscriptions have changed, not for publishing exclusively). I figured I will need an event loop and a unique ID & event Base for my timer, and register this event with the mqtt event loop using the function: esp_mqtt_client_register_event.

I have created a timer using esp_timer_create and in the timer callback want to post an event to the event loop using esp_event_post. This should trigger the MQTT event handler I wrote: my_mqtt_event_handler, which should publish a basic string to the PC.

Is it possible to include a new event for MQTT event loops so I can publish every few seconds? My handler does not seem like it is being called and using the given events from MQTT_EVENT_BEFORE_CONNECT through MQTT_EVENT_ERROR does not seem like it will work for unprompted publishing.

here is my C file, everything else in the folder is the same as the TCP example. Using ESP32-DOIT DevKit V1 off amazon. Using Mosquitto on Linux to subscribe to messages published by the ESP32.

app_main.c is attached below


ESP-IDF output once connected to wifi is this and it repeats the loop with no publish:

Code: Select all

I (5630) MQTT_EXAMPLE: Other event id:7
I (5790) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED
I (5800) MQTT_EXAMPLE: sent publish successful, msg_id=45308
I (5800) MQTT_EXAMPLE: sent publish successful, msg_id=0
I (5810) MQTT_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=45308
I (10630) MQTT_EXAMPLE: TIMER_EVENTS:TIMER_EVENT_EXPIRED: posting to default loop
I (15630) MQTT_EXAMPLE: TIMER_EVENTS:TIMER_EVENT_EXPIRED: posting to default loop
I (20630) MQTT_EXAMPLE: TIMER_EVENTS:TIMER_EVENT_EXPIRED: posting to default loop
Attachments
app_main.c
(9.1 KiB) Downloaded 157 times

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: loop MQTT publish message after timer ends, event loop issues

Postby ESP_Sprite » Sat Jan 15, 2022 2:06 am

I have been unable to successfully publish outside of the given example handler (as that is only triggered once the ESP32 has received data or the connection/subscriptions have changed, not for publishing exclusively).
That does not match my experience... as long as you're in a task (not an ISR) with enough stack etc, you should be able to publish data from anywhere. The example simply puts it in the event handler as that is convenient, not because that is necessary.

mattECE22
Posts: 2
Joined: Fri Jan 14, 2022 11:54 am

Re: loop MQTT publish message after timer ends, event loop issues

Postby mattECE22 » Sat Jan 15, 2022 3:25 am

I think I solved the issue, which avoids using the new event loop at all and has an independent publish method. The other "client" was not working so I made the client inside the example handler a global variable and wrote a publish method that I can now call from inside the timer callback. Let me know if this is a poor solution, my timing constraints are not life & death so if it skews the timer a small amount that is OK and it seems to work:

Code: Select all

//////////////
esp_mqtt_client_handle_t get_mqtt_client_handle(void)
{
    return global_client;
}
/////////////
static void publish(esp_mqtt_client_handle_t client,char message[], int length){
    int msg_id;
    msg_id = esp_mqtt_client_publish(client, "/topic/qos0", message, 0, 0, 0);
    ESP_LOGI(TAG, "sent publish, msg_id=%d", msg_id);
}

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 124 guests