Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion firmware/esp32/splitflap/mqtt_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mqtt_task.h"
#include "secrets.h"

bool forceFullRotation = 1;

MQTTTask::MQTTTask(SplitflapTask& splitflap_task, Logger& logger, const uint8_t task_core) :
Task("MQTT", 8192, 1, task_core),
Expand Down Expand Up @@ -45,7 +46,15 @@ void MQTTTask::mqttCallback(char *topic, byte *payload, unsigned int length) {
char buf[256];
snprintf(buf, sizeof(buf), "Received mqtt callback for topic %s, length %u", topic, length);
logger_.log(buf);
splitflap_task_.showString((const char *)payload, length);
if (strcmp(topic, MQTT_COMMAND_TOPIC) == 0) {
splitflap_task_.showString((const char *)payload, length, forceFullRotation);
} else if (strcmp(topic, MQTT_FORCE_FULL_ROTATION_COMMAND_TOPIC) == 0) {
if (length == 2) { // ON
forceFullRotation = 1;
} else {
forceFullRotation = 0; // OFF
}
}
}

void MQTTTask::connectMQTT() {
Expand All @@ -58,6 +67,15 @@ void MQTTTask::connectMQTT() {
char buf[256];
snprintf(buf, sizeof(buf), "{\"name\": \"%s\", \"command_topic\": \"%s\", \"state_topic\": \"%s\", \"unique_id\": \"%s\"}", HOSTNAME, MQTT_COMMAND_TOPIC, MQTT_COMMAND_TOPIC, HOSTNAME);
mqtt_client_.publish("homeassistant/text/splitflap/config", buf);

mqtt_client_.subscribe(MQTT_FORCE_FULL_ROTATION_COMMAND_TOPIC);

snprintf(buf, sizeof(buf), "{\"name\": \"%s\", \"command_topic\": \"%s\", \"state_topic\": \"%s\", \"unique_id\": \"%s\"}", "Splitflap force full rotation", MQTT_FORCE_FULL_ROTATION_COMMAND_TOPIC, MQTT_FORCE_FULL_ROTATION_COMMAND_TOPIC, HOSTNAME);
mqtt_client_.publish("homeassistant/switch/splitflap/config", buf);

snprintf(buf, sizeof(buf), "ON");
mqtt_client_.publish("homeassistant/switch/splitflap/state", buf);

logger_.log("Published MQTT discovery message");
} else {
snprintf(buf, sizeof(buf), "MQTT failed rc=%d will try again in 5 seconds", mqtt_client_.state());
Expand Down
1 change: 1 addition & 0 deletions firmware/esp32/splitflap/secrets.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
#define MQTT_USER "mqttuser"
#define MQTT_PASSWORD "megasecretpassword"
#define MQTT_COMMAND_TOPIC "homeassistant/text/splitflap/state"
#define MQTT_FORCE_FULL_ROTATION_COMMAND_TOPIC "homeassistant/switch/splitflap/state"