云中的Raspberry Pi传感器数据监控

时间:2019-10-13 17:03:47

标签: azure raspberry-pi azure-iot-hub azure-iot-sdk azure-iot-central

我正在建立我的第一个与云和树莓派GPIO引脚(或与此相关的传感器)有关的项目,但是我有些卡住,希望有人可以通过指出正确的方向来帮助我。

我有连接了Sensirion SCD30传感器的Raspberry Pi 4,通过遵循this guide,我成功地设法在不断更新的“ / run / sensors / scd30 / last”中检索传感器测量数据。

我的目标是将测量数据发送到一些免费的云服务,我当时考虑使用Azure IoT中心或IoT HUB,因为它们都有免费的选择。

我的问题是,如何获取此文件“ / run / sensors / scd30 / last”并将其以5或10秒的间隔转发到Azure,然后我可以在其中创建所有必需的仪表板和触发器?

1 个答案:

答案 0 :(得分:1)

您需要实现使用Azure IoT Hub Device SDK的应用程序,该应用程序可以将数据发送到IoT中心。

您将必须实现每4-10s从文件中读取一次数据,并使用上述SDK中的DeviceClient使用信息将信息发送到IoT中心。

下面是C#中的一个代码段,该代码段从DHt11温度/湿度传感器中提取数据,并每2秒钟将数据发送到IoT中心。

                        ...
                        var deviceClient = DeviceClient.CreateFromConnectionString("ConnectionString");                        

                        var dht = new DHT(pin, gpioController, DHTSensorTypes.DHT11);
                        while (true)
                        {
                            try
                            {
                                var measurement = new Measurement();
                                var dhtData = dht.ReadData();

                                 measurement.Temperature = (int)dhtData.TempCelcius;
                                 measurement.Humidity = (int)dhtData.Humidity;

                                 if (gpioController.IsPinOpen(pin))
                                 {
                                     gpioController.ClosePin(pin);
                                 }
                                }

                                SendMeasurementAsync(deviceClient, measurement).Wait();

                                Console.WriteLine(DateTime.UtcNow);
                                Console.WriteLine(" sent to iot hub temp: " + measurement.Temperature);
                                Console.WriteLine(" sent to iot hub hum: " + measurement.Humidity);
                            }
                            catch (DHTException)
                            {
                                Console.WriteLine(" problem reading sensor data ");
                            }
                            Task.Delay(2000).Wait();
                        }
                        .
                        .
                        .


    private static Task SendMeasurementAsync(DeviceClient deviceClient, Measurement measurement)
    {
        var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(measurement);
        var eventMessage = new Message(Encoding.UTF8.GetBytes(jsonString));

        return deviceClient.SendEventAsync(eventMessage);
    }

关于free tier,您可以拥有一个带免费套餐的IoT中心,其中包括所有功能。