0

I have Node-RED installed on my Raspberry Pi.

Connecting to Azure IoT Hub with Mosquitto using Node-RED.

Sometimes I'm in trouble because the connection with Azure IoT Hub is lost.

Checking with Log Analytics, the following error appears.

<ERROR>
This article describes the causes and solutions for 404104 DeviceConnectionClosedRemotely errors.

The cause is completely unknown, and I have not been able to find it even after investigating the countermeasures.

enter image description here

hardillb
  • 54,545
  • 11
  • 67
  • 105
Rio
  • 1
  • Could you please review the document link provided by JD Allen's response. The document's resolution steps may help you get unblocked. [troubleshoot-error-404104](https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-troubleshoot-error-404104-deviceconnectionclosedremotely) – SatishBoddu Jun 15 '20 at 22:57
  • Hi. Thank you for your answer. I also knew this link. How can I update the SAS token if it runs out? I'm in trouble because I don't understand. – Rio Jun 16 '20 at 00:43

2 Answers2

0

According to the Azure IOT doc, your SAS Token expired: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-troubleshoot-error-404104-deviceconnectionclosedremotely

JD Allen
  • 799
  • 5
  • 12
  • Hi. Thank you for your answer. I also knew this link. How can I update the SAS token if it runs out? I'm in trouble because I don't understand. – Rio Jun 16 '20 at 00:43
  • Good question. Not sure how the Azure IOT module in Node-Red sets this up. You may have to something like this: https://stackoverflow.com/questions/51728649/how-to-generate-an-azure-sas-token-via-javascript-node-js – JD Allen Jun 16 '20 at 02:14
  • Thanks!! I don't know immediately, so read it carefully. – Rio Jun 16 '20 at 02:45
  • I will also comment later. – Rio Jun 16 '20 at 02:45
0

I recommend to generate your SAS token for a longer duration.

Update:


A user voice is being submitted by @Rio, please navigate to When the connection of Azure IoT Hub from Node-RED is disconnected, an error occurs and it cannot be reconnected and vote and comment your requirement in support of asking this feature.


You have to re-create and re-configure the token in Node-Red when it expires. Please refer to Connect Node-Red to Azure IoT Edge

**via Azure CLI: az iot hub generate-sas-token

Or

via using code as commented by @JD Allen or like using c# shown below,

To get a basic understanding on how SAS Token refresh based on SharedAccessKey , please see this C# code snippet. Please make sure you have IoTHub connection string and deviceId as well. For more reading please refer azure-iot-sdk-csharp

namespace Microsoft.Azure.Devices.Client
{
    // Implementing SAS Token refresh based on a SharedAccessKey (SAK).
    internal class DeviceAuthenticationWithSakRefresh : DeviceAuthenticationWithTokenRefresh
    {
        private IotHubConnectionString _connectionString;

        public DeviceAuthenticationWithSakRefresh(
            string deviceId, 
            IotHubConnectionString connectionString) : base(deviceId)
        {
            _connectionString = connectionString;
        }

        protected override Task<string> SafeCreateNewToken(string iotHub, int suggestedTimeToLive)
        {
            var builder = new SharedAccessSignatureBuilder()
            {
                Key = _connectionString.SharedAccessKey,
                TimeToLive = TimeSpan.FromSeconds(suggestedTimeToLive),
            };

            if (_connectionString.SharedAccessKeyName == null)
            {
                builder.Target = "{0}/devices/{1}".FormatInvariant(
                    iotHub, 
                    WebUtility.UrlEncode(DeviceId));
            }
            else
            {
                builder.KeyName = _connectionString.SharedAccessKeyName;
                builder.Target = _connectionString.Audience;
            }

            return Task.FromResult(builder.ToSignature());
        }
    }
}

**Also please do refer the Protocol specifics doc.

The Azure IoT SDKs automatically generate tokens when connecting to the service. In some cases, the Azure IoT SDKs do not support all the protocols or all the authentication methods.

**Also refer to similar GitHub issues sas token renewal#613, Renew SAS token used by Azure Iot Hub DeviceClient #1127

SatishBoddu
  • 752
  • 6
  • 13
  • But how would someone do this in Node-Red?? – JD Allen Jun 16 '20 at 02:16
  • I have never used C#. First, let's check. Have you encountered this problem with Node-RED? – Rio Jun 16 '20 at 02:48
  • For how long (duration) was the first SAS key generated? – SatishBoddu Jun 16 '20 at 03:13
  • Connections are often lost about 30 minutes after starting Node-RED. Occasionally, the connection will last for 4 to 5 days. – Rio Jun 16 '20 at 05:04
  • @Rio, Could you please create a [User Voice Request](https://feedback.azure.com/forums/321918-azure-iot/filters/new) on this topic with your requirements in detail with a good Title. I will make sure to do a follow-up.Let me know the final posted link here, once you have submitted the request. – SatishBoddu Jun 16 '20 at 17:27
  • 1
    @SatishBoddu-MSFT Thank you for your reply. I have filled out the User Voice Request. Isn't what I'm doing wrong? – Rio Jun 17 '20 at 00:59
  • https://feedback.azure.com/forums/321918-azure-iot/suggestions/40683982-when-the-connection-of-azure-iot-hub-from-node-red – Rio Jun 17 '20 at 01:03
  • @Rio, Thank you! I have added the comment. Please bookmark this URL to followup on the request. I will also followup internally with Product Team. We have to gain more number of votes and comments to make it approved by the PG team. Thanks again for your contribution. – SatishBoddu Jun 17 '20 at 01:12
  • @Rio, Please accept my updated response as 'Answer' so that it is helpful to others as well looking with similar question and there by voting the user voice. – SatishBoddu Jun 17 '20 at 01:22