Skip to content

Commit 81c26b9

Browse files
committed
add analog sensor modules
1 parent 5ba4c21 commit 81c26b9

File tree

6 files changed

+324
-0
lines changed

6 files changed

+324
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Arduino-ESP32 Zigbee Analog Sensor Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) simple sensor device type with analog measuring.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Analog Sensor Functions
13+
14+
* After this board first starts up, it would be configured locally to report an analog value on change or every 30 seconds.
15+
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured value to the network.
16+
17+
## Hardware Required
18+
19+
* A USB cable for power supply and programming
20+
21+
### Configure the Project
22+
23+
In this example, the internal temperature sensor is used to demonstrate reading of the analog sensors.
24+
Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2).
25+
26+
#### Using Arduino IDE
27+
28+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
29+
30+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
31+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
32+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
33+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
34+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
35+
36+
## Troubleshooting
37+
38+
If the End device flashed with this example is not connecting to the coordinator, erase the flash of the End device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator.
39+
You can do the following:
40+
41+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
42+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
43+
44+
By default, the coordinator network is closed after rebooting or flashing new firmware.
45+
To open the network you have 2 options:
46+
47+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
48+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
49+
50+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
51+
52+
* **LED not blinking:** Check the wiring connection and the IO selection.
53+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
54+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
55+
56+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
57+
58+
## Contribute
59+
60+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
61+
62+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
63+
64+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
65+
66+
## Resources
67+
68+
* Official ESP32 Forum: [Link](https://esp32.com)
69+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
70+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
71+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
72+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
/**
15+
* @brief This example demonstrates Zigbee analog sensor.
16+
*
17+
* The example demonstrates how to use Zigbee library to create a end device analog sensor.
18+
*
19+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
20+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
21+
*
22+
* Please check the README.md for instructions and more detailed description.
23+
*
24+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
25+
* Modified by Pat Clay
26+
*/
27+
28+
#ifndef ZIGBEE_MODE_ED
29+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
30+
#endif
31+
32+
#include "Zigbee.h"
33+
34+
/* Zigbee analog sensor configuration */
35+
#define ANALOG_SENSOR_ENDPOINT_NUMBER 10
36+
37+
uint8_t button = BOOT_PIN;
38+
39+
ZigbeeAnalogSensor zbAnalogSensor = ZigbeeAnalogSensor(ANALOG_SENSOR_ENDPOINT_NUMBER);
40+
41+
void setup() {
42+
Serial.begin(115200);
43+
Serial.println("Starting...");
44+
45+
// Init button switch
46+
pinMode(button, INPUT_PULLUP);
47+
48+
// Optional: set Zigbee device name and model
49+
zbAnalogSensor.setManufacturerAndModel("WekaBuilt", "ZigbeePowerSensor");
50+
51+
// Add endpoints to Zigbee Core
52+
Zigbee.addEndpoint(&zbAnalogSensor);
53+
54+
Serial.println("Starting Zigbee...");
55+
// When all EPs are registered, start Zigbee in End Device mode
56+
if (!Zigbee.begin()) {
57+
Serial.println("Zigbee failed to start!");
58+
Serial.println("Rebooting...");
59+
ESP.restart();
60+
} else {
61+
Serial.println("Zigbee started successfully!");
62+
}
63+
Serial.println("Connecting to network");
64+
while (!Zigbee.connected()) {
65+
Serial.print(".");
66+
delay(100);
67+
}
68+
Serial.println("Connected");
69+
70+
// Set reporting interval for measurement in seconds, must be called after Zigbee.begin()
71+
// min_interval and max_interval in seconds, delta
72+
// if min = 1 and max = 0, reporting is sent only when temperature changes by delta
73+
// if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta
74+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
75+
zbAnalogSensor.setReporting(0, 2, 0);
76+
Serial.println("reporting set");
77+
78+
}
79+
80+
void loop() {
81+
static uint32_t timeCounter = 0;
82+
83+
// Read flow and pressure sensors every 2s
84+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
85+
float tsens_value = temperatureRead();
86+
Serial.printf("Updating voltage value to %.2f°C\r\n", tsens_value);
87+
zbAnalogSensor.setAnalog(tsens_value);
88+
}
89+
90+
// Checking button for factory reset and reporting
91+
if (digitalRead(button) == LOW) { // Push button pressed
92+
// Key debounce handling
93+
delay(100);
94+
int startTime = millis();
95+
while (digitalRead(button) == LOW) {
96+
delay(50);
97+
if ((millis() - startTime) > 3000) {
98+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
99+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
100+
delay(1000);
101+
Zigbee.factoryReset();
102+
}
103+
}
104+
}
105+
delay(100);
106+
zbAnalogSensor.report();
107+
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "ep/ZigbeeTempSensor.h"
1616
#include "ep/ZigbeeThermostat.h"
1717
#include "ep/ZigbeePressureSensor.h"
18+
#include "ep/ZigbeeAnalogSensor.h"
1819
#include "ep/ZigbeeFlowSensor.h"
1920
#include "ep/ZigbeeOccupancySensor.h"
2021
#include "ep/ZigbeeCarbonDioxideSensor.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "ZigbeeAnalogSensor.h"
2+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
3+
4+
esp_zb_cluster_list_t *zigbee_analog_sensor_clusters_create(zigbee_analog_sensor_cfg_t *analog_sensor) {
5+
esp_zb_basic_cluster_cfg_t *basic_cfg = analog_sensor ? &(analog_sensor->basic_cfg) : NULL;
6+
esp_zb_identify_cluster_cfg_t *identify_cfg = analog_sensor ? &(analog_sensor->identify_cfg) : NULL;
7+
esp_zb_analog_value_cluster_cfg_t *analog_value_cfg = analog_sensor ? &(analog_sensor->analog_value_cfg) : NULL;
8+
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
9+
esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
10+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
11+
esp_zb_cluster_list_add_analog_value_cluster(
12+
cluster_list, esp_zb_analog_value_cluster_create(analog_value_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE
13+
);
14+
return cluster_list;
15+
}
16+
17+
ZigbeeAnalogSensor::ZigbeeAnalogSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
18+
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;
19+
20+
//Create custom analog sensor configuration
21+
zigbee_analog_sensor_cfg_t analog_sensor_cfg = ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG();
22+
_cluster_list = zigbee_analog_sensor_clusters_create(&analog_sensor_cfg);
23+
24+
_ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0};
25+
}
26+
27+
void ZigbeeAnalogSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) {
28+
if (delta > 0) {
29+
log_e("Delta reporting is currently not supported by the sensor");
30+
}
31+
esp_zb_zcl_reporting_info_t reporting_info;
32+
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t));
33+
reporting_info.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV;
34+
reporting_info.ep = _endpoint;
35+
reporting_info.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE;
36+
reporting_info.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
37+
reporting_info.attr_id = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID;
38+
reporting_info.u.send_info.min_interval = min_interval;
39+
reporting_info.u.send_info.max_interval = max_interval;
40+
reporting_info.u.send_info.def_min_interval = min_interval;
41+
reporting_info.u.send_info.def_max_interval = max_interval;
42+
reporting_info.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID;
43+
reporting_info.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC, esp_zb_lock_acquire(portMAX_DELAY);
44+
esp_zb_zcl_update_reporting_info(&reporting_info);
45+
esp_zb_lock_release();
46+
47+
}
48+
49+
void ZigbeeAnalogSensor::setAnalog(float analog) {
50+
float zb_analog = analog;
51+
log_v("Updating sensor value...");
52+
/* Update sensor measured value */
53+
log_d("Setting value to %0.1f", zb_analog);
54+
esp_zb_lock_acquire(portMAX_DELAY);
55+
esp_zb_zcl_set_attribute_val(
56+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID,
57+
&zb_analog, false
58+
);
59+
esp_zb_lock_release();
60+
}
61+
62+
void ZigbeeAnalogSensor::report() {
63+
/* Send report attributes command */
64+
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
65+
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
66+
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_VALUE_PRESENT_VALUE_ID;
67+
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
68+
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_VALUE;
69+
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
70+
71+
esp_zb_lock_acquire(portMAX_DELAY);
72+
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
73+
esp_zb_lock_release();
74+
log_v("Analog report sent");
75+
}
76+
77+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Class of Zigbee Analog sensor endpoint inherited from common EP class */
2+
3+
#pragma once
4+
5+
#include "soc/soc_caps.h"
6+
#include "sdkconfig.h"
7+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
8+
9+
#include "ZigbeeEP.h"
10+
#include "ha/esp_zigbee_ha_standard.h"
11+
12+
// clang-format off
13+
#define ZIGBEE_DEFAULT_ANALOG_SENSOR_CONFIG() \
14+
{ \
15+
.basic_cfg = \
16+
{ \
17+
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \
18+
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \
19+
}, \
20+
.identify_cfg = \
21+
{ \
22+
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \
23+
}, \
24+
.analog_value_cfg = \
25+
{ \
26+
.out_of_service = 0, \
27+
.present_value = 0, \
28+
.status_flags = 1, \
29+
}, \
30+
}
31+
// clang-format on
32+
33+
typedef struct zigbee_analog_sensor_cfg_s {
34+
esp_zb_basic_cluster_cfg_t basic_cfg;
35+
esp_zb_identify_cluster_cfg_t identify_cfg;
36+
esp_zb_analog_value_cluster_cfg_t analog_value_cfg;
37+
} zigbee_analog_sensor_cfg_t;
38+
39+
class ZigbeeAnalogSensor : public ZigbeeEP {
40+
public:
41+
ZigbeeAnalogSensor(uint8_t endpoint);
42+
~ZigbeeAnalogSensor() {}
43+
44+
// Set the value
45+
void setAnalog(float analog);
46+
/*
47+
// Set the min and max value for the sensor
48+
void setMinMaxValue(float min, float max);
49+
50+
// Set the tolerance value for the sensor
51+
void setTolerance(float tolerance);
52+
*/
53+
// Set the reporting interval for in seconds and delta
54+
void setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta);
55+
56+
// Report the analog value
57+
void report();
58+
};
59+
60+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)