first draft to publish HA config

This commit is contained in:
Nicu Hodos 2022-10-10 10:24:51 +02:00
parent 65a14ba679
commit 55b5aa2a33
3 changed files with 44 additions and 4 deletions

View File

@ -5,7 +5,6 @@ namespace Bmp {
Adafruit_BMP280 bmp; // I2C Interface
struct {
const char* topic = "esp_clock/sensor/bmp280/data";
float temp;
float pressure;
float altitude;

View File

@ -1,13 +1,16 @@
#include <queue>
#include <AsyncMqttClient.h>
#include <ArduinoJson.h>
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
namespace Mqtt {
void publishConfig();
void publishCommand();
void publishBmp280();
Task tPublishConfig(TASK_IMMEDIATE, TASK_ONCE, publishConfig, &ts);
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
@ -27,7 +30,39 @@ namespace Mqtt {
}
} commands;
const char* bmpTopic = "esp_clock/sensor/bmp280/data";
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
void publishConfig() {
StaticJsonDocument<255> jsonDoc;
JsonObject device = jsonDoc.createNestedObject("device");
device["name"] = "ESP Clock";
JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["device_class"] = "temperature";
sensor["name"] = "Livingroom Temperature";
sensor["unique_id"] = "livingroom_temperature";
sensor["unit_of_measurement"] = "°C";
sensor["state_topic"] = bmpTopic;
sensor["value_template"] = "{{ value_json.temperature}}";
sensor["device"] = device;
char message[255];
serializeJson(jsonDoc, message);
client.publish("homeassistant/sensor/esp_clock_temperature/config", 0, false, message);
jsonDoc.clear();
sensor.clear();
sensor = jsonDoc.createNestedObject("sensor");
sensor["device_class"] = "pressure";
sensor["name"] = "Livingroom Pressure";
sensor["unique_id"] = "livingroom_pressure";
sensor["unit_of_measurement"] = "hPa";
sensor["state_topic"] = bmpTopic;
sensor["value_template"] = "{{ value_json.pressure}}";
sensor["device"] = device;
char message1[255];
serializeJson(jsonDoc, message);
client.publish("homeassistant/sensor/esp_clock_pressure/config", 0, false, message1);
}
void publishCommand() {
if (uint8_t cmd = commands.getCurrent()) {
@ -43,13 +78,18 @@ namespace Mqtt {
void publishBmp280() {
Bmp::data.readAll();
StaticJsonDocument<255> jsonDoc;
JsonObject sensor = jsonDoc.createNestedObject("sensor");
sensor["temperature"] = Bmp::data.temp;
sensor["pressure"] = Bmp::data.pressure;
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);
serializeJson(jsonDoc, message);
client.publish(bmpTopic, 0, false, message);
}
void setup() {
client.onConnect([](bool sessionPresent) {
tPublishConfig.enable();
tPublish.enableDelayed();
tPublishBmp.enable();
Serial.println("Connected to MQTT");

View File

@ -23,6 +23,7 @@ lib_deps =
arkhipenko/TaskScheduler@^3.4.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
adafruit/Adafruit BMP280 Library@^2.5.0
bblanchon/ArduinoJson@6.16.1
build_flags = -D IR=0 -D WIFI_ALWAYS_ON=1
[env:laptop_home]