|
| 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 illuminance sensor. |
| 17 | +* |
| 18 | +* The example demonstrates how to use Zigbee library to create a end device illuminance sensor. |
| 19 | +* The illuminance sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. |
| 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 MikaFromTheRoof (https://.com/MikaFromTheRoof) |
| 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 | +#define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9 |
| 36 | +uint8_t button = BOOT_PIN; |
| 37 | +uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected |
| 38 | + |
| 39 | +ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT); |
| 40 | + |
| 41 | +/********************* Illuminance sensor **************************/ |
| 42 | +static void illuminance_sensor_value_update(void *arg) { |
| 43 | +for (;;) { |
| 44 | +// read the raw analog value from the sensor |
| 45 | +int lsens_analog_raw = analogRead(illuminance_sensor_pin); |
| 46 | +Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw); |
| 47 | + |
| 48 | +// conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight) |
| 49 | +// depends on the value range of the raw analog sensor values and will need calibration for correct lux values |
| 50 | +// for demonstration purpose map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000) |
| 51 | +int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); |
| 52 | +Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw); |
| 53 | + |
| 54 | +// according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value |
| 55 | +// Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1) |
| 56 | +int lsens_illuminance_lux = round(pow(10, (lsens_illuminance_raw / 10000.0)) - 1); |
| 57 | +Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux); |
| 58 | + |
| 59 | +// Update illuminance in illuminance sensor EP |
| 60 | +zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here! |
| 61 | + |
| 62 | +delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance |
| 63 | +} |
| 64 | +} |
| 65 | + |
| 66 | +/********************* Arduino functions **************************/ |
| 67 | +void setup() { |
| 68 | +Serial.begin(115200); |
| 69 | + |
| 70 | +// Optional: configure analog input |
| 71 | +analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input) |
| 72 | +analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default |
| 73 | + |
| 74 | +// Init button for factory reset |
| 75 | +pinMode(button, INPUT_PULLUP); |
| 76 | + |
| 77 | +// Optional: Set Zigbee device name and model |
| 78 | +zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor"); |
| 79 | + |
| 80 | +// Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown |
| 81 | +zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS); |
| 82 | + |
| 83 | +// Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux) |
| 84 | +zbIlluminanceSensor.setMinMaxValue(0, 50000); |
| 85 | + |
| 86 | +// Optional: Set tolerance for raw illuminance value |
| 87 | +zbIlluminanceSensor.setTolerance(1); |
| 88 | + |
| 89 | +// Add endpoint to Zigbee Core |
| 90 | +Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core"); |
| 91 | +Zigbee.addEndpoint(&zbIlluminanceSensor); |
| 92 | + |
| 93 | +Serial.println("Starting Zigbee..."); |
| 94 | +// When all EPs are registered, start Zigbee in End Device mode |
| 95 | +if (!Zigbee.begin()) { |
| 96 | +Serial.println("Zigbee failed to start!"); |
| 97 | +Serial.println("Rebooting..."); |
| 98 | +ESP.restart(); |
| 99 | +} else { |
| 100 | +Serial.println("Zigbee started successfully!"); |
| 101 | +} |
| 102 | +Serial.println("Connecting to network"); |
| 103 | +while (!Zigbee.connected()) { |
| 104 | +Serial.print("."); |
| 105 | +delay(100); |
| 106 | +} |
| 107 | +Serial.println(); |
| 108 | + |
| 109 | +// Start illuminance sensor reading task |
| 110 | +xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL); |
| 111 | + |
| 112 | +// Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin() |
| 113 | +// min_interval and max_interval in seconds, delta |
| 114 | +// if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second |
| 115 | +// if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000 |
| 116 | +// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change |
| 117 | +// Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings |
| 118 | +zbIlluminanceSensor.setReporting(1, 0, 1000); |
| 119 | +} |
| 120 | + |
| 121 | +/********************* Main loop **************************/ |
| 122 | +void loop() { |
| 123 | +// Checking button for factory reset |
| 124 | +if (digitalRead(button) == LOW) { // Push button pressed |
| 125 | +// Key debounce handling |
| 126 | +delay(100); |
| 127 | +int startTime = millis(); |
| 128 | +while (digitalRead(button) == LOW) { |
| 129 | +delay(50); |
| 130 | +if ((millis() - startTime) > 3000) { |
| 131 | +// If key pressed for more than 3 secs, factory reset Zigbee and reboot |
| 132 | +Serial.println("Resetting Zigbee to factory and rebooting in 1s"); |
| 133 | +delay(1000); |
| 134 | +Zigbee.factoryReset(); |
| 135 | +} |
| 136 | +} |
| 137 | +// force report of illuminance when button is pressed |
| 138 | +zbIlluminanceSensor.report(); |
| 139 | +} |
| 140 | +delay(100); |
| 141 | +} |
0 commit comments