Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZigbeeCarbonDioxideSensor is reporting wrong value #10956

Closed
1 task done
cscognamiglio opened this issue Feb 11, 2025 · 1 comment
Closed
1 task done

ZigbeeCarbonDioxideSensor is reporting wrong value #10956

cscognamiglio opened this issue Feb 11, 2025 · 1 comment
Labels
Status: Awaiting triage Issue is waiting for triage

Comments

@cscognamiglio
Copy link

Board

ESP32C6

Device Description

XIAO IESP32C6 zigbee end device.

Hardware Configuration

A CO2 sensor (SGP30) and a temperature sensor (DHT22) are connected to the ESP32

Version

v3.1.0

IDE Name

Arduino IDE

Operating System

macOS 15.2

Flash frequency

80Mhz

PSRAM enabled

yes

Upload speed

921600

Description

Values printed on the serial console are correct, temperature/humidity reported by the ZigbeeTempSensor are also correct, but for some reason ZigbeeCarbonDioxideSensor is reporting a completely different value.

Debugging shows the correct value but zigbee2mqtt shows something else.

I thought it was just with an offset but it is actually random (?).

Image

Sketch

#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif

#include "ZigbeeCore.h"
#include "ep/ZigbeeTempSensor.h"
#include "ep/ZigbeeCarbonDioxideSensor.h"

#include <Wire.h>
#include "Adafruit_SGP30.h"
#include "DHT.h"

#define DHTTYPE DHT22
#define TEMP_SENSOR_ENDPOINT_NUMBER 10
#define CO2_SENSOR_ENDPOINT_NUMBER 11

DHT dht(2, DHTTYPE);

Adafruit_SGP30 sgp;

ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
ZigbeeCarbonDioxideSensor zbCarbonDioxideSensor = ZigbeeCarbonDioxideSensor(CO2_SENSOR_ENDPOINT_NUMBER);


uint32_t getAbsoluteHumidity(float temperature, float humidity) {
    // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
    const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
    const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
    return absoluteHumidityScaled;}

void setup() {
  Serial.printf("Initial setup.\n");
  Serial.begin(9600);

  Wire.begin();
  
  dht.begin();

  //Initialize sensor
  if (sgp.begin() == false) {
    Serial.println("No SGP30 Detected. Check connections.");
    while (1)
      ;
  }

  /* TEMP/HUMIDITY */
  // Optional: set Zigbee device name and model
  zbTempSensor.setManufacturerAndModel("Espressif", "ZigbeeTemperatureSensor");

  // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
  zbTempSensor.setMinMaxValue(10, 50);

  // Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
  zbTempSensor.setTolerance(1);

  // Change power source to battery with 100% capacity
  zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100);
  
  // Add humidity sensor
  zbTempSensor.addHumiditySensor(0, 100, 1);

  

  /* CO2/VOC */
  // Optional: set Zigbee device name and model
  zbCarbonDioxideSensor.setManufacturerAndModel("Espressif", "ZigbeeCarbonDioxideSensor");

  // Set minimum and maximum carbon dioxide measurement value in ppm
  zbCarbonDioxideSensor.setMinMaxValue(400, 1900);



  // Add endpoint to Zigbee Core
  Zigbee.addEndpoint(&zbTempSensor);

  // Add endpoints to Zigbee Core
  Zigbee.addEndpoint(&zbCarbonDioxideSensor);



  /* ZIGBEE DEVICE */

  // Create a custom Zigbee configuration for End Device with keep alive 10s to avoid interference with reporting data
  esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
  zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000;

  // When all EPs are registered, start Zigbee in End Device mode
  if (!Zigbee.begin(&zigbeeConfig, false)) {
    Serial.println("Zigbee failed to start!");
    Serial.println("Rebooting...");
    ESP.restart();
  }

  Serial.println("Connecting to network");
  while (!Zigbee.connected()) {
    Serial.print(".");
    delay(100);
  }

  Serial.println();
  Serial.println("Successfully connected to Zigbee network");

  // Delay approx 1s (may be adjusted) to allow establishing proper connection with coordinator, needed for sleepy devices
  delay(1000);

  // Start Temperature sensor reading task
  //xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL);

  // Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin()
  // min_interval and max_interval in seconds, delta (temp change in °C)
  // if min = 1 and max = 0, reporting is sent only when temperature changes by delta
  // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta
  // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of temperature change
  zbTempSensor.setReporting(1, 0, 1);
  zbTempSensor.setHumidityReporting(1, 0, 1);

  // Set reporting interval for carbon dioxide measurement to be done every 30 seconds, must be called after Zigbee.begin()
  // min_interval and max_interval in seconds, delta (carbon dioxide change in ppm)
  // if min = 1 and max = 0, reporting is sent only when carbon dioxide changes by delta
  // if min = 0 and max = 10, reporting is sent every 10 seconds or when carbon dioxide changes by delta
  // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of delta change
  zbCarbonDioxideSensor.setReporting(0, 2, 0);
  

}

