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

Simple MQTT sample for nrf9160

Hi everyone

I'm trying to implement the MQTT sample on my devkit but I'm a little bit lost. I never used the MQTT before, so its a little bit of a chocking to me.

From what I know, a broker is an element responsible for manage the publish and subscriptions of  mqtt protocol. We need a client and a server and publish/subscribe to topics for the communication happens. Is that alright?

So, I wanted to know if you can guide me.

I followed the instructions here "https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/nrf9160/mqtt_simple/README.html". But I were confused when configuring the mqtt broker.

I installed the Mosquitto for windows https://mosquitto.org/download/.

And checking https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.iotsdk.v0.9.0%2Fiot_sdk_user_guides_mosquitto.html I tried to configure the broker but I was completely lost. 

Like I said, I never used this or something close before and If you could guide me how to get this example working it would be great.

Sorry If this post is not so useful.

Have a nice day

Parents
  • Hi.

     

    From what I know, a broker is an element responsible for manage the publish and subscriptions of  mqtt protocol. We need a client and a server and publish/subscribe to topics for the communication happens. Is that alright?

     Yes, this is correct. In MQTT there is no direct communication between two "devices" (including applications running on servers and similar), but instead everything goes via a Broker. It is kind of similar to you subscribing to a magazine, which is sent to you by the post office, not the magazine itself.

     

     This documentation is for the "IoT SDK", which is now a part of the nRF5 SDK. For the nRF9160, you should stick to the nRF Connect SDK, which is our only SDK with support for the nRF9160.

    There are several public MQTT brokers available, and the mqtt_simple sample uses one of those, so you do not need to set up your own broker to test the sample.

    What version of NCS do you use (are you on the master branch, or using one of the tags)?

    If you use version 1.0.0, you need to change the hostname of the broker to "mqtt.eclipse.org". In later tags, this has already been done.

    You can then use an MQTT client (I recommend MQTT.fx) to publish to "/my/subscripe/topic" and subscribe to "/my/publish/topic". What you publish to "/my/subscribe/topic" will be published by the nRF91 to "/my/publish/topic".

    If you have further questions, feel free to as them!

    Best regards,

    Didrik

  • Hi. Thanks for fast reply.

    My NCS tag version is 1.0.0. 

    So I changed the hostname(from "iot.eclipse.org" to "mqtt.eclipse.org" at Kconfig, proj.conf and prj_qemu_x86. I dont know if it was to change in all 3 programs, but I changed in all.

    Now, after I flash the MQTT sample to nrf9160 devkit, it says I got a connection.

    OUTPUT:

    The MQTT simple sample started
    LTE Link Connecting ...
    LTE Link Connected!
    IPv4 Address found 137.135.83.217
    [mqtt_evt_handler:166] MQTT client connected!
    Subscribing to: my/subscribe/topic len 18
    [mqtt_evt_handler:216] SUBACK packet id: 1234

    After that I installed MQTT.fx. 

    When I open it I have two options: 

    ->Local Mosquitto;

    ->M2M eclipse;

    Which one should I select? And in any of that two options at "Edit connection Profiles" there's a Broker Address, different from one option to another. At local mosquitto is an ip, and at M2M eclipse is a link. Should I update the correct option to "mqtt.eclipse.org"??

    So, until I get a response to this reply, and not knowing if I'm doing it right I choose M2M ecplise but altered the broker from "m2m.eclipse.org" to "mqtt.ecplise.org". Correct me if I'm wrong.

    I pressed Connect and it connected, at least it says there.

    And now what should I do to see interaction with the devkit?

    Sorry if I didn't get there yet.

  • MQTT.fx lets you store different brokers. To add a new one, you can press the "+" button at the bottom right of the window in your second screenshot, or you can modify one of the existing ones (as you did). In the second screenshot, you can also change settings such as keep-alive time and TLS.

    In the "Publish" (see screenshot), you can publish to a topic by writing your message in the big text box that fills most of the window, and the topic in the small text box I have marked (you can also select topics you have previously published to from the drop-down menu). Also, remember that anyone can see what you are publishing to a public broker, so be careful not to share privet/sensitive information.

    You can also change to the "Subscribe" view, where you can subscribe to a topic in a similar way. You can also scan for topics, but the public brokers tend to have too many topics for you to find the one you are looking for.

  • I think I'm getting there. So after devkit gets connection to broker, at Link Monitor it says 

    Subscribing to: my/subscribe/topic len 18

    If I publish and subscribe to that specific topic from MQTT.fx shouldn't I see that messages at link monitor?

    Like it says here at point 3/4?? Because I'm not getting anything there.

  • From MQTT.fx you should publish to "my/subscribe/topic", and subscribe to "my/publish/topic". You should then see the message you published to "my/subscribe/topic" echoed on "my/publish/topic"

    (When testing right now, I had some trouble connecting to mqtt.eclipse.org (a disadvantage of using public brokers is that they are not always available), if you have similar problems, you could also try test.mosquitto.org)

    The log should look something like this:

  • It worked pretty well. I was already trying that but I was not publishing and subscribing to the right place. So thank you so much about that. One last question. Is it possible to measure the power consumption of MQTT sample? And if so, how is that possible? Is just flash MQTT sample at the devkit, and follow instructions like it says here https://devzone.nordicsemi.com/nordic/cellular-iot-guides/b/hardware-design/posts/measuring-psm-idle-current-on-the-nrf91-dk?

Reply Children