Guest User

C6 Zigbee battery monitor

a guest
Mar 3rd, 2026
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | Source Code | 0 0
  1. /**
  2.  * @brief Zigbee temperature and humidity sensor Zigbee Sleepy End Device.
  3.  *
  4.  * https://tutoduino.fr/tutoriels/esp32c6-zigbee/
  5.  * This code is based on example "Zigbee temperature and humidity sensor Sleepy device" created by Jan Procházka
  6.  * https://github.com/espressif/arduino-esp32/tree/master/libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy
  7.  */
  8. #ifndef ZIGBEE_MODE_ED
  9. #error "Zigbee end device mode is not selected in Tools->Zigbee mode"
  10. #endif
  11. // Comment or uncomment the following line to display or not debug traces in serial monitor of Arduino IDE
  12. #define DEBUG_TRACE
  13. #include "Zigbee.h"
  14.  
  15.  
  16. /* Zigbee battery sensor configuration */
  17. #define BATT_SENSOR_ENDPOINT_NUMBER 10
  18. #define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
  19. #define TIME_TO_SLEEP 10        /* Sleep for 10 seconds */
  20. ZigbeeElectricalMeasurement zbTempSensor = ZigbeeElectricalMeasurement(BATT_SENSOR_ENDPOINT_NUMBER);
  21.  
  22. // 3.7 V Li-Ion battery voltage
  23. const float minVoltage = 3.0;
  24. const float maxVoltage = 4.0;
  25. // Mapp float values to percentage
  26. uint8_t mapFloat(float x, float in_min, float in_max) {
  27.   float val;
  28.   val = (x - in_min) * (100) / (in_max - in_min);
  29.   if (val < 0) {
  30.     val = 0;
  31.   } else if (val > 100) {
  32.     val = 100;
  33.   }
  34.   return (uint8_t)val;
  35. }
  36. // Get battery voltage en V
  37. float getVbatt() {
  38.   uint32_t Vbatt = 0;
  39.   for (int i = 0; i < 16; i++) {
  40.     Vbatt += analogReadMilliVolts(A0);  // Read and accumulate ADC voltage
  41.   }
  42.   return (2 * Vbatt / 16 / 1000.0);  // Adjust for 1:2 divider and convert to volts
  43. }
  44.  
  45. // Get data from battery sensor and go to deep sleep mode
  46. void meausureAndSleep() {
  47.   uint8_t percentage;
  48.   float vBat;
  49.   // Measure battery voltage
  50.   vBat = getVbatt();
  51.   percentage = mapFloat(vBat, minVoltage, maxVoltage);
  52. #ifdef DEBUG_TRACE
  53.   Serial.printf("Battery: %.2fV (%d%%)\n", vBat, percentage);
  54. #endif
  55.   // Update battery percentage
  56.   zbTempSensor.setBatteryPercentage(percentage);
  57.   zbTempSensor.setBatteryVoltage(vBat * 10);  // voltage in 100mV
  58.   // Report values
  59.   zbTempSensor.reportBatteryPercentage();
  60. #ifdef DEBUG_TRACE
  61.   Serial.printf("Debug trace - inteded for temperature, but no sensor exists.");
  62. #endif
  63.   // Turn on the builtin LED for a very short time
  64.   flashLED(5);
  65.   // Add small delay to allow the data to be sent before going to sleep
  66.   delay(500);
  67.   // Put device to deep sleep
  68. #ifdef DEBUG_TRACE
  69.   Serial.printf("Going to sleep for %d seconds\r\n", TIME_TO_SLEEP);
  70. #endif
  71.   esp_deep_sleep_start();
  72. }
  73.  
  74. // Internal Led flash (n times)
  75. void flashLED(int n) {
  76.   for (int i = 0; i < n; i++) {
  77.     // Turn on LED for 100ms
  78.     digitalWrite(LED_BUILTIN, LOW);
  79.     delay(100);
  80.     digitalWrite(LED_BUILTIN, HIGH);
  81.     delay(200);
  82.   }
  83. }
  84.  
  85.  
  86. /********************* Arduino functions **************************/
  87. void setup() {
  88. #ifdef DEBUG_TRACE
  89.   Serial.begin(115200);
  90.   delay(100);
  91.   Serial.println();
  92.   Serial.println("Tutoduino Zigbee battery sensor start!");
  93. #endif
  94.   // Configure use of external antenna
  95.   pinMode(WIFI_ENABLE, OUTPUT);    // pinMode(3, OUTPUT);
  96.   digitalWrite(WIFI_ENABLE, LOW);  // digitalWrite(3, LOW); // Activate RF switch control
  97.   delay(100);
  98.   pinMode(WIFI_ANT_CONFIG, OUTPUT);     // pinMode(14, OUTPUT);
  99.   digitalWrite(WIFI_ANT_CONFIG, HIGH);  // digitalWrite(14, HIGH); // Use external antenna
  100.   // Configure builtin LED and turn it OFF (HIGH)
  101.   pinMode(LED_BUILTIN, OUTPUT);
  102.   digitalWrite(LED_BUILTIN, HIGH);
  103.   // Internal LED flash twice to indicate device start
  104.   flashLED(2);
  105.   // Configure A0 as ADC input for reading battery voltage
  106.   pinMode(A0, INPUT);
  107.   // Configure the wake up source and set to wake up every 10 seconds
  108.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  109.   // Optional: set Zigbee device name and model
  110.   zbTempSensor.setManufacturerAndModel("Tutoduino", "ESP32C6TempSensor");
  111.   // Set power source to battery, battery percentage and battery voltage (now 100% and 3.5V for demonstration)
  112.   // The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) or zbTempSensor.setBatteryVoltage(voltage) anytime after Zigbee.begin()
  113.   zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100, 35);
  114.   // Add endpoint to Zigbee Core
  115.   Zigbee.addEndpoint(&zbTempSensor);
  116.   // Create a default Zigbee configuration for End Device
  117.   esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
  118. #ifdef DEBUG_TRACE
  119.   Serial.println("Starting Zigbee");
  120.  
  121. #endif
  122.   // When all EPs are registered, start Zigbee in End Device mode
  123.   if (!Zigbee.begin(&zigbeeConfig, false)) {
  124.     // If Zigbee does not start with 30s default timeout (ZB_BEGIN_TIMEOUT_DEFAULT) then restart
  125.     flashLED(10);
  126. #ifdef DEBUG_TRACE
  127.     Serial.println("Zigbee failed to start!");
  128.     Serial.println("Rebooting ESP32!");
  129. #endif
  130.     ESP.restart();  // If Zigbee failed to start, reboot the device and try again
  131.   }
  132. #ifdef DEBUG_TRACE
  133.   esp_zb_set_tx_power(11);
  134.  
  135.   // read tx_power
  136.   int8_t power;
  137.  
  138.   esp_zb_get_tx_power(&power);
  139.   Serial.println(power);
  140.   Serial.println("Connecting to network");
  141. #endif
  142.   while (!Zigbee.connected()) {
  143. #ifdef DEBUG_TRACE
  144.     Serial.print(".");
  145. #endif
  146.     delay(1000);
  147.     flashLED(5);
  148.   }
  149. #ifdef DEBUG_TRACE
  150.   Serial.println("Successfully connected to Zigbee network");
  151. #endif
  152.   // Delay approx 1s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices
  153.   delay(1000);
  154.   // Call the function to measure temperature and put the device to deep sleep
  155.   meausureAndSleep();
  156. }
  157. void loop() {
  158.   // No actions are performed in the loop (the ESP32C6 enters the setup function when it exits deep sleep).
  159. }
Advertisement
Add Comment
Please, Sign In to add comment