remove IR specific code

This commit is contained in:
Nicu Hodos 2024-01-03 13:23:14 +01:00
parent dbc0eb7703
commit 9957ec6227
4 changed files with 1 additions and 138 deletions

View File

@ -1,102 +0,0 @@
#if IR
#include <AsyncMqttClient.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include "display.h"
#define IR_INPUT_PIN D6
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
namespace Ir {
void loop();
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
IRrecv irrecv(IR_INPUT_PIN);
bool avrOn = false;
uint8_t readCommand() {
uint8_t lastCommand = 0;
decode_results results;
if (irrecv.decode(&results)) {
if (results.decode_type == NEC && results.command != 0) {
Serial.print(F(" C=0x"));
Serial.print(results.command, HEX);
Serial.println();
lastCommand = results.command;
}
irrecv.resume(); // Receive the next value
}
return lastCommand;
}
void command(const char c[]) {
avrOn = true;
tCheckWifi.cancel();
Wifi::reconnect();
Display::displayText(c);
}
void loop() {
if (uint8_t lastCommand = readCommand()) {
switch (lastCommand)
{
case 0x9F:
avrOn = false;
tCheckWifi.restartDelayed();
Display::displayText("Off");
Mqtt::commands.push(lastCommand);
break;
case 0x12:
Display::displayText("Slp");
Mqtt::commands.push(lastCommand);
break;
case 0xC1:
Display::displayText("Mute");
Mqtt::commands.push(lastCommand);
break;
case 0xC4:
command("Play");
Mqtt::commands.push(lastCommand);
break;
case 0xC7:
avrOn ? Display::displayText(" Up") : Display::changeBrightness(true);
break;
case 0xC8:
avrOn ? Display::displayText(" Dn") : Display::changeBrightness(false);
break;
case 0xD0:
command("Stop");
Mqtt::commands.push(lastCommand);
break;
case 0xC0:
command("On");
Mqtt::commands.push(lastCommand);
break;
case 0x84:
Display::displayTemp(Bmp::data.readTemp());
break;
default:
Display::displayValue(lastCommand);
Mqtt::commands.push(lastCommand);
break;
}
}
}
void setup() {
irrecv.enableIRIn(); // Start the receiver
}
}
#else
namespace Ir {
bool avrOn = false;
void setup() {}
void loop() {}
}
#endif

View File

@ -1,6 +1,5 @@
#pragma once
#include <queue>
#include <AsyncMqttClient.h>
#include <ArduinoJson.h>
#include "ha.h"
@ -13,30 +12,14 @@
namespace Mqtt {
void publishInit();
void publishCommand();
void publishBmp280();
void connect();
void disconnect();
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts);
Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts);
Task tPublishCommand(TASK_SECOND, TASK_FOREVER, 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) {
if (client.connected()) queue.push(el);
}
void pop() {
queue.pop();
}
} commands;
const char* espClockTopic = "homeassistant/+/esp_clock/#";
void connect() {
@ -128,18 +111,6 @@ namespace Mqtt {
publish(Ha::Sensor::stateTopic, message);
}
void publishCommand() {
if (uint8_t cmd = commands.getCurrent()) {
char message[32];
sprintf(message, "%X", cmd);
if (publish(commands.topic, message) != 0) {
Serial.print(cmd, HEX);
Serial.println();
commands.queue.pop();
}
}
}
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
char msg[len + 1];
memcpy(msg, payload, len);
@ -157,14 +128,12 @@ namespace Mqtt {
Display::onHourFormatChanged(hourFormatMqtt->publishState);
client.onConnect([](bool sessionPresent) {
tPublishInit.enable();
tPublishCommand.enableDelayed();
client.subscribe(espClockTopic, 0);
tReConnect.disable();
Serial.println("Connected to MQTT");
});
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
tReConnect.enableDelayed();
tPublishCommand.disable();
Serial.println("Disconnected from MQTT");
});
client.onMessage(onMessage);

View File

@ -19,12 +19,11 @@ lib_deps =
adafruit/Adafruit BusIO@^1.9.8
jchristensen/Timezone@^1.2.4
marvinroger/AsyncMqttClient@^0.9.0
crankyoldgit/IRremoteESP8266@^2.7.18
arkhipenko/TaskScheduler@^3.7.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
adafruit/Adafruit BMP280 Library@^2.5.0
bblanchon/ArduinoJson@6.16.1
build_flags = -D IR=0 -D WIFI_ALWAYS_ON=1
build_flags = -D WIFI_ALWAYS_ON=1
[env:laptop_home]

View File

@ -17,7 +17,6 @@ Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
#include "bmp.h"
#include "ntp_time.h"
#include "ota.h"
#include "ir.h"
#define BUTTON D3
@ -49,7 +48,6 @@ void setup() {
Display::setup();
Ota::setup();
Ntp::setup();
Ir::setup();
Mqtt::setup();
Bmp::setup();
@ -70,7 +68,6 @@ void onWifiConnected() {
Serial.println("Wifi connected event");
Wifi::printStatus();
Ota::tLoop.enable();
if (!Ir::avrOn) tCheckWifi.restartDelayed();
Mqtt::connect();
if (time_t newTime = Ntp::updateTime()) {
Serial.println(asctime(localtime(&newTime)));