|
| 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 multistate input / output device. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device multistate device. |
| 19 | + * In the example, we have two multistate devices: |
| 20 | + * - zbMultistateDevice: uses defined application states from Zigbee specification |
| 21 | + * - zbMultistateDeviceCustom: uses custom application states (user defined) |
| 22 | + * |
| 23 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 24 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 25 | + * |
| 26 | + * Please check the README.md for instructions and more detailed description. |
| 27 | + * |
| 28 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 29 | + * Modified by Pat Clay |
| 30 | + */ |
| 31 | + |
| 32 | +#ifndef ZIGBEE_MODE_ZCZR |
| 33 | +#error "Zigbee coordinator/router device mode is not selected in Tools->Zigbee mode" |
| 34 | +#endif |
| 35 | + |
| 36 | +#include "Zigbee.h" |
| 37 | + |
| 38 | +/* Zigbee multistate device configuration */ |
| 39 | +#define MULTISTATE_DEVICE_ENDPOINT_NUMBER 1 |
| 40 | + |
| 41 | +uint8_t button = BOOT_PIN; |
| 42 | + |
| 43 | +// zbMultistateDevice will use defined application states |
| 44 | +ZigbeeMultistate zbMultistateDevice = ZigbeeMultistate(MULTISTATE_DEVICE_ENDPOINT_NUMBER); |
| 45 | + |
| 46 | +// zbMultistateDeviceCustom will use custom application states (user defined) |
| 47 | +ZigbeeMultistate zbMultistateDeviceCustom = ZigbeeMultistate(MULTISTATE_DEVICE_ENDPOINT_NUMBER + 1); |
| 48 | + |
| 49 | +const char* multistate_custom_state_names[6] = {"Off", "On", "UltraSlow", "Slow", "Fast", "SuperFast"}; |
| 50 | + |
| 51 | +void onStateChange(uint16_t state) { |
| 52 | + // print the state |
| 53 | + Serial.printf("Received state change: %d\r\n", state); |
| 54 | + // print the state name using the stored state names |
| 55 | + const char* const* state_names = zbMultistateDevice.getMultistateOutputStateNames(); |
| 56 | + if (state_names && state < zbMultistateDevice.getMultistateOutputStateNamesLength()) { |
| 57 | + Serial.printf("State name: %s\r\n", state_names[state]); |
| 58 | + } |
| 59 | + // print state index of possible options |
| 60 | + Serial.printf("State index: %d / %d\r\n", state, zbMultistateDevice.getMultistateOutputStateNamesLength() - 1); |
| 61 | +} |
| 62 | + |
| 63 | +void onStateChangeCustom(uint16_t state) { |
| 64 | + // print the state |
| 65 | + Serial.printf("Received state change: %d\r\n", state); |
| 66 | + // print the state name using the stored state names |
| 67 | + const char* const* state_names = zbMultistateDeviceCustom.getMultistateOutputStateNames(); |
| 68 | + if (state_names && state < zbMultistateDeviceCustom.getMultistateOutputStateNamesLength()) { |
| 69 | + Serial.printf("State name: %s\r\n", state_names[state]); |
| 70 | + } |
| 71 | + // print state index of possible options |
| 72 | + Serial.printf("State index: %d / %d\r\n", state, zbMultistateDevice.getMultistateOutputStateNamesLength() - 1); |
| 73 | + |
| 74 | + Serial.print("Changing to fan mode to: "); |
| 75 | + switch (state) { |
| 76 | + case 0: |
| 77 | + Serial.println("Off"); |
| 78 | + break; |
| 79 | + case 1: |
| 80 | + Serial.println("On"); |
| 81 | + break; |
| 82 | + case 2: |
| 83 | + Serial.println("Slow"); |
| 84 | + break; |
| 85 | + case 3: |
| 86 | + Serial.println("Medium"); |
| 87 | + break; |
| 88 | + case 4: |
| 89 | + Serial.println("Fast"); |
| 90 | + break; |
| 91 | + default: |
| 92 | + Serial.println("Invalid state"); |
| 93 | + break; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void setup() { |
| 98 | + Serial.begin(115200); |
| 99 | + |
| 100 | + // Init button switch |
| 101 | + pinMode(button, INPUT_PULLUP); |
| 102 | + |
| 103 | + // Optional: set Zigbee device name and model |
| 104 | + zbMultistateDevice.setManufacturerAndModel("Espressif", "ZigbeeMultistateDevice"); |
| 105 | + |
| 106 | + // Set up analog input |
| 107 | + zbMultistateDevice.addMultistateInput(); |
| 108 | + zbMultistateDevice.setMultistateInputApplication(ZB_MULTISTATE_APPLICATION_TYPE_0_INDEX); |
| 109 | + zbMultistateDevice.setMultistateInputDescription("Fan (on/off/auto)"); |
| 110 | + zbMultistateDevice.setMultistateInputStates(ZB_MULTISTATE_APPLICATION_TYPE_0_STATE_NAMES, ZB_MULTISTATE_APPLICATION_TYPE_0_NUM_STATES); |
| 111 | + |
| 112 | + // Set up analog output |
| 113 | + zbMultistateDevice.addMultistateOutput(); |
| 114 | + zbMultistateDevice.setMultistateOutputApplication(ZB_MULTISTATE_APPLICATION_TYPE_7_INDEX); |
| 115 | + zbMultistateDevice.setMultistateOutputDescription("Light (high/normal/low)"); |
| 116 | + zbMultistateDevice.setMultistateOutputStates(ZB_MULTISTATE_APPLICATION_TYPE_7_STATE_NAMES, ZB_MULTISTATE_APPLICATION_TYPE_7_NUM_STATES); |
| 117 | + |
| 118 | + // Set up custom output |
| 119 | + zbMultistateDeviceCustom.addMultistateOutput(); |
| 120 | + zbMultistateDeviceCustom.setMultistateOutputApplication(ZB_MULTISTATE_APPLICATION_TYPE_OTHER_INDEX); |
| 121 | + zbMultistateDeviceCustom.setMultistateOutputDescription("Fan (on/off/slow/medium/fast)"); |
| 122 | + zbMultistateDeviceCustom.setMultistateOutputStates(multistate_custom_state_names, 5); |
| 123 | + |
| 124 | + // Set callback function for multistate output change |
| 125 | + zbMultistateDevice.onMultistateOutputChange(onStateChange); |
| 126 | + zbMultistateDeviceCustom.onMultistateOutputChange(onStateChangeCustom); |
| 127 | + |
| 128 | + // Add endpoints to Zigbee Core |
| 129 | + Zigbee.addEndpoint(&zbMultistateDevice); |
| 130 | + Zigbee.addEndpoint(&zbMultistateDeviceCustom); |
| 131 | + |
| 132 | + Serial.println("Starting Zigbee..."); |
| 133 | + // When all EPs are registered, start Zigbee in Router Device mode |
| 134 | + if (!Zigbee.begin(ZIGBEE_ROUTER)) { |
| 135 | + Serial.println("Zigbee failed to start!"); |
| 136 | + Serial.println("Rebooting..."); |
| 137 | + ESP.restart(); |
| 138 | + } else { |
| 139 | + Serial.println("Zigbee started successfully!"); |
| 140 | + } |
| 141 | + Serial.println("Connecting to network"); |
| 142 | + while (!Zigbee.connected()) { |
| 143 | + Serial.print("."); |
| 144 | + delay(100); |
| 145 | + } |
| 146 | + Serial.println("Connected"); |
| 147 | +} |
| 148 | + |
| 149 | +void loop() { |
| 150 | + static uint32_t timeCounter = 0; |
| 151 | + |
| 152 | + // Checking button for factory reset and reporting |
| 153 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 154 | + // Key debounce handling |
| 155 | + delay(100); |
| 156 | + int startTime = millis(); |
| 157 | + while (digitalRead(button) == LOW) { |
| 158 | + delay(50); |
| 159 | + if ((millis() - startTime) > 3000) { |
| 160 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 161 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 162 | + delay(1000); |
| 163 | + Zigbee.factoryReset(); |
| 164 | + } |
| 165 | + } |
| 166 | + // For demonstration purposes, increment the multistate output/input value by 1 |
| 167 | + if (zbMultistateDevice.getMultistateOutput() < zbMultistateDevice.getMultistateOutputStateNamesLength() - 1) { |
| 168 | + zbMultistateDevice.setMultistateOutput(zbMultistateDevice.getMultistateOutput() + 1); |
| 169 | + zbMultistateDevice.reportMultistateOutput(); |
| 170 | + zbMultistateDevice.setMultistateInput(zbMultistateDevice.getMultistateOutput()); |
| 171 | + zbMultistateDevice.reportMultistateInput(); |
| 172 | + } else { |
| 173 | + zbMultistateDevice.setMultistateOutput(0); |
| 174 | + zbMultistateDevice.reportMultistateOutput(); |
| 175 | + zbMultistateDevice.setMultistateInput(zbMultistateDevice.getMultistateOutput()); |
| 176 | + zbMultistateDevice.reportMultistateInput(); |
| 177 | + } |
| 178 | + |
| 179 | + if (zbMultistateDeviceCustom.getMultistateOutput() < zbMultistateDeviceCustom.getMultistateOutputStateNamesLength() - 1) { |
| 180 | + zbMultistateDeviceCustom.setMultistateOutput(zbMultistateDeviceCustom.getMultistateOutput() + 1); |
| 181 | + zbMultistateDeviceCustom.reportMultistateOutput(); |
| 182 | + } else { |
| 183 | + zbMultistateDeviceCustom.setMultistateOutput(0); |
| 184 | + zbMultistateDeviceCustom.reportMultistateOutput(); |
| 185 | + } |
| 186 | + } |
| 187 | + delay(100); |
| 188 | +} |
0 commit comments