Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Two different ZigBee devices on one chip #11213

Discussion options

Hi! Is it possible to use one ESP32-H2 as two different ZigBee devices? For example, a switch and a temperature and humidity sensor. If so, how to write the code in Arduino IDE?

You must be logged in to vote

Hi! Yes it is possible. I have an example here from a community member for DimmableLight + Temp sensor. Its easy, you just need to add another endpoint with unique number before starting Zigbee with Zigbee.begin();. Kind of merge both examples together (light + sensor, for you switch + sensor).

// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is dist...

Replies: 1 comment

Comment options

Hi! Yes it is possible. I have an example here from a community member for DimmableLight + Temp sensor. Its easy, you just need to add another endpoint with unique number before starting Zigbee with Zigbee.begin();. Kind of merge both examples together (light + sensor, for you switch + sensor).

// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
 * @brief This example demonstrates Zigbee Dimmable light bulb.
 *
 * The example demonstrates how to use Zigbee library to create an end device with
 * dimmable light end point.
 * The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator.
 *
 * Proper Zigbee mode must be selected in Tools->Zigbee mode
 * and also the correct partition scheme must be selected in Tools->Partition Scheme.
 *
 * Please check the README.md for instructions and more detailed description.
 *
 * Created by [FaBjE](https://github.com/FaBjE) based on examples by [Jan Procházka](https://github.com/P-R-O-C-H-Y/)
 */
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
/* Zigbee dimmable light configuration */
#define ZIGBEE_LIGHT_ENDPOINT 10
#define TEMP_SENSOR_ENDPOINT_NUMBER 9
uint8_t led = LED_BUILTIN;
uint8_t button = BOOT_PIN;
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
/********************* RGB LED functions **************************/
void setLight(bool state, uint8_t level) {
 if (!state) {
 rgbLedWrite(led, 0, 0, 0);
 return;
 }
 rgbLedWrite(led, level, level, level);
 Serial.print("LED set to ");
 Serial.println(level);
}
static void temp_sensor_value_update(void *arg) {
 for (;;) {
 // Read temperature sensor value
 float tsens_value = temperatureRead();
 Serial.printf("Updated temperature sensor value to %.2f°C\r\n", tsens_value);
 // Update temperature value in Temperature sensor EP
 zbTempSensor.setTemperature(tsens_value);
 delay(1000);
 }
}
// Create a task on identify call to handle the identify function
void identify(uint16_t time) {
 static uint8_t blink = 1;
 log_d("Identify called for %d seconds", time);
 if (time == 0) {
 // If identify time is 0, stop blinking and restore light as it was used for identify
 zbDimmableLight.restoreLight();
 return;
 }
 rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
 blink = !blink;
}
/********************* Arduino functions **************************/
void setup() {
 Serial.begin(115200);
 // Init RMT and leave light OFF
 rgbLedWrite(led, 0, 0, 0);
 // Init button for factory reset
 pinMode(button, INPUT_PULLUP);
 // Set callback function for light change
 zbDimmableLight.onLightChange(setLight);
 // Optional: Set callback function for device identify
 zbDimmableLight.onIdentify(identify);
 // Optional: Set Zigbee device name and model
 zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
 // Add endpoint to Zigbee Core
 Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
 Zigbee.addEndpoint(&zbDimmableLight);
 // Optional: set Zigbee device name and model
 zbTempSensor.setManufacturerAndModel("Espressif", "ZigbeeTempSensor");
 // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
 zbTempSensor.setMinMaxValue(10, 50);
 // Optional: Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
 zbTempSensor.setTolerance(1);
 // Add endpoint to Zigbee Core
 Serial.println("Adding ZigbeeTempSensor endpoint to Zigbee Core");
 Zigbee.addEndpoint(&zbTempSensor);
 // When all EPs are registered, start Zigbee in End Device mode
 if (!Zigbee.begin()) {
 Serial.println("Zigbee failed to start!");
 Serial.println("Rebooting...");
 ESP.restart();
 }
 Serial.println("Connecting to network");
 while (!Zigbee.connected()) {
 Serial.print(".");
 delay(100);
 }
 Serial.println();
 // Start Temperature sensor reading task
 xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL);
 // Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin()
 // min_interval and max_interval in seconds, delta (temp change in 0,1 °C)
 // if min = 1 and max = 0, reporting is sent only when temperature changes by delta
 // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta
 // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of temperature change
 zbTempSensor.setReporting(1, 0, 1);
}
void loop() {
 // Checking button for factory reset
 if (digitalRead(button) == LOW) { // Push button pressed
 // Key debounce handling
 delay(100);
 int startTime = millis();
 while (digitalRead(button) == LOW) {
 delay(50);
 if ((millis() - startTime) > 3000) {
 // If key pressed for more than 3secs, factory reset Zigbee and reboot
 Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
 delay(1000);
 Zigbee.factoryReset();
 }
 }
 // Increase blightness by 50 every time the button is pressed
 zbDimmableLight.setLightLevel(zbDimmableLight.getLightLevel() + 50);
 zbTempSensor.reportTemperature();
 }
 delay(100);
}
You must be logged in to vote
0 replies
Answer selected by P-R-O-C-H-Y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /