61 lines
1.4 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
IRrecv irrecv(IR_INPUT_PIN);
decode_results results;
AsyncMqttClient mqttClient;
std::queue<uint8_t> irCommands;
uint8_t lastIrCommand = 0x9F;
void setupIr() {
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
Serial.println("Connecting to MQTT...");
mqttClient.connect();
irrecv.enableIRIn(); // Start the receiver
}
void sendCommand() {
if (!irCommands.empty() && mqttClient.connected()) {
char message[32];
sprintf(message, "%X", irCommands.front());
if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
Serial.print(irCommands.front(), HEX);
Serial.println();
irCommands.pop();
}
}
}
bool readIrCommand() {
bool newCommand = false;
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print(F(" C=0x"));
Serial.print(results.command, HEX);
Serial.println();
lastIrCommand = results.command;
irCommands.push(results.command);
newCommand = true;
}
irrecv.resume(); // Receive the next value
}
return newCommand;
}
uint8_t getCurrentCommand() {
return irCommands.empty() ? 0 : irCommands.front();
}
#endif