This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Sending commands to Thingy91 by subscribing to mqtt server

Hi Dev team,

I am working on a Thingy91 device, and I am sending data out through MQTT.

I need a functionality to send some commands to do assigned tasks from the MQTT server back to the LTE device.

Are there any nRF or Zephyr API that supports this functionality to send commands from any server to the LTE Device? 

If not, I was thinking to use the MQTT subscribe to receive commands and perform actions based on that.

What would be a feasible approach to implement the command functionality ?

Regards,

Adeel.

Parents
  • Hi,

     

    Are there any nRF or Zephyr API that supports this functionality to send commands from any server to the LTE Device? 

     There are many libraries for sending data between a server and the nRF9160. What you do with that data is up to you.

     

    What would be a feasible approach to implement the command functionality ?

     If you already have an MQTT connection, that sounds like the easiest way.

    MQTT let you send arbitrary data. What you do with that data is up to you. You could either have a way in the message to indicate that it is a command, or you could send the commands on a specific topic.

    Best regards,

    Didrik

  • Hi Didrik,

    Thanks for the reply. 

     There are many libraries for sending data between a server and the nRF9160.

    Could you let me know any examples of such libraries available ? I just want to build an interface wherein I assign a certain task based on a specific command. I just wanted to understand how the library helps me to communicate between a server and the LTE Device.

     If you already have an MQTT connection, that sounds like the easiest way.

    Yes, right now I have an enabled MQTT connection and I am subscribing to a topic and receiving the commands I try to send from a server on that topic. 

    But I was looking for any specific nRF or Zephyr libraries that help in this functionality.

    Regards,

    Adeel.

  • Adeel said:
    Could you let me know any examples of such libraries available ?

     The most common libraries: CoAP, LwM2M, MQTT, HTTP, cloud_client.

    Note that only LwM2M actually says anything about what data is sent. The other libraries just send arbitrary data, and it is up to you to define what the data means. and how it is encoded.

    But, again, MQTT is probably the most suitable protocol here, and you are already using it.

     

    Adeel said:
    But I was looking for any specific nRF or Zephyr libraries that help in this functionality.

     I don't quite understand what you need help to, or want the library to do.

    Here is some pseudocode showing roughly how I would have done it (which is also roughly how it is done in the asset_tracker):

    func handle_command_message(message):
        command = parse_command(message)
        switch command:
            SET_LIGHT:
                handle_set_light(command)
    
    func mqtt_evt_handler(evt):
        switch evt.type:
            MQTT_PUBLISH:
                if is_command_topic(evt.data.message.topic):
                    handle_command_message(evt.data.message.data)

  • Hi Didrik,

    Thanks for the pseudocode. I was trying to understand it but was a bit confused.

    Would it be possible if you could incorporate it in the mqtt_simple sample project so I could understand it a bit better. That would be a great help for me Slight smile.

    Basically I am currently trying to turn on the LED0 of the Thingy91, when I subscribe to a topic and receive a command "LIGHTON" from my server.

    Regards,

    Adeel.

  • The mqtt_evt_handler function is the same as in the mqtt_simple sample.

    If it receives the MQTT_EVT_PUBLISH event (which means that it has received a message on some topic), it checks if the topic is the command topic, and if it is, it calls a function that handles the command (handle_command_message()).

    The handle_command_message() function parses the command (from some unspecified format to something that is easier to work with in the code, e.g. an enum). When the command is parsed, the function checks what command it is, and calls a new function that handles that particular command.

    mqtt_evt_handler is already a part of the mqtt_simple sample. is_command_topic(), handle_command_message(), parse_command() and handle_set_light() (or handle_lighton() or handle_<this particular command>()) you must implement yourself.

Reply
  • The mqtt_evt_handler function is the same as in the mqtt_simple sample.

    If it receives the MQTT_EVT_PUBLISH event (which means that it has received a message on some topic), it checks if the topic is the command topic, and if it is, it calls a function that handles the command (handle_command_message()).

    The handle_command_message() function parses the command (from some unspecified format to something that is easier to work with in the code, e.g. an enum). When the command is parsed, the function checks what command it is, and calls a new function that handles that particular command.

    mqtt_evt_handler is already a part of the mqtt_simple sample. is_command_topic(), handle_command_message(), parse_command() and handle_set_light() (or handle_lighton() or handle_<this particular command>()) you must implement yourself.

Children
No Data
Related