|
| 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 electrical AC measurement device. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a router device that measures |
| 19 | + * AC electrical parameters like voltage, current, power, power factor and frequency. |
| 20 | + * |
| 21 | + * Proper Zigbee mode must be selected in Tools->Zigbee mode |
| 22 | + * and also the correct partition scheme must be selected in Tools->Partition Scheme. |
| 23 | + * |
| 24 | + * Please check the README.md for instructions and more detailed description. |
| 25 | + * |
| 26 | + * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 27 | + */ |
| 28 | + |
| 29 | +// Recommended to use Router mode, as this type of device is expected to be mains powered. |
| 30 | +#ifndef ZIGBEE_MODE_ZCZR |
| 31 | +#error "Zigbee coordinator/router mode is not selected in Tools->Zigbee mode" |
| 32 | +#endif |
| 33 | + |
| 34 | +#include "Zigbee.h" |
| 35 | + |
| 36 | +/* Zigbee AC measurement device configuration */ |
| 37 | +#define AC_ELECTRICAL_MEASUREMENT_ENDPOINT_NUMBER 1 |
| 38 | + |
| 39 | +uint8_t analogPin = A0; |
| 40 | +uint8_t button = BOOT_PIN; |
| 41 | + |
| 42 | +ZigbeeElectricalMeasurement zbElectricalMeasurement = ZigbeeElectricalMeasurement(AC_ELECTRICAL_MEASUREMENT_ENDPOINT_NUMBER); |
| 43 | + |
| 44 | +void onAnalogOutputChange(float analog_output) { |
| 45 | + Serial.printf("Received analog output change: %.1f\r\n", analog_output); |
| 46 | +} |
| 47 | + |
| 48 | +void setup() { |
| 49 | + Serial.begin(115200); |
| 50 | + Serial.println("Starting..."); |
| 51 | + |
| 52 | + // Init button switch |
| 53 | + pinMode(button, INPUT_PULLUP); |
| 54 | + |
| 55 | + // Set analog resolution to 10 bits |
| 56 | + analogReadResolution(10); |
| 57 | + |
| 58 | + // Optional: set Zigbee device name and model |
| 59 | + zbElectricalMeasurement.setManufacturerAndModel("Espressif", "ZigbeeElectricalMeasurementAC"); |
| 60 | + |
| 61 | + // Add analog clusters to Zigbee Analog according your needs |
| 62 | + zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A); |
| 63 | + zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A); |
| 64 | + zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A); |
| 65 | + zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER_FACTOR, ZIGBEE_AC_PHASE_TYPE_A); |
| 66 | + zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC); // frequency is not phase specific (shared) |
| 67 | + |
| 68 | + // Optional: set Multiplier/Divisor for the measurements |
| 69 | + zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, 1, 100); // 1/100 = 0.01V (1 unit of measurement = 0.01V = 10mV) |
| 70 | + zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, 1, 1000); // 1/1000 = 0.001A (1 unit of measurement = 0.001A = 1mA) |
| 71 | + zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, 1, 10); // 1/10 = 0.1W (1 unit of measurement = 0.1W = 100mW) |
| 72 | + zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, 1, 1000); // 1/1000 = 0.001Hz (1 unit of measurement = 0.001Hz = 1mHz) |
| 73 | + |
| 74 | + // Optional: set Min/max values for the measurements |
| 75 | + zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, 0, 30000); // 0-300.00V |
| 76 | + zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, 0, 10000); // 0-10.000A |
| 77 | + zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, 0, 32000); // 0-3200.0W |
| 78 | + zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, 0, 65000); // 0-65.000Hz |
| 79 | + |
| 80 | + // Optional: set power factor for the measurements |
| 81 | + zbElectricalMeasurement.setACPowerFactor(ZIGBEE_AC_PHASE_TYPE_A, 98); // 100 = 1.00, 0 = 0.00, -100 = -1.00 |
| 82 | + |
| 83 | + // Add endpoints to Zigbee Core |
| 84 | + Zigbee.addEndpoint(&zbElectricalMeasurement); |
| 85 | + |
| 86 | + Serial.println("Starting Zigbee..."); |
| 87 | + // When all EPs are registered, start Zigbee in Router mode |
| 88 | + if (!Zigbee.begin(ZIGBEE_ROUTER)) { |
| 89 | + Serial.println("Zigbee failed to start!"); |
| 90 | + Serial.println("Rebooting..."); |
| 91 | + ESP.restart(); |
| 92 | + } else { |
| 93 | + Serial.println("Zigbee started successfully!"); |
| 94 | + } |
| 95 | + Serial.println("Connecting to network"); |
| 96 | + while (!Zigbee.connected()) { |
| 97 | + Serial.print("."); |
| 98 | + delay(100); |
| 99 | + } |
| 100 | + Serial.println("Connected"); |
| 101 | + |
| 102 | + // Optional: Add reporting for AC measurements (this is overriden by HomeAssistant ZHA if used as a Zigbee coordinator) |
| 103 | + zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 10); // report every 30 seconds if value changes by 10 (0.1V) |
| 104 | + zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 100); // report every 30 seconds if value changes by 10 (0.1A) |
| 105 | + zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 10); // report every 30 seconds if value changes by 10 (1W) |
| 106 | + zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, 0, 30, 100); // report every 30 seconds if value changes by 100 (0.1Hz) |
| 107 | +} |
| 108 | + |
| 109 | +void loop() { |
| 110 | + static uint32_t timeCounter = 0; |
| 111 | + |
| 112 | + static uint8_t randomizer = 0; |
| 113 | + // Read ADC value as current to demonstrate the measurements and update the electrical measurement values every 2s |
| 114 | + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s |
| 115 | + uint16_t voltage = 23000 + randomizer; // 230.00 V |
| 116 | + uint16_t current = analogReadMilliVolts(analogPin); // demonstrates 0-3.3A |
| 117 | + int16_t power = ((voltage/100) * (current/1000) * 10); //calculate power in W |
| 118 | + uint16_t frequency = 50135; // 50.000 Hz |
| 119 | + Serial.printf("Updating AC voltage to %d (0.01V), current to %d (mA), power to %d (0.1W), frequency to %d (mHz)\r\n", voltage, current, power, frequency); |
| 120 | + |
| 121 | + // Update the measurements |
| 122 | + zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, voltage); |
| 123 | + zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, current); |
| 124 | + zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, power); |
| 125 | + zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, frequency); |
| 126 | + |
| 127 | + // Report the measurements |
| 128 | + zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A); |
| 129 | + zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A); |
| 130 | + zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A); |
| 131 | + zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC); |
| 132 | + |
| 133 | + randomizer+=10; |
| 134 | + } |
| 135 | + |
| 136 | + // Checking button for factory reset and reporting |
| 137 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 138 | + // Key debounce handling |
| 139 | + delay(100); |
| 140 | + int startTime = millis(); |
| 141 | + while (digitalRead(button) == LOW) { |
| 142 | + delay(50); |
| 143 | + if ((millis() - startTime) > 3000) { |
| 144 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 145 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 146 | + delay(1000); |
| 147 | + Zigbee.factoryReset(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + delay(100); |
| 152 | +} |
0 commit comments