private static void Dth12Test()
        {
            Dht12       dht = new Dht12(19);
            Temperature temp;
            Ratio       hum;
            GpioPin     led = GpioController.GetDefault().OpenPin(2);

            led.SetDriveMode(GpioPinDriveMode.Output);
            led.Write(GpioPinValue.High);

            while (true)
            {
                hum  = dht.Humidity;
                temp = dht.Temperature;
                if (dht.IsLastReadSuccessful)
                {
                    Debug.WriteLine($"Hum: {hum.Percent} %, Temp: {temp.DegreesCelsius} °C");

                    for (int i = 0; i < 5; i++)
                    {
                        Thread.Sleep(500);
                        led.Toggle();
                    }
                }
                else
                {
                    Debug.WriteLine("error reading dht");
                    led.Write(GpioPinValue.High);
                }

                Thread.Sleep(1000);
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello DHT!");
            Console.WriteLine("Select the DHT sensor you want to use:");
            Console.WriteLine(" 1. DHT10 on I2C");
            Console.WriteLine(" 2. DHT11 on GPIO");
            Console.WriteLine(" 3. DHT12 on GPIO");
            Console.WriteLine(" 4. DHT21 on GPIO");
            Console.WriteLine(" 5. DHT22 on GPIO");
            var choice = Console.ReadKey();

            Console.WriteLine();
            if (choice.KeyChar == '1')
            {
                Console.WriteLine("Press any key to stop the reading");
                // Init DHT10 through I2C
                I2cConnectionSettings settings = new I2cConnectionSettings(1, Dht10.DefaultI2cAddress);
                I2cDevice             device   = I2cDevice.Create(settings);

                using (Dht10 dht = new Dht10(device))
                {
                    Dht(dht);
                }

                return;
            }

            Console.WriteLine("Which pin do you want to use in the logical pin schema?");
            var pinChoise = Console.ReadLine();
            int pin;

            try
            {
                pin = Convert.ToInt32(pinChoise);
            }
            catch (Exception ex) when(ex is FormatException || ex is OverflowException)
            {
                Console.WriteLine("Can't convert pin number.");
                return;
            }

            Console.WriteLine("Press any key to stop the reading");

            switch (choice.KeyChar)
            {
            case '2':
                Console.WriteLine($"Reading temperature and humidity on DHT11, pin {pin}");
                using (var dht11 = new Dht11(pin))
                {
                    Dht(dht11);
                }

                break;

            case '3':
                Console.WriteLine($"Reading temperature and humidity on DHT12, pin {pin}");
                using (var dht12 = new Dht12(pin))
                {
                    Dht(dht12);
                }

                break;

            case '4':
                Console.WriteLine($"Reading temperature and humidity on DHT21, pin {pin}");
                using (var dht21 = new Dht21(pin))
                {
                    Dht(dht21);
                }

                break;

            case '5':
                Console.WriteLine($"Reading temperature and humidity on DHT22, pin {pin}");
                using (var dht22 = new Dht22(pin))
                {
                    Dht(dht22);
                }

                break;

            default:
                Console.WriteLine("Please select one of the option.");
                break;
            }
        }
Beispiel #3
0
        public void Initialize()
        {
            Console.WriteLine("Init...");

            sensor = new Dht12(Device.CreateI2cBus());
        }