|
| 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 windspeed sensor. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create a end device wind speed sensor. |
| 19 | + * The wind speed 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 coordinator mode is not selected in Tools->Zigbee mode" |
| 31 | +#endif |
| 32 | + |
| 33 | +#include "Zigbee.h" |
| 34 | + |
| 35 | +#define BUTTON_PIN 9 //Boot button for C6/H2 |
| 36 | +#define WIND_SPEED_SENSOR_ENDPOINT_NUMBER 10 |
| 37 | + |
| 38 | +ZigbeeWindSpeedSensor zbWindSpeedSensor = ZigbeeWindSpeedSensor(WIND_SPEED_SENSOR_ENDPOINT_NUMBER); |
| 39 | + |
| 40 | +/************************ WindSpeed sensor *****************************/ |
| 41 | +static void windspeed_sensor_value_update(void *arg) { |
| 42 | + for (;;) { |
| 43 | + // Read wind speed sensor value (simulated now by temperature sensor) |
| 44 | + float windspeed = temperatureRead(); |
| 45 | + log_v("Wind speed sensor value: %.2fm/s", windspeed); |
| 46 | + // Update windspeed value in Windspeed sensor EP |
| 47 | + zbWindSpeedSensor.setWindSpeed(windspeed); |
| 48 | + delay(1000); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/********************* Arduino functions **************************/ |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); |
| 55 | + while (!Serial) { |
| 56 | + delay(10); |
| 57 | + } |
| 58 | + |
| 59 | + // Init button switch |
| 60 | + pinMode(BUTTON_PIN, INPUT); |
| 61 | + |
| 62 | + // Optional: set Zigbee device name and model |
| 63 | + zbWindSpeedSensor.setManufacturerAndModel("Espressif", "ZigbeeWindSpeedSensor"); |
| 64 | + |
| 65 | + // Set minimum and maximum windspeed measurement value in m/s |
| 66 | + zbWindSpeedSensor.setMinMaxValue(0, 50); |
| 67 | + |
| 68 | + // Set tolerance for windspeed measurement in m/s (lowest possible value is 0.01 m/s) |
| 69 | + zbWindSpeedSensor.setTolerance(1); |
| 70 | + |
| 71 | + // Add endpoint to Zigbee Core |
| 72 | + Zigbee.addEndpoint(&zbWindSpeedSensor); |
| 73 | + |
| 74 | + Serial.println("Starting Zigbee..."); |
| 75 | + // When all EPs are registered, start Zigbee in End Device mode |
| 76 | + if (!Zigbee.begin()) { |
| 77 | + Serial.println("Zigbee failed to start!"); |
| 78 | + Serial.println("Rebooting..."); |
| 79 | + ESP.restart(); |
| 80 | + } else { |
| 81 | + Serial.println("Zigbee started successfully!"); |
| 82 | + } |
| 83 | + Serial.println("Connecting to network"); |
| 84 | + while (!Zigbee.connected()) { |
| 85 | + Serial.print("."); |
| 86 | + delay(100); |
| 87 | + } |
| 88 | + Serial.println(); |
| 89 | + |
| 90 | + // Start Wind speed sensor reading task |
| 91 | + xTaskCreate(windspeed_sensor_value_update, "wind_speed_sensor_update", 2048, NULL, 10, NULL); |
| 92 | + |
| 93 | + // Set reporting interval for windspeed measurement in seconds, must be called after Zigbee.begin() |
| 94 | + // min_interval and max_interval in seconds, delta (WindSpeed change in m/s) |
| 95 | + // if min = 1 and max = 0, reporting is sent only when windspeed changes by delta |
| 96 | + // if min = 0 and max = 10, reporting is sent every 10 seconds or windspeed changes by delta |
| 97 | + // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of windspeed change |
| 98 | + zbWindSpeedSensor.setReporting(1, 0, 1); |
| 99 | +} |
| 100 | + |
| 101 | +void loop() { |
| 102 | + // Checking button for factory reset |
| 103 | + if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed |
| 104 | + // Key debounce handling |
| 105 | + delay(100); |
| 106 | + int startTime = millis(); |
| 107 | + while (digitalRead(BUTTON_PIN) == LOW) { |
| 108 | + delay(50); |
| 109 | + if ((millis() - startTime) > 3000) { |
| 110 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 111 | + Serial.println("Resetting Zigbee to factory and rebooting in 1s."); |
| 112 | + delay(1000); |
| 113 | + Zigbee.factoryReset(); |
| 114 | + } |
| 115 | + } |
| 116 | + zbWindSpeedSensor.reportWindSpeed(); |
| 117 | + } |
| 118 | + delay(100); |
| 119 | +} |
0 commit comments