Ext Sens changed to HTU21D
This commit is contained in:
17
ESP_Midroom2/.vscode/settings.json
vendored
Normal file
17
ESP_Midroom2/.vscode/settings.json
vendored
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,5 +25,6 @@ lib_deps =
|
|||||||
ayushsharma82/AsyncElegantOTA @ ^2.2.6
|
ayushsharma82/AsyncElegantOTA @ ^2.2.6
|
||||||
ayushsharma82/WebSerial @ ^1.3.0
|
ayushsharma82/WebSerial @ ^1.3.0
|
||||||
jwrw/ESP_EEPROM @ ^2.0.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
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
//#include <Adafruit_AHTX0.h>
|
//#include <Adafruit_AHTX0.h>
|
||||||
#include "DHTesp.h"
|
//#include "DHTesp.h"
|
||||||
|
#include <Wire.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
@@ -10,6 +11,8 @@
|
|||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <ESP_EEPROM.h>
|
#include <ESP_EEPROM.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
#include <HTU2xD_SHT2x_Si70xx.h>
|
||||||
|
#include <MeteoFunctions.h>
|
||||||
|
|
||||||
#define WIFI_SSID "wf-home"
|
#define WIFI_SSID "wf-home"
|
||||||
#define WIFI_PASSWORD "0ndthnrf"
|
#define WIFI_PASSWORD "0ndthnrf"
|
||||||
@@ -25,12 +28,11 @@
|
|||||||
|
|
||||||
#define LAMP_OUT (D5)
|
#define LAMP_OUT (D5)
|
||||||
|
|
||||||
//Adafruit_AHTX0 aht;
|
HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution
|
||||||
DHTesp dht;
|
MeteoFunctions calc;
|
||||||
TempAndHumidity sTaH;
|
|
||||||
|
|
||||||
//sensors_event_t humidity, temp;
|
|
||||||
float temp_out, hum_out;
|
float temp_out, hum_out, windspeed, heatindex, dewpoint, apptemp;
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
@@ -44,6 +46,7 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
|||||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||||
void callback(char* topic, byte* payload, unsigned int length);
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
void reconnect();
|
void reconnect();
|
||||||
|
boolean getTiH(void);
|
||||||
|
|
||||||
unsigned long stled;
|
unsigned long stled;
|
||||||
float wMins = 0;
|
float wMins = 0;
|
||||||
@@ -56,6 +59,8 @@ AsyncWebServer server(80);
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
//Wire.begin(); // This function initializes the Wire library
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.hostname(HOSTNAME);
|
WiFi.hostname(HOSTNAME);
|
||||||
@@ -98,33 +103,41 @@ void setup() {
|
|||||||
pinMode(LED_MQ, OUTPUT);
|
pinMode(LED_MQ, OUTPUT);
|
||||||
pinMode(LED_WRK, OUTPUT);
|
pinMode(LED_WRK, OUTPUT);
|
||||||
pinMode(LAMP_OUT, OUTPUT);
|
pinMode(LAMP_OUT, OUTPUT);
|
||||||
dht.setup(D1, DHTesp::DHT22);
|
|
||||||
delay(dht.getMinimumSamplingPeriod());
|
connectToWifi();
|
||||||
sTaH = dht.getTempAndHumidity();
|
|
||||||
if(dht.getStatus() == dht.ERROR_NONE){
|
// while (ht2x.begin() != true) //reset sensor, set heater off, set resolution, check power (sensor doesn't operate correctly if VDD < +2.25v)
|
||||||
temp_out = sTaH.temperature - 2.0;
|
// {
|
||||||
hum_out = sTaH.humidity;
|
// //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 (! aht.begin()) {
|
|
||||||
// Serial.println("Could not find AHT? Check wiring");
|
// delay(500);
|
||||||
// aht_pesent = false;
|
|
||||||
// } else{
|
|
||||||
// if(aht.getEvent(&humidity, &temp)){
|
|
||||||
// temp_out = temp.temperature;
|
|
||||||
// hum_out = humidity.relative_humidity;
|
|
||||||
// }
|
|
||||||
// aht_pesent = true;
|
|
||||||
// }
|
// }
|
||||||
|
// 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.begin(4);
|
||||||
EEPROM.get(0, led_intense);
|
EEPROM.get(0, led_intense);
|
||||||
Serial.print("Led intense: ");
|
Serial.print("Led intense: ");
|
||||||
Serial.println(int(led_intense / 2.55));
|
Serial.println(int(led_intense / 2.55));
|
||||||
connectToWifi();
|
|
||||||
|
|
||||||
stled = millis();
|
stled = millis();
|
||||||
WebSerial.begin(&server);
|
WebSerial.begin(&server);
|
||||||
/* Attach Message Callback */
|
/* Attach Message Callback */
|
||||||
server.begin();
|
server.begin();
|
||||||
|
windspeed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -132,7 +145,6 @@ void loop() {
|
|||||||
static byte bSecs = 0;
|
static byte bSecs = 0;
|
||||||
char v[10];
|
char v[10];
|
||||||
static unsigned long ledTime = 0;
|
static unsigned long ledTime = 0;
|
||||||
float fTemp;
|
|
||||||
|
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
if(!client.connected()) reconnect();
|
if(!client.connected()) reconnect();
|
||||||
@@ -149,71 +161,28 @@ void loop() {
|
|||||||
if(stled + 300 < millis()){
|
if(stled + 300 < millis()){
|
||||||
digitalWrite(LED_WRK, HIGH);
|
digitalWrite(LED_WRK, HIGH);
|
||||||
}
|
}
|
||||||
if(cRun + 2000 <= millis()){
|
if(cRun + 10000 <= millis()){
|
||||||
cRun = millis();
|
cRun = millis();
|
||||||
Serial.println(bSecs);
|
Serial.println(bSecs);
|
||||||
sTaH = dht.getTempAndHumidity();
|
WebSerial.println("10 sec");
|
||||||
if(dht.getStatus() == dht.ERROR_NONE){
|
if(getTiH()){
|
||||||
fTemp = sTaH.temperature - 2.0;
|
//if(bSecs == 3){
|
||||||
temp_out += (fTemp - temp_out) / 150.0;
|
//bSecs = 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;
|
|
||||||
digitalWrite(LED_WRK, LOW);
|
digitalWrite(LED_WRK, LOW);
|
||||||
sprintf(v, "%.1f", temp_out);
|
sprintf(v, "%.1f", temp_out);
|
||||||
client.publish(TOPIC"tempout", v);
|
client.publish(TOPIC"tempout", v);
|
||||||
sprintf(v, "%.1f", hum_out);
|
sprintf(v, "%.1f", hum_out);
|
||||||
client.publish(TOPIC"humout", v);
|
client.publish(TOPIC"humout", v);
|
||||||
sprintf(v, "%.1f", dht.computeHeatIndex(temp_out, hum_out));
|
sprintf(v, "%.1f", heatindex);
|
||||||
client.publish(TOPIC"heatindex", v);
|
client.publish(TOPIC"heatindex", v);
|
||||||
sprintf(v, "%.1f", dht.computeDewPoint(temp_out, hum_out));
|
sprintf(v, "%.1f", dewpoint);
|
||||||
client.publish(TOPIC"dewpoint", v);
|
client.publish(TOPIC"dewpoint", v);
|
||||||
ComfortState cs;
|
sprintf(v, "%.1f", apptemp);
|
||||||
dht.getComfortRatio(cs, temp_out, hum_out);
|
client.publish(TOPIC"appTemp", v);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if((bSecs % 15) == 0){
|
if(bSecs == 3){
|
||||||
|
bSecs = 0;
|
||||||
wMins += 0.5;
|
wMins += 0.5;
|
||||||
Serial.println("halfMinTick");
|
Serial.println("halfMinTick");
|
||||||
sprintf(v, "%.1f", wMins);
|
sprintf(v, "%.1f", wMins);
|
||||||
@@ -272,6 +241,9 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
if (led) analogWrite(LAMP_OUT, led_intense);
|
if (led) analogWrite(LAMP_OUT, led_intense);
|
||||||
}
|
}
|
||||||
|
if(strcmp(topic, TOPIC"windspeed") == 0){
|
||||||
|
windspeed = strtof(val, NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
@@ -288,6 +260,7 @@ void reconnect() {
|
|||||||
client.publish(TOPIC"online", "1");
|
client.publish(TOPIC"online", "1");
|
||||||
client.subscribe(TOPIC"light");
|
client.subscribe(TOPIC"light");
|
||||||
client.subscribe(TOPIC"ledint");
|
client.subscribe(TOPIC"ledint");
|
||||||
|
client.subscribe(TOPIC"windspeed");
|
||||||
char vout[7];
|
char vout[7];
|
||||||
sprintf(vout, "%.1f", temp_out);
|
sprintf(vout, "%.1f", temp_out);
|
||||||
client.publish(TOPIC"tempout", vout);
|
client.publish(TOPIC"tempout", vout);
|
||||||
@@ -303,3 +276,56 @@ void reconnect() {
|
|||||||
}
|
}
|
||||||
digitalWrite(LED_MQ, HIGH);
|
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("<error>"));
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user