55 lines
1.4 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;
struct {
const char* topic = "esp_clock/sensor/ir/value";
std::queue<uint8_t> list;
uint8_t getCurrent() {
return list.empty() ? 0 : list.front();
}
void push(uint8_t el) {
list.push(el);
}
void pop() {
list.pop();
}
uint8_t front() {
return list.front();
}
} commands;
void publishCommand() {
if (!commands.list.empty() && client.connected()) {
char message[32];
uint8_t cmd = commands.front();
sprintf(message, "%X", cmd);
if (client.publish(commands.topic, 0, true, message) != 0) {
Serial.print(cmd, HEX);
Serial.println();
commands.pop();
}
}
}
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...");
}
}