|
| 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 simple Zigbee light switch. |
| 17 | + * |
| 18 | + * The example demonstrates how to use ESP Zigbee stack to control a light bulb. |
| 19 | + * The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator (Switch). |
| 20 | + * Button switch and Zigbee runs in separate tasks. |
| 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_ZCZR |
| 31 | +#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "Zigbee_core.h" |
| 35 | +#include "ep/ep_color_dimmer_switch.h" |
| 36 | + |
| 37 | +#include "esp_zigbee_core.h" |
| 38 | +#include "freertos/FreeRTOS.h" |
| 39 | +#include "freertos/task.h" |
| 40 | +#include "ha/esp_zigbee_ha_standard.h" |
| 41 | + |
| 42 | +#define SWITCH_ENDPOINT_NUMBER 5 |
| 43 | + |
| 44 | +/* Switch configuration */ |
| 45 | +#define GPIO_SWITCH GPIO_NUM_9 //Boot button for C6/H2 |
| 46 | + |
| 47 | +/* Zigbee switch */ |
| 48 | +ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBER); |
| 49 | + |
| 50 | +/********************* Arduino functions **************************/ |
| 51 | +void setup() { |
| 52 | + |
| 53 | + Serial.begin(115200); |
| 54 | + |
| 55 | + // Init button switch |
| 56 | + pinMode(GPIO_SWITCH, INPUT); |
| 57 | + |
| 58 | + //Optional: set Zigbee device name and model |
| 59 | + zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch"); |
| 60 | + |
| 61 | + //Optional to allow multiple light to bind to the switch |
| 62 | + zbSwitch.allowMultipleBinding(true); |
| 63 | + |
| 64 | + //Add endpoint to Zigbee Core |
| 65 | + Zigbee.addEndpoint(&zbSwitch); |
| 66 | + |
| 67 | + //Open network for 180 seconds after boot |
| 68 | + Zigbee.setRebootOpenNetwork(180); |
| 69 | + |
| 70 | + // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode |
| 71 | + Zigbee.begin(ZIGBEE_COORDINATOR); |
| 72 | + |
| 73 | + Serial.println("Waiting for Light to bound to the switch"); |
| 74 | + //Wait for switch to bound to a light: |
| 75 | + while(!zbSwitch.is_bound()) |
| 76 | + { |
| 77 | + Serial.printf("."); |
| 78 | + delay(500); |
| 79 | + } |
| 80 | + Serial.println(); |
| 81 | +} |
| 82 | + |
| 83 | +void loop() { |
| 84 | + // Handle button switch in loop() |
| 85 | + if (digitalRead(GPIO_SWITCH) == LOW) { // Push button pressed |
| 86 | + |
| 87 | + // Key debounce handling |
| 88 | + while (digitalRead(GPIO_SWITCH) == LOW) { |
| 89 | + delay(50); |
| 90 | + } |
| 91 | + // Toggle light |
| 92 | + zbSwitch.lightToggle(); |
| 93 | + } |
| 94 | + // Handle serial input to controll color and level of the light |
| 95 | + if (Serial.available()) { |
| 96 | + String command = Serial.readString(); |
| 97 | + |
| 98 | + if (command == "on") { |
| 99 | + zbSwitch.lightOn(); |
| 100 | + } else if (command == "off") { |
| 101 | + zbSwitch.lightOff(); |
| 102 | + } else if (command == "toggle") { |
| 103 | + zbSwitch.lightToggle(); |
| 104 | + } else if (command == "red") { |
| 105 | + zbSwitch.setLightColor(255, 0, 0); |
| 106 | + } else if (command == "green") { |
| 107 | + zbSwitch.setLightColor(0, 255, 0); |
| 108 | + } else if (command == "blue") { |
| 109 | + zbSwitch.setLightColor(0, 0, 255); |
| 110 | + } else if (command == "white") { |
| 111 | + zbSwitch.setLightColor(255, 255, 255); |
| 112 | + } else if (command == "color") { |
| 113 | + //wait for color value |
| 114 | + Serial.println("Enter red value (0-255):"); |
| 115 | + while (!Serial.available()) { |
| 116 | + delay(100); |
| 117 | + } |
| 118 | + int red = Serial.parseInt(); |
| 119 | + Serial.println("Enter green value (0-255):"); |
| 120 | + while (!Serial.available()) { |
| 121 | + delay(100); |
| 122 | + } |
| 123 | + int green = Serial.parseInt(); |
| 124 | + Serial.println("Enter blue value (0-255):"); |
| 125 | + while (!Serial.available()) { |
| 126 | + delay(100); |
| 127 | + } |
| 128 | + int blue = Serial.parseInt(); |
| 129 | + zbSwitch.setLightColor(red, green, blue); |
| 130 | + } else if (command == "level") { |
| 131 | + //wait for level value |
| 132 | + Serial.println("Enter level value (0-255):"); |
| 133 | + while (!Serial.available()) { |
| 134 | + delay(100); |
| 135 | + } |
| 136 | + int level = Serial.parseInt(); |
| 137 | + zbSwitch.setLightLevel(level); |
| 138 | + } else { |
| 139 | + Serial.println("Unknown command"); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // print the bound lights every 30 seconds |
| 144 | + static uint32_t last_print = 0; |
| 145 | + if (millis() - last_print > 30000) { |
| 146 | + last_print = millis(); |
| 147 | + zbSwitch.printBoundLights(); |
| 148 | + } |
| 149 | +} |
0 commit comments