forked from vmilea/pico_dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht_example.c
42 lines (35 loc) · 1.15 KB
/
dht_example.c
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
/*
* Copyright (c) 2021 Valentin Milea <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#include <dht.h>
#include <pico/stdlib.h>
#include <stdio.h>
// change this to match your setup
static const dht_model_t DHT_MODEL = DHT22;
static const uint DATA_PIN = 15;
static float celsius_to_fahrenheit(float temperature) {
return temperature * (9.0f / 5) + 32;
}
int main() {
stdio_init_all();
puts("\nDHT test");
dht_t dht;
dht_init(&dht, DHT_MODEL, pio0, DATA_PIN, true /* pull_up */);
do {
dht_start_measurement(&dht);
float humidity;
float temperature_c;
dht_result_t result = dht_finish_measurement_blocking(&dht, &humidity, &temperature_c);
if (result == DHT_RESULT_OK) {
printf("%.1f C (%.1f F), %.1f%% humidity\n", temperature_c, celsius_to_fahrenheit(temperature_c), humidity);
} else if (result == DHT_RESULT_TIMEOUT) {
puts("DHT sensor not responding. Please check your wiring.");
} else {
assert(result == DHT_RESULT_BAD_CHECKSUM);
puts("Bad checksum");
}
sleep_ms(2000);
} while (true);
}