move Ir logic into its own loop
This commit is contained in:
parent
eb29c2203d
commit
854b4a60b9
62
include/ir.h
62
include/ir.h
@ -5,7 +5,7 @@
|
|||||||
#include <IRremoteESP8266.h>
|
#include <IRremoteESP8266.h>
|
||||||
#include <IRrecv.h>
|
#include <IRrecv.h>
|
||||||
|
|
||||||
#define IR_INPUT_PIN 12
|
#define IR_INPUT_PIN D6
|
||||||
|
|
||||||
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
@ -14,20 +14,13 @@ namespace Ir {
|
|||||||
|
|
||||||
IRrecv irrecv(IR_INPUT_PIN);
|
IRrecv irrecv(IR_INPUT_PIN);
|
||||||
decode_results results;
|
decode_results results;
|
||||||
|
bool avrOn = false;
|
||||||
|
|
||||||
AsyncMqttClient mqttClient;
|
AsyncMqttClient mqttClient;
|
||||||
|
|
||||||
std::queue<uint8_t> commands;
|
std::queue<uint8_t> commands;
|
||||||
uint8_t lastCommand = 0x9F;
|
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() {
|
void publishCommand() {
|
||||||
if (!commands.empty() && mqttClient.connected()) {
|
if (!commands.empty() && mqttClient.connected()) {
|
||||||
char message[32];
|
char message[32];
|
||||||
@ -59,6 +52,57 @@ namespace Ir {
|
|||||||
uint8_t getCurrentCommand() {
|
uint8_t getCurrentCommand() {
|
||||||
return commands.empty() ? 0 : commands.front();
|
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
|
#endif
|
||||||
@ -57,4 +57,4 @@ public:
|
|||||||
Serial.print(rssi);
|
Serial.print(rssi);
|
||||||
Serial.println(" dBm");
|
Serial.println(" dBm");
|
||||||
}
|
}
|
||||||
};
|
} wifi;
|
||||||
@ -20,10 +20,14 @@ lib_deps =
|
|||||||
jchristensen/Timezone@^1.2.4
|
jchristensen/Timezone@^1.2.4
|
||||||
ottowinter/AsyncMqttClient-esphome@^0.8.5
|
ottowinter/AsyncMqttClient-esphome@^0.8.5
|
||||||
crankyoldgit/IRremoteESP8266@^2.7.18
|
crankyoldgit/IRremoteESP8266@^2.7.18
|
||||||
build_flags = -D IR=0
|
|
||||||
|
[env:dev_mode]
|
||||||
|
build_flags = -D IR=1
|
||||||
|
|
||||||
[env:laptop_home]
|
[env:laptop_home]
|
||||||
|
build_flags = -D IR=0
|
||||||
|
|
||||||
[env:ota_home]
|
[env:ota_home]
|
||||||
|
build_flags = -D IR=0
|
||||||
upload_port = 192.168.5.191
|
upload_port = 192.168.5.191
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
|
|||||||
@ -8,9 +8,6 @@
|
|||||||
#define STAY_CONNECTED_AFTER_BOOT 5*60
|
#define STAY_CONNECTED_AFTER_BOOT 5*60
|
||||||
|
|
||||||
int currentHour = -1;
|
int currentHour = -1;
|
||||||
bool avrOn = false;
|
|
||||||
|
|
||||||
Wifi wifi;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Display::setup();
|
Display::setup();
|
||||||
@ -24,47 +21,12 @@ void setup() {
|
|||||||
Ntp::setup();
|
Ntp::setup();
|
||||||
Display::adjustBrightness();
|
Display::adjustBrightness();
|
||||||
|
|
||||||
#if IR
|
|
||||||
Ir::setup();
|
Ir::setup();
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
#if IR
|
Ir::loop();
|
||||||
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
|
|
||||||
if ((currentHour != hour())) {
|
if ((currentHour != hour())) {
|
||||||
Display::adjustBrightness();
|
Display::adjustBrightness();
|
||||||
wifi.reconnect();
|
wifi.reconnect();
|
||||||
@ -76,7 +38,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Ota::loop();
|
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();
|
wifi.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user