Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @brief Zigbee temperature and humidity sensor Zigbee Sleepy End Device.
- *
- * https://tutoduino.fr/tutoriels/esp32c6-zigbee/
- * This code is based on example "Zigbee temperature and humidity sensor Sleepy device" created by Jan Procházka
- * https://github.com/espressif/arduino-esp32/tree/master/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy
- */
- #ifndef ZIGBEE_MODE_ED
- #error "Zigbee end device mode is not selected in Tools->Zigbee mode"
- #endif
- // Comment or uncomment the following line to display or not debug traces in serial monitor of Arduino IDE
- #define DEBUG_TRACE
- #include "Zigbee.h"
- /* Zigbee battery sensor configuration */
- #define BATT_SENSOR_ENDPOINT_NUMBER 10
- #define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
- #define TIME_TO_SLEEP 10 /* Sleep for 10 seconds */
- ZigbeeElectricalMeasurement zbTempSensor = ZigbeeElectricalMeasurement(BATT_SENSOR_ENDPOINT_NUMBER);
- // 3.7 V Li-Ion battery voltage
- const float minVoltage = 3.0;
- const float maxVoltage = 4.0;
- // Mapp float values to percentage
- uint8_t mapFloat(float x, float in_min, float in_max) {
- float val;
- val = (x - in_min) * (100) / (in_max - in_min);
- if (val < 0) {
- val = 0;
- } else if (val > 100) {
- val = 100;
- }
- return (uint8_t)val;
- }
- // Get battery voltage en V
- float getVbatt() {
- uint32_t Vbatt = 0;
- for (int i = 0; i < 16; i++) {
- Vbatt += analogReadMilliVolts(A0); // Read and accumulate ADC voltage
- }
- return (2 * Vbatt / 16 / 1000.0); // Adjust for 1:2 divider and convert to volts
- }
- // Get data from battery sensor and go to deep sleep mode
- void meausureAndSleep() {
- uint8_t percentage;
- float vBat;
- // Measure battery voltage
- vBat = getVbatt();
- percentage = mapFloat(vBat, minVoltage, maxVoltage);
- #ifdef DEBUG_TRACE
- Serial.printf("Battery: %.2fV (%d%%)\n", vBat, percentage);
- #endif
- // Update battery percentage
- zbTempSensor.setBatteryPercentage(percentage);
- zbTempSensor.setBatteryVoltage(vBat * 10); // voltage in 100mV
- // Report values
- zbTempSensor.reportBatteryPercentage();
- #ifdef DEBUG_TRACE
- Serial.printf("Debug trace - inteded for temperature, but no sensor exists.");
- #endif
- // Turn on the builtin LED for a very short time
- flashLED(5);
- // Add small delay to allow the data to be sent before going to sleep
- delay(500);
- // Put device to deep sleep
- #ifdef DEBUG_TRACE
- Serial.printf("Going to sleep for %d seconds\r\n", TIME_TO_SLEEP);
- #endif
- esp_deep_sleep_start();
- }
- // Internal Led flash (n times)
- void flashLED(int n) {
- for (int i = 0; i < n; i++) {
- // Turn on LED for 100ms
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(200);
- }
- }
- /********************* Arduino functions **************************/
- void setup() {
- #ifdef DEBUG_TRACE
- Serial.begin(115200);
- delay(100);
- Serial.println();
- Serial.println("Tutoduino Zigbee battery sensor start!");
- #endif
- // Configure use of external antenna
- pinMode(WIFI_ENABLE, OUTPUT); // pinMode(3, OUTPUT);
- digitalWrite(WIFI_ENABLE, LOW); // digitalWrite(3, LOW); // Activate RF switch control
- delay(100);
- pinMode(WIFI_ANT_CONFIG, OUTPUT); // pinMode(14, OUTPUT);
- digitalWrite(WIFI_ANT_CONFIG, HIGH); // digitalWrite(14, HIGH); // Use external antenna
- // Configure builtin LED and turn it OFF (HIGH)
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, HIGH);
- // Internal LED flash twice to indicate device start
- flashLED(2);
- // Configure A0 as ADC input for reading battery voltage
- pinMode(A0, INPUT);
- // Configure the wake up source and set to wake up every 10 seconds
- esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
- // Optional: set Zigbee device name and model
- zbTempSensor.setManufacturerAndModel("Tutoduino", "ESP32C6TempSensor");
- // Set power source to battery, battery percentage and battery voltage (now 100% and 3.5V for demonstration)
- // The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) or zbTempSensor.setBatteryVoltage(voltage) anytime after Zigbee.begin()
- zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100, 35);
- // Add endpoint to Zigbee Core
- Zigbee.addEndpoint(&zbTempSensor);
- // Create a default Zigbee configuration for End Device
- esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
- #ifdef DEBUG_TRACE
- Serial.println("Starting Zigbee");
- #endif
- // When all EPs are registered, start Zigbee in End Device mode
- if (!Zigbee.begin(&zigbeeConfig, false)) {
- // If Zigbee does not start with 30s default timeout (ZB_BEGIN_TIMEOUT_DEFAULT) then restart
- flashLED(10);
- #ifdef DEBUG_TRACE
- Serial.println("Zigbee failed to start!");
- Serial.println("Rebooting ESP32!");
- #endif
- ESP.restart(); // If Zigbee failed to start, reboot the device and try again
- }
- #ifdef DEBUG_TRACE
- esp_zb_set_tx_power(11);
- // read tx_power
- int8_t power;
- esp_zb_get_tx_power(&power);
- Serial.println(power);
- Serial.println("Connecting to network");
- #endif
- while (!Zigbee.connected()) {
- #ifdef DEBUG_TRACE
- Serial.print(".");
- #endif
- delay(1000);
- flashLED(5);
- }
- #ifdef DEBUG_TRACE
- Serial.println("Successfully connected to Zigbee network");
- #endif
- // Delay approx 1s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices
- delay(1000);
- // Call the function to measure temperature and put the device to deep sleep
- meausureAndSleep();
- }
- void loop() {
- // No actions are performed in the loop (the ESP32C6 enters the setup function when it exits deep sleep).
- }
Advertisement
Add Comment
Please, Sign In to add comment