Exemplo n.º 1
0
        private static void Main()
        {
            const ConnectorPin measurePin = ConnectorPin.P1Pin7;

            Console.WriteLine("DHT-11/DHT-22 Sample: measure humidity and temperature");
            Console.WriteLine();
            Console.WriteLine("\tMeasure: {0}", measurePin);
            Console.WriteLine();

            var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly);

            using (var pin = driver.InOut(measurePin))
                using (var dhtConnection = new Dht11Connection(pin))
                {
                    while (!Console.KeyAvailable)
                    {
                        var data = dhtConnection.GetData();
                        if (data != null)
                        {
                            Console.WriteLine("{0:0.00}% humidity, {1:0.0}°C, {2} attempts", data.RelativeHumidity.Percent, data.Temperature.DegreesCelsius, data.AttemptCount);
                        }
                        else
                        {
                            Console.WriteLine("Unable to read data");
                        }

                        Timer.Sleep(TimeSpan.FromSeconds(2));
                    }
                }
        }
Exemplo n.º 2
0
        private void Run(string[] args)
        {
            const ProcessorPin measurePin = ProcessorPin.Pin18;
            int seconds = 3;

            Console.WriteLine("DHT-11 / DHT-22: Measure temperature and humidity");
            Console.WriteLine($"Measure: {measurePin} every {seconds} second(s)");
            Console.WriteLine();

            var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly);

            using (var pin = driver.InOut(measurePin))
                using (var dhtConnection = new Dht11Connection(pin))
                {
                    while (!Console.KeyAvailable)
                    {
                        var data = dhtConnection.GetData();
                        if (data != null)
                        {
                            Console.WriteLine(
                                $"{data.RelativeHumidity.Percent:0.00}% humidity, {data.Temperature.DegreesCelsius:0.0}°C / {data.Temperature.DegreesFahrenheit:0.0}°F, {data.AttemptCount} attempt(s)");
                        }
                        else
                        {
                            Console.WriteLine("Unable to read sensor data");
                        }

                        System.Threading.Thread.Sleep(seconds * 1000);
                    }
                }
        }
Exemplo n.º 3
0
        private static void Main()
        {
            const ConnectorPin measurePin = ConnectorPin.P1Pin7;

            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddLogging();

            var sp = serviceCollection.BuildServiceProvider();

            sp.GetRequiredService <LoggerFactory>().AddConsole().AddDebug();
            var log = sp.GetRequiredService <ILogger <Program> >();

            log.LogInformation("DHT-11/DHT-22 Sample: measure humidity and temperature");
            log.LogInformation("\tMeasure: {0}", measurePin);

            var driver = GpioConnectionSettings.GetBestDriver(GpioConnectionDriverCapabilities.CanChangePinDirectionRapidly);

            using (var pin = driver.InOut(measurePin))
                using (var dhtConnection = new Dht11Connection(sp, pin))
                {
                    while (!Console.KeyAvailable)
                    {
                        var data = dhtConnection.GetData();
                        if (data != null)
                        {
                            Console.WriteLine("{0:0.00}% humidity, {1:0.0}°C, {2} attempts", data.RelativeHumidity.Percent, data.Temperature.DegreesCelsius, data.AttemptCount);
                        }
                        else
                        {
                            Console.WriteLine("Unable to read data");
                        }

                        Timer.Sleep(TimeSpan.FromSeconds(2));
                    }
                }
        }