0%

golang读取dht11温湿度器有感

最近疫情期间,想起了早已废弃的树莓派B版了,除了树莓派,还买了一大堆传感器;其中温湿度dht11传感器,算是应用最为广泛的,也最为01基础的,将来在自动化种植和饲养中,首先要应用的传感器,使用golang读取时序性要求高的应用,看来还是有些吃力,接下来将使用stm32代替树莓派,freeRTOS来与各种传感器打交道,可能不会有这么多弯路可走。

通过阅读dht11的时间序列和运行原理,使用golang重新写了一次驱动,结果让人很是烦恼。主要是时间精度不够,导致读数不准的问题,这个难以克服!后来想到了数据采集采用c语言编写,然后使用
go去一次性分析这个办法,结果发现github上早已经有实现的项目:
go-dht

以下是记录原理和接线的原理:
dht11的时序读取,皆是通过低电平触发开始,高电平结束;除了主控发送信号给dht11开始信号,是首先发送18ms
的低电平,然后拉高40us等待dht11回应之后,后面皆是dht11的响应和数据回复,而且时间极短;dht11的响应是
80us的低电平响应,然后恢复到高电平80us等待主控读取数据;读取数据是准备是以50us低电平等待,以高电平的长短表示0或者1来结束,40bit读取完毕以后,dht11自动拉高电平。

具体时序图可以参考这里

接线原理图如下图所示:

保证5V电压,也可以直接接树莓派的5v接口。同时需要注意GPIO口的引脚分布图,对应实物的位置,不要将线接错

golang版本(go-dht)

树莓派上的raspbian版本为4.19.97
go的版本是go1.12.15,armv6l平台的,直接下载,不要自行编译!

golang版本的实现依然是通过c语言编写,一次性采集数据回golang中进行解析,关键代码是调用了系统的高精度计时器clock_gettime。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
static int gpio_read_seq_until_timeout(Pin *pin,
int32_t timeout_msec, int32_t **arr, int32_t *len, Error **err) {
// ......
// 记录第一次的时间戳
clock_gettime(CLOCK_KIND, &last_t);

for (;;)
{
next_v = gpio_read(pin, err);
if (-1 == next_v) {
create_error(err, "failed to read value");
return -1;
}

// Check for edge trigger event.
// 触发一次高低电平的转换时
// 记录上一次到这一次所用的时间us
if (last_v != next_v) {
clock_gettime(CLOCK_KIND, &next_t);
i = 0;
k++;
if (k > MAX_PULSE_COUNT-1) {
create_error(err, "pulse count exceed limit in %d", MAX_PULSE_COUNT);
return -1;
}
values[k*2] = next_v;
// Save time duration in microseconds of last edge level.
values[k*2-1] = convert_timespec_to_usec(next_t) -
convert_timespec_to_usec(last_t);
last_v = next_v;
last_t = next_t;
}
// ......
}
// ......
return 0;
}

附录c语言版本

附录的这个c语言版本需要安装wiringPi库,请自行搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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; /* fahrenheit */

dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

/* pull pin down for 18 milliseconds */
pinMode( DHTPIN, OUTPUT );
digitalWrite( DHTPIN, LOW );
delay( 18 );
/* then pull it up for 40 microseconds */
digitalWrite( DHTPIN, HIGH );
delayMicroseconds( 40 );
/* prepare to read the pin */
pinMode( DHTPIN, INPUT );

/* detect change and read data */
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;

/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
dht11_dat[j / 8] <<= 1;
if ( counter > 16 )
dht11_dat[j / 8] |= 1;
j++;
}
}

/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
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 ); /* wait 1sec to refresh */
}

return(0);
}