64 lines
1.6 KiB
C++

#if IR
#include <queue>
#include <AsyncMqttClient.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#define IR_INPUT_PIN 12
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
namespace Ir {
IRrecv irrecv(IR_INPUT_PIN);
decode_results results;
AsyncMqttClient mqttClient;
std::queue<uint8_t> commands;
uint8_t lastCommand = 0x9F;
void setup() {
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
Serial.println("Connecting to MQTT...");
mqttClient.connect();
irrecv.enableIRIn(); // Start the receiver
}
void publishCommand() {
if (!commands.empty() && mqttClient.connected()) {
char message[32];
sprintf(message, "%X", commands.front());
if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
Serial.print(commands.front(), HEX);
Serial.println();
commands.pop();
}
}
}
bool readCommand() {
bool newCommand = false;
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print(F(" C=0x"));
Serial.print(results.command, HEX);
Serial.println();
lastCommand = results.command;
commands.push(results.command);
newCommand = true;
}
irrecv.resume(); // Receive the next value
}
return newCommand;
}
uint8_t getCurrentCommand() {
return commands.empty() ? 0 : commands.front();
}
}
#endif