diff --git a/ESP_Midroom2/platformio.ini b/ESP_Midroom2/platformio.ini index f102dbd..b091da8 100644 --- a/ESP_Midroom2/platformio.ini +++ b/ESP_Midroom2/platformio.ini @@ -28,3 +28,4 @@ lib_deps = #beegee-tokyo/DHT sensor library for ESPx @ ^1.19 https://github.com/enjoyneering/HTU2xD_SHT2x_Si70xx.git https://github.com/pilotak/MeteoFunctions.git + https://github.com/RobTillaart/SHT31.git diff --git a/ESP_Midroom2/src/main.cpp b/ESP_Midroom2/src/main.cpp index d9738f7..a13a00d 100644 --- a/ESP_Midroom2/src/main.cpp +++ b/ESP_Midroom2/src/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "SHT31.h" #include #define WIFI_SSID "wf-home" @@ -27,8 +28,9 @@ #define LED_WRK (D3) #define LAMP_OUT (D5) - -HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution +#define SHT31_ADDRESS 0x44 +SHT31 sht(SHT31_ADDRESS); +//HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution MeteoFunctions calc; @@ -70,7 +72,7 @@ void setup() { client.setServer(MQTT_SERV, 1883); client.setCallback(callback); client.setKeepAlive(20); - +#pragma region ArduinoOTA.onStart([]() { Serial.println("ArduinoOTA start"); }); @@ -94,6 +96,7 @@ void setup() { Serial.println("End Failed"); } }); +#pragma endregion ArduinoOTA.begin(); wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); @@ -104,6 +107,7 @@ void setup() { pinMode(LED_WRK, OUTPUT); pinMode(LAMP_OUT, OUTPUT); + //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) @@ -113,12 +117,25 @@ void setup() { // delay(500); // } - // digitalWrite(LED_WF, HIGH); + digitalWrite(LED_WF, HIGH); //ht2x.begin(); temp_out = NAN; hum_out = NAN; - ht2x.begin(); - getTiH(); + + 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(); + + //ht2x.begin(); + //getTiH(); //////////////////////////////// // dht.setup(D1, DHTesp::DHT22); // delay(dht.getMinimumSamplingPeriod()); @@ -158,7 +175,7 @@ void loop() { analogWrite(LAMP_OUT, ledLev); } } - if(stled + 300 < millis()){ + if(stled + 500 < millis()){ digitalWrite(LED_WRK, HIGH); } if(cRun + 10000 <= millis()){ @@ -170,9 +187,9 @@ void loop() { //bSecs = 0; digitalWrite(LED_WRK, LOW); sprintf(v, "%.1f", temp_out); - client.publish(TOPIC"tempout", v); + //client.publish(TOPIC"tempout", v); sprintf(v, "%.1f", hum_out); - client.publish(TOPIC"humout", v); + //client.publish(TOPIC"humout", v); sprintf(v, "%.1f", heatindex); client.publish(TOPIC"heatindex", v); sprintf(v, "%.1f", dewpoint); @@ -280,52 +297,90 @@ void reconnect() { boolean getTiH(void) { float htValue, fTemp; - htValue = ht2x.readTemperature(); //accuracy +-0.3C in range 0C..60C at 14-bit - Serial.print(F("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(F("")); - - 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"); + // 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 (htValue != HTU2XD_SHT2X_SI70XX_ERROR) //if temperature OK, measure RH & calculate compensated humidity - { - htValue = ht2x.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(F(" +-2%")); - hum_out = htValue; - } + 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"); + } } - if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) - { - if (!isnan(temp_out)) - temp_out += (fTemp - temp_out) / 30.0; - else - temp_out = fTemp; - 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; + 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(); + + // //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"); + // 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(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; } \ No newline at end of file