-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHT20.h
148 lines (124 loc) · 2.9 KB
/
DHT20.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Aosong AHT20/DHT20 sensor library for Raspberry Pi Pico.
Version: 0.0.1
Copyright 2022 Sampsa Penna, Kimi Malkamäki
*/
#ifndef PICO_DHT20_DEFINED
#define PICO_DHT20_DEFINED
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdint.h>
#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
#define DHT20_ERROR_CONNECT -11
#define DHT20_MISSING_BYTES -12
#define DHT20_ERROR_BYTES_ALL_ZERO -13
#define DHT20_ERROR_READ_TIMEOUT -14
#define DHT20_ERROR_LASTREAD -15
#define DHT20_ERROR_NORESET -16
#define DHT20_ERROR_BUSY -17
#ifdef USE_I2C_1
#define I2C_INST i2c1
#else
#define I2C_INST i2c0
#endif
#define DHT20_ADDRESS 0x38
typedef struct DHT20
{
float humidity;
float temperature;
float humOffset;
float tempOffset;
uint8_t status;
uint32_t lastRequest;
uint32_t lastRead;
uint32_t updateInterval;
uint8_t bytes[7];
uint8_t crc;
} DHT20;
/*
Prep constant i2c messages in program memory
*/
// Resets
static const uint8_t __in_flash() rst_msg_1[3] = {0x1B, 0x00, 0x00};
static const uint8_t __in_flash() rst_msg_2[3] = {0x1C, 0x00, 0x00};
static const uint8_t __in_flash() rst_msg_3[3] = {0x1E, 0x00, 0x00};
// Trigger measurement
static const uint8_t __in_flash() trigger_measurement[3] = {0xAC, 0x33, 0x00};
/*
Initialize the DHT sensor
*/
int DHT20_init(DHT20 *sens);
/*
Calculate checksum value (private)
*/
static uint8_t _crc8(uint8_t *ptr, uint8_t len);
/*
Reset sensor (private)
*/
static void resetSensor(DHT20 *sens);
/*
Start taking a temperature measurement
*/
int startMeasurement(DHT20 *sens);
/*
Read the finished measurement
*/
int readMeasurement(DHT20 *sens);
/*
Convert the raw data to sensible values
*/
int convert(DHT20 *sens);
/*
Take a complete measurement from the sensor without external timing
*/
int getMeasurement(DHT20 *sens);
/*
updateMeasurements this function does not sleep and updates based on the updateInterval variable
*/
int updateMeasurement(DHT20 *sens);
/*
Access the converted temperature & humidity
*/
float getHumidity(DHT20 *sens);
float getTemperature(DHT20 *sens);
/*
Offset getters and setters
*/
void setHumOffset(DHT20 *sens, float offset);
void setTempOffset(DHT20 *sens, float offset);
float getHumOffset(DHT20 *sens);
float getTempOffset(DHT20 *sens);
/*
set updateInterval for the updateMeasurement function. this interval should be over 1000ms
*/
void setUpdateInterval(struct DHT20 *sens, uint8_t time);
/*
Check the sensor status word
*/
uint8_t readStatus(DHT20 *sens);
/*
Check sensor calibration status
*/
bool isCalibrated(DHT20 *sens);
/*
Check if sensor needs a reset
*/
static bool needsReset(DHT20 *sens);
/*
Check status from last completed read()
*/
int internalStatus(DHT20 *sens);
/*
Check time since last read() call
*/
uint32_t lastRead(DHT20 *sens);
/*
Check time since last measurement request
*/
uint32_t lastRequest(DHT20 *sens);
#endif