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

Easiest way to subscribe to MQTT messages from Thingy91 on the nRF Cloud

Hi,

What's the easiest way to subscribe to the MQTT messages that the thingy91 sends to the nRF cloud?

I have tried setting it up with a node-red and a python client, but I'm never able to connect to the MQTT broker that is set up in the nRF cloud. I want to find out the easiest way to subscribe to these messages so that I can present them in a web application like node-red or similar.

The documentation that is found under APIs doesn't seem to help me, as I'm unable to connect to the broker. As I can see the data being sent to the nRF cloud, I figured it wouldn't be too difficult to just parse that data somewhere else.

Thanks

Best regards

Joakim Uddenfeldt

Parents
  • Hello Joakim, 
     

    The documentation that is found under APIs doesn't seem to help me, as I'm unable to connect to the broker.

    I assume you mean nRF Cloud API documentation, specifically the MQTT chapter?

    Can you share how you are trying to connect?

    Kind regards,
    Øyvind

  • Hi Øyvind,

    Here is what I have done:

    I start by downloading and installing new certificates on my Thingy using these instructions:

    https://devzone.nordicsemi.com/nordic/cellular-iot-guides/b/getting-started-cellular/posts/nrf-cloud-certificate-update

    After that, I register my Thingy to my nRF account using the GUI on the nRF Cloud website

    I then use the swagger gui to download the MQTT information, such as endpoint and topics using the following curl command

    curl -X GET "">api.nrfcloud.com/.../account" -H "accept: application/json" -H "Authorization: Bearer

    With the following python code, I try to subscribe to the messages that my thingy sends (which I can see in the nRF cloud GUI

    # Import package
    import paho.mqtt.client as mqtt
    import ssl
    import certifi
    
    #Defining mqtt input
    mqttEndpoint = "a2n7tk1kp18wix-ats.iot.us-east-1.amazonaws.com"
    mqttTopicPrefix = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/"
    #topics
    gateways = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/a/gateways"
    connections = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/a/connections"
    alerts = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/a/alerts"
    notifications ="prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/a/alerts/notifications"
    messagesPrefix = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/m"
    
    # Define Variables
    MQTT_PORT = 8883
    MQTT_KEEPALIVE_INTERVAL = 45
    MQTT_TOPIC = "prod/7215a082-a8f3-4507-a03d-a7e2e0d55387/m/"
    
    
    MQTT_HOST = "a2n7tk1kp18wix-ats.iot.us-east-1.amazonaws.com"
    CA_ROOT_CERT_FILE = "root-CA.cert"
    THING_CERT_FILE = "certificate.pem"
    THING_PRIVATE_KEY = "privatekey.key"
    
    # Define on connect event function
    # We shall subscribe to our Topic in this function
    def on_connect(mosq, obj, rc):
        mqttc.subscribe(MQTT_TOPIC, 0)
    
    # Define on_message event function. 
    # This function will be invoked every time,
    # a new message arrives for the subscribed topic 
    def on_message(mosq, obj, msg):
    	print "Topic: " + str(msg.topic)
    	print "QoS: " + str(msg.qos)
    	print "Payload: " + str(msg.payload)
    
    def on_subscribe(mosq, obj, mid, granted_qos):
        print("Subscribed to Topic: " + 
    	MQTT_MSG + " with QoS: " + str(granted_qos))
    
    # Initiate MQTT Client
    mqttc = mqtt.Client()
    
    # Assign event callbacks
    mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_subscribe = on_subscribe
    
    # Configure TLS Set
    mqttc.tls_set(certfile=THING_CERT_FILE, keyfile=THING_PRIVATE_KEY, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
    
    
    # Connect with MQTT Broker
    mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
    
    
    # Continue monitoring the incoming messages for subscribed topic
    mqttc.loop_forever()

    However, i'm never able to connect successfully? I have tried the same with node-red, but i'm unable to connect using that as well.

    Do you have any idea what I might be doing incorrectly? 

    Best regards

    Joakim Uddenfeldt

  • Hello Joakim, 

    First I must apologize for not being very clear when asking about the certificate manager. You are not supposed to click on Update Certificates. The only reason to use this is to get the certificates in the correct format to copy and paste into separate certificates, and then close the Certificate Manager. You will need to remove the device and add the device again to your nRF Cloud account.

    Joakim U said:
    I'm still uncertain what the issue can be, but the error I receive is that I can't connect to the broker, which might be due to the certification error that I pointed out above.

    Where are you receiving the error? And what error are you receiving? 

    Joakim U said:
    have tested that my tools work with another MQTT broker and it works fine.

    What MQTT broker did you use?

    Thanks!

  • Hi Øyvind,

    No worries, I might have been a bit unclear myself as well. What I have done is actually to use the certificate manager in the same way that you are describing, in other words, i'm only using it to get the certificates in the right format. As I'm using Windows, I assumed this was the best way of formatting the certificates. Once I have done that, I check the certificates against  this tool: https://www.sslshopper.com/certificate-decoder.html, which tells me if the certificate is correctly formatted.

    I'm currently using Node-red (https://nodered.org) to test out these connections. I haven't set up a broker, instead I'm trying to subscribe to the broker that my Thingy is sending its data to, which is the one you can see below. The error I receive is the following:

    8 Oct 08:31:15 - [info] [mqtt-broker:Thingy91] Connection failed to broker: mqtts://a2n7tk1kp18wix-ats.iot.us-east-1.amazonaws.com:8883

     I know it perhaps doesn't say much, but that's the feedback I get from that program.

    My workaround is to use HTTP requests to retrieve the messages, but it is not as nice implementation as MQTT.

    Best regards

    Joakim Uddenfeldt

  • Hello Joakim, 

    there have been updates on the nRF Cloud API. To get the correct certificates and client-id to use when connecting to the Thingy:91 broker, please run the following:

     http POST https://api.nrfcloud.com/v1/account/certificates Authorization:"Bearer b09f76779f504da08ffea13814024ce545f68b3b"

    This will output CA certificate, Client Certificate and Private Key, which you need to format correctly in separate files.

    As I am not familiar with Node-Red, I do not know how to answer questions about this. I have tested using Mosquitto, and I urge you test with this first to validate the connection. 

  • Hi Øyvind,

    I will start testing with Mosquitto and also the Curl command you sent above, in order to see if it helps. Did you use a Windows to format it or did it work better in Linux?

    Thanks

    Best regards

    Joakim Uddenfeldt

  • Apologies for the late reply. 

    Joakim U said:
    Did you use a Windows to format it or did it work better in Linux?

    I used a standard text editor which I copied the output to, and made sure to add a new line after \n (visible in the output). When this is done you should have the correct format, but may need to remove \n.

    Kind regards,
    Øyvind

Reply Children
No Data
Related