mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 21:03:23 +00:00
dht: fix humidity and temperature extraction for DHT22 (#358)
This commit is contained in:
committed by
Ron Evans
parent
45dce188f5
commit
02e4548966
@@ -231,12 +231,12 @@ endif
|
|||||||
DRIVERS = $(wildcard */)
|
DRIVERS = $(wildcard */)
|
||||||
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
|
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
|
||||||
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
|
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
|
||||||
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x dht keypad4x4 max72xx p1am tone tm1637 \
|
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x keypad4x4 max72xx p1am tone tm1637 \
|
||||||
pcf8563 mcp2515 servo sdcard rtl8720dn image cmd i2csoft hts221 lps22hb apds9960 axp192 xpt2046 \
|
pcf8563 mcp2515 servo sdcard rtl8720dn image cmd i2csoft hts221 lps22hb apds9960 axp192 xpt2046 \
|
||||||
ft6336 sx126x ssd1289
|
ft6336 sx126x ssd1289
|
||||||
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
|
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
|
||||||
|
|
||||||
unit-test:
|
unit-test:
|
||||||
@go test -v $(addprefix ./,$(TESTS))
|
@go test -v $(addprefix ./,$(TESTS))
|
||||||
|
|
||||||
test: clean fmt-check unit-test smoke-test
|
test: clean fmt-check unit-test smoke-test
|
||||||
|
|||||||
+3
-27
@@ -1,3 +1,6 @@
|
|||||||
|
//go:build tinygo
|
||||||
|
// +build tinygo
|
||||||
|
|
||||||
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
||||||
//
|
//
|
||||||
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
||||||
@@ -7,34 +10,10 @@
|
|||||||
package dht // import "tinygo.org/x/drivers/dht"
|
package dht // import "tinygo.org/x/drivers/dht"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// enum type for device type
|
|
||||||
type DeviceType uint8
|
|
||||||
|
|
||||||
// DeviceType specific parsing of information received from the sensor
|
|
||||||
func (d DeviceType) extractData(buf []byte) (temp int16, hum uint16) {
|
|
||||||
if d == DHT11 {
|
|
||||||
temp = int16(buf[2])
|
|
||||||
if buf[3]&0x80 > 0 {
|
|
||||||
temp = -1 - temp
|
|
||||||
}
|
|
||||||
temp *= 10
|
|
||||||
temp += int16(buf[3] & 0x0f)
|
|
||||||
hum = 10*uint16(buf[0]) + uint16(buf[1])
|
|
||||||
} else {
|
|
||||||
hum = binary.LittleEndian.Uint16(buf[0:2])
|
|
||||||
temp = int16(buf[3])<<8 + int16(buf[2]&0x7f)
|
|
||||||
if buf[2]&0x80 > 0 {
|
|
||||||
temp = -temp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Celsius and Fahrenheit temperature scales
|
// Celsius and Fahrenheit temperature scales
|
||||||
type TemperatureScale uint8
|
type TemperatureScale uint8
|
||||||
|
|
||||||
@@ -54,9 +33,6 @@ const (
|
|||||||
startTimeout = time.Millisecond * 200
|
startTimeout = time.Millisecond * 200
|
||||||
startingLow = time.Millisecond * 20
|
startingLow = time.Millisecond * 20
|
||||||
|
|
||||||
DHT11 DeviceType = iota
|
|
||||||
DHT22
|
|
||||||
|
|
||||||
C TemperatureScale = iota
|
C TemperatureScale = iota
|
||||||
F
|
F
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package dht
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeviceType is the enum type for device type
|
||||||
|
type DeviceType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
DHT11 DeviceType = iota
|
||||||
|
DHT22
|
||||||
|
)
|
||||||
|
|
||||||
|
// extractData parses information received from the sensor.
|
||||||
|
// The 2 first buffers are for the humidity and
|
||||||
|
// the 2 following corresponds to the temperature.
|
||||||
|
func (d DeviceType) extractData(buf []byte) (temp int16, hum uint16) {
|
||||||
|
switch d {
|
||||||
|
case DHT11:
|
||||||
|
hum = 10*uint16(buf[0]) + uint16(buf[1])
|
||||||
|
temp = int16(buf[2])
|
||||||
|
if buf[3]&0x80 > 0 {
|
||||||
|
temp = -1 - temp
|
||||||
|
}
|
||||||
|
temp *= 10
|
||||||
|
temp += int16(buf[3] & 0x0f)
|
||||||
|
case DHT22:
|
||||||
|
hum = binary.BigEndian.Uint16(buf[0:2])
|
||||||
|
temp = int16(buf[2]&0x7f)<<8 + int16(buf[3])
|
||||||
|
// the first bit corresponds to the sign bit
|
||||||
|
if buf[2]&0x80 > 0 {
|
||||||
|
temp = -temp
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// keeping this for retro-compatibility but not tested
|
||||||
|
hum = binary.LittleEndian.Uint16(buf[0:2])
|
||||||
|
temp = int16(buf[3])<<8 + int16(buf[2]&0x7f)
|
||||||
|
if buf[2]&0x80 > 0 {
|
||||||
|
temp = -temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package dht
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDeviceType_extractData(t *testing.T) {
|
||||||
|
bitStr := "0000001010001100000000010101111111101110"
|
||||||
|
buf := bitStringToBytes(bitStr)
|
||||||
|
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
d DeviceType
|
||||||
|
buf []byte
|
||||||
|
wantTemp int16
|
||||||
|
wantHum uint16
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// temp = 35.1C hum = 65.2%
|
||||||
|
name: "DHT22", d: DHT22, buf: buf, wantTemp: 351, wantHum: 652,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
gotTemp, gotHum := tc.d.extractData(tc.buf)
|
||||||
|
if gotTemp != tc.wantTemp {
|
||||||
|
t.Errorf("extractData() gotTemp = %v, want %v", gotTemp, tc.wantTemp)
|
||||||
|
}
|
||||||
|
if gotHum != tc.wantHum {
|
||||||
|
t.Errorf("extractData() gotHum = %v, want %v", gotHum, tc.wantHum)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func bitStringToBytes(s string) []byte {
|
||||||
|
b := make([]byte, (len(s)+(8-1))/8)
|
||||||
|
for i, r := range s {
|
||||||
|
if r < '0' || r > '1' {
|
||||||
|
panic("not in range")
|
||||||
|
}
|
||||||
|
b[i>>3] |= byte(r-'0') << uint(7-i&7)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
//go:build tinygo
|
||||||
|
// +build tinygo
|
||||||
|
|
||||||
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
||||||
//
|
//
|
||||||
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
//go:build tinygo
|
||||||
|
// +build tinygo
|
||||||
|
|
||||||
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
// Package dht provides a driver for DHTXX family temperature and humidity sensors.
|
||||||
//
|
//
|
||||||
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
// [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
//go:build tinygo
|
||||||
|
// +build tinygo
|
||||||
|
|
||||||
package dht // import "tinygo.org/x/drivers/dht"
|
package dht // import "tinygo.org/x/drivers/dht"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
Reference in New Issue
Block a user