Ext Sens changed to HTU21D
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
//#include <Adafruit_AHTX0.h>
|
||||
#include "DHTesp.h"
|
||||
//#include "DHTesp.h"
|
||||
#include <Wire.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Ticker.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
@@ -10,6 +11,8 @@
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <HTU2xD_SHT2x_Si70xx.h>
|
||||
#include <MeteoFunctions.h>
|
||||
|
||||
#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("<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