Windows 10 IoT Raspberry Pi 2:DHT22 / AM2302

时间:2023-01-13 07:29:46

I just wanted to start making experience with the DHT22/AM2302 (a temperature and humidity sensor), but I have no idea how to initialize and get the data of it ... I tried to use GpioPin:

我只是想开始使用DHT22 / AM2302(温度和湿度传感器),但我不知道如何初始化并获取它的数据...我试图使用GpioPin:

gpioController = GpioController.GetDefault();
if(gpioController == null)
{
    Debug.WriteLine("GpioController Initialization failed.");
    return;
}
sensorPin = gpioController.OpenPin(7); //Exception throws here
sensorPin.SetDriveMode(GpioPinDriveMode.Input);
Debug.WriteLine(sensorPin.Read());

but get the exception: "A resource required for this operation is disabled."

但得到异常:“禁用此操作所需的资源。”

After that I took a look at the library for the unixoids and found this:

之后,我看了一下unixoids的库,发现了这个:

https://github.com/technion/lol_dht22/blob/master/dht22.c

But I have no idea how to realize that in VCSharp using Windows 10, anyone an idea or experience?

但我不知道如何在使用Windows 10的VCSharp中实现这一点,任何人都有想法或经验?

Thank you very much in advance!

非常感谢你提前!

UPDATE:

I got the hint, that there is not GPIO-Pin 7 and this is true, so I re-tried it, but the GPIO-Output seems to be just HIGH or LOW ... So I have to use the I2C or the SPI ... According to this Project, I decided to try it out with SPI: http://microsoft.hackster.io/windowsiot/temperature-sensor-sample and making steps forward ... The difficulty now is to translate the above linked C-Library to the C-Sharp-SDK to receive the right data ...

我得到了提示,没有GPIO-Pin 7,这是真的,所以我重新尝试了,但GPIO输出似乎只是高或低...所以我必须使用I2C或SPI ...根据这个项目,我决定尝试使用SPI:http://microsoft.hackster.io/windowsiot/temperature-sensor-sample并向前迈进......现在的困难是翻译上面的链接C-Library到C-Sharp-SDK接收正确的数据......

private async void InitSPI()
{
    try
    {
        var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
        settings.ClockFrequency = 500000;
        settings.Mode = SpiMode.Mode0;

        string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME);
        var deviceInfo = await DeviceInformation.FindAllAsync(spiAqs);
        SpiDisplay = await SpiDevice.FromIdAsync(deviceInfo[0].Id, settings);
    }
    catch(Exception ex)
    {
        Debug.WriteLine("SPI Initialization failed: " + ex.Message);
    }
}

This works not so well, to be clear: It works just once on starting up the raspberry pi2, then starting / remote debugging the application, but after exiting the application and re-start them, the SPI Initialization fails.

这个工作效果不太好,需要明确的是:它只启动一次启动覆盆子pi2,然后启动/远程调试应用程序,但退出应用程序并重新启动后,SPI初始化失败。

And now Im working on reading the data from the pin and will show some Code in a future update. Any comments, answers and or advices are still welcome.

现在我正在努力从引脚读取数据,并在将来的更新中显示一些代码。任何评论,答案和建议仍然是受欢迎的。

1 个解决方案

#1


DHT22 requires very precise timing. Although Raspberry PI/Windows 10 IoT core is extremely fast, since it's an operating system where other things need to happen unless you write some sort of low-level driver (not C#) you won't be able to generate the timings necessary to communicate with a DHT22.

DHT22需要非常精确的定时。虽然Raspberry PI / Windows 10物联网核心速度非常快,但由于它是一个操作系统,除非你编写某种低级驱动程序(不是C#),否则需要进行其他操作,否则你将无法生成通信所需的时间用DHT22。

What I do is use a cheap Arduino Mini Pro for about $5 with the sole purpose to generate and send the correct timings between the microcontroller and the Raspberry Pi, then setup some sort of communication channel between the Arduino Mini Pro (I2C, Serial) to pull the data from the Arduino.

我所做的是以5美元左右的价格使用廉价的Arduino Mini Pro,其唯一目的是在微控制器和Raspberry Pi之间生成并发送正确的时序,然后在Arduino Mini Pro(I2C,Serial)之间设置某种通信通道。从Arduino中提取数据。

#1


DHT22 requires very precise timing. Although Raspberry PI/Windows 10 IoT core is extremely fast, since it's an operating system where other things need to happen unless you write some sort of low-level driver (not C#) you won't be able to generate the timings necessary to communicate with a DHT22.

DHT22需要非常精确的定时。虽然Raspberry PI / Windows 10物联网核心速度非常快,但由于它是一个操作系统,除非你编写某种低级驱动程序(不是C#),否则需要进行其他操作,否则你将无法生成通信所需的时间用DHT22。

What I do is use a cheap Arduino Mini Pro for about $5 with the sole purpose to generate and send the correct timings between the microcontroller and the Raspberry Pi, then setup some sort of communication channel between the Arduino Mini Pro (I2C, Serial) to pull the data from the Arduino.

我所做的是以5美元左右的价格使用廉价的Arduino Mini Pro,其唯一目的是在微控制器和Raspberry Pi之间生成并发送正确的时序,然后在Arduino Mini Pro(I2C,Serial)之间设置某种通信通道。从Arduino中提取数据。