首页 > 解决方案 > .NET Core IOT(不使用 Grove Shield 时没有来自 Seed Studio Grove DTH11 传感器的数据)

问题描述

我正在使用 Grove DTH11 传感器连接到树莓派 4,并使用 .NET Core IOT 和 GPIO 引脚从传感器获取读数。我将传感器直接连接到 Pi 而不使用 Grove Pi+ 或 Grove Shield。

问题是我没有从传感器获得任何读数。下面是我正在使用的代码

int pin = 4;
using (GpioController controller = new GpioController()) 
{ 
    Console.WriteLine("Opening Pin - " + pin);
    controller.OpenPin(pin, PinMode.Input);
    Console.WriteLine("Pin - " + pin + " opened successfully");
 
    using (Dht11 dth11Obj = new Dht11(pin, PinNumberingScheme.Logical)) 
    { 
        Console.WriteLine("Dht11 object created");
        while (!Console.KeyAvailable) 
        { 
            Console.WriteLine("reading data"); 
            var temp = dth11Obj.Temperature; 
            var hum = dth11Obj.Humidity; 
            Console.WriteLine("data read successfully"); 

            if (dth11Obj.IsLastReadSuccessful) 
            {
                Console.WriteLine($"Temperature: {temp.DegreesCelsius}\u00B0C, Relative humidity: {hum.Percent}%"); 
                Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(temp, hum).DegreesCelsius:0.#}\u00B0C"); 
                Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(temp, hum).DegreesCelsius:0.#}\u00B0C"); 
             } 
             else 
             { 
                 Console.WriteLine("Error reading DHT sensor"); 
             }
             
             Thread.Sleep(2000); 
        } 
    } 
} 

上述代码的输出如下。

Opening Pin - 4
Pin - 4 opened successfully
Dht11 object created
reading data

显示“读取数据”后执行卡住并且没有传感器读数。

Raspberry Pi 和 Sensor 的连接如下 -

Pin GND of Dth11 Sensor is Connected to Pin 6 of Raspberry Pi 4
Pin VCC of Dth11 Sensor is Connected to Pin 1 of Raspberry Pi 4
Pin NC  of Dth11 Sensor is Connected to Nothing
Pin SIG of Dth11 Sensor is Connected to Pin 7 (GPIO 4) of Raspberry Pi 4

标签: .net.net-coreraspberry-piiotgpio

解决方案


你不应该在之前打开别针。删除行controller.OpenPin(pin, PinMode.Input); 见:https ://github.com/dotnet/iot/blob/c8130309c99a35ce68a10c40ca9003b0261cc2ef/src/devices/Dhtxx/DhtBase.cs#L100


推荐阅读