From cafe4ec84fe509cb58c275acf995ba7b5b5e5bb9 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Mon, 6 Dec 2021 16:00:52 +0100 Subject: [PATCH 01/15] encapsulate commands into struct --- include/ir.h | 4 ++-- include/mqtt.h | 30 +++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/ir.h b/include/ir.h index 7161f78..d1938bb 100644 --- a/include/ir.h +++ b/include/ir.h @@ -56,11 +56,11 @@ namespace Ir { break; } } - if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) { + if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) { Display::changeBrightness(true); Mqtt::commands.pop(); } - if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) { + if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) { Display::changeBrightness(false); Mqtt::commands.pop(); } diff --git a/include/mqtt.h b/include/mqtt.h index 06e9590..603e4ab 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -11,24 +11,36 @@ namespace Mqtt { AsyncMqttClient client; - std::queue commands; + struct { + const char* topic = "esp_clock/sensor/ir/value"; + std::queue list; + uint8_t getCurrent() { + return list.empty() ? 0 : list.front(); + } + void push(uint8_t el) { + list.push(el); + } + void pop() { + list.pop(); + } + uint8_t front() { + return list.front(); + } + } commands; void publishCommand() { - if (!commands.empty() && client.connected()) { + if (!commands.list.empty() && client.connected()) { char message[32]; - sprintf(message, "%X", commands.front()); - if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { - Serial.print(commands.front(), HEX); + uint8_t cmd = commands.front(); + sprintf(message, "%X", cmd); + if (client.publish(commands.topic, 0, true, message) != 0) { + Serial.print(cmd, HEX); Serial.println(); commands.pop(); } } } - uint8_t getCurrentCommand() { - return commands.empty() ? 0 : commands.front(); - } - void setup() { client.onConnect([](bool sessionPresent) { tPublish.enableDelayed(); From a1151db62a5868f1f7ae7430c47ca9ba5ca9cd6b Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Mon, 6 Dec 2021 16:06:42 +0100 Subject: [PATCH 02/15] and change name to queue --- include/mqtt.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index 603e4ab..f219753 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -13,30 +13,30 @@ namespace Mqtt { struct { const char* topic = "esp_clock/sensor/ir/value"; - std::queue list; + std::queue queue; uint8_t getCurrent() { - return list.empty() ? 0 : list.front(); + return queue.empty() ? 0 : queue.front(); } void push(uint8_t el) { - list.push(el); + queue.push(el); } void pop() { - list.pop(); + queue.pop(); } uint8_t front() { - return list.front(); + return queue.front(); } } commands; void publishCommand() { - if (!commands.list.empty() && client.connected()) { + if (!commands.queue.empty() && client.connected()) { char message[32]; - uint8_t cmd = commands.front(); + uint8_t cmd = commands.queue.front(); sprintf(message, "%X", cmd); if (client.publish(commands.topic, 0, true, message) != 0) { Serial.print(cmd, HEX); Serial.println(); - commands.pop(); + commands.queue.pop(); } } } From 4f9d75034a8ce8682e9dffe510d72a73a4d3bf3e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 6 Dec 2021 22:54:01 +0100 Subject: [PATCH 03/15] small improvements --- include/display.h | 8 +++++--- include/ir.h | 3 +-- include/mqtt.h | 2 ++ platformio.ini | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/display.h b/include/display.h index b70ab70..2b8876c 100644 --- a/include/display.h +++ b/include/display.h @@ -5,7 +5,8 @@ #include "ntp_time.h" #define DISPLAY_ADDRESS 0x70 -#define BRIGHTNESS 1 +#define BRIGHTNESS 0 +#define BRIGHTNESS_STEP 1 namespace Display { @@ -36,7 +37,7 @@ namespace Display { } void changeBrightness(bool increase) { - increase ? brightness = (brightness + 2) % 15 : brightness = (brightness - 2) % 15; + increase ? brightness = (brightness + BRIGHTNESS_STEP) % 15 : brightness = (brightness - BRIGHTNESS_STEP) % 15; clockDisplay.setBrightness(brightness); } @@ -44,7 +45,7 @@ namespace Display { if (currentHour > 8 && currentHour < 17) { brightness = 11; } else { - brightness = 1; + brightness = BRIGHTNESS; } clockDisplay.setBrightness(brightness); } @@ -72,6 +73,7 @@ namespace Display { void displayValue(int value) { clockDisplay.print(value, HEX); + clockDisplay.writeDisplay(); } void setup() { diff --git a/include/ir.h b/include/ir.h index d1938bb..f81317c 100644 --- a/include/ir.h +++ b/include/ir.h @@ -21,7 +21,7 @@ namespace Ir { bool readCommand() { bool newCommand = false; if (irrecv.decode(&results)) { - if (results.decode_type == NEC) { + if (results.decode_type == NEC && results.command != 0) { Serial.print(F(" C=0x")); Serial.print(results.command, HEX); Serial.println(); @@ -40,7 +40,6 @@ namespace Ir { void loop() { if (readCommand()) { - Display::displayValue(lastCommand); switch (lastCommand) { case 0x9F: diff --git a/include/mqtt.h b/include/mqtt.h index f219753..d806042 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -44,9 +44,11 @@ namespace Mqtt { void setup() { client.onConnect([](bool sessionPresent) { tPublish.enableDelayed(); + Serial.println("Connected to MQTT"); }); client.onDisconnect([](AsyncMqttClientDisconnectReason reason) { tPublish.disable(); + Serial.println("Disconnected from MQTT"); }); client.setServer(MQTT_HOST, MQTT_PORT); Serial.println("Connecting to MQTT..."); diff --git a/platformio.ini b/platformio.ini index b8e069d..22936ca 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,9 +26,9 @@ lib_deps = build_flags = -D IR=1 [env:laptop_home] -build_flags = -D IR=0 +build_flags = -D IR=1 [env:ota_home] -build_flags = -D IR=0 +build_flags = -D IR=1 upload_port = 192.168.5.191 upload_protocol = espota From 6b3df25fd541a06ab855991467d8954e9424baa7 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 7 Dec 2021 09:25:59 +0100 Subject: [PATCH 04/15] display command --- include/display.h | 6 +++--- include/ir.h | 9 +++++++++ src/esp_clock.cpp | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/display.h b/include/display.h index 2b8876c..a6e9c96 100644 --- a/include/display.h +++ b/include/display.h @@ -17,7 +17,7 @@ namespace Display { // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); - void displayTime() { + void adjustTime() { int displayHour = hour(); int displayMinute = minute(); int displayValue = displayHour * 100 + displayMinute; @@ -62,7 +62,7 @@ namespace Display { } if (currentMin != minute()) { currentMin = minute(); - displayTime(); + adjustTime(); } } clockDisplay.drawColon(colonOn); @@ -79,7 +79,7 @@ namespace Display { void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(brightness); - displayTime(); + adjustTime(); displayColon(); } } diff --git a/include/ir.h b/include/ir.h index f81317c..0969daf 100644 --- a/include/ir.h +++ b/include/ir.h @@ -34,12 +34,21 @@ namespace Ir { return newCommand; } + void displayValue() { + Display::displayValue(lastCommand); + tBlinkColon.set(500, TASK_FOREVER, Display::displayColon); + tBlinkColon.enableDelayed(); + Display::adjustTime(); + } + void setup() { irrecv.enableIRIn(); // Start the receiver } void loop() { if (readCommand()) { + tBlinkColon.set(1000, TASK_ONCE, displayValue); + tBlinkColon.forceNextIteration(); switch (lastCommand) { case 0x9F: diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 1092ba9..bf8f5e1 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -12,6 +12,7 @@ StatusRequest wifiConnected; #include "mqtt.h" #include "ota.h" #include "display.h" +Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true); #include "ir.h" #define STAY_CONNECTED_AFTER_BOOT 5*60 @@ -19,7 +20,6 @@ StatusRequest wifiConnected; void wifiConnectedCallback(); void otaCallback(); -Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true); Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts); Task tWifiReconnect(Wifi::reconnect, &ts); Task tWifiConnected(wifiConnectedCallback, &ts); From 176984dfa59d8feeaec15863a8963f6a653ff8d3 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 10:14:15 +0100 Subject: [PATCH 05/15] put Ir::loop into a task --- src/esp_clock.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index bf8f5e1..e0f4b9e 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -21,6 +21,7 @@ void wifiConnectedCallback(); void otaCallback(); Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts); +Task tIr(TASK_IMMEDIATE, TASK_FOREVER, Ir::loop, &ts); Task tWifiReconnect(Wifi::reconnect, &ts); Task tWifiConnected(wifiConnectedCallback, &ts); @@ -43,8 +44,6 @@ void setup() { } void loop() { - Ir::loop(); - ts.execute(); } From 3df68fbc8b9481c876499d9e7de0267ee8f9054c Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 10:36:32 +0100 Subject: [PATCH 06/15] refactor display value logic --- include/display.h | 5 +++++ include/ir.h | 9 ++++----- platformio.ini | 4 ---- src/esp_clock.cpp | 3 +-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/display.h b/include/display.h index a6e9c96..6caeb41 100644 --- a/include/display.h +++ b/include/display.h @@ -1,3 +1,5 @@ +#pragma once + #include // Support for the Backpack FeatherWing #include // Adafruit's graphics library #include @@ -10,6 +12,9 @@ namespace Display { + void displayColon(); + Task tDisplay(500, TASK_FOREVER, Display::displayColon, &ts, true); + uint8_t brightness = BRIGHTNESS; int currentHour = -1; int currentMin = -1; diff --git a/include/ir.h b/include/ir.h index 0969daf..1a14334 100644 --- a/include/ir.h +++ b/include/ir.h @@ -1,9 +1,9 @@ #if IR -#include #include #include #include +#include "display.h" #define IR_INPUT_PIN D6 @@ -36,8 +36,7 @@ namespace Ir { void displayValue() { Display::displayValue(lastCommand); - tBlinkColon.set(500, TASK_FOREVER, Display::displayColon); - tBlinkColon.enableDelayed(); + Display::tDisplay.setCallback(Display::displayColon); Display::adjustTime(); } @@ -47,8 +46,8 @@ namespace Ir { void loop() { if (readCommand()) { - tBlinkColon.set(1000, TASK_ONCE, displayValue); - tBlinkColon.forceNextIteration(); + Display::tDisplay.setCallback(displayValue); + Display::tDisplay.forceNextIteration(); switch (lastCommand) { case 0x9F: diff --git a/platformio.ini b/platformio.ini index 22936ca..3382a3c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,14 +21,10 @@ lib_deps = ottowinter/AsyncMqttClient-esphome@^0.8.5 crankyoldgit/IRremoteESP8266@^2.7.18 arkhipenko/TaskScheduler@^3.4.0 - -[env:dev_mode] build_flags = -D IR=1 [env:laptop_home] -build_flags = -D IR=1 [env:ota_home] -build_flags = -D IR=1 upload_port = 192.168.5.191 upload_protocol = espota diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index e0f4b9e..2869b64 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -7,12 +7,11 @@ StatusRequest hourChanged; StatusRequest dayChanged; StatusRequest wifiConnected; +#include "display.h" #include "wifi.h" #include "ntp_time.h" #include "mqtt.h" #include "ota.h" -#include "display.h" -Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true); #include "ir.h" #define STAY_CONNECTED_AFTER_BOOT 5*60 From 900cf664b4c04bdb03c4c03c9d3a1cc92e355cd6 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 10:53:43 +0100 Subject: [PATCH 07/15] move loop tasks in their namespaces --- include/ir.h | 11 +++++++---- include/ota.h | 8 +++++--- src/esp_clock.cpp | 14 +++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/ir.h b/include/ir.h index 1a14334..df8ea5e 100644 --- a/include/ir.h +++ b/include/ir.h @@ -12,6 +12,9 @@ namespace Ir { + void loop(); + Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); + IRrecv irrecv(IR_INPUT_PIN); decode_results results; bool avrOn = false; @@ -40,10 +43,6 @@ namespace Ir { Display::adjustTime(); } - void setup() { - irrecv.enableIRIn(); // Start the receiver - } - void loop() { if (readCommand()) { Display::tDisplay.setCallback(displayValue); @@ -72,6 +71,10 @@ namespace Ir { Mqtt::commands.pop(); } } + + void setup() { + irrecv.enableIRIn(); // Start the receiver + } } #else diff --git a/include/ota.h b/include/ota.h index 69b736f..b834a8a 100644 --- a/include/ota.h +++ b/include/ota.h @@ -1,7 +1,9 @@ #include -namespace Ota -{ +namespace Ota { + + void loop(); + Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); void setup() { ArduinoOTA.onStart([]() { @@ -25,6 +27,6 @@ namespace Ota } void loop() { - ArduinoOTA.handle(); + ArduinoOTA.handle(); } } diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 2869b64..085f3d9 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -17,10 +17,9 @@ StatusRequest wifiConnected; #define STAY_CONNECTED_AFTER_BOOT 5*60 void wifiConnectedCallback(); -void otaCallback(); +void checkWifiCallback(); -Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts); -Task tIr(TASK_IMMEDIATE, TASK_FOREVER, Ir::loop, &ts); +Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); Task tWifiReconnect(Wifi::reconnect, &ts); Task tWifiConnected(wifiConnectedCallback, &ts); @@ -52,14 +51,15 @@ void wifiConnectedCallback() { Serial.println(asctime(localtime(&newTime))); Ntp::lastConnectedTime = newTime; } - tOta.enable(); + Ota::tLoop.enable(); + tCheckWifi.enable(); Mqtt::client.connect(); } -void otaCallback() { - Ota::loop(); +void checkWifiCallback() { if ((difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) { Wifi::disconnect(); - tOta.disable(); + Ota::tLoop.disable(); + tCheckWifi.disable(); } } From 24dee5c4ec3ac62f931d288ebe9b3b5a0d7c2dea Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 11:36:24 +0100 Subject: [PATCH 08/15] optimise wifi disconnect check --- include/ir.h | 1 + src/esp_clock.cpp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/ir.h b/include/ir.h index df8ea5e..19fac88 100644 --- a/include/ir.h +++ b/include/ir.h @@ -51,6 +51,7 @@ namespace Ir { { case 0x9F: avrOn = false; + tCheckWifi.enable(); break; case 0xC4: case 0xD0: diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 085f3d9..bf993be 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -1,11 +1,15 @@ #include +void onWifiConnected(); +void checkWifiCallback(); + #define _TASK_STATUS_REQUEST #include Scheduler ts; StatusRequest hourChanged; StatusRequest dayChanged; StatusRequest wifiConnected; +Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); #include "display.h" #include "wifi.h" @@ -16,12 +20,8 @@ StatusRequest wifiConnected; #define STAY_CONNECTED_AFTER_BOOT 5*60 -void wifiConnectedCallback(); -void checkWifiCallback(); - -Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); Task tWifiReconnect(Wifi::reconnect, &ts); -Task tWifiConnected(wifiConnectedCallback, &ts); +Task tWifiConnected(onWifiConnected, &ts); void setup() { Serial.begin(9600); @@ -45,19 +45,19 @@ void loop() { ts.execute(); } -void wifiConnectedCallback() { +void onWifiConnected() { Wifi::printStatus(); if (time_t newTime = Ntp::updateTime()) { Serial.println(asctime(localtime(&newTime))); Ntp::lastConnectedTime = newTime; } Ota::tLoop.enable(); - tCheckWifi.enable(); + if (!Ir::avrOn) tCheckWifi.enable(); Mqtt::client.connect(); } void checkWifiCallback() { - if ((difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) && !Ir::avrOn) { + if (difftime(now(), Ntp::lastConnectedTime) > STAY_CONNECTED_AFTER_BOOT) { Wifi::disconnect(); Ota::tLoop.disable(); tCheckWifi.disable(); From 72b654bb7fb449c7186f4337ebfd0638f5903507 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 11:44:53 +0100 Subject: [PATCH 09/15] simplify wifi reconnect --- include/display.h | 2 +- src/esp_clock.cpp | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/include/display.h b/include/display.h index 6caeb41..9aac3b9 100644 --- a/include/display.h +++ b/include/display.h @@ -63,7 +63,7 @@ namespace Display { currentHour = hour(); Display::adjustBrightness(); hourChanged.signal(); - if (currentHour == 8) dayChanged.signal(); + if (currentHour == 8) Wifi::reconnect(); } if (currentMin != minute()) { currentMin = minute(); diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index bf993be..40bee75 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -1,18 +1,18 @@ #include -void onWifiConnected(); void checkWifiCallback(); +void onWifiConnected(); #define _TASK_STATUS_REQUEST #include Scheduler ts; StatusRequest hourChanged; -StatusRequest dayChanged; StatusRequest wifiConnected; Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); +Task tWifiConnected(onWifiConnected, &ts); -#include "display.h" #include "wifi.h" +#include "display.h" #include "ntp_time.h" #include "mqtt.h" #include "ota.h" @@ -20,9 +20,6 @@ Task tCheckWifi(5000, TASK_FOREVER, checkWifiCallback, &ts); #define STAY_CONNECTED_AFTER_BOOT 5*60 -Task tWifiReconnect(Wifi::reconnect, &ts); -Task tWifiConnected(onWifiConnected, &ts); - void setup() { Serial.begin(9600); @@ -33,9 +30,7 @@ void setup() { Mqtt::setup(); hourChanged.setWaiting(); - dayChanged.setWaiting(); wifiConnected.setWaiting(); - tWifiReconnect.waitFor(&dayChanged); tWifiConnected.waitFor(&wifiConnected); Wifi::setup(); From 50b2a11ec1c81afe39daa0e7d6418a5c5692c5b8 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Tue, 7 Dec 2021 14:32:46 +0100 Subject: [PATCH 10/15] add functionality to display text --- include/display.h | 5 +++++ include/ir.h | 12 ++++++++++-- platformio.ini | 6 +++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/display.h b/include/display.h index 9aac3b9..acff32e 100644 --- a/include/display.h +++ b/include/display.h @@ -81,6 +81,11 @@ namespace Display { clockDisplay.writeDisplay(); } + void displayText(const char c[]) { + clockDisplay.print(c); + clockDisplay.writeDisplay(); + } + void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(brightness); diff --git a/include/ir.h b/include/ir.h index 19fac88..7791f28 100644 --- a/include/ir.h +++ b/include/ir.h @@ -43,15 +43,21 @@ namespace Ir { Display::adjustTime(); } + void displayText(const char c[]) { + Display::tDisplay.disable(); + Display::displayText(c); + Display::adjustTime(); + Display::tDisplay.enableDelayed(1000); + } + void loop() { if (readCommand()) { - Display::tDisplay.setCallback(displayValue); - Display::tDisplay.forceNextIteration(); switch (lastCommand) { case 0x9F: avrOn = false; tCheckWifi.enable(); + displayText("off"); break; case 0xC4: case 0xD0: @@ -60,6 +66,8 @@ namespace Ir { Wifi::reconnect(); break; default: + Display::tDisplay.setCallback(displayValue); + Display::tDisplay.forceNextIteration(); break; } } diff --git a/platformio.ini b/platformio.ini index 3382a3c..a2fdefd 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,9 +14,9 @@ board = d1_mini framework = arduino lib_deps = arduino-libraries/NTPClient@^3.1.0 - adafruit/Adafruit LED Backpack Library@^1.1.8 - adafruit/Adafruit GFX Library@^1.10.7 - adafruit/Adafruit BusIO@^1.6.0 + adafruit/Adafruit LED Backpack Library@^1.3.2 + adafruit/Adafruit GFX Library@^1.10.12 + adafruit/Adafruit BusIO@^1.9.8 jchristensen/Timezone@^1.2.4 ottowinter/AsyncMqttClient-esphome@^0.8.5 crankyoldgit/IRremoteESP8266@^2.7.18 From 5346e3f5a6a3ec500d6c5fa492f7ada4d68f1da3 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 7 Dec 2021 20:20:03 +0100 Subject: [PATCH 11/15] display text commands --- include/display.h | 12 ++++++++---- include/ir.h | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/display.h b/include/display.h index acff32e..4cb5caa 100644 --- a/include/display.h +++ b/include/display.h @@ -55,9 +55,7 @@ namespace Display { clockDisplay.setBrightness(brightness); } - void displayColon() { - static bool colonOn = false; - + void displayColon(bool colonOn) { if (colonOn) { if (currentHour != hour()) { currentHour = hour(); @@ -71,6 +69,12 @@ namespace Display { } } clockDisplay.drawColon(colonOn); + } + + void displayColon() { + static bool colonOn = false; + + displayColon(colonOn); clockDisplay.writeDisplay(); colonOn = !colonOn; @@ -82,7 +86,7 @@ namespace Display { } void displayText(const char c[]) { - clockDisplay.print(c); + clockDisplay.println(c); clockDisplay.writeDisplay(); } diff --git a/include/ir.h b/include/ir.h index 7791f28..95acb36 100644 --- a/include/ir.h +++ b/include/ir.h @@ -38,13 +38,16 @@ namespace Ir { } void displayValue() { + Display::tDisplay.disable(); + Display::displayColon(false); Display::displayValue(lastCommand); - Display::tDisplay.setCallback(Display::displayColon); Display::adjustTime(); + Display::tDisplay.enableDelayed(1000); } void displayText(const char c[]) { Display::tDisplay.disable(); + Display::displayColon(false); Display::displayText(c); Display::adjustTime(); Display::tDisplay.enableDelayed(1000); @@ -57,7 +60,7 @@ namespace Ir { case 0x9F: avrOn = false; tCheckWifi.enable(); - displayText("off"); + displayText("Off"); break; case 0xC4: case 0xD0: @@ -66,16 +69,17 @@ namespace Ir { Wifi::reconnect(); break; default: - Display::tDisplay.setCallback(displayValue); - Display::tDisplay.forceNextIteration(); + displayValue(); break; } } if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) { + displayText("Up"); Display::changeBrightness(true); Mqtt::commands.pop(); } if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) { + displayText("Down"); Display::changeBrightness(false); Mqtt::commands.pop(); } From 7cca316b531b817c4c3b554c12a29110c8ff0b5d Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 7 Dec 2021 22:48:41 +0100 Subject: [PATCH 12/15] add bmp --- include/bmp.h | 40 ++++++++++++++++++++++++++++++++++++++++ include/display.h | 5 +++++ platformio.ini | 2 ++ src/esp_clock.cpp | 6 +++++- 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 include/bmp.h diff --git a/include/bmp.h b/include/bmp.h new file mode 100644 index 0000000..06264a4 --- /dev/null +++ b/include/bmp.h @@ -0,0 +1,40 @@ +#include + +namespace Bmp { + + Adafruit_BMP280 bmp; // I2C Interface + + void setup() { + Serial.begin(9600); + Serial.println(F("BMP280 test")); + + if (!bmp.begin(0x76)) { + Serial.println(F("Could not find a valid BMP280 sensor, check wiring!")); + while (1); + } + + /* Default settings from datasheet. */ + bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ + Adafruit_BMP280::FILTER_X16, /* Filtering. */ + Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ + } + + void loop() { + Serial.print(F("Temperature = ")); + Serial.print(bmp.readTemperature()); + Serial.println(" *C"); + + Serial.print(F("Pressure = ")); + Serial.print(bmp.readPressure() / 100); //displaying the Pressure in hPa, you can change the unit + Serial.println(" hPa"); + + Serial.print(F("Approx altitude = ")); + Serial.print(bmp.readAltitude(1019.66)); //The "1019.66" is the pressure(hPa) at sea level in day in your region + Serial.println(" m"); //If you don't know it, modify it until you get your current altitude + + Serial.println(); + delay(2000); + } +} \ No newline at end of file diff --git a/include/display.h b/include/display.h index 4cb5caa..7a266eb 100644 --- a/include/display.h +++ b/include/display.h @@ -80,6 +80,11 @@ namespace Display { colonOn = !colonOn; } + void displayFloat(float value) { + clockDisplay.print(value); + clockDisplay.writeDisplay(); + } + void displayValue(int value) { clockDisplay.print(value, HEX); clockDisplay.writeDisplay(); diff --git a/platformio.ini b/platformio.ini index a2fdefd..9530ec0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,6 +21,8 @@ lib_deps = ottowinter/AsyncMqttClient-esphome@^0.8.5 crankyoldgit/IRremoteESP8266@^2.7.18 arkhipenko/TaskScheduler@^3.4.0 + adafruit/Adafruit Unified Sensor @ ^1.1.4 + adafruit/Adafruit BMP280 Library@^2.5.0 build_flags = -D IR=1 [env:laptop_home] diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 40bee75..cfc19ce 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -17,8 +17,9 @@ Task tWifiConnected(onWifiConnected, &ts); #include "mqtt.h" #include "ota.h" #include "ir.h" +#include "bmp.h" -#define STAY_CONNECTED_AFTER_BOOT 5*60 +#define STAY_CONNECTED_AFTER_BOOT 30*60 void setup() { Serial.begin(9600); @@ -28,6 +29,9 @@ void setup() { Ntp::setup(); Ir::setup(); Mqtt::setup(); + Bmp::setup(); + Display::displayFloat(Bmp::bmp.readTemperature()); + delay(3000); hourChanged.setWaiting(); wifiConnected.setWaiting(); From d3a44cd7d87dfbdff2db9641c576cc6b2f8dfa20 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 7 Dec 2021 20:20:03 +0100 Subject: [PATCH 13/15] display text commands --- include/display.h | 24 ++++++++++++++++++------ include/ir.h | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/include/display.h b/include/display.h index acff32e..cad554f 100644 --- a/include/display.h +++ b/include/display.h @@ -13,7 +13,7 @@ namespace Display { void displayColon(); - Task tDisplay(500, TASK_FOREVER, Display::displayColon, &ts, true); + Task tDisplay(500, TASK_FOREVER, displayColon, &ts, true); uint8_t brightness = BRIGHTNESS; int currentHour = -1; @@ -55,9 +55,7 @@ namespace Display { clockDisplay.setBrightness(brightness); } - void displayColon() { - static bool colonOn = false; - + void displayColon(bool colonOn) { if (colonOn) { if (currentHour != hour()) { currentHour = hour(); @@ -71,19 +69,33 @@ namespace Display { } } clockDisplay.drawColon(colonOn); + } + + void displayColon() { + static bool colonOn = false; + + displayColon(colonOn); clockDisplay.writeDisplay(); colonOn = !colonOn; } - void displayValue(int value) { + void displayValue(uint8_t value) { + tDisplay.disable(); + displayColon(false); clockDisplay.print(value, HEX); clockDisplay.writeDisplay(); + adjustTime(); + tDisplay.enableDelayed(1000); } void displayText(const char c[]) { - clockDisplay.print(c); + tDisplay.disable(); + displayColon(false); + clockDisplay.println(c); clockDisplay.writeDisplay(); + adjustTime(); + tDisplay.enableDelayed(1000); } void setup() { diff --git a/include/ir.h b/include/ir.h index 7791f28..353c8c4 100644 --- a/include/ir.h +++ b/include/ir.h @@ -37,17 +37,10 @@ namespace Ir { return newCommand; } - void displayValue() { - Display::displayValue(lastCommand); - Display::tDisplay.setCallback(Display::displayColon); - Display::adjustTime(); - } - - void displayText(const char c[]) { - Display::tDisplay.disable(); + void command(const char c[]) { + avrOn = true; + Wifi::reconnect(); Display::displayText(c); - Display::adjustTime(); - Display::tDisplay.enableDelayed(1000); } void loop() { @@ -57,26 +50,41 @@ namespace Ir { case 0x9F: avrOn = false; tCheckWifi.enable(); - displayText("off"); + Display::displayText("Off"); + break; + case 0x12: + command("Slp"); break; case 0xC4: + command("Play"); + break; case 0xD0: + command("Fire"); + break; case 0xC0: - avrOn = true; - Wifi::reconnect(); + command("On"); break; default: - Display::tDisplay.setCallback(displayValue); - Display::tDisplay.forceNextIteration(); + Display::displayValue(lastCommand); break; } } - if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) { - Display::changeBrightness(true); + if (Mqtt::commands.getCurrent() == 0xC7) { + if (!avrOn) { + Display::displayText("B Up"); + Display::changeBrightness(true); + } else { + Display::displayText(" Up"); + } Mqtt::commands.pop(); } - if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) { - Display::changeBrightness(false); + if (Mqtt::commands.getCurrent() == 0xC8) { + if (!avrOn) { + Display::displayText("B Dn"); + Display::changeBrightness(false); + } else { + Display::displayText(" Dn"); + } Mqtt::commands.pop(); } } From 5a8e015b14cf319d42c7d0a08613161a26e4e58b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 7 Dec 2021 23:36:24 +0100 Subject: [PATCH 14/15] display temp --- include/display.h | 4 ++++ include/ir.h | 4 +++- src/esp_clock.cpp | 4 +--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/display.h b/include/display.h index d321516..77a5a70 100644 --- a/include/display.h +++ b/include/display.h @@ -81,8 +81,12 @@ namespace Display { } void displayFloat(float value) { + tDisplay.disable(); + displayColon(false); clockDisplay.print(value); clockDisplay.writeDisplay(); + adjustTime(); + tDisplay.enableDelayed(1000); } void displayValue(uint8_t value) { diff --git a/include/ir.h b/include/ir.h index 353c8c4..1b71341 100644 --- a/include/ir.h +++ b/include/ir.h @@ -62,7 +62,9 @@ namespace Ir { command("Fire"); break; case 0xC0: - command("On"); + avrOn = true; + Wifi::reconnect(); + Display::displayFloat(Bmp::bmp.readTemperature()); break; default: Display::displayValue(lastCommand); diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index cfc19ce..da74a3e 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -13,11 +13,11 @@ Task tWifiConnected(onWifiConnected, &ts); #include "wifi.h" #include "display.h" +#include "bmp.h" #include "ntp_time.h" #include "mqtt.h" #include "ota.h" #include "ir.h" -#include "bmp.h" #define STAY_CONNECTED_AFTER_BOOT 30*60 @@ -30,8 +30,6 @@ void setup() { Ir::setup(); Mqtt::setup(); Bmp::setup(); - Display::displayFloat(Bmp::bmp.readTemperature()); - delay(3000); hourChanged.setWaiting(); wifiConnected.setWaiting(); From 534cf4ba2814e7b58012e1d90d39da02f0f9b9ea Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 8 Dec 2021 09:06:03 +0100 Subject: [PATCH 15/15] change texts --- include/display.h | 2 ++ include/ir.h | 36 +++++++++++++++--------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/include/display.h b/include/display.h index 77a5a70..150b47e 100644 --- a/include/display.h +++ b/include/display.h @@ -99,6 +99,8 @@ namespace Display { } void displayText(const char c[]) { + tDisplay.disable(); + displayColon(false); clockDisplay.println(c); clockDisplay.writeDisplay(); adjustTime(); diff --git a/include/ir.h b/include/ir.h index 1b71341..d21bdb8 100644 --- a/include/ir.h +++ b/include/ir.h @@ -55,15 +55,27 @@ namespace Ir { case 0x12: command("Slp"); break; + case 0xC1: + Display::displayText("Mute"); + break; case 0xC4: command("Play"); break; + case 0xC7: + avrOn ? Display::displayText(" Up") : Display::changeBrightness(true); + Mqtt::commands.pop(); + break; + case 0xC8: + avrOn ? Display::displayText(" Dn") : Display::changeBrightness(false); + Mqtt::commands.pop(); + break; case 0xD0: - command("Fire"); + command("Stop"); break; case 0xC0: - avrOn = true; - Wifi::reconnect(); + command("On"); + break; + case 0x84: Display::displayFloat(Bmp::bmp.readTemperature()); break; default: @@ -71,24 +83,6 @@ namespace Ir { break; } } - if (Mqtt::commands.getCurrent() == 0xC7) { - if (!avrOn) { - Display::displayText("B Up"); - Display::changeBrightness(true); - } else { - Display::displayText(" Up"); - } - Mqtt::commands.pop(); - } - if (Mqtt::commands.getCurrent() == 0xC8) { - if (!avrOn) { - Display::displayText("B Dn"); - Display::changeBrightness(false); - } else { - Display::displayText(" Dn"); - } - Mqtt::commands.pop(); - } } void setup() {