自制温湿度计备忘 所需软件库 接线方式 Arduino程序

15322390095832

序号 设备 数量 备注
1 Arduino 1 我用的是 Arduino UNO
2 OLED 显示屏 1 我用的是 IIC 的
3 温湿度传感器 我用 DHT22

所需软件库

序号 库名 作用 备注
1 U8glib 显示屏驱动
2 Wire 单线通讯库 用于DHT22通讯
3 dht DHT22的驱动

接线方式

Arduino 接口 设备接口
5V OLED 屏 VCC
3V DHT22传感器 VCC
GND 所有GND
A4 OLED 屏 SDA
A5 OLED 屏 SCL
02 DHT22 第2号引脚(VCC 旁边那个)

Arduino程序

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  
#include <Wire.h>
#include <dht.h>

U8GLIB_SSD1306_128X64 (U8G_I2C_OPT_NONE);
dht DHT;

#define DHT22_PIN 2

struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0,0,0,0,0,0,0,0};

int humidity;
double temperature;
double temperatureF;
int dewPoint1;
String thisTemp = "";
String thisHumidity = "";
String thisDewPoint = "";

void draw(void) {
u8g.setFont(u8g_font_9x15);
u8g.drawStr(39,10, "BD7IWD");
u8g.setFont(u8g_font_9x15);

thisTemp = String(temperature) + "260C";
const char* newTempC = (const char*) thisTemp.c_str();
u8g.drawStr(55,35, newTempC);
u8g.setFont(u8g_font_9x15);
u8g.drawStr(8,35, "Temp=");
u8g.setFont(u8g_font_9x15);
thisHumidity = String(humidity) + "%";
const char* newHumidity = (const char*) thisHumidity.c_str();
u8g.drawStr(95,55, newHumidity);
u8g.setFont(u8g_font_9x15);
u8g.drawStr(15,55, "humidity=");
}

void setup(void) {
Serial.begin(9600);
Wire.begin();
Serial.println("DHT22");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
}

void loop(void) {
uint32_t start = micros();
int chk = DHT.read22(DHT22_PIN);
uint32_t stop = micros();
stat.total++;
switch (chk)
{
case DHTLIB_OK:
stat.ok++;
Serial.print("OK,t");
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
Serial.print("Checksum error,t");
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
Serial.print("Time out error,t");
break;
case DHTLIB_ERROR_CONNECT:
stat.connect++;
Serial.print("Connect error,t");
break;
case DHTLIB_ERROR_ACK_L:
stat.ack_l++;
Serial.print("Ack Low error,t");
break;
case DHTLIB_ERROR_ACK_H:
stat.ack_h++;
Serial.print("Ack High error,t");
break;
default:
stat.unknown++;
Serial.print("Unknown error,t");
break;
}
temperature = DHT.temperature;
humidity = DHT.humidity;
dewPoint(temperature,humidity);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(2000);
}

double dewPoint(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
dewPoint1 = (b * temp) / (a - temp);
}


版权声明

CC

得未曾有 by 野鬼|BD7IWD is licensed under a Creative Commons BY-NC-ND 4.0 International License.
野鬼|BD7IWD 创作并维护的得未曾有采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于【得未曾有 | BD7IWD】,版权所有,侵权必究。


本文永久链接:http://www.bd7iwd.com/posts/14429/