SHT31
This commit is contained in:
@@ -28,3 +28,4 @@ lib_deps =
|
|||||||
#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/enjoyneering/HTU2xD_SHT2x_Si70xx.git
|
||||||
https://github.com/pilotak/MeteoFunctions.git
|
https://github.com/pilotak/MeteoFunctions.git
|
||||||
|
https://github.com/RobTillaart/SHT31.git
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <ESP_EEPROM.h>
|
#include <ESP_EEPROM.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <HTU2xD_SHT2x_Si70xx.h>
|
#include <HTU2xD_SHT2x_Si70xx.h>
|
||||||
|
#include "SHT31.h"
|
||||||
#include <MeteoFunctions.h>
|
#include <MeteoFunctions.h>
|
||||||
|
|
||||||
#define WIFI_SSID "wf-home"
|
#define WIFI_SSID "wf-home"
|
||||||
@@ -27,8 +28,9 @@
|
|||||||
#define LED_WRK (D3)
|
#define LED_WRK (D3)
|
||||||
|
|
||||||
#define LAMP_OUT (D5)
|
#define LAMP_OUT (D5)
|
||||||
|
#define SHT31_ADDRESS 0x44
|
||||||
HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution
|
SHT31 sht(SHT31_ADDRESS);
|
||||||
|
//HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution
|
||||||
MeteoFunctions calc;
|
MeteoFunctions calc;
|
||||||
|
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ void setup() {
|
|||||||
client.setServer(MQTT_SERV, 1883);
|
client.setServer(MQTT_SERV, 1883);
|
||||||
client.setCallback(callback);
|
client.setCallback(callback);
|
||||||
client.setKeepAlive(20);
|
client.setKeepAlive(20);
|
||||||
|
#pragma region
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("ArduinoOTA start");
|
Serial.println("ArduinoOTA start");
|
||||||
});
|
});
|
||||||
@@ -94,6 +96,7 @@ void setup() {
|
|||||||
Serial.println("End Failed");
|
Serial.println("End Failed");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
#pragma endregion
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
|
|
||||||
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
||||||
@@ -104,6 +107,7 @@ void setup() {
|
|||||||
pinMode(LED_WRK, OUTPUT);
|
pinMode(LED_WRK, OUTPUT);
|
||||||
pinMode(LAMP_OUT, OUTPUT);
|
pinMode(LAMP_OUT, OUTPUT);
|
||||||
|
|
||||||
|
//delay(5000);
|
||||||
connectToWifi();
|
connectToWifi();
|
||||||
|
|
||||||
// while (ht2x.begin() != true) //reset sensor, set heater off, set resolution, check power (sensor doesn't operate correctly if VDD < +2.25v)
|
// 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);
|
// delay(500);
|
||||||
// }
|
// }
|
||||||
// digitalWrite(LED_WF, HIGH);
|
digitalWrite(LED_WF, HIGH);
|
||||||
//ht2x.begin();
|
//ht2x.begin();
|
||||||
temp_out = NAN;
|
temp_out = NAN;
|
||||||
hum_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);
|
// dht.setup(D1, DHTesp::DHT22);
|
||||||
// delay(dht.getMinimumSamplingPeriod());
|
// delay(dht.getMinimumSamplingPeriod());
|
||||||
@@ -158,7 +175,7 @@ void loop() {
|
|||||||
analogWrite(LAMP_OUT, ledLev);
|
analogWrite(LAMP_OUT, ledLev);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(stled + 300 < millis()){
|
if(stled + 500 < millis()){
|
||||||
digitalWrite(LED_WRK, HIGH);
|
digitalWrite(LED_WRK, HIGH);
|
||||||
}
|
}
|
||||||
if(cRun + 10000 <= millis()){
|
if(cRun + 10000 <= millis()){
|
||||||
@@ -170,9 +187,9 @@ void loop() {
|
|||||||
//bSecs = 0;
|
//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", heatindex);
|
sprintf(v, "%.1f", heatindex);
|
||||||
client.publish(TOPIC"heatindex", v);
|
client.publish(TOPIC"heatindex", v);
|
||||||
sprintf(v, "%.1f", dewpoint);
|
sprintf(v, "%.1f", dewpoint);
|
||||||
@@ -280,41 +297,25 @@ void reconnect() {
|
|||||||
boolean getTiH(void)
|
boolean getTiH(void)
|
||||||
{
|
{
|
||||||
float htValue, fTemp;
|
float htValue, fTemp;
|
||||||
htValue = ht2x.readTemperature(); //accuracy +-0.3C in range 0C..60C at 14-bit
|
// uint16_t stat = sht.readStatus();
|
||||||
Serial.print(F("Temperature...............: "));
|
// WebSerial.println(stat, HEX);
|
||||||
if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) //HTU2XD_SHT2X_SI70XX_ERROR = 255, library returns 255 if error occurs
|
// int error = sht.getError();
|
||||||
{
|
// WebSerial.println(error, HEX);
|
||||||
Serial.print(htValue);
|
if (!sht.isConnected()){
|
||||||
Serial.println(F(" +-0.3C"));
|
WebSerial.println("SHT Not Connected");
|
||||||
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;
|
return false;
|
||||||
}
|
}
|
||||||
if (htValue != HTU2XD_SHT2X_SI70XX_ERROR) //if temperature OK, measure RH & calculate compensated humidity
|
if(sht.dataReady()){
|
||||||
{
|
bool success = sht.readData();
|
||||||
htValue = ht2x.getCompensatedHumidity(htValue); //accuracy +-2% in range 0%..100%/0C..80C at 12-bit, to compensates influence of T on RH
|
sht.requestData();
|
||||||
|
if (success){
|
||||||
if (htValue != HTU2XD_SHT2X_SI70XX_ERROR)
|
htValue = sht.getTemperature();
|
||||||
{
|
fTemp = htValue;
|
||||||
Serial.print(htValue);
|
|
||||||
Serial.println(F(" +-2%"));
|
|
||||||
hum_out = htValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (htValue != HTU2XD_SHT2X_SI70XX_ERROR)
|
|
||||||
{
|
|
||||||
if (!isnan(temp_out))
|
if (!isnan(temp_out))
|
||||||
temp_out += (fTemp - temp_out) / 30.0;
|
temp_out += (htValue - temp_out) / 30.0;
|
||||||
else
|
else
|
||||||
temp_out = fTemp;
|
temp_out = htValue;
|
||||||
|
htValue = sht.getHumidity();
|
||||||
if (!isnan(hum_out))
|
if (!isnan(hum_out))
|
||||||
hum_out += (htValue - hum_out) / 30.0;
|
hum_out += (htValue - hum_out) / 30.0;
|
||||||
else
|
else
|
||||||
@@ -326,6 +327,60 @@ boolean getTiH(void)
|
|||||||
WebSerial.print(fTemp);
|
WebSerial.print(fTemp);
|
||||||
WebSerial.print(", Hum: ");
|
WebSerial.print(", Hum: ");
|
||||||
WebSerial.println(htValue);
|
WebSerial.println(htValue);
|
||||||
}
|
|
||||||
return true;
|
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("<error>");
|
||||||
|
// //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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user