summaryrefslogtreecommitdiff
path: root/include/pio/libs/temphum/homekit/temphum.cpp
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-09-27 00:54:57 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-09-27 00:54:57 +0300
commitd3a295872c49defb55fc8e4e43e55550991e0927 (patch)
treeb9dca15454f9027d5a9dad0d4443a20de04dbc5d /include/pio/libs/temphum/homekit/temphum.cpp
parentb7cbc2571c1870b4582ead45277d0aa7f961bec8 (diff)
parentbdbb296697f55f4c3a07af43c9aaf7a9ea86f3d0 (diff)
Merge branch 'master' of ch1p.io:homekit
Diffstat (limited to 'include/pio/libs/temphum/homekit/temphum.cpp')
-rw-r--r--include/pio/libs/temphum/homekit/temphum.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/include/pio/libs/temphum/homekit/temphum.cpp b/include/pio/libs/temphum/homekit/temphum.cpp
new file mode 100644
index 0000000..e69b3a5
--- /dev/null
+++ b/include/pio/libs/temphum/homekit/temphum.cpp
@@ -0,0 +1,89 @@
+#ifndef CONFIG_TARGET_ESP01
+#include <Arduino.h>
+#endif
+#include <homekit/logging.h>
+#include "temphum.h"
+
+namespace homekit::temphum {
+
+void Sensor::setup() const {
+#ifndef CONFIG_TARGET_ESP01
+ pinMode(CONFIG_SDA_GPIO, OUTPUT);
+ pinMode(CONFIG_SCL_GPIO, OUTPUT);
+
+ Wire.begin(CONFIG_SDA_GPIO, CONFIG_SCL_GPIO);
+#else
+ Wire.begin();
+#endif
+}
+
+void Sensor::writeCommand(int reg) const {
+ Wire.beginTransmission(dev_addr);
+ Wire.write(reg);
+ Wire.endTransmission();
+ delay(500); // wait for the measurement to be ready
+}
+
+SensorData Si7021::read() {
+ uint8_t error = 0;
+ writeCommand(0xf3); // command to measure temperature
+ Wire.requestFrom(dev_addr, 2);
+ if (Wire.available() < 2) {
+ PRINTLN("Si7021: 0xf3: could not read 2 bytes");
+ error = 1;
+ }
+ uint16_t temp_raw = Wire.read() << 8 | Wire.read();
+ double temperature = ((175.72 * temp_raw) / 65536.0) - 46.85;
+
+ writeCommand(0xf5); // command to measure humidity
+ Wire.requestFrom(dev_addr, 2);
+ if (Wire.available() < 2) {
+ PRINTLN("Si7021: 0xf5: could not read 2 bytes");
+ error = 1;
+ }
+ uint16_t hum_raw = Wire.read() << 8 | Wire.read();
+ double humidity = ((125.0 * hum_raw) / 65536.0) - 6.0;
+
+ return {
+ .error = error,
+ .temp = temperature,
+ .rh = humidity
+ };
+}
+
+SensorData DHT12::read() {
+ SensorData sd;
+ byte raw[5];
+ sd.error = 1;
+
+ 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;
+
+ sd.error = 0;
+
+end:
+ return sd;
+}
+
+}