Skip to content

Commit

Permalink
fix(thing): decrease usage of JSON buffers
Browse files Browse the repository at this point in the history
- Disabled barometric pressure, dew point properties for Model M
- Removed uptime property
  • Loading branch information
malokhvii-eduard committed Dec 3, 2021
1 parent 0d518b6 commit cac3d53
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 169 deletions.
3 changes: 1 addition & 2 deletions include/WebThingConfig.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#ifndef AIRLY_INCLUDE_WEB_THINGS_CONFIG_H_
#define AIRLY_INCLUDE_WEB_THINGS_CONFIG_H_

#if !defined(LARGE_JSON_BUFFERS)
#define LARGE_JSON_BUFFERS 1
#endif

#if !defined(ARDUINOJSON_USE_LONG_LONG)
#define ARDUINOJSON_USE_LONG_LONG 1
#endif
Expand Down
7 changes: 7 additions & 0 deletions include/airly/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@

#if defined(THING_MODEL_B)
#define THING_HAS_BME280
#define THING_HAS_TEMPERATURE_PROPERTY
#define THING_HAS_HUMIDITY_PROPERTY
#define THING_HAS_BAROMETRIC_PRESSURE_PROPERTY
#define THING_HAS_DEW_POINT_PROPERTY
#elif defined(THING_MODEL_M)
#define THING_HAS_BME280
#define THING_HAS_MHZ19
#define THING_HAS_TEMPERATURE_PROPERTY
#define THING_HAS_HUMIDITY_PROPERTY
#define THING_HAS_CARBON_DIOXIDE_PROPERTY
#endif

#if !defined(SCHEDULER_INTERVAL_UPDATE_PROPERTIES)
Expand Down
23 changes: 15 additions & 8 deletions include/airly/I18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@
namespace i18n {

extern const char* DeviceTitle;
extern const char* UptimePropertyDescription;
extern const char* UptimePropertyTitle;

#if defined(THING_HAS_BME280)
#if defined(THING_HAS_TEMPERATURE_PROPERTY)
extern const char* TemperaturePropertyDescription;
extern const char* TemperaturePropertyTitle;
#endif

#if defined(THING_HAS_HUMIDITY_PROPERTY)
extern const char* HumidityPropertyDescription;
extern const char* HumidityPropertyTitle;
#endif

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
extern const char* BarometricPressurePropertyDescription;
extern const char* BarometricPressurePropertyTitle;
#endif

#if defined(THING_HAS_DEW_POINT_PROPERTY)
extern const char* DewPointPropertyDescription;
extern const char* DewPointPropertyTitle;
extern const char* HumidityPropertyDescription;
extern const char* HumidityPropertyTitle;
extern const char* TemperaturePropertyDescription;
extern const char* TemperaturePropertyTitle;
#endif

#if defined(THING_HAS_MHZ19)
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
extern const char* CarbonDioxidePropertyDescription;
extern const char* CarbonDioxidePropertyTitle;
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/airly/properties/BarometricPressure.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
/* WebThings framework */
#include <Thing.h>

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
void describeBarometricPressureProperty(ThingDevice* device);
void setBarometricPressureProperty(const float barometricPressure);
#endif

#endif // AIRLY_INCLUDE_AIRLY_PROPERTIES_BAROMETRIC_PRESSURE_H_
2 changes: 2 additions & 0 deletions include/airly/properties/CarbonDioxide.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
/* WebThings framework */
#include <Thing.h>

#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
void describeCarbonDioxideProperty(ThingDevice* device);
void setCarbonDioxideProperty(const unsigned short carbonDioxide);
#endif

#endif // AIRLY_INCLUDE_AIRLY_PROPERTIES_CARBON_DIOXIDE_H_
2 changes: 2 additions & 0 deletions include/airly/properties/DewPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
/* WebThings framework */
#include <Thing.h>

#if defined(THING_HAS_DEW_POINT_PROPERTY)
void describeDewPointProperty(ThingDevice* device);
void setDewPointProperty(const float DewPoint);
float calculateDewPoint(const float temperature, const float humidity);
#endif

#endif // AIRLY_INCLUDE_AIRLY_PROPERTIES_DEW_POINT_H_
2 changes: 2 additions & 0 deletions include/airly/properties/Humidity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
/* WebThings framework */
#include <Thing.h>

#if defined(THING_HAS_HUMIDITY_PROPERTY)
void describeHumidityProperty(ThingDevice* device);
void setHumidityProperty(const float humidity);
#endif

#endif // AIRLY_INCLUDE_AIRLY_PROPERTIES_HUMIDITY_H_
2 changes: 2 additions & 0 deletions include/airly/properties/Temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
/* WebThings framework */
#include <Thing.h>

#if defined(THING_HAS_TEMPERATURE_PROPERTY)
void describeTemperatureProperty(ThingDevice* device);
void setTemperatureProperty(const float temperature);
#endif

#endif // AIRLY_INCLUDE_AIRLY_PROPERTIES_TEMPERATURE_H_
13 changes: 0 additions & 13 deletions include/airly/properties/Uptime.h

This file was deleted.

51 changes: 33 additions & 18 deletions src/airly/Device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,63 @@
/* Preprocessor-based localization */
#include <airly/I18n.h>

/* Common properties */
#include <airly/properties/Uptime.h>

#if defined(THING_HAS_BME280)
/* Properties for Bosch Sensortec BME280 */
/* Properties */
#include <airly/properties/BarometricPressure.h>
#include <airly/properties/CarbonDioxide.h>
#include <airly/properties/DewPoint.h>
#include <airly/properties/Humidity.h>
#include <airly/properties/Temperature.h>

#define BME280_CAPABILITIES \
"TemperatureSensor", "HumiditySensor", "BarometricPressureSensor",
#if defined(THING_HAS_TEMPERATURE_PROPERTY)
#define TEMPERATURE_SENSOR_CAPABILITY "TemperatureSensor",
#else
#define BME280_CAPABILITIES
#define TEMPERATURE_SENSOR_CAPABILITY
#endif

#if defined(THING_HAS_MHZ19)
/* Properties for Winsen MH-Z19 */
#include <airly/properties/CarbonDioxide.h>
#if defined(THING_HAS_HUMIDITY_PROPERTY)
#define HUMIDITY_SENSOR_CAPABILITY "HumiditySensor",
#else
#define HUMIDITY_SENSOR_CAPABILITY
#endif

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
#define BAROMETRIC_PRESSURE_SENSOR_CAPABILITY "BarometricPressureSensor",
#else
#define BAROMETRIC_PRESSURE_SENSOR_CAPABILITY
#endif

#define MHZ19_CAPABILITIES "AirQualitySensor",
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
#define AIR_QUALITY_SENSOR_CAPABILITY "AirQualitySensor",
#else
#define MHZ19_CAPABILITIES
#define AIR_QUALITY_SENSOR_CAPABILITY
#endif

static const char* deviceCapabilities[] = {
BME280_CAPABILITIES MHZ19_CAPABILITIES nullptr};
TEMPERATURE_SENSOR_CAPABILITY HUMIDITY_SENSOR_CAPABILITY
BAROMETRIC_PRESSURE_SENSOR_CAPABILITY
AIR_QUALITY_SENSOR_CAPABILITY nullptr};
static ThingDevice device("0", i18n::DeviceTitle, deviceCapabilities);

