Skip to content

Commit 3972764

Browse files
committed
initial commit
0 parents  commit 3972764

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

.vscode/extensions.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

lib/telaire_T9602/telaire_T9602.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

lib/telaire_T9602/telaire_T9602.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef TELAIRE_T9602_H
2+
#define TELAIRE_T9602_H
3+
4+
#include <Arduino.h>
5+
#include <Wire.h>
6+
7+
class T9602 {
8+
public:
9+
T9602();
10+
void begin(uint8_t address = 0x28); // Default sensor I2C address
11+
void updateMeasurements(); // Fetches new measurement data from the sensor
12+
float getHumidity(); // Returns the last measured humidity value
13+
float getTemperature(); // Returns the last measured temperature value
14+
String getString(bool takeNewReadings = false); // Returns formatted string with temperature and humidity
15+
String getHeader(); // Returns CSV header string
16+
17+
private:
18+
uint8_t sensorAddress; // I2C address of the T9602 sensor
19+
float relativeHumidity; // Last measured relative humidity
20+
float temperature; // Last measured temperature
21+
};
22+
23+
#endif // TELAIRE_T9602_H

platformio.ini

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:d1_mini_lite]
12+
platform = espressif8266
13+
board = d1_mini_lite
14+
framework = arduino

src/main.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
*
3+
* Read Telaire T9602 temperature and humidity sensor data and print to serial
4+
*
5+
* Author: Bjoern Heller <tec aett sixtopia.net>
6+
*/
7+
#include "telaire_T9602.h"
8+
#include <Arduino.h>
9+
#include <Wire.h>
10+
11+
T9602 telaireSensor;
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
telaireSensor.begin();
16+
}
17+
18+
void loop() {
19+
20+
telaireSensor.updateMeasurements();
21+
float temperature = telaireSensor.getTemperature();
22+
float humidity = telaireSensor.getHumidity();
23+
24+
char temperatureString[8];
25+
dtostrf(temperature, 6, 2, temperatureString);
26+
char humidityString[8];
27+
dtostrf(humidity, 6, 2, humidityString);
28+
29+
Serial.print("Temp: ");
30+
Serial.print(temperatureString);
31+
Serial.print("; Humidity: ");
32+
Serial.println(humidityString);
33+
34+
// Toggle LED to indicate data
35+
digitalWrite(LED_BUILTIN, LOW);
36+
delay(100);
37+
digitalWrite(LED_BUILTIN, HIGH);
38+
39+
delay(1000); // Wait 1s before the next loop iteration
40+
}

0 commit comments

Comments
 (0)