MQTT data_publish() in timer handler !!

Hi,

Using the "mqtt_simple" example I found that the data_publish () function does not work for a timer handler.

In the case of the example below, when the data_publish () function is called by the timer, a memory violation occurs and the system is reset.

How can I resolve this issue where the data_publish () function can be called by the timer?

Regards,
Neculai


s

static void button_handler(uint32_t button_states, uint32_t has_changed)
{
      if (has_changed & button_states &
          BIT(CONFIG_BUTTON_EVENT_BTN_NUM - 1)) {


      int ret;

      ret = data_publish(&client,                               // OK, Working well !
                        MQTT_QOS_1_AT_LEAST_ONCE,
                        CONFIG_BUTTON_EVENT_PUBLISH_MSG,
                        sizeof(CONFIG_BUTTON_EVENT_PUBLISH_MSG)-1);
      if (ret) {
                   LOG_ERR("Publish failed: %d", ret);
                }
      }
}


static void data_interval_timer_handler(struct k_timer *timer)
{
    ARG_UNUSED(timer);

    int ret;
    ret = data_publish(&client,                                         // Error !!, system violation !!
                        MQTT_QOS_1_AT_LEAST_ONCE,
                        CONFIG_BUTTON_EVENT_PUBLISH_MSG,
                        sizeof(CONFIG_BUTTON_EVENT_PUBLISH_MSG)-1);
    if (ret) {
                  LOG_ERR("Publish failed: %d", ret);
              }

}

in main() function
k_timer_start(&data_interval_timer,      // start timer, OK!
                K_MINUTES(1),
                K_MINUTES(1));

Parents Reply Children
No Data
Related