|
| 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 analog input / output device. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device analog device. |
| 19 | + * |
| 20 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 21 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 22 | + * |
| 23 | + * Please check the README.md for instructions and more detailed description. |
| 24 | + * |
| 25 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 26 | + * Modified by Pat Clay |
| 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 analog device configuration */ |
| 36 | +#define BINARY_DEVICE_ENDPOINT_NUMBER 1 |
| 37 | + |
| 38 | +uint8_t binaryPin = A0; |
| 39 | +uint8_t button = BOOT_PIN; |
| 40 | + |
| 41 | +ZigbeeBinary zbBinaryFan = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER); |
| 42 | +ZigbeeBinary zbBinaryZone = ZigbeeBinary(BINARY_DEVICE_ENDPOINT_NUMBER + 1); |
| 43 | + |
| 44 | +bool binaryStatus = false; |
| 45 | + |
| 46 | +void onAnalogOutputChange(float analog_output) { |
| 47 | + Serial.printf("Received analog output change: %.1f\r\n", analog_output); |
| 48 | +} |
| 49 | + |
| 50 | +void setup() { |
| 51 | + Serial.begin(115200); |
| 52 | + Serial.println("Starting..."); |
| 53 | + |
| 54 | + // Init button switch |
| 55 | + pinMode(button, INPUT_PULLUP); |
| 56 | + |
| 57 | + // Set analog resolution to 10 bits |
| 58 | + analogReadResolution(10); |
| 59 | + |
| 60 | + // Optional: set Zigbee device name and model |
| 61 | + zbBinaryFan.setManufacturerAndModel("Espressif", "ZigbeeBinarySensor"); |
| 62 | + |
| 63 | + // Set up binary fan status input (HVAC) |
| 64 | + zbBinaryFan.addBinaryInput(); |
| 65 | + zbBinaryFan.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_HVAC_FAN_STATUS); |
| 66 | + zbBinaryFan.setBinaryInputDescription("Fan Status"); |
| 67 | + |
| 68 | + // Set up binary zone armed input (Security) |
| 69 | + zbBinaryZone.addBinaryInput(); |
| 70 | + zbBinaryZone.setBinaryInputApplication(BINARY_INPUT_APPLICATION_TYPE_SECURITY_ZONE_ARMED); |
| 71 | + zbBinaryZone.setBinaryInputDescription("Zone Armed"); |
| 72 | + |
| 73 | + // Add endpoints to Zigbee Core |
| 74 | + Zigbee.addEndpoint(&zbBinaryFan); |
| 75 | + Zigbee.addEndpoint(&zbBinaryZone); |
| 76 | + |
| 77 | + Serial.println("Starting Zigbee..."); |
| 78 | + // When all EPs are registered, start Zigbee in End Device mode |
| 79 | + if (!Zigbee.begin()) { |
| 80 | + Serial.println("Zigbee failed to start!"); |
| 81 | + Serial.println("Rebooting..."); |
| 82 | + ESP.restart(); |
| 83 | + } else { |
| 84 | + Serial.println("Zigbee started successfully!"); |
| 85 | + } |
| 86 | + Serial.println("Connecting to network"); |
| 87 | + while (!Zigbee.connected()) { |
| 88 | + Serial.print("."); |
| 89 | + delay(100); |
| 90 | + } |
| 91 | + Serial.println("Connected"); |
| 92 | + |
| 93 | + // // Optional: Add reporting for analog input |
| 94 | + // zbBinaryFan.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 |
| 95 | + // zbBinaryZone.setBinaryInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10 |
| 96 | +} |
| 97 | + |
| 98 | +void loop() { |
| 99 | + // Checking button for factory reset and reporting |
| 100 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 101 | + // Key debounce handling |
| 102 | + delay(100); |
| 103 | + int startTime = millis(); |
| 104 | + while (digitalRead(button) == LOW) { |
| 105 | + delay(50); |
| 106 | + if ((millis() - startTime) > 3000) { |
| 107 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 108 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 109 | + delay(1000); |
| 110 | + Zigbee.factoryReset(); |
| 111 | + } |
| 112 | + } |
| 113 | + // Toggle binary input |
| 114 | + binaryStatus = !binaryStatus; |
| 115 | + zbBinaryFan.setBinaryInput(binaryStatus); |
| 116 | + zbBinaryZone.setBinaryInput(binaryStatus); |
| 117 | + zbBinaryFan.reportBinaryInput(); |
| 118 | + zbBinaryZone.reportBinaryInput(); |
| 119 | + } |
| 120 | + delay(100); |
| 121 | +} |
0 commit comments