Skip to content

Instantly share code, notes, and snippets.

@rido-min
Last active June 2, 2020 07:12
Show Gist options
  • Save rido-min/850f840315633216b34d5655d5ef89b9 to your computer and use it in GitHub Desktop.
Save rido-min/850f840315633216b34d5655d5ef89b9 to your computer and use it in GitHub Desktop.
simple-dotnet-device
{
"@context": "dtmi:dtdl:context;2",
"@id": "dtmi:com:example:mypnpdevice;1",
"displayName": "MyPnPDevice",
"@type": "Interface",
"contents": [
{
"@type": "Property",
"name": "myProperty",
"schema": "string"
},
{
"@type": "Telemetry",
"name": "temperature",
"schema": "double"
}
]
}
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string connectionString = "";
var client = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);
Console.WriteLine("client connected");
await client.UpdateReportedPropertiesAsync(new TwinCollection("{ myProp : 45.2 }"));
Console.WriteLine("Property Updated");
await client.SetMethodHandlerAsync("reboot", (MethodRequest req, object ctx) => {
Console.WriteLine("REBOOT");
return Task.FromResult(new MethodResponse(200)); },
client);
for (int i = 0; i < 100; i++)
{
await client.SendEventAsync(new Message(Encoding.UTF8.GetBytes("{ temperature = 12.3 }")));
Console.WriteLine("Telemetry sent");
await Task.Delay(1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment