move Ir logic into its own loop

This commit is contained in:
Nicu Hodos 2021-12-05 11:13:58 +01:00
parent eb29c2203d
commit 854b4a60b9
4 changed files with 62 additions and 52 deletions

View File

@ -5,7 +5,7 @@
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#define IR_INPUT_PIN 12
#define IR_INPUT_PIN D6
#define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883
@ -14,20 +14,13 @@ 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 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];
@ -59,6 +52,57 @@ namespace Ir {
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

View File

@ -57,4 +57,4 @@ public:
Serial.print(rssi);
Serial.println(" dBm");
}
};
} wifi;

View File

@ -20,10 +20,14 @@ lib_deps =
jchristensen/Timezone@^1.2.4
ottowinter/AsyncMqttClient-esphome@^0.8.5
crankyoldgit/IRremoteESP8266@^2.7.18
build_flags = -D IR=0
[env:dev_mode]
build_flags = -D IR=1
[env:laptop_home]
build_flags = -D IR=0
[env:ota_home]
build_flags = -D IR=0
upload_port = 192.168.5.191
upload_protocol = espota

View File

@ -8,9 +8,6 @@
#define STAY_CONNECTED_AFTER_BOOT 5*60
int currentHour = -1;
bool avrOn = false;
Wifi wifi;
void setup() {
Display::setup();
@ -24,47 +21,12 @@ void setup() {
Ntp::setup();
Display::adjustBrightness();
#if IR
Ir::setup();
#endif
}
void loop() {
#if IR
if (Ir::readCommand()) {
Display::displayValue(Ir::lastCommand);
Display::displayColon(false);
delay(1000);
switch (Ir::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
Ir::mqttClient.connect();
}
break;
default:
break;
}
}
if (!avrOn && Ir::getCurrentCommand() == 0xC7) {
Display::changeBrightness(true);
Ir::commands.pop();
}
if (!avrOn && Ir::getCurrentCommand() == 0xC8) {
Display::changeBrightness(false);
Ir::commands.pop();
}
Ir::publishCommand();
#endif
Ir::loop();
if ((currentHour != hour())) {
Display::adjustBrightness();
wifi.reconnect();
@ -76,7 +38,7 @@ void loop() {
}
if (WiFi.status() == WL_CONNECTED) {
Ota::loop();
if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !avrOn) {
if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) {
wifi.disconnect();
}
}