Reputation: 11
I'm building my first project which is related with cloud & raspberry Pi GPIO pins (or sensors for that matter) but I got a bit stuck and hope that someone could help me by pointing to the right direction.
I have Raspberry Pi 4 with attached Sensirion SCD30 sensor and by following this guide I successfully managed to retrieve sensor measurement data in "/run/sensors/scd30/last" which is constantly updating.
My goal is to send the measurement data to some free of charge cloud service, I was thinking about Azure IoT Central or IoT HUB since both have free options.
My question is, how can I take this file "/run/sensors/scd30/last" and forward it with 5 or 10 second interval to Azure where I could then make all the necessary dashboards and triggers?
Upvotes: 1
Views: 224
Reputation: 2654
You need to implement the application that is using Azure IoT Hub Device SDK, and that can send data to the IoT Hub.
You will have to implement reading the data from the file every 4-10s and send information to the IoT Hub by using DeviceClient from the SDK mentioned above.
Below is one code snippet in C#, that extracts the data from DHt11 temperature/humidity sensor and sends the data to the IoT Hub every 2s.
...
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);
}
Regarding the free tier, you can have one IoT Hub with Free tier per subscription with all features included.
Upvotes: 1