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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package dht11
import (
"errors"
"log"
"time"
"github.com/stianeikeland/go-rpio"
)
type tempState int
const (
initPullDown tempState = iota
initPullUp
dataFirstPullDown
dataPullUp
dataPullDown
)
type SensorData struct {
TempC int
TempF float32
Humidity int
}
type Device struct {
StartLowTime time.Duration
LowTime time.Duration
HighTime time.Duration
ReadDelay time.Duration
}
var RaspberryPi Device
func init() {
// These were determined experimentally using a logic analyser a Raspberry
// Pi 3 B+. They may vary for other device types and processor speeds.
RaspberryPi = Device{
StartLowTime: 4 * time.Microsecond,
LowTime: 15 * time.Millisecond,
HighTime: 50 * time.Millisecond,
ReadDelay: 1 * time.Microsecond,
}
}
type DHT11 struct {
Device *Device
Pin *rpio.Pin
}
func NewRaspberryPiDHT11(pin *rpio.Pin) *DHT11 {
return &DHT11{
Device: &RaspberryPi,
Pin: pin,
}
}
func (d *DHT11) sendDataRequest() {
d.Pin.Output()
d.Pin.Low()
time.Sleep(d.Device.StartLowTime)
d.Pin.High()
time.Sleep(d.Device.HighTime)
d.Pin.Low()
time.Sleep(d.Device.LowTime)
d.Pin.Input()
d.Pin.PullUp()
}
func (d *DHT11) gatherRawData() []rpio.State {
var i int
var last rpio.State
data := make([]rpio.State, 0)
for i = 0; i < 500; i++ {
current := d.Pin.Read()
time.Sleep(d.Device.ReadDelay)
data = append(data, current)
if last != current {
i = 0
last = current
}
}
return data
}
func (d *DHT11) parseSignals(input []rpio.State) ([]int, error) {
state := initPullDown
lengths := make([]int, 0, 40)
currentLength := 0
for _, current := range input {
currentLength++
switch state {
case initPullDown:
if current == rpio.Low {
state = initPullUp
}
case initPullUp:
if current == rpio.High {
state = dataFirstPullDown
}
case dataFirstPullDown:
if current == rpio.Low {
state = dataPullUp
}
case dataPullUp:
if current == rpio.High {
currentLength = 0
state = dataPullDown
}
case dataPullDown:
if current == rpio.Low {
lengths = append(lengths, currentLength)
state = dataPullUp
}
}
}
if len(lengths) != 40 {
return nil, errors.New("not enough data returned")
}
return lengths, nil
}
func (d *DHT11) signalsToBytes(lengths []int) ([]int, error) {
bytes := make([]int, 0, 5)
longestUp, shortestUp := 0, 1000
for _, length := range lengths {
if length < shortestUp {
shortestUp = length
}
if length > longestUp {
longestUp = length
}
}
halfway := shortestUp + (longestUp-shortestUp)/2
bits := make([]bool, 0, len(lengths))
for _, length := range lengths {
bit := false
if length > halfway {
bit = true
}
bits = append(bits, bit)
}
currentByte := 0
for i, bit := range bits {
currentByte <<= 1
if bit {
currentByte |= 1
} else {
currentByte |= 0
}
if (i+1)%8 == 0 {
bytes = append(bytes, currentByte)
currentByte = 0
}
}
checksum := (bytes[0] + bytes[1] + bytes[2] + bytes[3]) & 255
if bytes[4] != checksum {
return nil, errors.New("CRC error")
}
return bytes, nil
}
func (d *DHT11) GetSensorData() (*SensorData, error) {
d.sendDataRequest()
rawData := d.gatherRawData()
signals, err := d.parseSignals(rawData)
if err != nil {
return nil, err
}
dataBytes, err := d.signalsToBytes(signals)
if err != nil {
return nil, err
}
return &SensorData{
TempC: dataBytes[2],
TempF: celsiusToFarenheit(dataBytes[2]),
Humidity: dataBytes[0],
}, nil
}
func (d *DHT11) GetSensorDataWithRetry(rc int) (data *SensorData, err error) {
for i := 0; i < rc; i++ {
data, err = d.GetSensorData()
if err == nil {
return
} else {
log.Printf("error")
time.Sleep(1 * time.Second)
continue
}
}
return
}
func celsiusToFarenheit(c int) float32 {
return float32(c)*1.8 + 32
}
|