使用Raspberry Pi 3 + Windows 10 IoT Core + DHT22

时间:2016-04-28 13:08:49

标签: windows-10-iot-core

想知道Raspberry Pi 3 + Windows 10 IoT Core + DHT22是否有任何样品接线和代码?谢谢!

3 个答案:

答案 0 :(得分:0)

static class DHT22
{
    private const int intGpioData = 18;
    private static GpioPin gpioData;

    public static void Start()
    {
        //Init GPIO And Capture Data Every 2000ms
        GpioController gpioController = GpioController.GetDefault();
        gpioData = gpioController.OpenPin(intGpioData);
        gpioData.SetDriveMode(GpioPinDriveMode.InputPullUp);
        new Timer(new TimerCallback((obj) => { GetData(); }), null, 2000, 2000);
    }

    private static void GetData()
    {
        byte[] data = new byte[40];
        gpioData.SetDriveMode(GpioPinDriveMode.Output);
        gpioData.Write(GpioPinValue.Low);
        Task.Delay(1).Wait();
        gpioData.SetDriveMode(GpioPinDriveMode.InputPullUp);

        //Record Data
        while (gpioData.Read() == GpioPinValue.High) ;
        while (gpioData.Read() == GpioPinValue.Low) ;
        while (gpioData.Read() == GpioPinValue.High) ;
        byte low;
        for (int i = 0; i < 40; i++)
        {
            low = 0;
            data[i] = 0;
            while (gpioData.Read() == GpioPinValue.Low && low <= byte.MaxValue) 
                   low++;
            while (gpioData.Read() == GpioPinValue.High && data[i] <= byte.MaxValue) 
                   data[i]++;
        }

        //Analyze Data
        byte humiH = 0;
        byte humiL = 0;
        byte tempH = 0;
        byte tempL = 0;
        byte sum = 0;
        for (short i = 7; i >= 0; i--)
        {
            byte bit = data[7 - i] >= 11 ? (byte)1 : (byte)0;
            humiH += (byte)(bit << i);
        }
        for (short i = 7; i >= 0; i--)
        {
            byte bit = data[15 - i] >= 11 ? (byte)1 : (byte)0;
            humiL += (byte)(bit << i);
        }
        for (short i = 7; i >= 0; i--)
        {
            byte bit = data[23 - i] >= 11 ? (byte)1 : (byte)0;
            tempH += (byte)(bit << i);
        }
        for (short i = 7; i >= 0; i--)
        {
            byte bit = data[31 - i] >= 11 ? (byte)1 : (byte)0;
            tempL += (byte)(bit << i);
        }
        for (short i = 7; i >= 0; i--)
        {
            byte bit = data[39 - i] >= 11 ? (byte)1 : (byte)0;
            sum += (byte)(bit << i);
        }

        //Verify Data
        if ((byte)(humiH + humiL + tempH + tempL) == sum)
        {
            double humidity = (double)(humiH * 256 + humiL) / 10;
            double temperature = (double)(tempH * 256 + tempL) / 10;
            Debug.WriteLine(humidity + "% " + temperature + "°C");
        }
    }
}

答案 1 :(得分:0)

如果你进入教程标签,它会显示一些示例代码和用于点亮LED的接线。 我希望这能回答你的问题。

CodeMaster5678

答案 2 :(得分:0)

如果您使用的是C ++,这是一个例子。 https://github.com/Microsoft/Windows-iotcore-samples/tree/develop/Samples/GpioOneWire

另外,如果你想坚持使用C#,这确实有效。我今天用2个dht11传感器。

这是github链接: https://github.com/porrey/Dht

Nuget包: https://www.nuget.org/packages/Dht/

如果您对C ++感到满意,那就使用它。如果不是,我推荐Nuget包,只是因为我确认它今天有效。 dht11传感器似乎有很多差异。我在两个传感器上有3度不同。