SHT31
This commit is contained in:
@@ -12,8 +12,8 @@
|
|||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = d1_mini
|
board = d1_mini
|
||||||
framework = arduino
|
framework = arduino
|
||||||
;upload_protocol = espota
|
upload_protocol = espota
|
||||||
;upload_port = 192.168.1.136
|
upload_port = 192.168.1.136
|
||||||
lib_deps =
|
lib_deps =
|
||||||
knolleary/PubSubClient @ ^2.8
|
knolleary/PubSubClient @ ^2.8
|
||||||
ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0
|
ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0
|
||||||
@@ -24,3 +24,4 @@ lib_deps =
|
|||||||
https://github.com/pilotak/MeteoFunctions.git
|
https://github.com/pilotak/MeteoFunctions.git
|
||||||
;https://github.com/RobTillaart/SHT31.git
|
;https://github.com/RobTillaart/SHT31.git
|
||||||
;ayushsharma82/ElegantOTA@^3.1.2
|
;ayushsharma82/ElegantOTA@^3.1.2
|
||||||
|
https://github.com/adafruit/Adafruit_SHT31.git
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
//#include <Adafruit_AHTX0.h>
|
|
||||||
//#include "DHTesp.h"
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
@@ -8,17 +6,15 @@
|
|||||||
#include <ESPAsyncTCP.h>
|
#include <ESPAsyncTCP.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <WebSerial.h>
|
#include <WebSerial.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
|
||||||
#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>
|
||||||
|
#include "SPI.h"
|
||||||
|
#include "Adafruit_SHT31.h"
|
||||||
|
|
||||||
#define WIFI_SSID "wf-home"
|
#define WIFI_SSID "wf-home"
|
||||||
#define WIFI_PASSWORD "0ndthnrf"
|
#define WIFI_PASSWORD "0ndthnrf"
|
||||||
#define WIFI_SSID2 "BR"
|
|
||||||
#define WIFI_PASSWORD2 "499727479o"
|
|
||||||
#define MQTT_SERV "192.168.1.111"
|
#define MQTT_SERV "192.168.1.111"
|
||||||
#define TOPIC "home/midroom/bed/"
|
#define TOPIC "home/midroom/bed/"
|
||||||
#define HOSTNAME "ESP_Midroom2"
|
#define HOSTNAME "ESP_Midroom2"
|
||||||
@@ -29,10 +25,9 @@
|
|||||||
|
|
||||||
#define LAMP_OUT (D5)
|
#define LAMP_OUT (D5)
|
||||||
#define SHT31_ADDRESS 0x44
|
#define SHT31_ADDRESS 0x44
|
||||||
//SHT31 sht;
|
|
||||||
HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution
|
|
||||||
MeteoFunctions calc;
|
|
||||||
|
|
||||||
|
MeteoFunctions calc;
|
||||||
|
Adafruit_SHT31 sht31 = Adafruit_SHT31();
|
||||||
|
|
||||||
float temp_out, hum_out, windspeed, heatindex, dewpoint, apptemp;
|
float temp_out, hum_out, windspeed, heatindex, dewpoint, apptemp;
|
||||||
|
|
||||||
@@ -56,7 +51,6 @@ bool led, aht_pesent;
|
|||||||
uint8_t led_intense;
|
uint8_t led_intense;
|
||||||
uint8_t ledLev;
|
uint8_t ledLev;
|
||||||
|
|
||||||
ESP8266WiFiMulti wifiMulti;
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -66,8 +60,6 @@ void setup() {
|
|||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.hostname(HOSTNAME);
|
WiFi.hostname(HOSTNAME);
|
||||||
//wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
|
|
||||||
//wifiMulti.addAP(WIFI_SSID2, WIFI_PASSWORD2);
|
|
||||||
|
|
||||||
client.setServer(MQTT_SERV, 1883);
|
client.setServer(MQTT_SERV, 1883);
|
||||||
client.setCallback(callback);
|
client.setCallback(callback);
|
||||||
@@ -110,40 +102,21 @@ void setup() {
|
|||||||
//delay(5000);
|
//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)
|
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
|
||||||
// {
|
Serial.println("Couldn't find SHT31");
|
||||||
// //Serial.println(F("HTU2xD/SHT2x not connected, fail or VDD < +2.25v")); //(F()) save string to flash & keeps dynamic memory free
|
while (1) delay(1);
|
||||||
// digitalWrite(LED_WF, LOW);
|
}
|
||||||
|
|
||||||
|
Serial.print("Heater Enabled State: ");
|
||||||
|
if (sht31.isHeaterEnabled())
|
||||||
|
Serial.println("ENABLED");
|
||||||
|
else
|
||||||
|
Serial.println("DISABLED");
|
||||||
|
|
||||||
|
|
||||||
// delay(500);
|
|
||||||
// }
|
|
||||||
digitalWrite(LED_WF, HIGH);
|
|
||||||
ht2x.begin();
|
|
||||||
temp_out = NAN;
|
temp_out = NAN;
|
||||||
hum_out = NAN;
|
hum_out = NAN;
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
//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: ");
|
||||||
@@ -211,7 +184,6 @@ void loop() {
|
|||||||
|
|
||||||
void connectToWifi() {
|
void connectToWifi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
//wifiMulti.run();
|
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,91 +268,37 @@ void reconnect() {
|
|||||||
|
|
||||||
boolean getTiH(void)
|
boolean getTiH(void)
|
||||||
{
|
{
|
||||||
float htValue, fTemp;
|
float t = sht31.readTemperature();
|
||||||
// uint16_t stat = sht.readStatus();
|
float h = sht31.readHumidity();
|
||||||
// WebSerial.println(stat, HEX);
|
|
||||||
// int error = sht.getError();
|
|
||||||
// WebSerial.println(error, HEX);
|
|
||||||
// if (!sht.isConnected()){
|
|
||||||
// WebSerial.println("SHT Not Connected");
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// 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");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// 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
|
if (! isnan(t)) { // check if 'is not a number'
|
||||||
//ht2x.setHeater(false); //true=heater on, false=heater off
|
Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t");
|
||||||
//ht2x.setResolution(HUMD_12BIT_TEMP_14BIT); //humidity 12-bit, temperature 14-bit
|
WebSerial.printf("Temp *C = %.2f", t);
|
||||||
WebSerial.println("HTU Bad");
|
if (!isnan(temp_out))
|
||||||
|
temp_out += (t - temp_out) / 30.0;
|
||||||
|
else
|
||||||
|
temp_out = t;
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to read temperature");
|
||||||
|
WebSerial.println("Failed to read temperature");
|
||||||
return false;
|
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)
|
if (! isnan(h)) { // check if 'is not a number'
|
||||||
{
|
Serial.print("Hum. % = "); Serial.println(h);
|
||||||
Serial.print(htValue);
|
WebSerial.printf("Hum. %% = %.2f\n", h);
|
||||||
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))
|
if (!isnan(hum_out))
|
||||||
hum_out += (htValue - hum_out) / 30.0;
|
hum_out += (h - hum_out) / 30.0;
|
||||||
else
|
else
|
||||||
hum_out = htValue;
|
hum_out = h;
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to read humidity");
|
||||||
|
WebSerial.println("Failed to read humidity");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
heatindex = calc.heatIndex_c(temp_out, hum_out);
|
heatindex = calc.heatIndex_c(temp_out, hum_out);
|
||||||
dewpoint = calc.dewPoint_c(temp_out, hum_out);
|
dewpoint = calc.dewPoint_c(temp_out, hum_out);
|
||||||
apptemp = calc.apparentTemp_c(temp_out, hum_out, windspeed);
|
apptemp = calc.apparentTemp_c(temp_out, hum_out, windspeed);
|
||||||
WebSerial.print("Temp: ");
|
|
||||||
WebSerial.print(fTemp);
|
|
||||||
WebSerial.print(", Hum: ");
|
|
||||||
WebSerial.println(htValue);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user