|
| 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 Zigbee Temperature Dimmable light bulb. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create an end device with |
| 19 | + * temperature 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 Hannes Beckmann |
| 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 ZIGBEE_TEMP_LIGHT_ENDPOINT 11 |
| 37 | +uint8_t led = RGB_BUILTIN; |
| 38 | +uint8_t button = BOOT_PIN; |
| 39 | + |
| 40 | +ZigbeeTemperatureDimmableLight zbTempLight = ZigbeeTemperatureDimmableLight(ZIGBEE_TEMP_LIGHT_ENDPOINT); |
| 41 | + |
| 42 | +/********************* LED functions **************************/ |
| 43 | +uint16_t kelvinToMireds(uint16_t kelvin) { |
| 44 | + return 1000000 / kelvin; |
| 45 | +} |
| 46 | + |
| 47 | +uint16_t miredsToKelvin(uint16_t mireds) { |
| 48 | + return 1000000 / mireds; |
| 49 | +} |
| 50 | + |
| 51 | +void setTempLight(bool state, uint8_t level, uint16_t mireds) { |
| 52 | + if (!state) { |
| 53 | + rgbLedWrite(led, 0, 0, 0); |
| 54 | + return; |
| 55 | + } |
| 56 | + float brightness = (float)level / 255; |
| 57 | + // Convert mireds to color temperature (K) and map to white/yellow |
| 58 | + uint16_t kelvin = miredsToKelvin(mireds); |
| 59 | + uint8_t warm = constrain(map(kelvin, 2000, 6500, 255, 0), 0, 255); |
| 60 | + uint8_t cold = constrain(map(kelvin, 2000, 6500, 0, 255), 0, 255); |
| 61 | + rgbLedWrite(led, warm * brightness, warm * brightness, cold * brightness); |
| 62 | +} |
| 63 | + |
| 64 | +void identify(uint16_t time) { |
| 65 | + static uint8_t blink = 1; |
| 66 | + log_d("Identify called for %d seconds", time); |
| 67 | + if (time == 0) { |
| 68 | + zbTempLight.restoreLight(); |
| 69 | + return; |
| 70 | + } |
| 71 | + rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink); |
| 72 | + blink = !blink; |
| 73 | +} |
| 74 | + |
| 75 | +void setup() { |
| 76 | + Serial.begin(115200); |
| 77 | + rgbLedWrite(led, 0, 0, 0); |
| 78 | + pinMode(button, INPUT_PULLUP); |
| 79 | + zbTempLight.onLightChange(setTempLight); |
| 80 | + zbTempLight.onIdentify(identify); |
| 81 | + zbTempLight.setManufacturerAndModel("Espressif", "ZBTempLightBulb"); |
| 82 | + // High Kelvin -> Low Mireds: Min and Max is switched |
| 83 | + zbTempLight.setMinMaxTemperature(kelvinToMireds(6500), kelvinToMireds(2000)); |
| 84 | + Serial.println("Adding ZigbeeTemperatureDimmableLight endpoint to Zigbee Core"); |
| 85 | + Zigbee.addEndpoint(&zbTempLight); |
| 86 | + if (!Zigbee.begin()) { |
| 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 | + if (digitalRead(button) == LOW) { |
| 101 | + delay(100); |
| 102 | + int startTime = millis(); |
| 103 | + while (digitalRead(button) == LOW) { |
| 104 | + delay(50); |
| 105 | + if ((millis() - startTime) > 3000) { |
| 106 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 107 | + delay(1000); |
| 108 | + Zigbee.factoryReset(); |
| 109 | + } |
| 110 | + } |
| 111 | + zbTempLight.setLightLevel(zbTempLight.getLightLevel() + 50); |
| 112 | + } |
| 113 | + delay(100); |
| 114 | +} |
0 commit comments