loop MQTT publish message after timer ends, event loop issues
Posted: 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:
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