Пример #1
0
        static async Task Main(string[] args)
        {
            // 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()).Celsius.ToString("F2") + "\u00B0C");
                }
            }
            else
            {
                // More advanced way, with rescanning the bus and iterating devices per 1-wire bus
                foreach (var busId in OneWireBus.EnumerateBusIds())
                {
                    var bus = new OneWireBus(busId);
                    Console.WriteLine($"Found bus '{bus.BusId}', scanning for devices ...");
                    await bus.ScanForDeviceChangesAsync();

                    foreach (var devId in bus.EnumerateDeviceIds())
                    {
                        var dev = new OneWireDevice(busId, devId);
                        Console.WriteLine($"Found family '{dev.Family}' device '{dev.DeviceId}' on '{bus.BusId}'");
                        if (OneWireThermometerDevice.IsCompatible(busId, devId))
                        {
                            var devTemp = new OneWireThermometerDevice(busId, devId);
                            Console.WriteLine("Temperature reported by device: " + (await devTemp.ReadTemperatureAsync()).Celsius.ToString("F2") + "\u00B0C");
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// https://github.com/dotnet/iot/tree/master/src/devices/OneWire/samples
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        static async Task Main(string[] args)
        {
            // below ID is valid only for my device
            var ds18b20DeviceId = "28-0119501b89ff";
            // you can remove condition from "FirstOrDefault"
            var oneWireThermometerDevice = OneWireThermometerDevice.EnumerateDevices().FirstOrDefault(e => e.DeviceId == ds18b20DeviceId);

            if (oneWireThermometerDevice == null)
            {
                Console.WriteLine("Cannot find device \"28-0119501b89ff\"");
                return;
            }

            double?lastTemp = null;

            while (true)
            {
                var temp = await oneWireThermometerDevice.ReadTemperatureAsync();

                if (!lastTemp.HasValue || Math.Abs((decimal)(lastTemp.Value - temp.Celsius)) > 1)
                {
                    lastTemp = temp.Celsius;
                    var formattedTemp = temp.Celsius.ToString("F2") + "\u00B0C";
                    Console.WriteLine($"{DateTime.Now:G} {formattedTemp}");
                }

                Thread.Sleep(1000);
            }
        }
Пример #3
0
        /// <inheritdoc />
        public void OpenPins(HardwareConfiguration configuration)
        {
            this.controller = new GpioController(PinNumberingScheme.Logical);

            foreach (var pin in configuration.Pins)
            {
                if (pin.Mode == HardwarePinConfigurationMode.Input)
                {
                    this.controller.OpenPin(pin.LogicalPin);

                    this.controller.RegisterCallbackForPinValueChangedEvent(
                        pin.LogicalPin,
                        PinEventTypes.Falling | PinEventTypes.Rising | PinEventTypes.None,
                        this.OnPinValueChanged);
                }
                else
                {
                    this.controller.OpenPin(pin.LogicalPin, PinMode.Output);
                    this.controller.Write(pin.LogicalPin, PinValue.High);
                }
            }

            this.pins = configuration.Pins.ToList();

            this.devices = OneWireThermometerDevice.EnumerateDevices().ToList();
            foreach (var device in this.devices)
            {
                this.logger.LogDebug($"Found device bus={device.BusId}, id={device.DeviceId}, type={device.Family.ToString()}");
            }
        }
        public TemperatureReader()
        {
            var devices = OneWireThermometerDevice.EnumerateDevices().ToList();

            Console.WriteLine("Devices START");
            devices.ForEach(e => Console.WriteLine($"DeviceId: {e.DeviceId}"));
            Console.WriteLine("END Devices");
            this.oneWireThermometerDevice = devices.FirstOrDefault();
            if (oneWireThermometerDevice == null)
            {
                Console.WriteLine("Cannot find any device");
            }
        }
Пример #5
0
        public async Task DS18B20SicaklikOku()
        {
            try
            {
                string busId    = OneWireBus.EnumerateBusIds().First();
                string deviceId = OneWireDevice.EnumerateDeviceIds().First().devId;
                OneWireThermometerDevice ds18B20Dev = new OneWireThermometerDevice(busId, deviceId);
                Temperature sicaklik = await ds18B20Dev.ReadTemperatureAsync();

                Sicaklik = sicaklik.Celsius;
            }
            catch (Exception hata)
            {
                SonHata = hata;
            }
        }
Пример #6
0
        public static void RunAndDisplay()
        {
            using var controller = new GpioController(PinNumberingScheme.Logical);

            for (int i = 0; i < controller.PinCount; i++)
            {
                Console.WriteLine(string.Concat(
                                      $"Pin {i:000}",
                                      $" | Input = {controller.IsPinModeSupported(i, PinMode.Input)}",
                                      $" | InputPullDown = {controller.IsPinModeSupported(i, PinMode.InputPullDown)}",
                                      $" | InputPullUp = {controller.IsPinModeSupported(i, PinMode.InputPullUp)}",
                                      $" | Output = {controller.IsPinModeSupported(i, PinMode.Output)}"
                                      ));
            }

            // Quick and simple way to find a thermometer and print the temperature
            Console.WriteLine("Enumerating temperature sensors...");
            foreach (var dev in OneWireThermometerDevice.EnumerateDevices())
            {
                var temperature = dev.ReadTemperature().Celsius;
                Console.WriteLine($"Temperature reported by '{dev.DeviceId}':  {temperature}°C");
            }
        }