【问题标题】:Connect to IoT Hub without using the Azure Client SDK不使用 Azure 客户端 SDK 连接到 IoT 中心
【发布时间】:2017-08-03 14:25:09
【问题描述】:

我想不使用客户端 SDK 连接到 Azure IoT Hub。 在 https://azure.microsoft.com/nb-no/blog/upload-files-from-devices-with-azure-iot-hub/ 有关于如何做到这一点的文档 1) 获取存储的 SAS URI
2) 通知 IoT 中心上传完成

但在此之前,您需要使用 DeviceConnectionString 连接到 IoT 中心。有没有人有一个例子/提示如何做到这一点并上传文件?

【问题讨论】:

    标签: c# azure azure-iot-hub


    【解决方案1】:

    如果您不想使用 SDK(我很想知道为什么),您可以找到所有 REST API 参考文档here。 有关存储的 SAS URI 的详细信息是 here。 对于文件上传通知,它是here。 通过身份验证+这些,您应该能够通过 IoT Hub 实现文件上传。

    【讨论】:

    • @oliverbloch “我很想知道为什么”并非所有物联网设备都与 SDK 兼容。
    【解决方案2】:

    这是我的ATWINC1500Arduino AVR 从 IoT 中心读取的实现(修改端点并更改为 POST):

    #define NAMESPACE "{your-iot-hub}.azure-devices.net"
    #define AUTHORIZATION_HEADER "Authorization: SharedAccessSignature sr=xxxxxxxxxxxxxxxxxxxx"
    
    void httpRequest() {
      Serial.println("\nConnecting to IoT Hub...");
      if (client.connect(NAMESPACE, 443)) {
        Serial.println("Connected.");
        // Send HTTP request:
        client.println("GET /devices/{your_device_id}/messages/devicebound?api-version=2016-02-03 HTTP/1.1");
        client.println("Host: {your-iot-hub}.azure-devices.net");
        client.println(AUTHORIZATION_HEADER);
        client.println("User-Agent: Atmel ATWINC1500");
        client.println("Connection: close");
        client.println();
      }
    }
    

    我刚刚使用Device Explorer 生成了一个有效期为 2 年的 SAS 密钥。

    我很确定如果没有实时时钟,我无法计算自己的 SAS,而 AVR 没有。Oliver 可以确认。

    【讨论】:

    • 好吧,实际上我认为@w00zert 询问的是用于文件上传的 Azure 存储 SAS 密钥,而不是用于建立与 Azure IoT 中心的设备连接的 SAS 令牌
    • 他似乎对这两个问题都感到疑惑,@w00zert,你能澄清一下你在追求什么吗?更重要的是,您针对什么设备进行编码并且无法利用 SDK?
    【解决方案3】:

    您可以关注“File uploads with IoT Hub”,分四步完成:

    1. 将 Azure 存储帐户与 IoT 中心相关联。在这里,我们通过 Azure 门户执行此操作。在您的 iot hub 中找到 File upload,然后选择您的存储帐户的存储容器。
    2. 通过向 IoT 中心发送 POST 来初始化文件上传,如下所示:

    IoT Hub 返回以下数据:

    1. 设备使用 Azure 存储 SDK 将文件上传到存储。你可以关注this tutorial。代码如下所示:

          // Parse the connection string and return a reference to the storage account.
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
              CloudConfigurationManager.GetSetting("StorageConnectionString"));
      
          // Create the blob client.
          CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
      
          // Retrieve reference to a previously created container.
          CloudBlobContainer container = blobClient.GetContainerReference("azureportaldeploy");
      
          // Retrieve reference to a blob named "myblob".
          CloudBlockBlob blockBlob = container.GetBlockBlobReference("device1/testfileupload2");
      
          // Create or overwrite the "myblob" blob with contents from a local file.
          using (var fileStream = System.IO.File.OpenRead(@"{your file path}\testfileupload2.txt"))
          {
              blockBlob.UploadFromStream(fileStream);
          }
      
    2. 上传完成后,设备会向 IoT 中心发送一个 POST,如下所示:

    此外,您可以download the blob查看上传的文件。 并使用以下代码检查文件上传通知:

        private async static Task ReceiveFileUploadNotificationAsync()
        {
            var notificationReceiver = serviceClient.GetFileNotificationReceiver();
    
            Console.WriteLine("\nReceiving file upload notification from service");
            while (true)
            {
                var fileUploadNotification = await notificationReceiver.ReceiveAsync();
                if (fileUploadNotification == null) continue;
    
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Received file upload noticiation: {0}", string.Join(", ", fileUploadNotification.BlobName));
                Console.ResetColor();
    
                await notificationReceiver.CompleteAsync(fileUploadNotification);
            }
        }
    

    【讨论】:

      【解决方案4】:

      Here 是一个客户端测试应用程序示例,其源代码可在不使用 SDK 的情况下与 Azure IoT Hub 进行交互。它包括文件上传等。它使用M2MQTT library

      有些人想知道您为什么要在没有 SDK 的情况下这样做。事实上,并非所有的物联网设备都支持 SDK。

      【讨论】:

      • 投反对票的人请解释你投反对票的原因。我的回答回答了这个问题,这个问题是一个常见的用例。
      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      相关资源
      最近更新 更多