control led over mqtt
This commit is contained in:
parent
2b5fa8249e
commit
949b4f40d1
@ -76,4 +76,13 @@ namespace Ha {
|
||||
jsonDoc["command_topic"] = topic;
|
||||
serializeJson(jsonDoc, output);
|
||||
}
|
||||
|
||||
void buildLedConfig(char(&output)[JSON_SIZE], const char* topic) {
|
||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||
buildDeviceConfig(jsonDoc);
|
||||
jsonDoc["name"] = "ESP Clock Led";
|
||||
jsonDoc["unique_id"] = "esp_clock_led";
|
||||
jsonDoc["command_topic"] = topic;
|
||||
serializeJson(jsonDoc, output);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,30 +36,33 @@ namespace Mqtt {
|
||||
|
||||
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
|
||||
const char* restartTopic = "homeassistant/button/esp_clock/restart";
|
||||
const char* ledTopic = "homeassistant/switch/esp_clock/led/set";
|
||||
const char* espClockTopic = "homeassistant/+/esp_clock/#";
|
||||
|
||||
void connect() {
|
||||
client.connect();
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
client.unsubscribe(espClockTopic);
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
void publishTempConfig() {
|
||||
char message[JSON_SIZE];
|
||||
Ha::buildSensorConfig(message, Ha::TemperatureConfig{"Living room Temperature", "living_room_temperature", bmpTopic});
|
||||
Ha::buildSensorConfig(message, Ha::TemperatureConfig{ "Living room Temperature", "living_room_temperature", bmpTopic });
|
||||
client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message);
|
||||
}
|
||||
|
||||
void publishPressureConfig() {
|
||||
char message[JSON_SIZE];
|
||||
Ha::buildSensorConfig(message, Ha::PressureConfig{"Living room Pressure", "living_room_pressure", bmpTopic});
|
||||
Ha::buildSensorConfig(message, Ha::PressureConfig{ "Living room Pressure", "living_room_pressure", bmpTopic });
|
||||
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message);
|
||||
}
|
||||
|
||||
void publishAltitudeConfig() {
|
||||
char message[JSON_SIZE];
|
||||
Ha::buildSensorConfig(message, Ha::AltitudeConfig{"Living room Altitude", "living_room_altitude", bmpTopic});
|
||||
Ha::buildSensorConfig(message, Ha::AltitudeConfig{ "Living room Altitude", "living_room_altitude", bmpTopic });
|
||||
client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message);
|
||||
}
|
||||
|
||||
@ -69,12 +72,18 @@ namespace Mqtt {
|
||||
client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message);
|
||||
}
|
||||
|
||||
void publishLedConfig() {
|
||||
char message[JSON_SIZE];
|
||||
Ha::buildLedConfig(message, ledTopic);
|
||||
client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message);
|
||||
}
|
||||
|
||||
void publishConfig() {
|
||||
publishTempConfig();
|
||||
publishPressureConfig();
|
||||
publishAltitudeConfig();
|
||||
publishRestartConfig();
|
||||
client.subscribe(restartTopic, 0);
|
||||
publishLedConfig();
|
||||
ts.deleteTask(tPublishConfig);
|
||||
}
|
||||
|
||||
@ -102,9 +111,19 @@ namespace Mqtt {
|
||||
}
|
||||
|
||||
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if (String{ restartTopic }.equals(topic) && String { payload }.equals("PRESS")) {
|
||||
char msg[len + 1];
|
||||
memcpy(msg, payload, len);
|
||||
msg[len] = 0;
|
||||
if (String{ restartTopic }.equals(topic) && String { "PRESS" }.equals(msg)) {
|
||||
ESP.restart();
|
||||
}
|
||||
if (String{ ledTopic }.equals(topic)) {
|
||||
if (String{ "ON" }.equals(msg)) {
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
} else {
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@ -112,6 +131,7 @@ namespace Mqtt {
|
||||
tPublishConfig.enable();
|
||||
tPublishBmp.enableIfNot();
|
||||
tPublishCommand.enableDelayed();
|
||||
client.subscribe(espClockTopic, 0);
|
||||
tReConnect.disable();
|
||||
Serial.println("Connected to MQTT");
|
||||
});
|
||||
|
||||
@ -18,7 +18,7 @@ lib_deps =
|
||||
adafruit/Adafruit GFX Library@^1.10.12
|
||||
adafruit/Adafruit BusIO@^1.9.8
|
||||
jchristensen/Timezone@^1.2.4
|
||||
ottowinter/AsyncMqttClient-esphome@^0.8.5
|
||||
marvinroger/AsyncMqttClient@^0.9.0
|
||||
crankyoldgit/IRremoteESP8266@^2.7.18
|
||||
arkhipenko/TaskScheduler@^3.7.0
|
||||
adafruit/Adafruit Unified Sensor @ ^1.1.4
|
||||
@ -29,7 +29,7 @@ build_flags = -D IR=0 -D WIFI_ALWAYS_ON=1
|
||||
[env:laptop_home]
|
||||
|
||||
[env:ota_home]
|
||||
upload_port = esp-clock
|
||||
upload_port = 192.168.6.191
|
||||
upload_protocol = espota
|
||||
upload_flags =
|
||||
--host_port=10000
|
||||
upload_flags =
|
||||
--host_port=10000
|
||||
|
||||
@ -26,6 +26,9 @@ void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
Display::setup();
|
||||
Ota::setup();
|
||||
Ntp::setup();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user