first commit
commit
5b9cb905d1
|
|
@ -0,0 +1,274 @@
|
||||||
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#else
|
||||||
|
#include "WiFi.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <LeifHomieLib.h>
|
||||||
|
#include <AHT10.h>
|
||||||
|
#include <THextras.h>
|
||||||
|
|
||||||
|
#ifndef STASSID
|
||||||
|
#define STASSID "Cisco22379"
|
||||||
|
#define STAPSK "D5kKVu7TZc"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MQTT_IP
|
||||||
|
#define MQTT_IP "172.16.2.70"
|
||||||
|
#define MQTT_USER "th"
|
||||||
|
#define MQTT_PASS "h82b7782"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
HomieDevice homie;
|
||||||
|
|
||||||
|
const char* ssid = STASSID;
|
||||||
|
const char* password = STAPSK;
|
||||||
|
|
||||||
|
const int SDA_PIN = 0;
|
||||||
|
const int SCL_PIN = 2;
|
||||||
|
|
||||||
|
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
||||||
|
THextras THe;
|
||||||
|
const int READ_INTERVAL = 10000;
|
||||||
|
unsigned long lastReadingSent = 0;
|
||||||
|
|
||||||
|
|
||||||
|
// We'll need a place to save pointers to our created properties so that we can access them again later.
|
||||||
|
HomieProperty * tPropTemperature=NULL;
|
||||||
|
HomieProperty * tPropHumidex=NULL;
|
||||||
|
|
||||||
|
HomieProperty * hPropRelHumidity=NULL;
|
||||||
|
HomieProperty * hPropAbsHumidity=NULL;
|
||||||
|
HomieProperty * hPropDewPoint=NULL;
|
||||||
|
|
||||||
|
HomieProperty * cPropComfortRatio=NULL;
|
||||||
|
HomieProperty * cPropComfortState=NULL;
|
||||||
|
HomieProperty * cPropPerceptionState=NULL;
|
||||||
|
HomieProperty * cPropComfortString=NULL;
|
||||||
|
HomieProperty * cPropPerceptionString=NULL;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
// Setup AHT10
|
||||||
|
|
||||||
|
Serial.println("Setting up AHT10...");
|
||||||
|
while (myAHT10.begin(SDA_PIN, SCL_PIN) != true)
|
||||||
|
{
|
||||||
|
Serial.println(F("AHT10 Error"));
|
||||||
|
Serial.println(F("AHT10 not connected or fail to load calibration coefficient"));
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(3000); // delay so that first reading is accurate. Maybe?
|
||||||
|
// Connect to WiFi network
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
|
||||||
|
would try to act as both a client and an access-point and could cause
|
||||||
|
network-issues with your other WiFi-devices on your WiFi-network. */
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
Serial.println("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
HomieLibRegisterDebugPrintCallback([](const char * szText){ Serial.printf("%s",szText);});
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
HomieNode * tNode=homie.NewNode();
|
||||||
|
tNode->strID="temperature";
|
||||||
|
tNode->strFriendlyName="Temperature";
|
||||||
|
tNode->strType="temperature";
|
||||||
|
|
||||||
|
HomieProperty* tProp;
|
||||||
|
|
||||||
|
tPropTemperature=tProp=tNode->NewProperty();
|
||||||
|
tProp->strID="temperature";
|
||||||
|
tProp->SetUnit("°C");
|
||||||
|
tProp->datatype=homieFloat;
|
||||||
|
tProp->strFriendlyName="Temperature";
|
||||||
|
|
||||||
|
tPropHumidex=tProp=tNode->NewProperty();
|
||||||
|
tProp->strID="humidex";
|
||||||
|
tProp->SetUnit("");
|
||||||
|
tProp->datatype=homieFloat;
|
||||||
|
tProp->strFriendlyName="Humidex";
|
||||||
|
|
||||||
|
|
||||||
|
HomieNode * hNode=homie.NewNode();
|
||||||
|
hNode->strID="humidity";
|
||||||
|
hNode->strFriendlyName="Humidity";
|
||||||
|
hNode->strType="humidity";
|
||||||
|
|
||||||
|
HomieProperty* hProp;
|
||||||
|
|
||||||
|
hPropRelHumidity=hProp=hNode->NewProperty();
|
||||||
|
hProp->strID="relHumidity";
|
||||||
|
hProp->SetUnit("%");
|
||||||
|
hProp->datatype=homieFloat;
|
||||||
|
hProp->strFriendlyName="Relative Humidity";
|
||||||
|
|
||||||
|
hPropAbsHumidity=hProp=hNode->NewProperty();
|
||||||
|
hProp->strID="absHumidity";
|
||||||
|
hProp->SetUnit("g/m³");
|
||||||
|
hProp->datatype=homieFloat;
|
||||||
|
hProp->strFriendlyName="Absolute Humidity";
|
||||||
|
|
||||||
|
hPropDewPoint=hProp=hNode->NewProperty();
|
||||||
|
hProp->strID="dewPoint";
|
||||||
|
hProp->SetUnit("°C");
|
||||||
|
hProp->datatype=homieFloat;
|
||||||
|
hProp->strFriendlyName="Dew Point";
|
||||||
|
|
||||||
|
HomieNode * cNode=homie.NewNode();
|
||||||
|
cNode->strID="comfort";
|
||||||
|
cNode->strFriendlyName="Comfort";
|
||||||
|
cNode->strType="comfort";
|
||||||
|
|
||||||
|
HomieProperty* cProp;
|
||||||
|
|
||||||
|
cPropComfortRatio=cProp=cNode->NewProperty();
|
||||||
|
cProp->strID="comfortRatio";
|
||||||
|
cProp->SetUnit("%");
|
||||||
|
cProp->datatype=homieFloat;
|
||||||
|
cProp->strFriendlyName="Comfort Ratio";
|
||||||
|
|
||||||
|
cPropComfortState=cProp=cNode->NewProperty();
|
||||||
|
cProp->strID="comfortState";
|
||||||
|
cProp->SetUnit("");
|
||||||
|
cProp->datatype=homieInt;
|
||||||
|
cProp->strFriendlyName="Comfort State";
|
||||||
|
|
||||||
|
cPropPerceptionState=cProp=cNode->NewProperty();
|
||||||
|
cProp->strID="perceptionState";
|
||||||
|
cProp->SetUnit("");
|
||||||
|
cProp->datatype=homieInt;
|
||||||
|
cProp->strFriendlyName="Perception State";
|
||||||
|
|
||||||
|
cPropComfortString=cProp=cNode->NewProperty();
|
||||||
|
cProp->strID="comfortString";
|
||||||
|
cProp->SetUnit("");
|
||||||
|
cProp->datatype=homieString;
|
||||||
|
cProp->strFriendlyName="Comfort String";
|
||||||
|
|
||||||
|
cPropPerceptionString=cProp=cNode->NewProperty();
|
||||||
|
cProp->strID="perceptionString";
|
||||||
|
cProp->SetUnit("");
|
||||||
|
cProp->datatype=homieString;
|
||||||
|
cProp->strFriendlyName="Perception String";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
homie.strFriendlyName="AHT10 Temp and Humidity";
|
||||||
|
homie.strID="AHT10-1";
|
||||||
|
homie.strID.toLowerCase();
|
||||||
|
|
||||||
|
homie.strMqttServerIP=MQTT_IP;
|
||||||
|
homie.strMqttUserName=MQTT_USER;
|
||||||
|
homie.strMqttPassword=MQTT_PASS;
|
||||||
|
|
||||||
|
homie.Init();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
homie.Loop();
|
||||||
|
|
||||||
|
if(homie.IsConnected())
|
||||||
|
{
|
||||||
|
float temperature{};
|
||||||
|
float humidity{};
|
||||||
|
float valueTH{};
|
||||||
|
|
||||||
|
if (millis() - lastReadingSent >= READ_INTERVAL)
|
||||||
|
{
|
||||||
|
valueTH = myAHT10.readTemperature(AHT10_FORCE_READ_DATA); //read 6-bytes over I2C
|
||||||
|
|
||||||
|
if (valueTH != AHT10_ERROR)
|
||||||
|
{
|
||||||
|
temperature = valueTH;
|
||||||
|
}
|
||||||
|
else Serial.println("Error reading temperature");
|
||||||
|
|
||||||
|
|
||||||
|
valueTH = myAHT10.readHumidity(AHT10_USE_READ_DATA); //use same 6-bytes
|
||||||
|
if (valueTH != AHT10_ERROR)
|
||||||
|
{
|
||||||
|
humidity = valueTH;
|
||||||
|
}
|
||||||
|
else Serial.println("Error reading humidity");
|
||||||
|
|
||||||
|
ComfortState comfortState;
|
||||||
|
float humidex = THe.computeHumidex(temperature, humidity);
|
||||||
|
float dewPoint = THe.computeDewPoint(temperature, humidity);
|
||||||
|
float comfortRatio = THe.getComfortRatio(comfortState, temperature, humidity);
|
||||||
|
String comfortString = THe.getComfortString(comfortState);
|
||||||
|
PerceptionState perceptionState = THe.computePerception(temperature, humidity);
|
||||||
|
float absHumidity = THe.computeAbsoluteHumidity(temperature, humidity);
|
||||||
|
String perceptionString = THe.getPerceptionString(perceptionState);
|
||||||
|
|
||||||
|
if(tPropTemperature) tPropTemperature->SetValue(String(temperature));
|
||||||
|
Serial.print("Temperature: ");
|
||||||
|
Serial.println(temperature);
|
||||||
|
|
||||||
|
if(tPropHumidex) tPropHumidex->SetValue(String(humidex));
|
||||||
|
Serial.print("Humidex: ");
|
||||||
|
Serial.println(humidex);
|
||||||
|
|
||||||
|
if(hPropRelHumidity) hPropRelHumidity->SetValue(String(humidity));
|
||||||
|
Serial.print("Relative Humidity: ");
|
||||||
|
Serial.println(humidity);
|
||||||
|
|
||||||
|
if(hPropAbsHumidity) hPropAbsHumidity->SetValue(String(absHumidity));
|
||||||
|
Serial.print("Absolute Humidity: ");
|
||||||
|
Serial.println(absHumidity);
|
||||||
|
|
||||||
|
if(hPropDewPoint) hPropDewPoint->SetValue(String(dewPoint));
|
||||||
|
Serial.print("Dew Point: ");
|
||||||
|
Serial.println(dewPoint);
|
||||||
|
|
||||||
|
if(cPropComfortRatio) cPropComfortRatio->SetValue(String(comfortRatio));
|
||||||
|
Serial.print("Comfort Ratio: ");
|
||||||
|
Serial.println(comfortRatio);
|
||||||
|
|
||||||
|
if(cPropComfortState) cPropComfortState->SetValue(String(comfortState));
|
||||||
|
Serial.print("Comfort State: ");
|
||||||
|
Serial.println(comfortState);
|
||||||
|
|
||||||
|
if(cPropPerceptionState) cPropPerceptionState->SetValue(String(perceptionState));
|
||||||
|
Serial.print("Perception State: ");
|
||||||
|
Serial.println(perceptionState);
|
||||||
|
|
||||||
|
if(cPropComfortString) cPropComfortString->SetValue(comfortString);
|
||||||
|
Serial.print("Comfort String: ");
|
||||||
|
Serial.println(comfortString);
|
||||||
|
|
||||||
|
if(cPropPerceptionString) cPropPerceptionString->SetValue(perceptionString);
|
||||||
|
Serial.print("Perception String: ");
|
||||||
|
Serial.println(perceptionString);
|
||||||
|
|
||||||
|
|
||||||
|
lastReadingSent = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue