#if IR #include #include #include #include #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 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