Philosophy using MQTT in NRF9160 with other codes

I am taking mqqt_simple of NRF9160 as reference for this question,

This is questing regarding, what is recommendation / Philosophy of using MQTT in NRF9160 with other codes e.g. sensor data or gps data or machine learning etc.

here is my understanding of how MQTT code loops works,

=========================================

Once connection is made to MQTT server, program will enter while loop and continuously poll for incoming data, as shown below.

err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));  // for most of the time main loop will be stuck here.

This loop will timeout after CONFIG_MQTT_KEEPALIVE,during this time code will come out of poll section thus we can run other routines in the code.

In addition if you have interrupt routine, then also poll will exiting and during this time code in ISR and other code in main loop will run.

Multi-thread may be option but still the same result as thread running poll function will block other thread until it times out or  ISR occurs.

My question is,

Code above works as provided but when MQTT code has to work will other section code , e.g. polling other sensor every second or some longer time consuming routing, then poll routine will block/consume most part of while loop time. So how to get round this problem.

Regards

Parents
  • Hi,

     

    Once connection is made to MQTT server, program will enter while loop and continuously poll for incoming data, as shown below.

    err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));  // for most of the time main loop will be stuck here.

    This loop will timeout after CONFIG_MQTT_KEEPALIVE,during this time code will come out of poll section thus we can run other routines in the code.

    In addition if you have interrupt routine, then also poll will exiting and during this time code in ISR and other code in main loop will run.

    Multi-thread may be option but still the same result as thread running poll function will block other thread until it times out or  ISR occurs.

    poll() does not block, it will yield for other threads.

    It'll "block" the originating thread until it finishes (ie. sleep period expires or data is received), but all other threads are free to be scheduled.

    Code above works as provided but when MQTT code has to work will other section code , e.g. polling other sensor every second or some longer time consuming routing, then poll routine will block/consume most part of while loop time. So how to get round this problem.

    You can use a work queue, or a dedicated thread for your other logic:

    https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/kernel/services/threads/workqueue.html

     

    Kind regards,

    Håkon

  • HI Håkon

    Thanks for your response. I am testing as per your suggestions.

    Will git back if I see any issues.

    Regards

Reply Children
No Data
Related