diff --git a/include/bmp.h b/include/bmp.h index 06264a4..4fcdf65 100644 --- a/include/bmp.h +++ b/include/bmp.h @@ -4,6 +4,22 @@ namespace Bmp { Adafruit_BMP280 bmp; // I2C Interface + struct { + const char* topic = "esp_clock/sensor/bmp280/data"; + float temp; + float pressure; + float altitude; + float readTemp() { + temp = bmp.readTemperature() - 2; + return temp; + } + void readAll() { + readTemp(); + pressure = bmp.readPressure() / 100; + altitude = bmp.readAltitude(1006); + } + } data; + void setup() { Serial.begin(9600); Serial.println(F("BMP280 test")); diff --git a/include/ir.h b/include/ir.h index 0a1ce29..07a451f 100644 --- a/include/ir.h +++ b/include/ir.h @@ -39,6 +39,7 @@ namespace Ir { void command(const char c[]) { avrOn = true; + tCheckWifi.disable(); Wifi::reconnect(); Display::displayText(c); } @@ -76,7 +77,7 @@ namespace Ir { command("On"); break; case 0x84: - Display::displayTemp(Bmp::bmp.readTemperature()); + Display::displayTemp(Bmp::data.readTemp()); break; default: Display::displayValue(lastCommand); diff --git a/include/mqtt.h b/include/mqtt.h index 6b210df..c344be2 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -7,7 +7,9 @@ namespace Mqtt { void publishCommand(); + void publishBmp280(); Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); + Task tPublishBmp(TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); AsyncMqttClient client; @@ -25,6 +27,8 @@ namespace Mqtt { } } commands; + const char* bmpTopic = "esp_clock/sensor/bmp280/data"; + void publishCommand() { if (!commands.queue.empty() && client.connected()) { char message[32]; @@ -38,13 +42,24 @@ namespace Mqtt { } } + void publishBmp280() { + if (client.connected()) { + Bmp::data.readAll(); + char message[255]; + sprintf(message, "{\"temperature\":%.2f, \"pressure\":%.2f, \"altitude\": %.2f}", Bmp::data.temp, Bmp::data.pressure, Bmp::data.altitude); + client.publish(bmpTopic, 0, true, message); + } + } + void setup() { client.onConnect([](bool sessionPresent) { tPublish.enableDelayed(); + tPublishBmp.enable(); Serial.println("Connected to MQTT"); }); client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { tPublish.disable(); + tPublishBmp.disable(); Serial.println("Disconnected from MQTT"); }); client.setServer(MQTT_HOST, MQTT_PORT);