void describeDevice(WebThingAdapter* adapter) {
#if defined(THING_HAS_BME280)
#if defined(THING_HAS_TEMPERATURE_PROPERTY)
describeTemperatureProperty(&device);
#endif

#if defined(THING_HAS_HUMIDITY_PROPERTY)
describeHumidityProperty(&device);
#endif

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
describeBarometricPressureProperty(&device);
#endif

#if defined(THING_HAS_DEW_POINT_PROPERTY)
describeDewPointProperty(&device);
#endif

#if defined(THING_HAS_MHZ19)
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
describeCarbonDioxideProperty(&device);
#endif

describeUptimeProperty(&device);

adapter->addDevice(&device);
}
23 changes: 15 additions & 8 deletions src/airly/I18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
namespace i18n {

const char* DeviceTitle = "";
const char* UptimePropertyDescription = "";
const char* UptimePropertyTitle = "";

#if defined(THING_HAS_BME280)
#if defined(THING_HAS_TEMPERATURE_PROPERTY)
const char* TemperaturePropertyDescription = "";
const char* TemperaturePropertyTitle = "";
#endif

#if defined(THING_HAS_HUMIDITY_PROPERTY)
const char* HumidityPropertyDescription = "";
const char* HumidityPropertyTitle = "";
#endif

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
const char* BarometricPressurePropertyDescription = "";
const char* BarometricPressurePropertyTitle = "";
#endif

#if defined(THING_HAS_DEW_POINT_PROPERTY)
const char* DewPointPropertyDescription = "";
const char* DewPointPropertyTitle = "";
const char* HumidityPropertyDescription = "";
const char* HumidityPropertyTitle = "";
const char* TemperaturePropertyDescription = "";
const char* TemperaturePropertyTitle = "";
#endif

#if defined(THING_HAS_MHZ19)
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
const char* CarbonDioxidePropertyDescription = "";
const char* CarbonDioxidePropertyTitle = "";
#endif
Expand Down
File renamed without changes.
61 changes: 49 additions & 12 deletions src/airly/Thing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,62 @@ static void offBuiltinLed() { digitalWrite(LED_BUILTIN, HIGH); }

static Bme280TwoWire bme280;

static void beginTwoWire() { Wire.begin(I2C_SDA, I2C_SCL); }

static void beginBme280() {
bme280.begin(BME280_ADDRESS);
bme280.setSettings(Bme280Settings::indoor());

auto settings = Bme280Settings::indoor();

#if !defined(THING_HAS_TEMPERATURE_PROPERTY) && \
!defined(THING_HAS_DEW_POINT_PROPERTY)
settings.temperatureOversampling = Bme280Oversampling::Off;
#endif

#if !defined(THING_HAS_HUMIDITY_PROPERTY) && \
!defined(THING_HAS_DEW_POINT_PROPERTY)
settings.humidityOversampling = Bme280Oversampling::Off;
#endif

#if !defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
settings.pressureOversampling = Bme280Oversampling::Off;
#endif

bme280.setSettings(settings);

delay(1000); // 1 second
}

static bool pollBme280(void *) {
#if defined(THING_HAS_TEMPERATURE_PROPERTY) || \
defined(THING_HAS_DEW_POINT_PROPERTY)
auto temperature = bme280.getTemperature();
#endif

#if defined(THING_HAS_HUMIDITY_PROPERTY) || \
defined(THING_HAS_DEW_POINT_PROPERTY)
auto humidity = bme280.getHumidity();
auto barometricPressure = bme280.getPressure();
auto dewPoint = calculateDewPoint(temperature, humidity);
#endif

#if defined(THING_HAS_TEMPERATURE_PROPERTY)
setTemperatureProperty(temperature);
#endif

#if defined(THING_HAS_HUMIDITY_PROPERTY)
setHumidityProperty(humidity);
#endif

#if defined(THING_HAS_BAROMETRIC_PRESSURE_PROPERTY)
auto barometricPressure = bme280.getPressure();
setBarometricPressureProperty(barometricPressure);
#endif

#if defined(THING_HAS_DEW_POINT_PROPERTY)
auto dewPoint = calculateDewPoint(temperature, humidity);
if (!isnan(dewPoint)) {
setDewPointProperty(dewPoint);
}
#endif

return true;
}
Expand All @@ -89,7 +125,9 @@ static bool pollBme280(void *) {
#include <Mhz19.h>

/* Properties */
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
#include <airly/properties/CarbonDioxide.h>
#endif

#define MHZ19_UART_BAUDRATE 9600 // kbps

Expand All @@ -113,12 +151,16 @@ static void beginMhz19() {
}

static bool pollMhz19(void *) {
#if defined(THING_HAS_CARBON_DIOXIDE_PROPERTY)
if (isMhz19PreheatingDone) {
auto carbonDioxide = mhz19.getCarbonDioxide();
if (carbonDioxide < 0) carbonDioxide = 0;
if (carbonDioxide < 0) {
carbonDioxide = 0;
}

setCarbonDioxideProperty(carbonDioxide);
}
#endif

return true;
}
Expand All @@ -129,14 +171,9 @@ static bool pollMhz19(void *) {
// -------------------------------------------------------------------------- //
// Adapter, common properties
// -------------------------------------------------------------------------- //
/* Properties */
#include <airly/properties/Uptime.h>

static WebThingAdapter *adapter = nullptr;

static bool updateProperties(void *) {
updateUptimeProperty();

#if defined(THING_HAS_MHZ19)
if (!isMhz19PreheatingDone) {
if (mhz19.isReady()) {
Expand All @@ -148,7 +185,9 @@ static bool updateProperties(void *) {

if (WiFi.status() == WL_CONNECTED) {
adapter->update();
} else {
}

if (WiFi.status() == WL_DISCONNECTED) {
WiFi.reconnect();
}

Expand Down Expand Up @@ -196,8 +235,6 @@ static String getThingIdentity() { return getMacAddressWithoutColon(); }

static void beginBuiltinLed() { pinMode(LED_BUILTIN, OUTPUT); }

static void beginTwoWire() { Wire.begin(I2C_SDA, I2C_SCL); }

void beginThing() {
beginBuiltinLed();
onBuiltinLed();
Expand Down
Loading

0 comments on commit cac3d53

Please sign in to comment.