-
Notifications
You must be signed in to change notification settings - Fork 0
/
htu21.go
executable file
·152 lines (125 loc) · 3.02 KB
/
htu21.go
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
149
150
151
152
package htu21
import (
"fmt"
"sync"
"time"
"github.com/sigurn/crc8"
"periph.io/x/conn/v3"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/physic"
)
// NewI2C returns an object that communicates over I²C to HTU21
// environmental sensor.
//
// HTU21 I²C device address is 0x40, according to the datasheet
// https://www.te.com/usa-en/models/4/00/028/790/CAT-HSC0004.html
func NewI2C(b i2c.Bus, addr uint16) (*Dev, error) {
d := &Dev{d: &i2c.Dev{Bus: b, Addr: addr}}
if err := d.Reset(); err != nil {
return nil, err
}
return d, nil
}
// Dev is a handle to an initialized HTU21 device.
type Dev struct {
d conn.Conn
mx sync.Mutex
}
// Performs soft reset of the device
// 0xFE - code for soft reset
// 15 milliseconds delay follows
func (d *Dev) Reset() error {
d.mx.Lock()
defer d.mx.Unlock()
err := d.writeCommands([]byte{0xFE})
if err != nil {
return d.wrap(err)
}
time.Sleep(time.Millisecond * 15)
return nil
}
// Sense requests a one time measurement as °C and % of relative humidity.
// 0xF3, delay 50 ms - temperature
// 0xF5, delay 16 ms - humidity
func (d *Dev) Sense(e *physic.Env) error {
d.mx.Lock()
defer d.mx.Unlock()
// Temperature | 0xF3, delay 50 ms
err := d.writeCommands([]byte{0xF3})
if err != nil {
return d.wrap(err)
}
time.Sleep(50 * time.Millisecond)
rawData, err := d.read()
if err != nil {
return d.wrap(err)
}
data, err := d.validate(rawData)
if err != nil {
return d.wrap(err)
}
temp := -46.85 + (175.72 * (float32(data) / 65536))
e.Temperature.Set(fmt.Sprintf("%fC", temp))
// Humidity | 0xF5, delay 16 ms
err = d.writeCommands([]byte{0xF5})
if err != nil {
return d.wrap(err)
}
time.Sleep(16 * time.Millisecond)
rawData, err = d.read()
if err != nil {
return d.wrap(err)
}
data, err = d.validate(rawData)
if err != nil {
return d.wrap(err)
}
var humidity = float32(data)
humidity = -6 + (125 * (humidity / 65536))
e.Humidity.Set(fmt.Sprintf("%f%%", humidity))
return nil
}
var crc8Table = crc8.MakeTable(crc8.Params{
Poly: 0x31,
Init: 0x0,
RefIn: false,
RefOut: false,
XorOut: 0x0,
Check: 0x0,
})
// CRC Checksum using the polynomial given in the datasheet
func validateCrc(data []byte, expectedCrc byte) error {
checksum := crc8.Checksum(data, crc8Table)
if expectedCrc != checksum {
return fmt.Errorf("CRC Checksum error. Expected %v, actual %v", expectedCrc, checksum)
}
return nil
}
func (d *Dev) validate(rawData []byte) (uint16, error) {
if e := validateCrc(rawData[:2], rawData[2]); e != nil {
return 0, e
}
data := uint16(rawData[0])
data <<= 8
data |= uint16(rawData[1])
return data, nil
}
// writeCommands writes a command to the device.
// Warning: b may be modified!
func (d *Dev) writeCommands(b []byte) error {
if err := d.d.Tx(b, nil); err != nil {
return d.wrap(err)
}
return nil
}
// read from the device
func (d *Dev) read() ([]byte, error) {
b := make([]byte, 3)
if err := d.d.Tx(nil, b); err != nil {
return nil, d.wrap(err)
}
return b, nil
}
func (d *Dev) wrap(err error) error {
return fmt.Errorf("%e", err)
}