Azure IoT Hub - Node - Connectivity Loss

Hi,

Started using "Azure IoT hub" node from here. It sends the data as a payload in MQTT/HTTP.

If internet connectivity fails or IoT hub is not available while sending a data from node red how to handle? Can we get any response like connectivity error or message?

I haven't used that node, but the first thing I would do is to attach a Catch node to it and see if it generates errors for you.

However, I would not hold out too much hope as I see it has not been updated in three years.

Watch for node status changes and handle accordingly. When connection is lost it should be reflected in the node status

can we send a data to Azure IoT Hub using MQTT out node? Like publish MQTT.

Catch node doesn't give any connectivity loss error or information. Is there any other way to send data from Node RED to Azure IoT Hub?

Does the status show anything helpful as suggested by @aderici? If so you can use a status node.

That is okay. But if internet connectivity is lost then node status is not changing. This one is not reliable.

Does the node send a message at all in the error condition?
If so then feed it into a debug node configured to show Complete Message and see what is in there.

@sathishkumarc14 You can also use http to communicate with iot hub, see an example here. Copy and import this flow :

[{"id":"54de72b2.ff5d2c","type":"function","z":"ef196bfe.de8c48","name":"prepare IoT Hub message","func":"msg.payload = JSON.stringify({\"hello\": \"world\"})\nmsg.headers = {}\nmsg.headers[\"Authorization\"] = {iotHubSasToken}`;\nmsg.url = `https://{iotHubName}.azure-devices.net/devices/${deviceId}/messages/events?api-version=2018-06-30\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":240,"wires":[["a33ab64.dfac048"]]},{"id":"a33ab64.dfac048","type":"http request","z":"ef196bfe.de8c48","name":"IoT Hub HttpS","method":"POST","ret":"txt","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":500,"y":240,"wires":[["8cfb0902.4cfea8"]]},{"id":"62458b2.8d37c74","type":"inject","z":"ef196bfe.de8c48","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":180,"wires":[["54de72b2.ff5d2c"]]},{"id":"8cfb0902.4cfea8","type":"debug","z":"ef196bfe.de8c48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":180,"wires":[]}]

You will need to generate a "SAS Token" for your device id in order to use this method.

Best regards,

Thanks @aderici It is working fine.

Have a look at this blog post (here), recently I was in similar situation like yours, found the solution for it using MQTT nodes for connection to Azure IoT Hub.

1 Like

@nikhilkinkar Great information. Thanks.

Can we log the data if MQTT fails? How can we identify the MQTT connectivity failure?

1 Like

That is what the MQTT LWT (Last Will and Testament) feature is for. You will find that described (and a lot more besides) in MQTT Essentials - All Core Concepts explained

[Edit] Actually that might not be the right answer. Where is it that you are are trying to detect the error and where is the MQTT broker, and which comms link is it that is failing?

I’ve been through all this, use http, you’ll thank me later :slight_smile:

@sathishkumarc14 if you are saving node-red logs, whenever there is any change in mqtt node connectivity you can get it from node-red logs. About identifying the mqtt connectivity failure, you can use node-red catch flow for catching any future errors.
I would add one more point here, I have used mqtt nodes with AWS IoT core, it is working from a longer time without any issues. While working with Azure I have seen that the documentation for Azure is quite difficult to get to the point as compared to AWS (I am new to Azure).