summaryrefslogtreecommitdiff
path: root/platformio/temphum/src/temphum.cpp
diff options
context:
space:
mode:
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