用于读取Raspberry pi上传感器数据的库>> [

问题描述 投票:0回答:1
最近我一直在玩Raspberry pi上的GPIO引脚。在尝试温度传感器(特别是DHT11)并尝试找到一些代码使其工作时,我注意到了一些对我来说似乎很奇怪的东西。我找到的所有代码都是使用特定于传感器类型的库

类似于此python代码:

import sys import Adafruit_DHT while True: humidity, temperature = Adafruit_DHT.read_retry(11, 4) print 'Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(temperature, humidity)

或像这样的C代码完全从头开始实现:

#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define MAXTIMINGS 85 #define DHTPIN 7 int dht11_dat[5] = { 0, 0, 0, 0, 0 }; void read_dht11_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; float f; dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0; pinMode( DHTPIN, OUTPUT ); digitalWrite( DHTPIN, LOW ); delay( 18 ); digitalWrite( DHTPIN, HIGH ); delayMicroseconds( 40 ); pinMode( DHTPIN, INPUT ); for ( i = 0; i < MAXTIMINGS; i++ ) { counter = 0; while ( digitalRead( DHTPIN ) == laststate ) { counter++; delayMicroseconds( 1 ); if ( counter == 255 ) { break; } } laststate = digitalRead( DHTPIN ); if ( counter == 255 ) break; 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) ) ) { f = dht11_dat[2] * 9. / 5. + 32; printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f ); }else { printf( "Data not good, skip\n" ); } } int main( void ) { printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" ); if ( wiringPiSetup() == -1 ) exit( 1 ); while ( 1 ) { read_dht11_dat(); delay( 1000 ); } return(0); }

所以我想知道为什么没有c库会只返回从特定引脚读取的所有传感器的原始数据

基本接管此部分:

void read_dht11_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; float f; dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0; pinMode( DHTPIN, OUTPUT ); digitalWrite( DHTPIN, LOW ); delay( 18 ); digitalWrite( DHTPIN, HIGH ); delayMicroseconds( 40 ); pinMode( DHTPIN, INPUT ); for ( i = 0; i < MAXTIMINGS; i++ ) { counter = 0; while ( digitalRead( DHTPIN ) == laststate ) { counter++; delayMicroseconds( 1 ); if ( counter == 255 ) { break; } } laststate = digitalRead( DHTPIN ); if ( counter == 255 ) break; 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) ) ) { f = dht11_dat[2] * 9. / 5. + 32; printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f ); }else { printf( "Data not good, skip\n" ); } }

为什么?

最近我一直在玩Raspberry pi上的GPIO引脚。在尝试温度传感器(特别是DHT11)并尝试查找一些代码以使其工作时,我注意到了一些...

python c raspberry-pi microcontroller
1个回答
0
投票
首先,您需要知道温度传感器通过单个GPIO引脚(可以为HIGH或LOW)传递数据。该传感器的温度数据每个由5个字节组成。这5个字节将存储在dht11_dat数组中:
© www.soinside.com 2019 - 2024. All rights reserved.