|
| 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 OTA support on light bulb. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device light bulb with OTA support. |
| 19 | + * The light bulb is a Zigbee end 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_ED |
| 30 | +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" |
| 31 | +#endif |
| 32 | + |
| 33 | +#include "Zigbee.h" |
| 34 | + |
| 35 | +/* Zigbee light bulb configuration */ |
| 36 | +#define ZIGBEE_LIGHT_ENDPOINT 1 |
| 37 | +uint8_t led = RGB_BUILTIN; |
| 38 | +uint8_t button = BOOT_PIN; |
| 39 | + |
| 40 | +/* Zigbee OTA configuration */ |
| 41 | +#define OTA_UPGRADE_RUNNING_FILE_VERSION 0x01010100 // Increment this value when the running image is updated |
| 42 | +#define OTA_UPGRADE_DOWNLOADED_FILE_VERSION 0x01010101 // Increment this value when the downloaded image is updated |
| 43 | +#define OTA_UPGRADE_HW_VERSION 0x0101 // The hardware version, this can be used to differentiate between different hardware versions |
| 44 | + |
| 45 | +ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT); |
| 46 | + |
| 47 | +/********************* RGB LED functions **************************/ |
| 48 | +void setLED(bool value) { |
| 49 | + digitalWrite(led, value); |
| 50 | +} |
| 51 | + |
| 52 | +/********************* Arduino functions **************************/ |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); |
| 55 | + |
| 56 | + // Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood) |
| 57 | + pinMode(led, OUTPUT); |
| 58 | + digitalWrite(led, LOW); |
| 59 | + |
| 60 | + // Init button for factory reset |
| 61 | + pinMode(button, INPUT_PULLUP); |
| 62 | + |
| 63 | + // Optional: set Zigbee device name and model |
| 64 | + zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb"); |
| 65 | + |
| 66 | + // Set callback function for light change |
| 67 | + zbLight.onLightChange(setLED); |
| 68 | + |
| 69 | + // Add OTA client to the light bulb |
| 70 | + zbLight.addOTAClient(OTA_UPGRADE_RUNNING_FILE_VERSION, OTA_UPGRADE_DOWNLOADED_FILE_VERSION, OTA_UPGRADE_HW_VERSION); |
| 71 | + |
| 72 | + // Add endpoint to Zigbee Core |
| 73 | + Serial.println("Adding ZigbeeLight endpoint to Zigbee Core"); |
| 74 | + Zigbee.addEndpoint(&zbLight); |
| 75 | + |
| 76 | + // When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE |
| 77 | + if (!Zigbee.begin()) { |
| 78 | + Serial.println("Zigbee failed to start!"); |
| 79 | + Serial.println("Rebooting..."); |
| 80 | + ESP.restart(); |
| 81 | + } |
| 82 | + Serial.println("Connecting to network"); |
| 83 | + while (!Zigbee.connected()) { |
| 84 | + Serial.print("."); |
| 85 | + delay(100); |
| 86 | + } |
| 87 | + Serial.println(); |
| 88 | + |
| 89 | + // Start Zigbee OTA client query, first request is within a minute and the next requests are sent every hour automatically |
| 90 | + zbLight.requestOTAUpdate(); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() { |
| 94 | + // Checking button for factory reset |
| 95 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 96 | + // Key debounce handling |
| 97 | + delay(100); |
| 98 | + int startTime = millis(); |
| 99 | + while (digitalRead(button) == LOW) { |
| 100 | + delay(50); |
| 101 | + if ((millis() - startTime) > 3000) { |
| 102 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 103 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 104 | + delay(1000); |
| 105 | + Zigbee.factoryReset(); |
| 106 | + } |
| 107 | + } |
| 108 | + // Toggle light by pressing the button |
| 109 | + zbLight.setLight(!zbLight.getLightState()); |
| 110 | + } |
| 111 | + delay(100); |
| 112 | +} |
0 commit comments