43 lines
1.1 KiB
C++

#include <queue>
#include <AsyncMqttClient.h>
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
namespace Mqtt {
void publishCommand();
Task tPublish(TASK_SECOND, TASK_FOREVER, Mqtt::publishCommand, &ts);
AsyncMqttClient client;
std::queue<uint8_t> commands;
void publishCommand() {
if (!commands.empty() && client.connected()) {
char message[32];
sprintf(message, "%X", commands.front());
if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
Serial.print(commands.front(), HEX);
Serial.println();
commands.pop();
}
}
}
uint8_t getCurrentCommand() {
return commands.empty() ? 0 : commands.front();
}
void setup() {
client.onConnect([](bool sessionPresent) {
tPublish.enableDelayed();
});
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
tPublish.disable();
});
client.setServer(MQTT_HOST, MQTT_PORT);
Serial.println("Connecting to MQTT...");
}
}