|
| 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 temperature sensor. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device temperature sensor. |
| 19 | + * The temperature 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 Jan Procházka (https://github.com/P-R-O-C-H-Y/) |
| 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 flow + pressure sensor configuration */ |
| 36 | +#define FLOW_SENSOR_ENDPOINT_NUMBER 10 |
| 37 | +#define PRESSURE_SENSOR_ENDPOINT_NUMBER 11 |
| 38 | + |
| 39 | +uint8_t button = BOOT_PIN; |
| 40 | + |
| 41 | +ZigbeeFlowSensor zbFlowSensor = ZigbeeFlowSensor(FLOW_SENSOR_ENDPOINT_NUMBER); |
| 42 | +ZigbeePressureSensor zbPressureSensor = ZigbeePressureSensor(PRESSURE_SENSOR_ENDPOINT_NUMBER); |
| 43 | + |
| 44 | +/************************ Temp sensor *****************************/ |
| 45 | +static void sensors_reading(void *arg) { |
| 46 | + for (;;) { |
| 47 | + // Read Pressure and Flow sensors value - here is chip temperature used as a dummy value for demonstration |
| 48 | + float flow_value = temperatureRead(); |
| 49 | + uint16_t pressure_value = (uint16_t)temperatureRead()*100; //*100 for demonstration so the value is in 1-3hPa |
| 50 | + Serial.printf("Updating flow sensor value to %.2f\r\n", flow_value); |
| 51 | + zbFlowSensor.setFlow(flow_value); |
| 52 | + Serial.printf("Updating pressure sensor value to %.2f\r\n", pressure_value); |
| 53 | + zbPressureSensor.setPressure(pressure_value); |
| 54 | + delay(1000); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/********************* Arduino functions **************************/ |
| 59 | +void setup() { |
| 60 | + Serial.begin(115200); |
| 61 | + |
| 62 | + // Init button switch |
| 63 | + pinMode(button, INPUT_PULLUP); |
| 64 | + |
| 65 | + // Optional: set Zigbee device name and model |
| 66 | + zbFlowSensor.setManufacturerAndModel("Espressif", "ZigbeeFlowSensor"); |
| 67 | + |
| 68 | + // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) |
| 69 | + zbFlowSensor.setMinMaxValue(0, 100); |
| 70 | + |
| 71 | + // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) |
| 72 | + zbFlowSensor.setTolerance(1); |
| 73 | + |
| 74 | + // Optional: set Zigbee device name and model |
| 75 | + zbPressureSensor.setManufacturerAndModel("Espressif", "ZigbeePressureSensor"); |
| 76 | + |
| 77 | + // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) |
| 78 | + zbPressureSensor.setMinMaxValue(0, 30); |
| 79 | + |
| 80 | + // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) |
| 81 | + zbPressureSensor.setTolerance(1); |
| 82 | + |
| 83 | + // Add endpoints to Zigbee Core |
| 84 | + Zigbee.addEndpoint(&zbFlowSensor); |
| 85 | + Zigbee.addEndpoint(&zbPressureSensor); |
| 86 | + |
| 87 | + Serial.println("Starting Zigbee..."); |
| 88 | + // When all EPs are registered, start Zigbee in End Device mode |
| 89 | + if (!Zigbee.begin()) { |
| 90 | + Serial.println("Zigbee failed to start!"); |
| 91 | + Serial.println("Rebooting..."); |
| 92 | + ESP.restart(); |
| 93 | + } else { |
| 94 | + Serial.println("Zigbee started successfully!"); |
| 95 | + } |
| 96 | + Serial.println("Connecting to network"); |
| 97 | + while (!Zigbee.connected()) { |
| 98 | + Serial.print("."); |
| 99 | + delay(100); |
| 100 | + } |
| 101 | + Serial.println(); |
| 102 | + |
| 103 | + // Start Flow and Pressure sensor reading task |
| 104 | + xTaskCreate(sensors_reading, "flow_pressure_sensors_read", 2048, NULL, 10, NULL); |
| 105 | + |
| 106 | + // Set reporting interval for flow and pressure measurement in seconds, must be called after Zigbee.begin() |
| 107 | + // min_interval and max_interval in seconds, delta (pressure change in Pa, flow change in 0,1 m3/h) |
| 108 | + // if min = 1 and max = 0, reporting is sent only when temperature changes by delta |
| 109 | + // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta |
| 110 | + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change |
| 111 | + zbFlowSensor.setReporting(0, 30, 1); |
| 112 | + zbPressureSensor.setReporting(0, 30, 1); |
| 113 | +} |
| 114 | + |
| 115 | +void loop() { |
| 116 | + // Checking button for factory reset |
| 117 | + if (digitalRead(button) == LOW) { // Push button pressed |
| 118 | + // Key debounce handling |
| 119 | + delay(100); |
| 120 | + int startTime = millis(); |
| 121 | + while (digitalRead(button) == LOW) { |
| 122 | + delay(50); |
| 123 | + if ((millis() - startTime) > 3000) { |
| 124 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 125 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 126 | + delay(1000); |
| 127 | + Zigbee.factoryReset(); |
| 128 | + } |
| 129 | + } |
| 130 | + zbFlowSensor.report(); |
| 131 | + zbPressureSensor.report(); |
| 132 | + } |
| 133 | + delay(100); |
| 134 | +} |
0 commit comments