|
| 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 PM2.5 sensor. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device PM2.5 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 PM2.5 sensor configuration */ |
| 35 | +#define PM2_5_SENSOR_ENDPOINT_NUMBER 1 |
| 36 | +uint8_t button = BOOT_PIN; |
| 37 | + |
| 38 | +ZigbeePM25Sensor zbPM25Sensor = ZigbeePM25Sensor(PM2_5_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 | + zbPM25Sensor.setManufacturerAndModel("Espressif", "ZigbeePM25Sensor"); |
| 48 | + |
| 49 | + // Set minimum and maximum PM2.5 measurement value in µg/m³ |
| 50 | + zbPM25Sensor.setMinMaxValue(0, 350); |
| 51 | + |
| 52 | + // Set tolerance for PM2.5 measurement in µg/m³ |
| 53 | + zbPM25Sensor.setTolerance(0.1); |
| 54 | + |
| 55 | + // Add endpoints to Zigbee Core |
| 56 | + Zigbee.addEndpoint(&zbPM25Sensor); |
| 57 | + |
| 58 | + Serial.println("Starting Zigbee..."); |
| 59 | + // When all EPs are registered, start Zigbee in End Device mode |
| 60 | + if (!Zigbee.begin()) { |
| 61 | + Serial.println("Zigbee failed to start!"); |
| 62 | + Serial.println("Rebooting..."); |
| 63 | + ESP.restart(); |
| 64 | + } else { |
| 65 | + Serial.println("Zigbee started successfully!"); |
| 66 | + } |
| 67 | + Serial.println("Connecting to network"); |
| 68 | + while (!Zigbee.connected()) { |
| 69 | + Serial.print("."); |
| 70 | + delay(100); |
| 71 | + } |
| 72 | + Serial.println(); |
| 73 | + |
| 74 | + // Set reporting interval for PM2.5 measurement to be done every 30 seconds, must be called after Zigbee.begin() |
| 75 | + // min_interval and max_interval in seconds, delta (PM2.5 change in µg/m³) |
| 76 | + // if min = 1 and max = 0, reporting is sent only when PM2.5 changes by delta |
| 77 | + // if min = 0 and max = 10, reporting is sent every 10 seconds or when PM2.5 changes by delta |
| 78 | + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change |
| 79 | + zbPM25Sensor.setReporting(0, 30, 0); |
| 80 | +} |
| 81 | + |
| 82 | +void loop() { |
| 83 | + static uint32_t timeCounter = 0; |
| 84 | + // Read PM2.5 sensor every 2s |
| 85 | + if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s |
| 86 | + // Read sensor value - here is chip temperature used + 50 as a dummy value for demonstration |
| 87 | + float pm25_value = 50.5 + temperatureRead(); |
| 88 | + Serial.printf("Updating PM2.5 sensor value to %0.1f µg/m³\r\n", pm25_value); |
| 89 | + zbPM25Sensor.setPM25(pm25_value); |
| 90 | + } |
| 91 | + |
| 92 | + // Checking button for factory reset and reporting |
| 93 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 94 | + // Key debounce handling |
| 95 | + delay(100); |
| 96 | + int startTime = millis(); |
| 97 | + while (digitalRead(button) == LOW) { |
| 98 | + delay(50); |
| 99 | + if ((millis() - startTime) > 3000) { |
| 100 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 101 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 102 | + delay(1000); |
| 103 | + Zigbee.factoryReset(); |
| 104 | + } |
| 105 | + } |
| 106 | + zbPM25Sensor.report(); |
| 107 | + } |
| 108 | + delay(100); |
| 109 | +} |
0 commit comments