|
| 1 | +// Copyright 2024 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 Zigbee Dimmable light bulb. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create an end device with |
| 19 | + * dimmable light end point. |
| 20 | + * The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. |
| 21 | + * |
| 22 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 23 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 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_ED |
| 31 | +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "Zigbee.h" |
| 35 | + |
| 36 | +#define LED_PIN RGB_BUILTIN |
| 37 | +#define BUTTON_PIN 9 // C6/H2 Boot button |
| 38 | +#define ZIGBEE_LIGHT_ENDPOINT 10 |
| 39 | + |
| 40 | +ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT); |
| 41 | + |
| 42 | +/********************* LED functions **************************/ |
| 43 | +void setLight(bool state, uint8_t level) |
| 44 | +{ |
| 45 | + rgbLedWrite(LED_PIN, level, level, level); |
| 46 | +} |
| 47 | + |
| 48 | +// Create a task on identify call to handle the identify function |
| 49 | +void identify(uint16_t time) |
| 50 | +{ |
| 51 | + static uint8_t blink = 1; |
| 52 | + log_d("Identify called for %d seconds", time); |
| 53 | + if (time == 0) |
| 54 | + { |
| 55 | + // If identify time is 0, stop blinking and restore light as it was used for identify |
| 56 | + zbDimmableLight.restoreLight(); |
| 57 | + return; |
| 58 | + } |
| 59 | + rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink); |
| 60 | + blink = !blink; |
| 61 | +} |
| 62 | + |
| 63 | +/********************* Arduino functions **************************/ |
| 64 | +void setup() |
| 65 | +{ |
| 66 | + // Init RMT and leave light OFF |
| 67 | + rgbLedWrite(LED_PIN, 0, 0, 0); |
| 68 | + |
| 69 | + // Init button for factory reset |
| 70 | + pinMode(BUTTON_PIN, INPUT_PULLUP); |
| 71 | + |
| 72 | + // Set callback function for light change |
| 73 | + zbDimmableLight.onLightChange(setLight); |
| 74 | + |
| 75 | + // Optional: Set callback function for device identify |
| 76 | + zbDimmableLight.onIdentify(identify); |
| 77 | + |
| 78 | + // Optional: Set Zigbee device name and model |
| 79 | + zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb"); |
| 80 | + |
| 81 | + // Add endpoint to Zigbee Core |
| 82 | + log_d("Adding ZigbeeLight endpoint to Zigbee Core"); |
| 83 | + Zigbee.addEndpoint(&zbDimmableLight); |
| 84 | + |
| 85 | + // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE |
| 86 | + log_d("Calling Zigbee.begin()"); |
| 87 | + Zigbee.begin(); |
| 88 | +} |
| 89 | + |
| 90 | +void loop() |
| 91 | +{ |
| 92 | + // Checking button for factory reset |
| 93 | + if (digitalRead(BUTTON_PIN) == LOW) |
| 94 | + { // Push button pressed |
| 95 | + // Key debounce handling |
| 96 | + delay(100); |
| 97 | + int startTime = millis(); |
| 98 | + while (digitalRead(BUTTON_PIN) == LOW) |
| 99 | + { |
| 100 | + delay(50); |
| 101 | + if ((millis() - startTime) > 3000) |
| 102 | + { |
| 103 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 104 | + Serial.printf("Resetting Zigbee to factory settings, reboot.\n"); |
| 105 | + Zigbee.factoryReset(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + delay(100); |
| 110 | +} |
0 commit comments