Skip to content

Commit 77a1c2f

Browse files
feat: Add DHT sensor (ffenix113#54)
feat/dht : Add DHT sensor Co-authored-by: bbadrignans <bbadrignans@seclab-security.com>
1 parent 492a2bc commit 77a1c2f

File tree

7 files changed

+119
-2
lines changed

7 files changed

+119
-2
lines changed

docs/sensors/supported_sensors.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ When this change will be done - all sensors officially supported by Zephyr, will
1010
Full per-sensor options will be defined later. For now please refer to [configuration file](../using_the_cli/configuration_file.md) documentation or [latest configuration](https://github.com/ffenix113/zigbee_home/blob/develop/zigbee.yml) in repository for sensor configuration options.
1111

1212
* `bme280` & `bme680` - Bosch BME280 / BME680
13-
* `scd4x` - Sensirion SCD41 ([driver](https://github.com/nobodyguy/sensirion_zephyr_drivers) by [nobodyguy](https://github.com/nobodyguy))
13+
* `dht` - Aosong DHT11 / DHT22 / AM2302
14+
* `scd4x` - Sensirion SCD41 ([driver](https://github.com/nobodyguy/sensirion_zephyr_drivers) by [nobodyguy](https://github.com/nobodyguy))

examples/sensor_dht/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## A board with Aosong DHT sensor attached
2+
3+
Simple example with one DHT sensor attached and using 1-wire interface (GPIO).

examples/sensor_dht/zigbee.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
general:
2+
board: nrf52840dongle_nrf52840
3+
4+
board:
5+
6+
sensors:
7+
# Add a dht sensor from aosong manufacturer
8+
# If DHT22 or AM2302 is used, variant must be set to "dht22". DHT11 does not need variant.
9+
- type: dht
10+
variant: "dht22"
11+
pin:
12+
port: 1
13+
pin: 13

sensor/aosong/dht.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package aosong
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ffenix113/zigbee_home/sensor/base"
7+
"github.com/ffenix113/zigbee_home/templates/extenders"
8+
"github.com/ffenix113/zigbee_home/types/appconfig"
9+
dt "github.com/ffenix113/zigbee_home/types/devicetree"
10+
"github.com/ffenix113/zigbee_home/types/generator"
11+
"github.com/ffenix113/zigbee_home/zcl/cluster"
12+
"github.com/ffenix113/zigbee_home/types"
13+
)
14+
15+
type DHT struct {
16+
*base.Base `yaml:",inline"`
17+
Pin types.Pin
18+
Variant string `yaml:"variant"`
19+
}
20+
21+
func NewDHT() *DHT {
22+
return &DHT{
23+
Variant: "",
24+
}
25+
}
26+
27+
func (b DHT) String() string {
28+
return "Aosong"
29+
}
30+
31+
func (DHT) Clusters() cluster.Clusters {
32+
return []cluster.Cluster{
33+
cluster.Temperature{
34+
MinMeasuredValue: -40,
35+
MaxMeasuredValue: 80,
36+
Tolerance: 1,
37+
},
38+
cluster.NewRelativeHumidity(0, 100),
39+
}
40+
}
41+
42+
func (b DHT) AppConfig() []appconfig.ConfigValue {
43+
return []appconfig.ConfigValue{
44+
appconfig.NewValue("CONFIG_GPIO").Required(appconfig.Yes),
45+
appconfig.CONFIG_DHT.Required(appconfig.Yes),
46+
}
47+
}
48+
49+
func (b DHT) ApplyOverlay(tree *dt.DeviceTree) error {
50+
51+
pinctrlNode := tree.FindSpecificNode(dt.SearchByLabel(dt.NodeLabelPinctrl))
52+
if pinctrlNode == nil {
53+
return dt.ErrNodeNotFound(dt.NodeLabelPinctrl)
54+
}
55+
56+
pinLabel := fmt.Sprintf("gpio%d %d (GPIO_PULL_UP | GPIO_ACTIVE_LOW)", b.Pin.Port, b.Pin.Pin)
57+
58+
props:= []dt.Property{
59+
dt.NewProperty("compatible", dt.FromValue("aosong,dht")),
60+
dt.NewProperty("status", dt.FromValue("okay")),
61+
dt.NewProperty("dio-gpios", dt.Angled(dt.Label(pinLabel))),
62+
}
63+
// Add variant property only if Variant is not emtpy
64+
// For dht22 or AM2302 devices the string "dht22;" must be added to device tree overlay
65+
// See : https://docs.zephyrproject.org/latest/build/dts/api/bindings/sensor/aosong,dht.html
66+
if b.Variant != "" {
67+
props = append(props, dt.NewProperty(b.Variant, nil))
68+
}
69+
70+
pinctrlNode.AddNodes(
71+
&dt.Node{
72+
Name: "dht22",
73+
Label: b.Label(),
74+
Properties: props,
75+
},
76+
)
77+
78+
return nil
79+
}
80+
81+
func (DHT) Extenders() []generator.Extender {
82+
return []generator.Extender{
83+
extenders.NewSensor(),
84+
extenders.GPIO{},
85+
}
86+
}

types/appconfig/known.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var (
4747
CONFIG_NET_IP_ADDR_CHECK = NewValue("CONFIG_NET_IP_ADDR_CHECK").Default(No)
4848
CONFIG_NET_UDP = NewValue("CONFIG_NET_UDP").Default(No)
4949

50+
// Sensors
51+
CONFIG_DHT = NewValue("CONFIG_DHT").Default(Yes)
52+
5053
// Debug
5154
CONFIG_ZBOSS_HALT_ON_ASSERT = NewValue("CONFIG_ZBOSS_HALT_ON_ASSERT").Default(Yes)
5255
CONFIG_RESET_ON_FATAL_ERROR = NewValue("CONFIG_RESET_ON_FATAL_ERROR").Default(No)

types/sensor/known.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/ffenix113/zigbee_home/sensor"
88
"github.com/ffenix113/zigbee_home/sensor/base"
99
"github.com/ffenix113/zigbee_home/sensor/bosch"
10+
"github.com/ffenix113/zigbee_home/sensor/aosong"
1011
"github.com/ffenix113/zigbee_home/sensor/sensirion"
1112
)
1213

@@ -62,6 +63,9 @@ var knownSensors = map[string]func() Sensor{
6263
// and does not expose resistance to Zigbee.
6364
"bme680": fromConstructor(bosch.NewBME680),
6465

66+
// Aosong
67+
"dht": fromConstructor(aosong.NewDHT),
68+
6569
// Sensirion
6670
"scd4x": fromType[*sensirion.SCD4X],
6771
}

zigbee.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ sensors:
126126
# port: 0
127127
# pin: 6
128128
# inverted: true
129+
# Add a dht sensor from aosong manufacturer
130+
# If DHT22 or AM2302 is used, variant must be set to "dht22". DHT11 does not need variant.
131+
# - type: dht
132+
# variant: "dht22"
133+
# pin:
134+
# port: 1
135+
# pin: 13
129136
#
130137
# power_config uses ADC to measure positive voltage
131138
# on pin 0.04 and report it as a battery voltage.
@@ -148,4 +155,4 @@ sensors:
148155
# pin: 0.04
149156
# oversampling: 4
150157
# min_moisture_mv: 730
151-
# max_moisture_mv: 430
158+
# max_moisture_mv: 430

0 commit comments

Comments
 (0)