108 lines
2.7 KiB
C++
108 lines
2.7 KiB
C++
#if IR
|
|
|
|
#include <queue>
|
|
#include <AsyncMqttClient.h>
|
|
#include <IRremoteESP8266.h>
|
|
#include <IRrecv.h>
|
|
|
|
#define IR_INPUT_PIN D6
|
|
|
|
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
|
#define MQTT_PORT 1883
|
|
|
|
namespace Ir {
|
|
|
|
IRrecv irrecv(IR_INPUT_PIN);
|
|
decode_results results;
|
|
bool avrOn = false;
|
|
|
|
AsyncMqttClient mqttClient;
|
|
|
|
std::queue<uint8_t> commands;
|
|
uint8_t lastCommand = 0x9F;
|
|
|
|
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();
|
|
}
|
|
|
|
void setup() {
|
|
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
|
Serial.println("Connecting to MQTT...");
|
|
mqttClient.connect();
|
|
|
|
irrecv.enableIRIn(); // Start the receiver
|
|
}
|
|
|
|
void loop() {
|
|
if (readCommand()) {
|
|
Display::displayValue(lastCommand);
|
|
// Display::displayColon(false);
|
|
delay(1000);
|
|
switch (lastCommand)
|
|
{
|
|
case 0x9F:
|
|
avrOn = false;
|
|
Ntp::timeAtStartup = now();
|
|
break;
|
|
case 0xC4:
|
|
case 0xD0:
|
|
case 0xC0:
|
|
avrOn = true;
|
|
if (WiFi.status() == WL_DISCONNECTED) {
|
|
wifi.reconnect();
|
|
// connect on wifi connected
|
|
mqttClient.connect();
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
if (!avrOn && getCurrentCommand() == 0xC7) {
|
|
Display::changeBrightness(true);
|
|
commands.pop();
|
|
}
|
|
if (!avrOn && getCurrentCommand() == 0xC8) {
|
|
Display::changeBrightness(false);
|
|
commands.pop();
|
|
}
|
|
publishCommand();
|
|
}
|
|
}
|
|
|
|
#else
|
|
namespace Ir {
|
|
bool avrOn = false;
|
|
|
|
void setup() {}
|
|
void loop() {}
|
|
}
|
|
#endif |