|
| 1 | +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief This example demonstrates simple Zigbee Range Extender (router). |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a Zigbee network ragbe extender (router). |
| 19 | + * |
| 20 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 21 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 22 | + * |
| 23 | + * Please check the README.md for instructions and more detailed description. |
| 24 | + * |
| 25 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 26 | + */ |
| 27 | + |
| 28 | +#ifndef ZIGBEE_MODE_ZCZR |
| 29 | +#error "Zigbee coordinator/router mode is not selected in Tools->Zigbee mode" |
| 30 | +#endif |
| 31 | + |
| 32 | +#include "Zigbee.h" |
| 33 | + |
| 34 | +/* Zigbee light bulb configuration */ |
| 35 | +#define USE_CUSTOM_ZIGBEE_CONFIG 1 |
| 36 | +#define ZIGBEE_EXTENDER_ENDPOINT 1 |
| 37 | +uint8_t led = RGB_BUILTIN; |
| 38 | +uint8_t button = BOOT_PIN; |
| 39 | + |
| 40 | +ZigbeeRangeExtender zbExtender = ZigbeeRangeExtender(ZIGBEE_EXTENDER_ENDPOINT); |
| 41 | + |
| 42 | +/************************** Identify ******************************/ |
| 43 | +// Create a task on identify call to handle the identify function |
| 44 | +void identify(uint16_t time) { |
| 45 | + static uint8_t blink = 1; |
| 46 | + log_d("Identify called for %d seconds", time); |
| 47 | + if (time == 0) { |
| 48 | + digitalWrite(led, LOW); |
| 49 | + return; |
| 50 | + } |
| 51 | + digitalWrite(led, blink); |
| 52 | + blink = !blink; |
| 53 | +} |
| 54 | + |
| 55 | +/********************* Arduino functions **************************/ |
| 56 | +void setup() { |
| 57 | + Serial.begin(115200); |
| 58 | + |
| 59 | + // Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood) |
| 60 | + pinMode(led, OUTPUT); |
| 61 | + digitalWrite(led, LOW); |
| 62 | + |
| 63 | + // Init button for factory reset |
| 64 | + pinMode(button, INPUT_PULLUP); |
| 65 | + |
| 66 | + // Optional: Set callback function for device identify |
| 67 | + zbExtender.onIdentify(identify); |
| 68 | + |
| 69 | + // Optional: Set Zigbee device name and model |
| 70 | + zbExtender.setManufacturerAndModel("Espressif", "ZigbeeRangeExtender"); |
| 71 | + |
| 72 | + // Add endpoint to Zigbee Core |
| 73 | + Serial.println("Adding Zigbee Extender endpoint to Zigbee Core"); |
| 74 | + Zigbee.addEndpoint(&zbExtender); |
| 75 | + |
| 76 | +#if USE_CUSTOM_ZIGBEE_CONFIG |
| 77 | + // Optional: Create a custom Zigbee configuration for Zigbee Extender |
| 78 | + esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ROUTER_CONFIG(); |
| 79 | + zigbeeConfig.nwk_cfg.zczr_cfg.max_children = 20; // 10 is default |
| 80 | + |
| 81 | + // When all EPs are registered, start Zigbee with custom config |
| 82 | + if (!Zigbee.begin(&zigbeeConfig)) { |
| 83 | +#else |
| 84 | + // When all EPs are registered, start Zigbee as ROUTER device |
| 85 | + if (!Zigbee.begin(ZIGBEE_ROUTER)) { |
| 86 | +#endif |
| 87 | + Serial.println("Zigbee failed to start!"); |
| 88 | + Serial.println("Rebooting..."); |
| 89 | + ESP.restart(); |
| 90 | + } |
| 91 | + Serial.println("Connecting to network"); |
| 92 | + while (!Zigbee.connected()) { |
| 93 | + Serial.print("."); |
| 94 | + delay(100); |
| 95 | + } |
| 96 | + Serial.println(); |
| 97 | +} |
| 98 | + |
| 99 | +void loop() { |
| 100 | + // Checking button for factory reset |
| 101 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 102 | + // Key debounce handling |
| 103 | + delay(100); |
| 104 | + int startTime = millis(); |
| 105 | + while (digitalRead(button) == LOW) { |
| 106 | + delay(50); |
| 107 | + if ((millis() - startTime) > 3000) { |
| 108 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 109 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 110 | + delay(1000); |
| 111 | + Zigbee.factoryReset(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments