Skip to content

Commit bf39dc3

Browse files
committed
✨ Replace TMP102 with HTU21DF
1 parent 18e1e06 commit bf39dc3

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

esp8266.ino

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
#include <Adafruit_Sensor.h>
55
#include <TSL2561.h>
66
#include <Adafruit_BME280.h>
7-
#include <SparkFunTMP102.h>
7+
#include <Adafruit_HTU21DF.h>
88

99
// Configuration
1010
#define WIFI_SSID ""
1111
#define WIFI_PASS ""
1212
#define WEB_SERVER_PORT 80
1313
#define I2C_ADDR_TSL2561 0x39
1414
#define I2C_ADDR_BME280 0x76
15-
#define I2C_ADDR_TMP102 0x48
1615

1716
// Web server
1817
ESP8266WebServer webServer(WEB_SERVER_PORT);
1918

2019
// Sensors
2120
TSL2561 tsl2561(I2C_ADDR_TSL2561);
2221
Adafruit_BME280 bme280;
23-
TMP102 tmp102(I2C_ADDR_TMP102);
22+
Adafruit_HTU21DF htu21df;
2423

2524
void setup()
2625
{
@@ -55,7 +54,7 @@ void handleRoot()
5554
float temperature = readTemperature();
5655

5756
// Read humidity (%)
58-
float humidity = bme280.readHumidity();
57+
float humidity = readHumidity();
5958

6059
// Read pressure (hPa)
6160
float pressure = bme280.readPressure() / 100;
@@ -83,11 +82,20 @@ void handleRoot()
8382
webServer.send(200, "application/json", response);
8483
}
8584

86-
float readTemperature() {
85+
float readHumidity()
86+
{
87+
float bme280Humidity = bme280.readHumidity();
88+
float htu21dfHumidity = htu21df.readHumidity();
89+
90+
return (htu21dfHumidity + bme280Humidity) / 2;
91+
}
92+
93+
float readTemperature()
94+
{
8795
float bme280Temperature = bme280.readTemperature();
88-
float tmp102Temperature = tmp102.readTempC();
96+
float htu21dfTemperature = htu21df.readTemperature();
8997

90-
return (tmp102Temperature + tmp102Temperature + bme280Temperature) / 3;
98+
return (htu21dfTemperature + bme280Temperature) / 2;
9199
}
92100

93101
void setupWiFi()
@@ -123,7 +131,7 @@ void setupSensors()
123131
{
124132
setupSensorTSL2561();
125133
setupSensorBME280();
126-
setupSensorTMP102();
134+
setupSensorHTU21DF();
127135
}
128136

129137
void setupSensorTSL2561()
@@ -156,12 +164,17 @@ void setupSensorBME280()
156164
Serial.println("[BME280] Connected!");
157165
}
158166

159-
void setupSensorTMP102()
167+
void setupSensorHTU21DF()
160168
{
161-
Serial.println("[TMP102] Setup");
162-
Serial.println("[TMP102] Connecting..");
169+
Serial.println("[HTU21DF] Setup");
170+
Serial.print("[HTU21DF] Connecting..");
163171

164-
tmp102.begin();
172+
while (!htu21df.begin())
173+
{
174+
delay(200);
175+
Serial.print(".");
176+
}
177+
Serial.println();
165178

166-
Serial.println("[TMP102] Assuming connected!");
179+
Serial.println("[HTU21DF] Connected!");
167180
}

0 commit comments

Comments
 (0)