int counter = 0;

void loop() {
  //First fifteen readings will be
  //CO2: 400 ppm  TVOC: 0 ppb

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  sgp.setHumidity(getAbsoluteHumidity(t, h));

  //measure CO2 and TVOC levels
  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  
  Serial.print("TEMP "); Serial.print(t); Serial.print("°\t");
  Serial.print("HUM "); Serial.print(h); Serial.print("%\n");

  Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
  Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm\n");
  
  // Update temperature and humidity values in Temperature sensor EP
  zbTempSensor.setTemperature(t);
  zbTempSensor.setHumidity(h);

  uint16_t carbon_dioxide_value = sgp.eCO2;
  Serial.print("eCO2 Zigbee: "); Serial.print(carbon_dioxide_value); Serial.println(" ppm\n");
  //zbCarbonDioxideSensor.setCarbonDioxide((uint16_t)sgp.eCO2);
  zbCarbonDioxideSensor.setCarbonDioxide(carbon_dioxide_value);

  // Report temperature and humidity values
  zbTempSensor.report();

  zbCarbonDioxideSensor.report();

  // if (! sgp.IAQmeasureRaw()) {
  //   Serial.println("Raw Measurement failed");
  //   return;
  // }
  // Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t");
  // Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("\n");
 
  delay(2500);

  counter++;
  if (counter == 30) {
    counter = 0;

    uint16_t TVOC_base, eCO2_base;
    if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      Serial.println("Failed to get baseline readings");
      return;
    }
    Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
    Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
  }




}

Debug Message

[ 29331][V][ZigbeeTempSensor.cpp:58] setTemperature(): Updating temperature sensor value...
[ 29332][D][ZigbeeTempSensor.cpp:60] setTemperature(): Setting temperature to 2390
[ 29332][V][ZigbeeTempSensor.cpp:99] setHumidity(): Updating humidity sensor value...
[ 29333][D][ZigbeeTempSensor.cpp:101] setHumidity(): Setting humidity to 5140
eCO2 Zigbee: 426 ppm

[ 29336][V][ZigbeeCarbonDioxideSensor.cpp:69] setCarbonDioxide(): Updating carbon dioxide sensor value...
[ 29337][D][ZigbeeCarbonDioxideSensor.cpp:71] setCarbonDioxide(): Setting carbon dioxide to 426.0
[ 29338][V][ZigbeeTempSensor.cpp:80] reportTemperature(): Temperature report sent
[ 29340][V][ZigbeeTempSensor.cpp:122] reportHumidity(): Humidity report sent
[ 29340][V][ZigbeeCarbonDioxideSensor.cpp:92] report(): Carbon dioxide report sent
[ 29364][V][ZigbeeHandlers.cpp:134] zb_cmd_default_resp_handler(): Received default response: from address(0x0), src_endpoint(1) to dst_endpoint(10), cluster(0x405) with status 0x0
[ 29381][V][ZigbeeHandlers.cpp:134] zb_cmd_default_resp_handler(): Received default response: from address(0x0), src_endpoint(1) to dst_endpoint(11), cluster(0x40d) with status 0x0
[ 31377][V][ZigbeeHandlers.cpp:134] zb_cmd_default_resp_handler(): Received default response: from address(0x0), src_endpoint(1) to dst_endpoint(11), cluster(0x40d) with status 0x0
TEMP 23.90°	HUM 51.40%
TVOC 12 ppb	eCO2 432 ppm

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@cscognamiglio cscognamiglio added the Status: Awaiting triage Issue is waiting for triage label Feb 11, 2025
@cscognamiglio
Copy link
Author

OK closing this because it seems to be a problem with zigbee2mqtt, using the development branch solved the issue, sorry for the noise.

issue is here: Koenkk/zigbee2mqtt#26181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage
Projects
None yet
Development

No branches or pull requests

1 participant