Unlike most of the Azure IoT Hub client examples the names and number of sensor values will only be known when messages received over the nRF24L01 wireless link are processed so the JSON message payload has to be constructed on the fly.
Using the Newtonsoft.Json NuGet package and Linq + JObject made this much easier than expected so I have added some code improve robustness.
/* Copyright ® 2018 Jan devMobile Software, All Rights Reserved THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. You can do what you want with this code, acknowledgment would be nice. http://www.devmobile.co.nz */ using System; using System.Text; using System.Threading; using Microsoft.Azure.Devices.Client; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace devMobile.IoT.MicrosoftIoTCentral.Desktop.DynamicPayload { class Program { const string DeviceConnectionString = "YourDeviceConnectionStringFromIoTCentralGoesHere"; const string TelemetryDataPointPropertyNameFormat = @"{0}-{1}"; const double temperatureBase = 20.0; const double temperatureRange = 10.0; const double humidityBase = 70.0; const double humidityRange = 20.0; const double batteryVoltageBase = 3.00; const double batteryVoltageRange = -1.00; static readonly TimeSpan feedUpdateDelay = new TimeSpan(0, 0, 15); static void Main(string[] args) { DeviceClient Client = null; try { Console.WriteLine("Connecting to IoI hub"); Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Amqp); Console.WriteLine(" Connected"); } catch (Exception ex) { Console.WriteLine("Error connecting or sending data to IoT Central: {0}", ex.Message); return; } while (true) { // Then send simulated temperature, humidity & battery voltage data Random random = new Random(); double temperature = temperatureBase + random.NextDouble() * temperatureRange; double humidity = humidityBase + random.NextDouble() * humidityRange; double batteryVoltage = batteryVoltageBase + random.NextDouble() * batteryVoltageRange; Console.WriteLine("Temperature {0}°C Humidity {1}% Battery Voltage {2}V", temperature.ToString("F1"), humidity.ToString("F0"), batteryVoltage.ToString("F2")); // Populate the data point - JObject telemetryDataPoint = new JObject(); // This could be simplified but for field gateway will use this style string sensorDeviceSerialNumber = "0123456789ABCDEF"; // intentionally created and initialised at this level as sensor device will send over NRF24 link telemetryDataPoint.Add(string.Format(TelemetryDataPointPropertyNameFormat, sensorDeviceSerialNumber, "T"), temperature.ToString("F1")); telemetryDataPoint.Add(string.Format(TelemetryDataPointPropertyNameFormat, sensorDeviceSerialNumber, "H"), humidity.ToString("F0")); telemetryDataPoint.Add(string.Format(TelemetryDataPointPropertyNameFormat, sensorDeviceSerialNumber, "V"), batteryVoltage.ToString("F2")); string messageString = JsonConvert.SerializeObject(telemetryDataPoint); Console.WriteLine("{0:hh:mm:ss} > Sending telemetry: {1}", DateTime.Now, messageString); try { using (Message message = new Message(Encoding.ASCII.GetBytes(messageString))) { Client.SendEventAsync(message).Wait(); Console.WriteLine(" Sent"); } } catch (Exception ex) { Console.WriteLine("Error sending data to IoT Central: {0}", ex.Message); } Thread.Sleep(feedUpdateDelay); } } } }
The application produces very similar output to the basic desktop client