问候,
我最近买了一个温湿度传感器(aosong am2302)。我将它连接到 rasp pi 3,它与 adafruit 库的配合非常出色。当我尝试让它在另一 block 板上工作时,问题就出现了(描述为 here )。我用过this library用于GPIO读取。我修改了 beaglebone 文件以进行 gpio 映射,仅此而已。我运行了测试并且它们工作了,所以基本上该库看起来可以工作。所以在那之后,我对传感器读取器进行了编码,但它不起作用,我不知道为什么。 运行传感器读取器后,如果我检查文件系统,gpio 就会被导出。
传感器的安装方式如下: -电源至 5V(也尝试过 3.3v) -VCC 至 GPIO -接地。
这里是代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/wait.h>
#include <sys/time.h>
#include "libsoc_gpio.h" //library for gpio reading
#define MAXTIMINGS 10
#define SILENT 0
int bits[MAXTIMINGS+1],data[5];
int readDHT(int pin,int allowRetry);
int
main(int argc, char **argv)
{ int dhtpin;
if (argc>1)dhtpin=atoi(argv[1]);
else printf("Introduce pin");
if( SILENT < 1 ) {
printf("Using pin #%d\n", dhtpin);
}
readDHT(dhtpin,5);
return 0;
}
int
readDHT(int pin, int allowRetry)
{
int bitidx=0;
int counter = 0;
int i=0,j=0;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
gpio *gpio_output = libsoc_gpio_request(pin, LS_SHARED); //export GPIO
if (gpio_output == NULL)
{
fprintf(stderr, "Failed to open GPIO %d\n", pin);
return -1;
}
libsoc_gpio_set_direction(gpio_output, OUTPUT);
if (libsoc_gpio_get_direction(gpio_output) != OUTPUT)
{
fprintf(stderr, "Failed to set direction to OUTPUT\n");
if (gpio_output)
{
libsoc_gpio_free(gpio_output);
}
return -1;
}
libsoc_gpio_set_level(gpio_output, LOW);
libsoc_gpio_set_level(gpio_output, HIGH);
libsoc_gpio_set_direction(gpio_output, INPUT);
/* Wait for pin to drop */
while (libsoc_gpio_get_level(gpio_output) == HIGH)
{
if(counter++>10000){
printf("ERROR: Pin never dropped\n");
return 1;
}
}
if (i<= MAXTIMINGS)
{
counter =0;
while (libsoc_gpio_get_level(gpio_output) == LOW){
if(counter++ == 1000)
break;
}
counter =0;
while(libsoc_gpio_get_level(gpio_output) == HIGH){
if (counter++==1000)
break;
}
bits[bitidx++] = counter;
i++;
}
/* read data */
for (i = 1; i < bitidx; i++) {
data[j / 8] <<= 1;
if(bits[i]>200){
data[j/8] |= 1;
}
j++;
}
if( SILENT < 1 ) {
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
}
if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
float f, h;
h = data[0] * 256 + data[1];
h /= 10;
f = (data[2] & 0x7F)* 256 + data[3];
f /= 10.0;
if (data[2] & 0x80) f *= -1;
printf("CTemp: %.1f\nFTemp: %.1f\nHum: %.1f%\n", f, ((f*9)/5)+32, h);
} else if( allowRetry > 0 ) {
sleep(1);
if( SILENT < 1 ) {
printf( "Error getting information. Retrying\n" );
}
return readDHT(pin, --allowRetry );
} else {
if( SILENT < 1 ) {
printf( "Error getting information. Retries exhausted.\n" );
}
return 1;
}
return 0;
if (gpio_output)
{
libsoc_gpio_free(gpio_output);
}
/* Check we got all the data and checksum matches */
}
使用此代码,我得到“Pin 从未掉落”,因此 pin 永远不会变为 0,因此它不会报告数据。所以我决定尝试使用 bash,看看 pin 是否会下降到 0。我的编码与之前的代码相同,但在 bash 中并查看 pin 的值(始终为 1,不会下降)。到了这一点,我用完了所有选项,传感器可以工作(它没有损坏),库可以工作,但是这台机器上的传感器没有。关于如何找到解决方案的任何线索或想法?
谢谢:)
最佳答案
尝试一些可能会让你前进的事情......
首先将计数器“counter++>10000”增加到更大的值,可能是您正在运行的新处理器以更快的速度增加计数器,并且您只是在引脚下降之前超时。
如果这不起作用,请删除源中的超时计数器和永久循环,同时删除传感器,然后用一根电线将数据线物理拉到地,看看您的代码/新处理器是否捕获信号发生变化,至少您知道您的源/硬件配置正确,以便您可以将精力集中在其他地方。
仔细检查新处理器“高”电平与传感器“高”阈值之间的电压兼容性,以确保它在数据线上捕获“1”。
让我们知道你进展如何!
托尼
关于c - 温度传感器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46324018/