我的Raspberry Pi上安装了Node-RED。
使用Node-RED与Mosquitto连接到Azure IoT Hub。
有时我遇到了麻烦,因为与 Azure IoT Hub 的连接丢失了。
用Log Analytics检查,出现以下错误。
<ERROR>
This article describes the causes and solutions for 404104 DeviceConnectionClosedRemotely errors.
原因完全不明,我调查了对策也没能找到。
根据 Azure IOT 文档,您的 SAS 令牌已过期。 https:/docs.microsoft.comen-usazureiot-Hubiot-Hub-troubleshoot-error-404104-deviceconnectionclosedremotely.
我建议你生成SAS令牌的时间要长一些。
更新一下。
一个用户的声音正在由@Rio提交,请浏览至 当Azure IoT Hub与Node-RED的连接断开时,会出现错误,无法重新连接。 并投票和评论你的需求,支持问这个功能。
当Node-Red中的token过期时,你必须重新创建并重新配置它。请参考 将Node-Red连接到Azure IoT边缘。
**通过Azure CLI。az iot hub generate-sas-token。
或
通过使用代码,如@JD Allen所评论的那样,或像使用c#那样,如下所示。
要基本了解SAS令牌如何基于SharedAccessKey刷新,请看这个C#代码片段。请确保你也有IoTHub连接字符串和设备ID。更多阅读请参考 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());
}
}
}
**还请参考《中国社会科学报》。议定书的具体内容 doc.
该 Azure IoT SDK在连接到服务时,Azure IoT SDK 会自动生成令牌。在某些情况下,Azure IoT SDK 不支持所有协议或所有认证方法。
**另请参考类似的 GitHub 问题。SAS令牌更新#613, 更新Azure Iot Hub DeviceClient使用的SAS令牌 #1127