#include "temphum.h" #include "logging.h" #include 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 }; } }