Skip to content

Commit

Permalink
Add BME280 calibration offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk committed Sep 27, 2020
1 parent d1a75c2 commit 70dda32
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
14 changes: 7 additions & 7 deletions Source/zcl_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ void zclApp_Init(byte task_id) {
LREP("Build %s \r\n", zclApp_DateCodeNT);

HalI2CInit();

osal_start_reload_timer(zclApp_TaskID, APP_REPORT_EVT, APP_REPORT_DELAY);

}
Expand Down Expand Up @@ -232,7 +231,8 @@ static void zclApp_ReadSensors(void) {
if (zclApp_Sensors.Temperature == 1) {
LREPMaster("ReadDS18B20 error\r\n");
} else {
LREP("ReadDS18B20 t=%d\r\n", zclApp_Sensors.Temperature);
zclApp_Sensors.Temperature += zclApp_Config.TemperatureOffset;
LREP("ReadDS18B20 t=%d offset=\r\n", zclApp_Sensors.Temperature, zclApp_Config.TemperatureOffset);
}
break;
case 3:
Expand Down Expand Up @@ -317,11 +317,11 @@ static void zclApp_ReadBME280(struct bme280_dev *dev) {
int8_t rslt = bme280_get_sensor_data(BME280_ALL, &bme_results, dev);
if (rslt == BME280_OK) {
LREP("ReadBME280 t=%ld, p=%ld, h=%ld\r\n", bme_results.temperature, bme_results.pressure, bme_results.humidity);
zclApp_Sensors.BME280_Temperature_Sensor_MeasuredValue = (int16)bme_results.temperature;
zclApp_Sensors.BME280_PressureSensor_MeasuredValueHPA = bme_results.pressure;
zclApp_Sensors.BME280_PressureSensor_MeasuredValue = bme_results.pressure / 100;
zclApp_Sensors.BME280_HumiditySensor_MeasuredValue = (uint16)(bme_results.humidity * 100 / 1024);
zclApp_Sensors.Temperature = (int16)bme_results.temperature;
zclApp_Sensors.BME280_Temperature_Sensor_MeasuredValue = (int16)bme_results.temperature + zclApp_Config.TemperatureOffset;
zclApp_Sensors.BME280_PressureSensor_MeasuredValueHPA = bme_results.pressure + zclApp_Config.PressureOffset;
zclApp_Sensors.BME280_PressureSensor_MeasuredValue = bme_results.pressure / 100 + zclApp_Config.PressureOffset / 100;
zclApp_Sensors.BME280_HumiditySensor_MeasuredValue = (uint16)(bme_results.humidity * 100 / 1024) + zclApp_Config.HumidityOffset;
zclApp_Sensors.Temperature = (int16)bme_results.temperature + zclApp_Config.TemperatureOffset;
} else {
LREP("ReadBME280 bme280_get_sensor_data error %d\r\n", rslt);
}
Expand Down
21 changes: 15 additions & 6 deletions Source/zcl_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ extern "C" {


// Carbon Dioxide (CO2)
#define ATTRID_CO2_MEASURED_VALUE 0x0000
#define ATTRID_CO2_TOLERANCE 0x0003
#define ATTRID_ENABLE_ABC 0x0202
#define ATTRID_LED_FEEDBACK 0x0203
#define ATTRID_THRESHOLD1_PPM 0x0204
#define ATTRID_THRESHOLD2_PPM 0x0205
#define ATTRID_CO2_MEASURED_VALUE 0x0000
#define ATTRID_CO2_TOLERANCE 0x0003
#define ATTRID_ENABLE_ABC 0x0202
#define ATTRID_LED_FEEDBACK 0x0203
#define ATTRID_THRESHOLD1_PPM 0x0204
#define ATTRID_THRESHOLD2_PPM 0x0205

#define ATTRID_TemperatureOffset 0x0210
#define ATTRID_PressureOffset 0x0210
#define ATTRID_HumidityOffset 0x0210


#define ATTRID_MS_PRESSURE_MEASUREMENT_MEASURED_VALUE_HPA 0x0200

Expand All @@ -67,6 +72,10 @@ typedef struct {
uint8 EnableABC;
uint16 Threshold1_PPM;
uint16 Threshold2_PPM;
int16 TemperatureOffset;
uint32 PressureOffset;
int16 HumidityOffset;

} application_config_t;

typedef struct {
Expand Down
14 changes: 13 additions & 1 deletion Source/zcl_app_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const uint8 zclApp_PowerSource = POWER_SOURCE_MAINS_1_PHASE;
// FYI: https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms
#define DEFAULT_Threshold1_PPM 1000
#define DEFAULT_Threshold2_PPM 2000
#define DEFAULT_TemperatureOffset 0
#define DEFAULT_PressureOffset 0
#define DEFAULT_HumidityOffset 0



Expand All @@ -66,7 +69,10 @@ application_config_t zclApp_Config = {
.LedFeedback = DEFAULT_LedFeedback,
.EnableABC = DEFAULT_EnableABC,
.Threshold1_PPM = DEFAULT_Threshold1_PPM,
.Threshold2_PPM = DEFAULT_Threshold2_PPM
.Threshold2_PPM = DEFAULT_Threshold2_PPM,
.TemperatureOffset = DEFAULT_TemperatureOffset,
.PressureOffset = DEFAULT_PressureOffset,
.HumidityOffset = DEFAULT_HumidityOffset
};

sensors_state_t zclApp_Sensors = {
Expand Down Expand Up @@ -96,9 +102,15 @@ CONST zclAttrRec_t zclApp_AttrsFirstEP[] = {
{BASIC, {ATTRID_BASIC_SW_BUILD_ID, ZCL_DATATYPE_CHAR_STR, R, (void *)zclApp_DateCode}},

{TEMP, {ATTRID_MS_TEMPERATURE_MEASURED_VALUE, ZCL_INT16, RR, (void *)&zclApp_Sensors.Temperature}},
{TEMP, {ATTRID_TemperatureOffset, ZCL_INT16, RW, (void *)&zclApp_Config.TemperatureOffset}},


{PRESSURE, {ATTRID_MS_PRESSURE_MEASUREMENT_MEASURED_VALUE, ZCL_INT16, RR, (void *)&zclApp_Sensors.BME280_PressureSensor_MeasuredValue}},
{PRESSURE, {ATTRID_MS_PRESSURE_MEASUREMENT_MEASURED_VALUE_HPA, ZCL_UINT32, RR, (void *)&zclApp_Sensors.BME280_PressureSensor_MeasuredValueHPA}},
{PRESSURE, {ATTRID_PressureOffset, ZCL_UINT32, RW, (void *)&zclApp_Config.PressureOffset}},

{HUMIDITY, {ATTRID_MS_RELATIVE_HUMIDITY_MEASURED_VALUE, ZCL_UINT16, RR, (void *)&zclApp_Sensors.BME280_HumiditySensor_MeasuredValue}},
{HUMIDITY, {ATTRID_HumidityOffset, ZCL_INT16, RW, (void *)&zclApp_Config.HumidityOffset}},

{CO2, {ATTRID_CO2_MEASURED_VALUE, ZCL_SINGLE, RR, (void *)&zclApp_Sensors.CO2_PPM}},
{CO2, {ATTRID_CO2_TOLERANCE, ZCL_SINGLE, RW, (void *)&zclApp_Config.SensorTolerance}},
Expand Down

0 comments on commit 70dda32

Please sign in to comment.