57 lines
1.5 KiB
C++
57 lines
1.5 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> queue;
|
|
uint8_t getCurrent() {
|
|
return queue.empty() ? 0 : queue.front();
|
|
}
|
|
void push(uint8_t el) {
|
|
queue.push(el);
|
|
}
|
|
void pop() {
|
|
queue.pop();
|
|
}
|
|
uint8_t front() {
|
|
return queue.front();
|
|
}
|
|
} commands;
|
|
|
|
void publishCommand() {
|
|
if (!commands.queue.empty() && client.connected()) {
|
|
char message[32];
|
|
uint8_t cmd = commands.queue.front();
|
|
sprintf(message, "%X", cmd);
|
|
if (client.publish(commands.topic, 0, true, message) != 0) {
|
|
Serial.print(cmd, HEX);
|
|
Serial.println();
|
|
commands.queue.pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
client.onConnect([](bool sessionPresent) {
|
|
tPublish.enableDelayed();
|
|
Serial.println("Connected to MQTT");
|
|
});
|
|
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
|
tPublish.disable();
|
|
Serial.println("Disconnected from MQTT");
|
|
});
|
|
client.setServer(MQTT_HOST, MQTT_PORT);
|
|
Serial.println("Connecting to MQTT...");
|
|
}
|
|
}
|