|
| 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 Zigbee library 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_ED |
| 31 | +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "Zigbee.h" |
| 35 | + |
| 36 | +/* Zigbee Smart button () configuration */ |
| 37 | +#define SWITCH_1_ENDPOINT_NUMBER 5 // SINGLE PRESS |
| 38 | +#define SWITCH_2_ENDPOINT_NUMBER 6 // DOUBLE PRESS |
| 39 | +#define SWITCH_3_ENDPOINT_NUMBER 7 // LONG PRESS |
| 40 | + |
| 41 | +uint8_t button = BOOT_PIN; |
| 42 | + |
| 43 | +// Timing thresholds (in milliseconds) |
| 44 | +const uint16_t debounceTime = 100; // Debounce time |
| 45 | +const uint16_t doublePressTime = 200; // Max time between double presses |
| 46 | +const uint16_t longPressTime = 1000; // Time to detect a long press |
| 47 | +const uint16_t rebootPressTime = 5000; // Time to detect a long press for reboot |
| 48 | + |
| 49 | +// Variables to track button state |
| 50 | +int buttonState = HIGH; // Current state of the button (HIGH means not pressed) |
| 51 | +int lastButtonState = HIGH; // Previous state of the button |
| 52 | + |
| 53 | +// Timing variables |
| 54 | +unsigned long lastDebounceTime = 0; // Last time the button state changed |
| 55 | +unsigned long firstPressTime = 0; // Time of the first press in a sequence |
| 56 | +unsigned long buttonPressStart = 0; // Time when the button was first pressed down |
| 57 | + |
| 58 | +// Flags |
| 59 | +bool singlePressDetected = false; |
| 60 | +bool doublePressDetected = false; |
| 61 | +bool longPressDetected = false; |
| 62 | +bool rebootPressDetected = false; |
| 63 | +bool longPressHandled = false; |
| 64 | +bool doublePressHandled = false; |
| 65 | + |
| 66 | +ZigbeeSmartButton zbSmartButton1 = ZigbeeSmartButton(SWITCH_1_ENDPOINT_NUMBER); |
| 67 | +ZigbeeSmartButton zbSmartButton2 = ZigbeeSmartButton(SWITCH_2_ENDPOINT_NUMBER); |
| 68 | +ZigbeeSmartButton zbSmartButton3 = ZigbeeSmartButton(SWITCH_3_ENDPOINT_NUMBER); |
| 69 | + |
| 70 | +/********************* Arduino functions **************************/ |
| 71 | +void setup() { |
| 72 | + Serial.begin(115200); |
| 73 | + |
| 74 | + // Configure button switch |
| 75 | + pinMode(button, INPUT_PULLUP); |
| 76 | + |
| 77 | + //Optional: set Zigbee device name and model |
| 78 | + zbSmartButton1.setManufacturerAndModel("Espressif", "ZigbeeSmartButton"); |
| 79 | + zbSmartButton2.setManufacturerAndModel("Espressif", "ZigbeeSmartButton"); |
| 80 | + zbSmartButton3.setManufacturerAndModel("Espressif", "ZigbeeSmartButton"); |
| 81 | + |
| 82 | + //Add endpoints to Zigbee Core |
| 83 | + Zigbee.addEndpoint(&zbSmartButton1); |
| 84 | + Zigbee.addEndpoint(&zbSmartButton2); |
| 85 | + Zigbee.addEndpoint(&zbSmartButton3); |
| 86 | + |
| 87 | + // // When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode |
| 88 | + // if (!Zigbee.begin()) { |
| 89 | + // Serial.println("Zigbee failed to start!"); |
| 90 | + // Serial.println("Short press the button to reboot, long press to factory reset Zigbee."); |
| 91 | + // // on button press reboot, on long press factory reset |
| 92 | + // while(true){ |
| 93 | + // if (digitalRead(button) == LOW) { |
| 94 | + // delay(100); |
| 95 | + // int startTime = millis(); |
| 96 | + // while (digitalRead(button) == LOW) { |
| 97 | + // delay(50); |
| 98 | + // if ((millis() - startTime) > 3000) { |
| 99 | + // Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 100 | + // delay(1000); |
| 101 | + // Zigbee.factoryReset(); |
| 102 | + // } |
| 103 | + // } |
| 104 | + // ESP.restart(); |
| 105 | + // } |
| 106 | + // delay(1); |
| 107 | + // } |
| 108 | + // } |
| 109 | + // Serial.println("Connecting to network"); |
| 110 | + // while (!Zigbee.connected()) { |
| 111 | + // Serial.print("."); |
| 112 | + // delay(100); |
| 113 | + // } |
| 114 | + // Serial.println(); |
| 115 | +} |
| 116 | + |
| 117 | +void loop() { |
| 118 | + // Read the button state |
| 119 | + int reading = digitalRead(button); |
| 120 | + |
| 121 | + // Debounce handling |
| 122 | + if (reading != lastButtonState) { |
| 123 | + lastDebounceTime = millis(); |
| 124 | + } |
| 125 | + |
| 126 | + if ((millis() - lastDebounceTime) > debounceTime) { |
| 127 | + // If the state has stabilized, update button state |
| 128 | + if (reading != buttonState) { |
| 129 | + buttonState = reading; |
| 130 | + |
| 131 | + // Detect press events |
| 132 | + if (buttonState == LOW) { |
| 133 | + buttonPressStart = millis(); |
| 134 | + |
| 135 | + if (millis() - firstPressTime <= doublePressTime && firstPressTime != 0) { |
| 136 | + doublePressDetected = true; |
| 137 | + firstPressTime = 0; // Reset for the next sequence |
| 138 | + } else { |
| 139 | + firstPressTime = millis(); |
| 140 | + } |
| 141 | + } else { |
| 142 | + // Button released |
| 143 | + unsigned long pressDuration = millis() - buttonPressStart; |
| 144 | + |
| 145 | + if (!longPressHandled && pressDuration >= rebootPressTime) { |
| 146 | + rebootPressDetected = true; |
| 147 | + } else if (!longPressHandled && pressDuration >= longPressTime) { |
| 148 | + longPressDetected = true; |
| 149 | + } else if (!doublePressHandled && !doublePressDetected && pressDuration < longPressTime) { |
| 150 | + singlePressDetected = true; |
| 151 | + } |
| 152 | + |
| 153 | + longPressHandled = false; // Reset for next press |
| 154 | + |
| 155 | + // // If button is pressed again in a short time, it's a double press so reset the single press flag |
| 156 | + // if (singlePressDetected){ |
| 157 | + // // wait for the double press time window |
| 158 | + |
| 159 | + // } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // Handle events |
| 165 | + if (singlePressDetected && !doublePressDetected) { |
| 166 | + Serial.println("Single press detected"); |
| 167 | + singlePressDetected = false; |
| 168 | + // zbSmartButton1.toggle(); |
| 169 | + } |
| 170 | + |
| 171 | + if (doublePressDetected) { |
| 172 | + Serial.println("Double press detected"); |
| 173 | + doublePressDetected = false; |
| 174 | + doublePressHandled = true; |
| 175 | + // zbSmartButton2.toggle(); |
| 176 | + } |
| 177 | + |
| 178 | + if (longPressDetected) { |
| 179 | + Serial.println("Long press detected"); |
| 180 | + longPressDetected = false; |
| 181 | + longPressHandled = true; |
| 182 | + // zbSmartButton3.toggle(); |
| 183 | + } |
| 184 | + |
| 185 | + if (rebootPressDetected) { |
| 186 | + Serial.println("Reboot press detected. Rebooting..."); |
| 187 | + rebootPressDetected = false; |
| 188 | + longPressHandled = true; |
| 189 | + // Zigbee.factoryReset(); |
| 190 | + } |
| 191 | + |
| 192 | + lastButtonState = reading; |
| 193 | +} |
0 commit comments