|
| 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 Zigbee carbon dioxide sensor. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device carbon dioxide sensor. |
| 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 | + */ |
| 27 | + |
| 28 | +#ifndef ZIGBEE_MODE_ED |
| 29 | +#error "Zigbee end device mode is not selected in Tools->Zigbee mode" |
| 30 | +#endif |
| 31 | + |
| 32 | +#include "Zigbee.h" |
| 33 | + |
| 34 | +/* Zigbee carbon dioxide sensor configuration */ |
| 35 | +#define CARBON_DIOXIDE_SENSOR_ENDPOINT_NUMBER 10 |
| 36 | +uint8_t button = BOOT_PIN; |
| 37 | + |
| 38 | +ZigbeeCarbonDioxideSensor zbCarbonDioxideSensor = ZigbeeCarbonDioxideSensor(CARBON_DIOXIDE_SENSOR_ENDPOINT_NUMBER); |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(115200); |
| 42 | + |
| 43 | + // Init button switch |
| 44 | + pinMode(button, INPUT_PULLUP); |
| 45 | + |
| 46 | + // Optional: set Zigbee device name and model |
| 47 | + zbCarbonDioxideSensor.setManufacturerAndModel("Espressif", "ZigbeeCarbonDioxideSensor"); |
| 48 | + |
| 49 | + // Set minimum and maximum carbon dioxide measurement value in ppm |
| 50 | + zbCarbonDioxideSensor.setMinMaxValue(0, 1500); |
| 51 | + |
| 52 | + // Add endpoints to Zigbee Core |
| 53 | + Zigbee.addEndpoint(&zbCarbonDioxideSensor); |
| 54 | + |
| 55 | + Serial.println("Starting Zigbee..."); |
| 56 | + // When all EPs are registered, start Zigbee in End Device mode |
| 57 | + if (!Zigbee.begin()) { |
| 58 | + Serial.println("Zigbee failed to start!"); |
| 59 | + Serial.println("Rebooting..."); |
| 60 | + ESP.restart(); |
| 61 | + } else { |
| 62 | + Serial.println("Zigbee started successfully!"); |
| 63 | + } |
| 64 | + Serial.println("Connecting to network"); |
| 65 | + while (!Zigbee.connected()) { |
| 66 | + Serial.print("."); |
| 67 | + delay(100); |
| 68 | + } |
| 69 | + Serial.println(); |
| 70 | + |
| 71 | + // Set reporting interval for carbon dioxide measurement to be done every 30 seconds, must be called after Zigbee.begin() |
| 72 | + // min_interval and max_interval in seconds, delta (carbon dioxide change in ppm) |
| 73 | + // if min = 1 and max = 0, reporting is sent only when carbon dioxide changes by delta |
| 74 | + // if min = 0 and max = 10, reporting is sent every 10 seconds or when carbon dioxide changes by delta |
| 75 | + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change |
| 76 | + zbCarbonDioxideSensor.setReporting(0, 30, 0); |
| 77 | +} |
| 78 | + |
| 79 | +void loop() { |
| 80 | + static uint32_t timeCounter = 0; |
| 81 | + // Read carbon dioxide sensor every 2s |
| 82 | + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s |
| 83 | + // Read sensor value - here is chip temperature used + 300 as a dummy value for demonstration |
| 84 | + uint16_t carbon_dioxide_value = 300 + (uint16_t)temperatureRead(); |
| 85 | + Serial.printf("Updating carbon dioxide sensor value to %d ppm\r\n", carbon_dioxide_value); |
| 86 | + zbCarbonDioxideSensor.setCarbonDioxide(carbon_dioxide_value); |
| 87 | + } |
| 88 | + |
| 89 | + // Checking button for factory reset and reporting |
| 90 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 91 | + // Key debounce handling |
| 92 | + delay(100); |
| 93 | + int startTime = millis(); |
| 94 | + while (digitalRead(button) == LOW) { |
| 95 | + delay(50); |
| 96 | + if ((millis() - startTime) > 3000) { |
| 97 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 98 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 99 | + delay(1000); |
| 100 | + Zigbee.factoryReset(); |
| 101 | + } |
| 102 | + } |
| 103 | + zbCarbonDioxideSensor.report(); |
| 104 | + } |
| 105 | + delay(100); |
| 106 | +} |
0 commit comments