forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
41 lines (39 loc) · 1.69 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using System.Threading.Tasks;
using Iot.Device.OneWire;
// Make sure you can access the bus device before requesting a device scan (or run using sudo)
// $ sudo chmod a+rw /sys/bus/w1/devices/w1_bus_master1/w1_master_*
if (args.Any(_ => _ == "temp"))
{
// Quick and simple way to find a thermometer and print the temperature
foreach (var dev in OneWireThermometerDevice.EnumerateDevices())
{
Console.WriteLine($"Temperature reported by '{dev.DeviceId}': " +
(await dev.ReadTemperatureAsync()).DegreesCelsius.ToString("F2") + "\u00B0C");
}
}
else
{
// More advanced way, with rescanning the bus and iterating devices per 1-wire bus
foreach (string busId in OneWireBus.EnumerateBusIds())
{
OneWireBus bus = new(busId);
Console.WriteLine($"Found bus '{bus.BusId}', scanning for devices ...");
await bus.ScanForDeviceChangesAsync();
foreach (string devId in bus.EnumerateDeviceIds())
{
OneWireDevice dev = new(busId, devId);
Console.WriteLine($"Found family '{dev.Family}' device '{dev.DeviceId}' on '{bus.BusId}'");
if (OneWireThermometerDevice.IsCompatible(busId, devId))
{
OneWireThermometerDevice devTemp = new(busId, devId);
Console.WriteLine("Temperature reported by device: " +
(await devTemp.ReadTemperatureAsync()).DegreesCelsius.ToString("F2") +
"\u00B0C");
}
}
}
}