From 26d6b6dab18cc6537f98d25f3a01a2a1f30d7fcb Mon Sep 17 00:00:00 2001 From: lexa Date: Mon, 12 Aug 2024 17:33:16 +0300 Subject: [PATCH] SHT31 --- ESP_Midroom2/platformio.ini | 5 +- ESP_Midroom2/src/main.cpp | 168 +++++++++--------------------------- 2 files changed, 46 insertions(+), 127 deletions(-) diff --git a/ESP_Midroom2/platformio.ini b/ESP_Midroom2/platformio.ini index 83fefe3..f4fcc58 100644 --- a/ESP_Midroom2/platformio.ini +++ b/ESP_Midroom2/platformio.ini @@ -12,8 +12,8 @@ platform = espressif8266 board = d1_mini framework = arduino -;upload_protocol = espota -;upload_port = 192.168.1.136 +upload_protocol = espota +upload_port = 192.168.1.136 lib_deps = knolleary/PubSubClient @ ^2.8 ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0 @@ -24,3 +24,4 @@ lib_deps = https://github.com/pilotak/MeteoFunctions.git ;https://github.com/RobTillaart/SHT31.git ;ayushsharma82/ElegantOTA@^3.1.2 + https://github.com/adafruit/Adafruit_SHT31.git \ No newline at end of file diff --git a/ESP_Midroom2/src/main.cpp b/ESP_Midroom2/src/main.cpp index d662da6..daa5959 100644 --- a/ESP_Midroom2/src/main.cpp +++ b/ESP_Midroom2/src/main.cpp @@ -1,6 +1,4 @@ #include -//#include -//#include "DHTesp.h" #include #include #include @@ -8,17 +6,15 @@ #include #include #include -#include #include #include -#include -//#include "SHT31.h" +//#include #include +#include "SPI.h" +#include "Adafruit_SHT31.h" #define WIFI_SSID "wf-home" #define WIFI_PASSWORD "0ndthnrf" -#define WIFI_SSID2 "BR" -#define WIFI_PASSWORD2 "499727479o" #define MQTT_SERV "192.168.1.111" #define TOPIC "home/midroom/bed/" #define HOSTNAME "ESP_Midroom2" @@ -29,10 +25,9 @@ #define LAMP_OUT (D5) #define SHT31_ADDRESS 0x44 -//SHT31 sht; -HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution -MeteoFunctions calc; +MeteoFunctions calc; +Adafruit_SHT31 sht31 = Adafruit_SHT31(); float temp_out, hum_out, windspeed, heatindex, dewpoint, apptemp; @@ -56,7 +51,6 @@ bool led, aht_pesent; uint8_t led_intense; uint8_t ledLev; -ESP8266WiFiMulti wifiMulti; AsyncWebServer server(80); void setup() { @@ -66,8 +60,6 @@ void setup() { WiFi.mode(WIFI_STA); WiFi.persistent(false); WiFi.hostname(HOSTNAME); - //wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); - //wifiMulti.addAP(WIFI_SSID2, WIFI_PASSWORD2); client.setServer(MQTT_SERV, 1883); client.setCallback(callback); @@ -110,40 +102,21 @@ void setup() { //delay(5000); connectToWifi(); - // while (ht2x.begin() != true) //reset sensor, set heater off, set resolution, check power (sensor doesn't operate correctly if VDD < +2.25v) - // { - // //Serial.println(F("HTU2xD/SHT2x not connected, fail or VDD < +2.25v")); //(F()) save string to flash & keeps dynamic memory free - // digitalWrite(LED_WF, LOW); + if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr + Serial.println("Couldn't find SHT31"); + while (1) delay(1); + } - // delay(500); - // } - digitalWrite(LED_WF, HIGH); - ht2x.begin(); + Serial.print("Heater Enabled State: "); + if (sht31.isHeaterEnabled()) + Serial.println("ENABLED"); + else + Serial.println("DISABLED"); + + temp_out = NAN; hum_out = NAN; - // if(!sht.begin()){ - // Serial.println("SHT not ready"); - // } - - // uint16_t stat = sht.readStatus(); - // Serial.print(stat, HEX); - // //delay(500); - // sht.read(false); - // temp_out = sht.getTemperature(); - // hum_out = sht.getRawHumidity(); - // sht.requestData(); - - //getTiH(); -//////////////////////////////// - // dht.setup(D1, DHTesp::DHT22); - // delay(dht.getMinimumSamplingPeriod()); - // sTaH = dht.getTempAndHumidity(); - // if(dht.getStatus() == dht.ERROR_NONE){ - // temp_out = sTaH.temperature - 2.0; - // hum_out = sTaH.humidity; - // } -//////////////////////////////// EEPROM.begin(4); EEPROM.get(0, led_intense); Serial.print("Led intense: "); @@ -211,7 +184,6 @@ void loop() { void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); - //wifiMulti.run(); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); } @@ -296,91 +268,37 @@ void reconnect() { boolean getTiH(void) { - float htValue, fTemp; - // uint16_t stat = sht.readStatus(); - // WebSerial.println(stat, HEX); - // int error = sht.getError(); - // WebSerial.println(error, HEX); - // if (!sht.isConnected()){ - // WebSerial.println("SHT Not Connected"); - // return false; - // } - // if(sht.dataReady()){ - // bool success = sht.readData(); - // sht.requestData(); - // if (success){ - // htValue = sht.getTemperature(); - // fTemp = htValue; - // if (!isnan(temp_out)) - // temp_out += (htValue - temp_out) / 30.0; - // else - // temp_out = htValue; - // htValue = sht.getHumidity(); - // if (!isnan(hum_out)) - // hum_out += (htValue - hum_out) / 30.0; - // else - // hum_out = htValue; - // heatindex = calc.heatIndex_c(temp_out, hum_out); - // dewpoint = calc.dewPoint_c(temp_out, hum_out); - // apptemp = calc.apparentTemp_c(temp_out, hum_out, windspeed); - // WebSerial.print("Temp: "); - // WebSerial.print(fTemp); - // WebSerial.print(", Hum: "); - // WebSerial.println(htValue); - // return true; - // } - // else{ - // WebSerial.print("No data"); - // } - // } - // return false; - htValue = ht2x.readTemperature(); //accuracy +-0.3C in range 0C..60C at 14-bit - Serial.print("Temperature...............: "); - if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) //HTU2XD_SHT2X_SI70XX_ERROR = 255, library returns 255 if error occurs - { - Serial.print(htValue); - Serial.println(F(" +-0.3C")); - fTemp = htValue; - } - else - { - Serial.println(""); - //ht2x.begin(); + float t = sht31.readTemperature(); + float h = sht31.readHumidity(); - //ht2x.softReset(); //last chance to make it alive, all registers (except heater bit) will set to default - //ht2x.setHeater(false); //true=heater on, false=heater off - //ht2x.setResolution(HUMD_12BIT_TEMP_14BIT); //humidity 12-bit, temperature 14-bit - WebSerial.println("HTU Bad"); + if (! isnan(t)) { // check if 'is not a number' + Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t"); + WebSerial.printf("Temp *C = %.2f", t); + if (!isnan(temp_out)) + temp_out += (t - temp_out) / 30.0; + else + temp_out = t; + } else { + Serial.println("Failed to read temperature"); + WebSerial.println("Failed to read temperature"); return false; } - if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) //if temperature OK, measure RH & calculate compensated humidity - { - htValue = ht2x.readHumidity();// getCompensatedHumidity(htValue); //accuracy +-2% in range 0%..100%/0C..80C at 12-bit, to compensates influence of T on RH - - if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) - { - Serial.print(htValue); - Serial.println(" +-2%"); - hum_out = htValue; - } - } - if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) - { - if (!isnan(temp_out)) - temp_out += (fTemp - temp_out) / 30.0; - else - temp_out = fTemp; + + if (! isnan(h)) { // check if 'is not a number' + Serial.print("Hum. % = "); Serial.println(h); + WebSerial.printf("Hum. %% = %.2f\n", h); if (!isnan(hum_out)) - hum_out += (htValue - hum_out) / 30.0; + hum_out += (h - hum_out) / 30.0; else - hum_out = htValue; - heatindex = calc.heatIndex_c(temp_out, hum_out); - dewpoint = calc.dewPoint_c(temp_out, hum_out); - apptemp = calc.apparentTemp_c(temp_out, hum_out, windspeed); - WebSerial.print("Temp: "); - WebSerial.print(fTemp); - WebSerial.print(", Hum: "); - WebSerial.println(htValue); + hum_out = h; + } else { + Serial.println("Failed to read humidity"); + WebSerial.println("Failed to read humidity"); + return false; } + + heatindex = calc.heatIndex_c(temp_out, hum_out); + dewpoint = calc.dewPoint_c(temp_out, hum_out); + apptemp = calc.apparentTemp_c(temp_out, hum_out, windspeed); return true; -} \ No newline at end of file +}