summaryrefslogtreecommitdiff
path: root/platformio/temphum/src/temphum.cpp
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-05-11 04:18:08 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-05-11 04:18:12 +0300
commit0aba139aeff8ff80757c5d36502413299a0b449e (patch)
tree2b8e760ff14d4691783eb7c7d341f093199aab82 /platformio/temphum/src/temphum.cpp
parent586d84b0c0a8b4dc1b5057733892b754397234ec (diff)
mqtt, esp: add new esp8266-based device
Diffstat (limited to 'platformio/temphum/src/temphum.cpp')
-rw-r--r--platformio/temphum/src/temphum.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/platformio/temphum/src/temphum.cpp b/platformio/temphum/src/temphum.cpp
new file mode 100644
index 0000000..0b6fbbe
--- /dev/null
+++ b/platformio/temphum/src/temphum.cpp
@@ -0,0 +1,53 @@
+#include "temphum.h"
+#include "logging.h"
+#include <Arduino.h>
+
+namespace homekit::temphum {
+
+static const int addr = 0x40;
+
+void setup() {
+ pinMode(D2, OUTPUT);
+ pinMode(D3, OUTPUT);
+
+ Wire.begin(D2, D3);
+
+ 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
+ Wire.endTransmission();
+
+ delay(500); // wait for the measurement to be ready
+
+ // Read the temperature measurement from the Si7021 sensor
+ Wire.requestFrom(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);
+ uint16_t hum_raw = Wire.read() << 8 | Wire.read();
+ double humidity = ((125.0 * hum_raw) / 65536.0) - 6.0;
+
+ return {
+ .temp = temperature,
+ .rh = humidity
+ };
+}
+
+} \ No newline at end of file