|
| 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 fan control. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a router device fan control. |
| 19 | + * The fan control is a Zigbee router device, which is controlled by a Zigbee coordinator. |
| 20 | + * |
| 21 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 22 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 23 | + * |
| 24 | + * Please check the README.md for instructions and more detailed description. |
| 25 | + * |
| 26 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef ZIGBEE_MODE_ZCZR |
| 30 | +#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" |
| 31 | +#endif |
| 32 | + |
| 33 | +#include "Zigbee.h" |
| 34 | + |
| 35 | +/* Zigbee light bulb configuration */ |
| 36 | +#define ZIGBEE_FAN_CONTROL_ENDPOINT 1 |
| 37 | +uint8_t led = RGB_BUILTIN; // To demonstrate the current fan control mode |
| 38 | +uint8_t button = BOOT_PIN; |
| 39 | + |
| 40 | +ZigbeeFanControl zbFanControl = ZigbeeFanControl(ZIGBEE_FAN_CONTROL_ENDPOINT); |
| 41 | + |
| 42 | +/********************* fan control callback function **************************/ |
| 43 | +void setFan(ZigbeeFanMode mode) { |
| 44 | + switch (mode) { |
| 45 | + case FAN_MODE_OFF: |
| 46 | + rgbLedWrite(led, 0, 0, 0); // Off |
| 47 | + Serial.println("Fan mode: OFF"); |
| 48 | + break; |
| 49 | + case FAN_MODE_LOW: |
| 50 | + rgbLedWrite(led, 0, 0, 255); // Blue |
| 51 | + Serial.println("Fan mode: LOW"); |
| 52 | + break; |
| 53 | + case FAN_MODE_MEDIUM: |
| 54 | + rgbLedWrite(led, 255, 255, 0); // Yellow |
| 55 | + Serial.println("Fan mode: MEDIUM"); |
| 56 | + break; |
| 57 | + case FAN_MODE_HIGH: |
| 58 | + rgbLedWrite(led, 255, 0, 0); // Red |
| 59 | + Serial.println("Fan mode: HIGH"); |
| 60 | + break; |
| 61 | + case FAN_MODE_ON: |
| 62 | + rgbLedWrite(led, 255, 255, 255); // White |
| 63 | + Serial.println("Fan mode: ON"); |
| 64 | + break; |
| 65 | + default: |
| 66 | + log_e("Unhandled fan mode: %d", mode); |
| 67 | + break; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/********************* Arduino functions **************************/ |
| 72 | +void setup() { |
| 73 | + Serial.begin(115200); |
| 74 | + |
| 75 | + // Init LED that will be used to indicate the current fan control mode |
| 76 | + rgbLedWrite(led, 0, 0, 0); |
| 77 | + |
| 78 | + // Init button for factory reset |
| 79 | + pinMode(button, INPUT_PULLUP); |
| 80 | + |
| 81 | + //Optional: set Zigbee device name and model |
| 82 | + zbFanControl.setManufacturerAndModel("Espressif", "ZBFanControl"); |
| 83 | + |
| 84 | + // Set the fan mode sequence to LOW_MED_HIGH |
| 85 | + zbFanControl.setFanModeSequence(FAN_MODE_SEQUENCE_LOW_MED_HIGH); |
| 86 | + |
| 87 | + // Set callback function for fan mode change |
| 88 | + zbFanControl.onFanModeChange(setFan); |
| 89 | + |
| 90 | + //Add endpoint to Zigbee Core |
| 91 | + Serial.println("Adding ZigbeeFanControl endpoint to Zigbee Core"); |
| 92 | + Zigbee.addEndpoint(&zbFanControl); |
| 93 | + |
| 94 | + // When all EPs are registered, start Zigbee in ROUTER mode |
| 95 | + if (!Zigbee.begin(ZIGBEE_ROUTER)) { |
| 96 | + Serial.println("Zigbee failed to start!"); |
| 97 | + Serial.println("Rebooting..."); |
| 98 | + ESP.restart(); |
| 99 | + } |
| 100 | + Serial.println("Connecting to network"); |
| 101 | + while (!Zigbee.connected()) { |
| 102 | + Serial.print("."); |
| 103 | + delay(100); |
| 104 | + } |
| 105 | + Serial.println(); |
| 106 | +} |
| 107 | + |
| 108 | +void loop() { |
| 109 | + // Checking button for factory reset |
| 110 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 111 | + // Key debounce handling |
| 112 | + delay(100); |
| 113 | + int startTime = millis(); |
| 114 | + while (digitalRead(button) == LOW) { |
| 115 | + delay(50); |
| 116 | + if ((millis() - startTime) > 3000) { |
| 117 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 118 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 119 | + delay(1000); |
| 120 | + Zigbee.factoryReset(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + delay(100); |
| 125 | +} |
0 commit comments