|
| 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 Gateway functionality. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library on ESP32s to create a Zigbee Gateway, updating the time from NTP server. |
| 19 | + * The Gateway is able to communicate with Zigbee end devices and send/receive data to/from them. |
| 20 | + * The Gateway is also able to communicate with the cloud or other devices over Wi-Fi / BLE. |
| 21 | + * |
| 22 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode->Zigbee ZCZR (coordinator/router) |
| 23 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme->Zigbee ZCZR |
| 24 | + * |
| 25 | + * Please check the README.md for instructions and more detailed description. |
| 26 | + * |
| 27 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 28 | + */ |
| 29 | + |
| 30 | +#ifndef ZIGBEE_MODE_ZCZR |
| 31 | +#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "Zigbee.h" |
| 35 | +#include <WiFi.h> |
| 36 | +#include "time.h" |
| 37 | +#include "esp_sntp.h" |
| 38 | + |
| 39 | +/* Zigbee gateway configuration */ |
| 40 | +#define GATEWAY_ENDPOINT_NUMBER 1 |
| 41 | +#define GATEWAY_RCP_UART_PORT UART_NUM_1 // UART 0 is used for Serial communication |
| 42 | +#define GATEWAY_RCP_RX_PIN 4 |
| 43 | +#define GATEWAY_RCP_TX_PIN 5 |
| 44 | + |
| 45 | +ZigbeeGateway zbGateway = ZigbeeGateway(GATEWAY_ENDPOINT_NUMBER); |
| 46 | + |
| 47 | +/* Wi-Fi credentials */ |
| 48 | +const char *ssid = "your-ssid"; |
| 49 | +const char *password = "your-password"; |
| 50 | + |
| 51 | +/* NTP server configuration */ |
| 52 | +const char *ntpServer1 = "pool.ntp.org"; |
| 53 | +const char *ntpServer2 = "time.nist.gov"; |
| 54 | +const long gmtOffset_sec = 3600; |
| 55 | +const int daylightOffset_sec = 3600; |
| 56 | +const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional) |
| 57 | + |
| 58 | +/* Time structure */ |
| 59 | +struct tm timeinfo; |
| 60 | + |
| 61 | +/********************* Arduino functions **************************/ |
| 62 | +void setup() { |
| 63 | + Serial.begin(115200); |
| 64 | + |
| 65 | + // Initialize Wi-Fi and connect to AP |
| 66 | + WiFi.begin(ssid, password); |
| 67 | + esp_sntp_servermode_dhcp(1); // (optional) |
| 68 | + |
| 69 | + Serial.print("Connecting to WiFi"); |
| 70 | + |
| 71 | + while (WiFi.status() != WL_CONNECTED) { |
| 72 | + delay(500); |
| 73 | + Serial.print("."); |
| 74 | + } |
| 75 | + Serial.println("WiFi connected"); |
| 76 | + |
| 77 | + // Initialize Zigbee and Begin Zigbee stack |
| 78 | + // Optional: set Zigbee device name and model |
| 79 | + zbGateway.setManufacturerAndModel("Espressif", "ZigbeeGateway"); |
| 80 | + zbGateway.addTimeCluster(timeinfo, gmtOffset_sec); |
| 81 | + |
| 82 | + // Add endpoint to Zigbee Core |
| 83 | + Serial.println("Adding Zigbee Gateway endpoint"); |
| 84 | + Zigbee.addEndpoint(&zbGateway); |
| 85 | + |
| 86 | + // Optional: Open network for 180 seconds after boot |
| 87 | + Zigbee.setRebootOpenNetwork(180); |
| 88 | + |
| 89 | + // Set custom radio configuration for RCP communication |
| 90 | + esp_zb_radio_config_t radio_config = ZIGBEE_DEFAULT_UART_RCP_RADIO_CONFIG(); |
| 91 | + radio_config.radio_uart_config.port = GATEWAY_RCP_UART_PORT; |
| 92 | + radio_config.radio_uart_config.rx_pin = (gpio_num_t)GATEWAY_RCP_RX_PIN; |
| 93 | + radio_config.radio_uart_config.tx_pin = (gpio_num_t)GATEWAY_RCP_TX_PIN; |
| 94 | + |
| 95 | + Zigbee.setRadioConfig(radio_config); |
| 96 | + |
| 97 | + // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR or ZIGBEE_ROUTER mode |
| 98 | + if (!Zigbee.begin(ZIGBEE_COORDINATOR)) { |
| 99 | + Serial.println("Zigbee failed to start!"); |
| 100 | + Serial.println("Rebooting..."); |
| 101 | + ESP.restart(); |
| 102 | + } |
| 103 | + |
| 104 | + // set notification call-back function |
| 105 | + sntp_set_time_sync_notification_cb(timeavailable); |
| 106 | + sntp_set_sync_interval(30000); // sync every 30 seconds |
| 107 | + |
| 108 | + // config time zone and NTP servers |
| 109 | + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2); |
| 110 | +} |
| 111 | + |
| 112 | +void loop() { |
| 113 | + // Nothing to do here in this example |
| 114 | +} |
| 115 | + |
| 116 | +void printLocalTime() { |
| 117 | + if (!getLocalTime(&timeinfo)) { |
| 118 | + Serial.println("No time available (yet)"); |
| 119 | + return; |
| 120 | + } |
| 121 | + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); |
| 122 | + zbGateway.setTime(timeinfo); |
| 123 | + Serial.println("Time updated in Zigbee Gateway"); |
| 124 | +} |
| 125 | + |
| 126 | +// Callback function (gets called when time adjusts via NTP) |
| 127 | +void timeavailable(struct timeval *t) { |
| 128 | + Serial.println("Got time adjustment from NTP!"); |
| 129 | + printLocalTime(); |
| 130 | +} |
0 commit comments