温度传感器不工作
Temperature sensor not working
问候,
最近买了一个温湿度传感器(aosong am2302)。我把它连接到一个 rasp pi 3 上,它与 adafruit 库一起工作就像魅力一样。当我试图让它在另一块板上工作时,问题就来了(描述 here). I used this library 用于 gpio 读取。我修改了用于 gpio 映射的 beaglebone 文件,仅此而已。我 运行 测试并且它们工作,所以基本上 lib 看起来可以工作。所以在那之后,我对传感器 reader 进行编码,但它不工作,我不知道为什么。
在我 运行 传感器 reader 之后,如果我检查文件系统,则导出 gpio。
传感器是这样安装的:
-电源至 5V(也尝试使用 3.3v)
-VCC 到 GPIO
-接地到 GND。
这里是代码
#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 never dropped",因此 pin 永远不会变为 0,因此它不会报告数据。所以我决定尝试使用 bash 并查看 pin 是否下降到 0。我编码与之前的代码相同但在 bash 中并查看 pin 的值(始终为 1,不下降)。谈到这一点,我 运行 没有选择,传感器工作(它没有坏),图书馆工作但这台机器上的传感器没有。关于如何找到解决方案的任何线索或想法?
谢谢:)
尝试一些可能会让你前进的事情....
首先将计数器 "counter++>10000" 增加到更大的值,可能是您使用的新处理器 运行 以更快的速度递增计数器,您只是超时在别针掉落之前。
如果这不起作用,请移除超时计数器并在您的源中永远循环,同时移除传感器,然后用一根电线将数据线物理拉到地面,看看您的 code/new 处理器捕获信号变化,至少你知道你的 source/hardware 配置正确,这样你就可以将精力集中在其他地方。
仔细检查新处理器 'high' 级别与传感器 'high' 阈值之间的电压兼容性,以确保它在数据上捕获 'one'线.
让我们知道你过得怎么样!
托尼
问候,
最近买了一个温湿度传感器(aosong am2302)。我把它连接到一个 rasp pi 3 上,它与 adafruit 库一起工作就像魅力一样。当我试图让它在另一块板上工作时,问题就来了(描述 here). I used this library 用于 gpio 读取。我修改了用于 gpio 映射的 beaglebone 文件,仅此而已。我 运行 测试并且它们工作,所以基本上 lib 看起来可以工作。所以在那之后,我对传感器 reader 进行编码,但它不工作,我不知道为什么。 在我 运行 传感器 reader 之后,如果我检查文件系统,则导出 gpio。
传感器是这样安装的: -电源至 5V(也尝试使用 3.3v) -VCC 到 GPIO -接地到 GND。
这里是代码
#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 never dropped",因此 pin 永远不会变为 0,因此它不会报告数据。所以我决定尝试使用 bash 并查看 pin 是否下降到 0。我编码与之前的代码相同但在 bash 中并查看 pin 的值(始终为 1,不下降)。谈到这一点,我 运行 没有选择,传感器工作(它没有坏),图书馆工作但这台机器上的传感器没有。关于如何找到解决方案的任何线索或想法?
谢谢:)
尝试一些可能会让你前进的事情....
首先将计数器 "counter++>10000" 增加到更大的值,可能是您使用的新处理器 运行 以更快的速度递增计数器,您只是超时在别针掉落之前。
如果这不起作用,请移除超时计数器并在您的源中永远循环,同时移除传感器,然后用一根电线将数据线物理拉到地面,看看您的 code/new 处理器捕获信号变化,至少你知道你的 source/hardware 配置正确,这样你就可以将精力集中在其他地方。
仔细检查新处理器 'high' 级别与传感器 'high' 阈值之间的电压兼容性,以确保它在数据上捕获 'one'线.
让我们知道你过得怎么样!
托尼