|
| 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 and humidity sensor Sleepy device. |
| 17 | + * |
| 18 | + * The example demonstrates how to use Zigbee library to create an end device temperature and humidity sensor. |
| 19 | + * The sensor is a Zigbee end device, which is reporting data to the Zigbee network. |
| 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 TEMP_SENSOR_ENDPOINT_NUMBER 10 |
| 37 | + |
| 38 | +#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ |
| 39 | +#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */ |
| 40 | + |
| 41 | +ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); |
| 42 | + |
| 43 | +/************************ Temp sensor *****************************/ |
| 44 | +void meausureAndSleep() { |
| 45 | + // Measure temperature sensor value |
| 46 | + float temperature = temperatureRead(); |
| 47 | + |
| 48 | + // Use temparture value as humidity value to demonstrate both temperature and humidity |
| 49 | + float humidity = temperature; |
| 50 | + |
| 51 | + // Update temperature and humidity values in Temperature sensor EP |
| 52 | + zbTempSensor.setTemperature(temperature); |
| 53 | + zbTempSensor.setHumidity(humidity); |
| 54 | + |
| 55 | + // Report temperature and humidity values |
| 56 | + zbTempSensor.reportTemperature(); |
| 57 | + zbTempSensor.reportHumidity(); |
| 58 | + |
| 59 | + log_d("Temperature: %.2f°C, Humidity: %.2f%", temperature, humidity); |
| 60 | + |
| 61 | + // Put device to deep sleep |
| 62 | + esp_deep_sleep_start(); |
| 63 | +} |
| 64 | + |
| 65 | +/********************* Arduino functions **************************/ |
| 66 | +void setup() { |
| 67 | + // Init button switch |
| 68 | + pinMode(BUTTON_PIN, INPUT_PULLUP); |
| 69 | + |
| 70 | + // Configure the wake up source and set to wake up every 5 seconds |
| 71 | + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); |
| 72 | + |
| 73 | + // Optional: set Zigbee device name and model |
| 74 | + zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensorTest"); |
| 75 | + |
| 76 | + // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) |
| 77 | + zbTempSensor.setMinMaxValue(10, 50); |
| 78 | + |
| 79 | + // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) |
| 80 | + zbTempSensor.setTolerance(1); |
| 81 | + |
| 82 | + // Set power source to battery and set battery percentage to measured value (now 100% for demonstration) |
| 83 | + // The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) anytime |
| 84 | + zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100); |
| 85 | + |
| 86 | + // Add humidity cluster to the temperature sensor device with min, max and tolerance values |
| 87 | + zbTempSensor.addHumiditySensor(0, 100, 1); |
| 88 | + |
| 89 | + // Add endpoint to Zigbee Core |
| 90 | + Zigbee.addEndpoint(&zbTempSensor); |
| 91 | + |
| 92 | + // Create a custom Zigbee configuration for End Device with keep alive 10s to avoid interference with reporting data |
| 93 | + esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG(); |
| 94 | + zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000; |
| 95 | + |
| 96 | + // When all EPs are registered, start Zigbee in End Device mode |
| 97 | + Zigbee.begin(&zigbeeConfig, false); |
| 98 | + |
| 99 | + // Wait for Zigbee to start |
| 100 | + while (!Zigbee.isStarted()) { |
| 101 | + delay(100); |
| 102 | + } |
| 103 | + |
| 104 | + // Delay 5s to allow establishing connection with coordinator, needed for sleepy devices |
| 105 | + delay(5000); |
| 106 | +} |
| 107 | + |
| 108 | +void loop() { |
| 109 | + // Checking button for factory reset |
| 110 | + if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed |
| 111 | + // Key debounce handling |
| 112 | + delay(100); |
| 113 | + int startTime = millis(); |
| 114 | + while (digitalRead(BUTTON_PIN) == LOW) { |
| 115 | + delay(50); |
| 116 | + if ((millis() - startTime) > 3000) { |
| 117 | + // If key pressed for more than 3secs, factory reset Zigbee and reboot |
| 118 | + Zigbee.factoryReset(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Call the function to measure temperature and put the device to sleep |
| 124 | + meausureAndSleep(); |
| 125 | +} |
0 commit comments