diff --git a/platformio.ini b/platformio.ini index 286e171..5954619 100644 --- a/platformio.ini +++ b/platformio.ini @@ -76,6 +76,7 @@ lib_deps = ${common.lib_deps} [sensors] lib_deps = adafruit/Adafruit Unified Sensor@^1.1.4 + adafruit/Adafruit AHTX0@^2.0.3 adafruit/Adafruit BME280 Library@^2.2.2 adafruit/Adafruit BMP085 Library@^1.2.1 adafruit/Adafruit BMP280 Library@^2.6.3 diff --git a/src/AHTX0.cpp b/src/AHTX0.cpp new file mode 100644 index 0000000..4c2ddeb --- /dev/null +++ b/src/AHTX0.cpp @@ -0,0 +1,82 @@ +#ifdef SENSORS +#include "AHTX0.h" + +#include "globals.h" +#include "mqtt.h" +#include "defaults.h" +#include +#include "string_utils.h" + +#include + +namespace AHTX0 +{ + Adafruit_AHTX0* aht; + long AHTX0_status; + String AHTX0_I2c; + int AHTX0_I2c_Bus; + unsigned long AHTX0PreviousMillis = 0; + int sensorInterval = 60000; + bool initialized = false; + + void Setup() + { + if (!I2C_Bus_1_Started && !I2C_Bus_2_Started) return; + + aht = new Adafruit_AHTX0(); + if (AHTX0_I2c == "0x38") { + AHTX0_status = aht->begin(AHTX0_I2c_Bus == 1 ? &Wire : &Wire1, 0x38); + } else if (AHTX0_I2c == "0x39") { + AHTX0_status = aht->begin(AHTX0_I2c_Bus == 1 ? &Wire : &Wire1, 0x39); + } else { + return; + } + + if (!AHTX0_status) { + Serial.println("[AHTX0] Couldn't find a sensor, check your wiring and I2C address!"); + } else { + initialized = true; + } + } + + void ConnectToWifi() + { + AsyncWiFiSettings.html("h4", "AHTX0 - Temperature + Humidity Sensor:"); + AHTX0_I2c_Bus = AsyncWiFiSettings.integer("AHTX0_I2c_Bus", 1, 2, DEFAULT_I2C_BUS, "I2C Bus"); + AHTX0_I2c = AsyncWiFiSettings.string("AHTX0_I2c", "", "I2C address (0x38 or 0x39)"); + } + + void SerialReport() + { + if (!I2C_Bus_1_Started && !I2C_Bus_2_Started) return; + if (AHTX0_I2c.isEmpty()) return; + Serial.print("AHTX0_I2c Sensor: "); + Serial.println(AHTX0_I2c + " on bus " + AHTX0_I2c_Bus); + } + + void Loop() + { + if (!I2C_Bus_1_Started && !I2C_Bus_2_Started) return; + if (!initialized) return; + + if (AHTX0PreviousMillis == 0 || millis() - AHTX0PreviousMillis >= sensorInterval) { + sensors_event_t humidity, temp; + aht->getEvent(&humidity, &temp); + + pub((roomsTopic + "/AHTX0_temperature").c_str(), 0, 1, String(temp.temperature).c_str()); + pub((roomsTopic + "/AHTX0_humidity").c_str(), 0, 1, String(humidity.relative_humidity).c_str()); + + AHTX0PreviousMillis = millis(); + } + } + + bool SendDiscovery() + { + if (AHTX0_I2c.isEmpty()) return true; + + return sendSensorDiscovery("AHTX0 Temperature", EC_NONE, "temperature", "°C") + && sendSensorDiscovery("AHTX0 Humidity", EC_NONE, "humidity", "%"); + } +} + +#endif diff --git a/src/AHTX0.h b/src/AHTX0.h new file mode 100644 index 0000000..0f03b28 --- /dev/null +++ b/src/AHTX0.h @@ -0,0 +1,14 @@ +#pragma once +#ifdef SENSORS +#include + +namespace AHTX0 +{ + void ConnectToWifi(); + void SerialReport(); + bool SendDiscovery(); + void Setup(); + void Loop(); +} + +#endif diff --git a/src/main.cpp b/src/main.cpp index 2fefd51..faa6217 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,7 @@ bool sendTelemetry(unsigned int totalSeen, unsigned int totalFpSeen, int unsigne && Battery::SendDiscovery() #ifdef SENSORS && DHT::SendDiscovery() + && AHTX0::SendDiscovery() && BH1750::SendDiscovery() && BME280::SendDiscovery() && BMP180::SendDiscovery() @@ -174,6 +175,7 @@ void setupNetwork() { AsyncWiFiSettings.heading("I2C Sensors ℹ️", false); + AHTX0::ConnectToWifi(); BH1750::ConnectToWifi(); BME280::ConnectToWifi(); BMP180::ConnectToWifi(); @@ -234,6 +236,7 @@ void setupNetwork() { I2C::SerialReport(); #ifdef SENSORS DHT::SerialReport(); + AHTX0::SerialReport(); BH1750::SerialReport(); BME280::SerialReport(); BMP180::SerialReport(); @@ -503,6 +506,7 @@ void setup() { #ifdef SENSORS DHT::Setup(); I2C::Setup(); + AHTX0::Setup(); BH1750::Setup(); BME280::Setup(); BMP180::Setup(); @@ -533,6 +537,7 @@ void loop() { #endif #ifdef SENSORS DHT::Loop(); + AHTX0::Loop(); BH1750::Loop(); BME280::Loop(); BMP180::Loop(); diff --git a/src/main.h b/src/main.h index 0287271..158a438 100644 --- a/src/main.h +++ b/src/main.h @@ -35,6 +35,7 @@ #include "BME280.h" #include "BMP180.h" #include "BMP280.h" +#include "AHTX0.h" #include "DHT.h" #include "HX711.h" #include "SHT.h"