|
| 1 | +#include "telaire_T9602.h" |
| 2 | + |
| 3 | +// Constructor |
| 4 | +T9602::T9602() { |
| 5 | + sensorAddress = 0x28; // Default T9602 sensor I2C address |
| 6 | + relativeHumidity = -9999; |
| 7 | + temperature = -9999; |
| 8 | +} |
| 9 | + |
| 10 | +void T9602::begin(uint8_t address) { |
| 11 | + sensorAddress = address; |
| 12 | + Wire.begin(); |
| 13 | +} |
| 14 | + |
| 15 | +void T9602::updateMeasurements() { |
| 16 | + uint8_t data[4] = {0}; |
| 17 | + Wire.beginTransmission(sensorAddress); |
| 18 | + Wire.write(0x00); // Command to read data |
| 19 | + Wire.endTransmission(); |
| 20 | + Wire.requestFrom(sensorAddress, 4); // Request 4 bytes of data |
| 21 | + |
| 22 | + // Store the 4 bytes of data |
| 23 | + for (int i = 0; i < 4; ++i) { |
| 24 | + data[i] = Wire.read(); |
| 25 | + } |
| 26 | + |
| 27 | + // Calculate relative humidity |
| 28 | + relativeHumidity = static_cast<float>(((data[0] & 0x3F) << 8) + data[1]) / 16384.0 * 100.0; |
| 29 | + // Calculate temperature |
| 30 | + temperature = static_cast<float>((static_cast<unsigned>(data[2]) * 64 + (static_cast<unsigned>(data[3]) >> 2)) / 16384.0 * 165.0 - 40.0); |
| 31 | +} |
| 32 | + |
| 33 | +float T9602::getHumidity() { |
| 34 | + return relativeHumidity; |
| 35 | +} |
| 36 | + |
| 37 | +float T9602::getTemperature() { |
| 38 | + return temperature; |
| 39 | +} |
| 40 | + |
| 41 | +String T9602::getString(bool takeNewReadings) { |
| 42 | + if (takeNewReadings) { |
| 43 | + updateMeasurements(); |
| 44 | + } |
| 45 | + |
| 46 | + // Adjusted format to "Temp: value; Humidity: value" |
| 47 | + return "Temp: " + String(temperature) + "; Humidity: " + String(relativeHumidity); |
| 48 | +} |
| 49 | + |
| 50 | +String T9602::getHeader() { |
| 51 | + return "Relative Humidity [%],Temp [C],"; |
| 52 | +} |
0 commit comments