From 7cf9166dcbbd52d72e795a9e764dfe53462bc94b Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 22 May 2023 06:31:51 +0300 Subject: pio/temphum: add support for multiple sensors, si7021 and dht12 as of now --- platformio/temphum/src/main.cpp | 10 ++++-- platformio/temphum/src/temphum.cpp | 73 +++++++++++++++++++++++--------------- platformio/temphum/src/temphum.h | 32 ++++++++++++++--- 3 files changed, 80 insertions(+), 35 deletions(-) diff --git a/platformio/temphum/src/main.cpp b/platformio/temphum/src/main.cpp index 83f35b4..6e878a1 100644 --- a/platformio/temphum/src/main.cpp +++ b/platformio/temphum/src/main.cpp @@ -38,6 +38,7 @@ static volatile enum WiFiConnectionState wifi_state = WiFiConnectionState::WAITI static void* service = nullptr; static WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler; static Ticker wifiTimer; +temphum::BaseSensor* sensor = nullptr; #if MQTT_BLINK static StopWatch blinkStopWatch; @@ -104,7 +105,12 @@ void setup() { Serial.begin(115200); #endif - temphum::setup(); +#if CONFIG_MODULE == HOMEKIT_SI7021 + sensor = new temphum::Si7021(); +#elif CONFIG_MODULE == HOMEKIT_DHT12 + sensor = new temphum::DHT12(); +#endif + sensor->setup(); auto cfg = config::read(); if (config::isDirty(cfg)) { @@ -170,7 +176,7 @@ void loop() { } else if (mqtt->diagnosticsStopWatch.elapsed(10000)) { mqtt->sendDiagnostics(); - auto data = temphum::read(); + auto data = sensor->read(); PRINT("temp:"); PRINT(data.temp); PRINT(", rh: "); diff --git a/platformio/temphum/src/temphum.cpp b/platformio/temphum/src/temphum.cpp index 9fcc2cc..164f01e 100644 --- a/platformio/temphum/src/temphum.cpp +++ b/platformio/temphum/src/temphum.cpp @@ -1,14 +1,12 @@ #ifndef CONFIG_TARGET_ESP01 #include #endif - +#include #include "temphum.h" namespace homekit::temphum { -static const int addr = 0x40; - -void setup() { +void BaseSensor::setup() const { #ifndef CONFIG_TARGET_ESP01 pinMode(CONFIG_SDA_GPIO, OUTPUT); pinMode(CONFIG_SCL_GPIO, OUTPUT); @@ -17,43 +15,62 @@ void setup() { #else Wire.begin(); #endif - - Wire.beginTransmission(addr); - Wire.write(0xfe); - Wire.endTransmission(); - - delay(500); } -struct data read() { - // Request temperature measurement from the Si7021 sensor - Wire.beginTransmission(addr); - Wire.write(0xF3); // command to measure temperature +void BaseSensor::writeCommand(int reg) const { + Wire.beginTransmission(dev_addr); + Wire.write(reg); Wire.endTransmission(); - delay(500); // wait for the measurement to be ready +} - // Read the temperature measurement from the Si7021 sensor - Wire.requestFrom(addr, 2); +SensorData Si7021::read() { + writeCommand(0xf3); // command to measure temperature + Wire.requestFrom(dev_addr, 2); uint16_t temp_raw = Wire.read() << 8 | Wire.read(); double temperature = ((175.72 * temp_raw) / 65536.0) - 46.85; - // Request humidity measurement from the Si7021 sensor - Wire.beginTransmission(addr); - Wire.write(0xF5); // command to measure humidity - Wire.endTransmission(); - - delay(500); // wait for the measurement to be ready - - // Read the humidity measurement from the Si7021 sensor - Wire.requestFrom(addr, 2); + writeCommand(0xf5); // command to measure humidity + Wire.requestFrom(dev_addr, 2); uint16_t hum_raw = Wire.read() << 8 | Wire.read(); double humidity = ((125.0 * hum_raw) / 65536.0) - 6.0; return { - .temp = temperature, - .rh = humidity + .temp = temperature, + .rh = humidity }; } +SensorData DHT12::read() { + SensorData sd; + byte raw[5]; + + writeCommand(0); + Wire.requestFrom(dev_addr, 5); + + if (Wire.available() < 5) { + PRINTLN("DHT12: could not read 5 bytes"); + goto end; + } + + // Parse the received data + for (uint8_t i = 0; i < 5; i++) + raw[i] = Wire.read(); + + if (((raw[0] + raw[1] + raw[2] + raw[3]) & 0xff) != raw[4]) { + PRINTLN("DHT12: checksum error"); + goto end; + } + + // Calculate temperature and humidity values + sd.temp = raw[2] + (raw[3] & 0x7f) * 0.1; + if (raw[3] & 0x80) + sd.temp *= -1; + + sd.rh = raw[0] + raw[1] * 0.1; + +end: + return sd; +} + } \ No newline at end of file diff --git a/platformio/temphum/src/temphum.h b/platformio/temphum/src/temphum.h index 824b9bf..3d8f373 100644 --- a/platformio/temphum/src/temphum.h +++ b/platformio/temphum/src/temphum.h @@ -4,12 +4,34 @@ namespace homekit::temphum { -struct data { - double temp; // celsius - double rh; // relative humidity percentage +struct SensorData { + double temp = 0; // celsius + double rh = 0; // relative humidity percentage }; -void setup(); -struct data read(); + +class BaseSensor { +protected: + int dev_addr; +public: + explicit BaseSensor(int dev) : dev_addr(dev) {} + void setup() const; + void writeCommand(int reg) const; + virtual SensorData read() = 0; +}; + + +class Si7021 : public BaseSensor { +public: + SensorData read() override; + Si7021() : BaseSensor(0x40) {} +}; + + +class DHT12 : public BaseSensor { +public: + SensorData read() override; + DHT12() : BaseSensor(0x5c) {} +}; } \ No newline at end of file -- cgit v1.2.3