extract Mqtt functionality outside Ir
This commit is contained in:
parent
25dcda2dfa
commit
167606a19e
43
include/ir.h
43
include/ir.h
@ -16,23 +16,8 @@ namespace Ir {
|
||||
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)) {
|
||||
@ -41,7 +26,7 @@ namespace Ir {
|
||||
Serial.print(results.command, HEX);
|
||||
Serial.println();
|
||||
lastCommand = results.command;
|
||||
commands.push(results.command);
|
||||
Mqtt::commands.push(results.command);
|
||||
newCommand = true;
|
||||
}
|
||||
irrecv.resume(); // Receive the next value
|
||||
@ -49,52 +34,36 @@ namespace Ir {
|
||||
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::lastConnectedTime = now();
|
||||
break;
|
||||
case 0xC4:
|
||||
case 0xD0:
|
||||
case 0xC0:
|
||||
avrOn = true;
|
||||
if (WiFi.status() == WL_DISCONNECTED) {
|
||||
Wifi::reconnect();
|
||||
// connect on wifi connected
|
||||
mqttClient.connect();
|
||||
}
|
||||
Wifi::reconnect();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!avrOn && getCurrentCommand() == 0xC7) {
|
||||
if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) {
|
||||
Display::changeBrightness(true);
|
||||
commands.pop();
|
||||
Mqtt::commands.pop();
|
||||
}
|
||||
if (!avrOn && getCurrentCommand() == 0xC8) {
|
||||
if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) {
|
||||
Display::changeBrightness(false);
|
||||
commands.pop();
|
||||
Mqtt::commands.pop();
|
||||
}
|
||||
publishCommand();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
42
include/mqtt.h
Normal file
42
include/mqtt.h
Normal file
@ -0,0 +1,42 @@
|
||||
#include <queue>
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
namespace Mqtt {
|
||||
|
||||
void publishCommand();
|
||||
Task tPublish(TASK_SECOND, TASK_FOREVER, Mqtt::publishCommand, &ts);
|
||||
|
||||
AsyncMqttClient client;
|
||||
|
||||
std::queue<uint8_t> commands;
|
||||
|
||||
void publishCommand() {
|
||||
if (!commands.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);
|
||||
Serial.println();
|
||||
commands.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t getCurrentCommand() {
|
||||
return commands.empty() ? 0 : commands.front();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
client.onConnect([](bool sessionPresent) {
|
||||
tPublish.enableDelayed();
|
||||
});
|
||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||
tPublish.disable();
|
||||
});
|
||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||
Serial.println("Connecting to MQTT...");
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ StatusRequest wifiConnected;
|
||||
|
||||
#include "wifi.h"
|
||||
#include "ntp_time.h"
|
||||
#include "mqtt.h"
|
||||
#include "ota.h"
|
||||
#include "display.h"
|
||||
#include "ir.h"
|
||||
@ -27,12 +28,10 @@ void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
Display::setup();
|
||||
|
||||
Ota::setup();
|
||||
|
||||
Ntp::setup();
|
||||
|
||||
Ir::setup();
|
||||
Mqtt::setup();
|
||||
|
||||
hourChanged.setWaiting();
|
||||
dayChanged.setWaiting();
|
||||
@ -56,6 +55,7 @@ void wifiConnectedCallback() {
|
||||
Ntp::lastConnectedTime = newTime;
|
||||
}
|
||||
tOta.enable();
|
||||
Mqtt::client.connect();
|
||||
}
|
||||
|
||||
void otaCallback() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user