问题描述
我正在尝试在我的RP2上运行以下示例,但我似乎无法使其正常工作。
I'm trying to get the following sample running on my RP2, but I can't seem to get it work properly.
传感器连接到GND,3,3V和GPIO 6.
The sensor is connected to GND, 3,3V and GPIO 6.
基于
this article 我在C#中重新创建了样本
Based on this article I've recreated the sample in C#
这是我的代码:
using System;
using System.Collections;
using System.Threading.Tasks;
using Windows.Devices.Gpio;
namespace TempHumHeadless
{
class TempHumSensor
{
private const int SENSOR_PIN = 6;
private GpioPin pinSensor;
public TempHumSensor()
{
InitGPIO();
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
throw new Exception("There is no GPIO controller on this device.");
}
pinSensor = gpio.OpenPin(SENSOR_PIN);
if (pinSensor == null)
{
throw new Exception("No GPIO Pins initialized");
}
pinSensor.Write(GpioPinValue.Low);
Task.Delay(5000).Wait();
}
~TempHumSensor()
{
pinSensor.Dispose();
}
private int[] dht11_dat = new int[5] { 0, 0, 0, 0, 0 };
private const int MAX_TIMINGS = 85;
byte counter = 0;
byte j = 0, i;
private GpioPinValue laststate = GpioPinValue.High;
public void ReadValues()
{
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
SetupSensor();
for (i = 0; i < MAX_TIMINGS; i++)
{
counter = 0;
while (pinSensor.Read() == laststate)
{
counter++;
Task.Delay(1);
if (counter == 255)
{
break;
}
}
laststate = pinSensor.Read();
if (counter == 255)
{
break;
}
//跳过前3个样本
if((i> = 4)&&(i%2 == 0))
{
dht11_dat [j / 8]<< = 1;
if(counter> 16)
dht11_dat [j / 8] | = 1;
j ++;
}
}
if((j> = 40)&&(dht11_dat [4] ==((dht11_dat [0] + dht11_dat [1] + dht11_dat [2] + dht11_dat [3])& 0xFF)))
{
if((dht11_dat [0] == 0)&&(dht11_dat [2] = = 0))
返回;
string s = string.Format(" Humidity =%d。%d %% Temperature =%d。%d * C",
dht11_dat [0],dht11_dat [1] ,dht11_dat [2],dht11_dat [3]);
}
}
private void SetupSensor()
{
pinSensor.SetDriveMode(GpioPinDriveMode.Output);
pinSensor.Write(GpioPinValue.Low);
Task.Delay(18).Wait();
pinSensor.Write(GpioPinValue.High);
Task.Delay(40).Wait();
pinSensor.SetDriveMode(GpioPinDriveMode.Input);
}
}
}
// Skip first 3 samples if ((i >= 4) && (i % 2 == 0)) { dht11_dat[j / 8] <<= 1; if (counter > 16) dht11_dat[j / 8] |= 1; j++; } } if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { if ((dht11_dat[0] == 0) && (dht11_dat[2] == 0)) return; string s = string.Format("Humidity = %d.%d %% Temperature = %d.%d * C", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]); } } private void SetupSensor() { pinSensor.SetDriveMode(GpioPinDriveMode.Output); pinSensor.Write(GpioPinValue.Low); Task.Delay(18).Wait(); pinSensor.Write(GpioPinValue.High); Task.Delay(40).Wait(); pinSensor.SetDriveMode(GpioPinDriveMode.Input); } }}
不幸的是我没有命中行,创建字符串的位置。输出总是GPIOValue.High而且永远不会改变。
Unfortunately I don't hit line, where the string is created. The Output is always GPIOValue.High and never changes.
非常感谢任何帮助。
提供此帖子"按现状"没有保证,也没有赋予任何权利。
This posting is provided "AS IS" with no warranties, and confers no rights.
推荐答案
&NBSP; 感谢您的提问。 我已经开始关注它们,但遗憾的是我现在没有立即回复。
如果我确实有跟进问题,我会告诉你,或者如果你在此期间做了任何突破,请让我更新。
Thanks for your question(s). I’ve started to look at them but unfortunately don’t have an immediate answer I can provide you at this time. If I do have follow up questions I’ll let you know, or if you make any break-thrus in the meantime please keep me updated.
这篇关于帮助DHT11温湿度样品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!