.net - .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
解决方案
你不应该在之前打开别针。删除行controller.OpenPin(pin, PinMode.Input);
见:https ://github.com/dotnet/iot/blob/c8130309c99a35ce68a10c40ca9003b0261cc2ef/src/devices/Dhtxx/DhtBase.cs#L100
推荐阅读
- react-native - 使用 React Native Paper 的模态对话框?
- javascript - 如何调试由转换成一堆 eval() 语句的 JavaScript 代码组成的 Chrome 扩展?
- java - 如何从 ArrayList 中删除重复的联系人
- f# - 如何在 f# 中将两个不同类型的序列组合成元组?
- c# - 如何将json文件转换为List
- swiftui - 如何在 SwiftUI 中设置按钮焦点?
- scala - Akka 流 IOResult:如何检查错误?
- sql-server - 带有 SQL Server 的爬虫 html 站点(Ole 自动化过程)
- linux - 如何在 Linux 上运行的 .NET Core 应用中使用 7zip?
- excel - 根据列中的日期将行中的特定单元格复制到月份工作表