From 28c963e8c3f2d2231584696ea8aa99e04a8adbde Mon Sep 17 00:00:00 2001 From: lexa Date: Fri, 3 May 2024 16:48:29 +0300 Subject: [PATCH] Ext Sens changed to HTU21D --- ESP_Midroom2/.vscode/settings.json | 17 +++ ESP_Midroom2/platformio.ini | 5 +- ESP_Midroom2/src/main.cpp | 184 ++++++++++++++++------------- 3 files changed, 125 insertions(+), 81 deletions(-) create mode 100644 ESP_Midroom2/.vscode/settings.json diff --git a/ESP_Midroom2/.vscode/settings.json b/ESP_Midroom2/.vscode/settings.json new file mode 100644 index 0000000..34919a2 --- /dev/null +++ b/ESP_Midroom2/.vscode/settings.json @@ -0,0 +1,17 @@ +{ + "files.associations": { + "array": "cpp", + "*.tcc": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "memory": "cpp", + "random": "cpp", + "initializer_list": "cpp", + "ranges": "cpp", + "regex": "cpp" + } +} diff --git a/ESP_Midroom2/platformio.ini b/ESP_Midroom2/platformio.ini index cbf2448..f102dbd 100644 --- a/ESP_Midroom2/platformio.ini +++ b/ESP_Midroom2/platformio.ini @@ -25,5 +25,6 @@ lib_deps = ayushsharma82/AsyncElegantOTA @ ^2.2.6 ayushsharma82/WebSerial @ ^1.3.0 jwrw/ESP_EEPROM @ ^2.0.0 - beegee-tokyo/DHT sensor library for ESPx @ ^1.19 - + #beegee-tokyo/DHT sensor library for ESPx @ ^1.19 + https://github.com/enjoyneering/HTU2xD_SHT2x_Si70xx.git + https://github.com/pilotak/MeteoFunctions.git diff --git a/ESP_Midroom2/src/main.cpp b/ESP_Midroom2/src/main.cpp index 57d9703..d9738f7 100644 --- a/ESP_Midroom2/src/main.cpp +++ b/ESP_Midroom2/src/main.cpp @@ -1,6 +1,7 @@ #include //#include -#include "DHTesp.h" +//#include "DHTesp.h" +#include #include #include #include @@ -10,6 +11,8 @@ #include #include #include +#include +#include #define WIFI_SSID "wf-home" #define WIFI_PASSWORD "0ndthnrf" @@ -25,12 +28,11 @@ #define LAMP_OUT (D5) -//Adafruit_AHTX0 aht; -DHTesp dht; -TempAndHumidity sTaH; +HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution +MeteoFunctions calc; -//sensors_event_t humidity, temp; -float temp_out, hum_out; + +float temp_out, hum_out, windspeed, heatindex, dewpoint, apptemp; WiFiClient espClient; PubSubClient client(espClient); @@ -44,6 +46,7 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event); void onWifiDisconnect(const WiFiEventStationModeDisconnected& event); void callback(char* topic, byte* payload, unsigned int length); void reconnect(); +boolean getTiH(void); unsigned long stled; float wMins = 0; @@ -56,6 +59,8 @@ AsyncWebServer server(80); void setup() { Serial.begin(9600); + //Wire.begin(); // This function initializes the Wire library + WiFi.mode(WIFI_STA); WiFi.persistent(false); WiFi.hostname(HOSTNAME); @@ -98,33 +103,41 @@ void setup() { pinMode(LED_MQ, OUTPUT); pinMode(LED_WRK, OUTPUT); pinMode(LAMP_OUT, OUTPUT); - 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; - } - // if (! aht.begin()) { - // Serial.println("Could not find AHT? Check wiring"); - // aht_pesent = false; - // } else{ - // if(aht.getEvent(&humidity, &temp)){ - // temp_out = temp.temperature; - // hum_out = humidity.relative_humidity; - // } - // aht_pesent = true; + + 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); + + // delay(500); // } + // digitalWrite(LED_WF, HIGH); + //ht2x.begin(); + temp_out = NAN; + hum_out = NAN; + ht2x.begin(); + 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: "); Serial.println(int(led_intense / 2.55)); - connectToWifi(); stled = millis(); WebSerial.begin(&server); /* Attach Message Callback */ server.begin(); + windspeed = 0; } void loop() { @@ -132,7 +145,6 @@ void loop() { static byte bSecs = 0; char v[10]; static unsigned long ledTime = 0; - float fTemp; ArduinoOTA.handle(); if(!client.connected()) reconnect(); @@ -149,71 +161,28 @@ void loop() { if(stled + 300 < millis()){ digitalWrite(LED_WRK, HIGH); } - if(cRun + 2000 <= millis()){ + if(cRun + 10000 <= millis()){ cRun = millis(); Serial.println(bSecs); - sTaH = dht.getTempAndHumidity(); - if(dht.getStatus() == dht.ERROR_NONE){ - fTemp = sTaH.temperature - 2.0; - temp_out += (fTemp - temp_out) / 150.0; - hum_out += (sTaH.humidity - hum_out) / 150.0; - WebSerial.print("Temp: "); - WebSerial.print(fTemp); - WebSerial.print(", Hum: "); - WebSerial.println(sTaH.humidity); - if(bSecs == 29){ - bSecs = 0; + WebSerial.println("10 sec"); + if(getTiH()){ + //if(bSecs == 3){ + //bSecs = 0; digitalWrite(LED_WRK, LOW); sprintf(v, "%.1f", temp_out); client.publish(TOPIC"tempout", v); sprintf(v, "%.1f", hum_out); client.publish(TOPIC"humout", v); - sprintf(v, "%.1f", dht.computeHeatIndex(temp_out, hum_out)); + sprintf(v, "%.1f", heatindex); client.publish(TOPIC"heatindex", v); - sprintf(v, "%.1f", dht.computeDewPoint(temp_out, hum_out)); + sprintf(v, "%.1f", dewpoint); client.publish(TOPIC"dewpoint", v); - ComfortState cs; - dht.getComfortRatio(cs, temp_out, hum_out); - String comfortStatus; - switch(cs) { - case Comfort_OK: - comfortStatus = "Нормально"; - break; - case Comfort_TooHot: - comfortStatus = "Очень жарко"; - break; - case Comfort_TooCold: - comfortStatus = "Очень холодно"; - break; - case Comfort_TooDry: - comfortStatus = "Очень сухо"; - break; - case Comfort_TooHumid: - comfortStatus = "Очень влажно"; - break; - case Comfort_HotAndHumid: - comfortStatus = "Жарко и влажно"; - break; - case Comfort_HotAndDry: - comfortStatus = "Жарко и сухо"; - break; - case Comfort_ColdAndHumid: - comfortStatus = "Холодно и влажно}"; - break; - case Comfort_ColdAndDry: - comfortStatus = "Холодно и сухо"; - break; - default: - comfortStatus = "Неизвестно"; - break; - }; - client.publish(TOPIC"comfortstatus", comfortStatus.c_str()); - byte hp = dht.computePerception(temp_out, hum_out); - itoa(hp, v, 10); - client.publish(TOPIC"perception", v); - } + sprintf(v, "%.1f", apptemp); + client.publish(TOPIC"appTemp", v); + //} } - if((bSecs % 15) == 0){ + if(bSecs == 3){ + bSecs = 0; wMins += 0.5; Serial.println("halfMinTick"); sprintf(v, "%.1f", wMins); @@ -272,6 +241,9 @@ void callback(char* topic, byte* payload, unsigned int length) { EEPROM.commit(); if (led) analogWrite(LAMP_OUT, led_intense); } + if(strcmp(topic, TOPIC"windspeed") == 0){ + windspeed = strtof(val, NULL); + } } void reconnect() { @@ -288,6 +260,7 @@ void reconnect() { client.publish(TOPIC"online", "1"); client.subscribe(TOPIC"light"); client.subscribe(TOPIC"ledint"); + client.subscribe(TOPIC"windspeed"); char vout[7]; sprintf(vout, "%.1f", temp_out); client.publish(TOPIC"tempout", vout); @@ -303,3 +276,56 @@ void reconnect() { } digitalWrite(LED_MQ, HIGH); } + +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"); + 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 (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