extract Mqtt functionality outside Ir
This commit is contained in:
parent
25dcda2dfa
commit
167606a19e
41
include/ir.h
41
include/ir.h
@ -16,23 +16,8 @@ namespace Ir {
|
|||||||
decode_results results;
|
decode_results results;
|
||||||
bool avrOn = false;
|
bool avrOn = false;
|
||||||
|
|
||||||
AsyncMqttClient mqttClient;
|
|
||||||
|
|
||||||
std::queue<uint8_t> commands;
|
|
||||||
uint8_t lastCommand = 0x9F;
|
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 readCommand() {
|
||||||
bool newCommand = false;
|
bool newCommand = false;
|
||||||
if (irrecv.decode(&results)) {
|
if (irrecv.decode(&results)) {
|
||||||
@ -41,7 +26,7 @@ namespace Ir {
|
|||||||
Serial.print(results.command, HEX);
|
Serial.print(results.command, HEX);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
lastCommand = results.command;
|
lastCommand = results.command;
|
||||||
commands.push(results.command);
|
Mqtt::commands.push(results.command);
|
||||||
newCommand = true;
|
newCommand = true;
|
||||||
}
|
}
|
||||||
irrecv.resume(); // Receive the next value
|
irrecv.resume(); // Receive the next value
|
||||||
@ -49,52 +34,36 @@ namespace Ir {
|
|||||||
return newCommand;
|
return newCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t getCurrentCommand() {
|
|
||||||
return commands.empty() ? 0 : commands.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
|
||||||
Serial.println("Connecting to MQTT...");
|
|
||||||
mqttClient.connect();
|
|
||||||
|
|
||||||
irrecv.enableIRIn(); // Start the receiver
|
irrecv.enableIRIn(); // Start the receiver
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (readCommand()) {
|
if (readCommand()) {
|
||||||
Display::displayValue(lastCommand);
|
Display::displayValue(lastCommand);
|
||||||
// Display::displayColon(false);
|
|
||||||
delay(1000);
|
|
||||||
switch (lastCommand)
|
switch (lastCommand)
|
||||||
{
|
{
|
||||||
case 0x9F:
|
case 0x9F:
|
||||||
avrOn = false;
|
avrOn = false;
|
||||||
Ntp::lastConnectedTime = now();
|
|
||||||
break;
|
break;
|
||||||
case 0xC4:
|
case 0xC4:
|
||||||
case 0xD0:
|
case 0xD0:
|
||||||
case 0xC0:
|
case 0xC0:
|
||||||
avrOn = true;
|
avrOn = true;
|
||||||
if (WiFi.status() == WL_DISCONNECTED) {
|
|
||||||
Wifi::reconnect();
|
Wifi::reconnect();
|
||||||
// connect on wifi connected
|
|
||||||
mqttClient.connect();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!avrOn && getCurrentCommand() == 0xC7) {
|
if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) {
|
||||||
Display::changeBrightness(true);
|
Display::changeBrightness(true);
|
||||||
commands.pop();
|
Mqtt::commands.pop();
|
||||||
}
|
}
|
||||||
if (!avrOn && getCurrentCommand() == 0xC8) {
|
if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) {
|
||||||
Display::changeBrightness(false);
|
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 "wifi.h"
|
||||||
#include "ntp_time.h"
|
#include "ntp_time.h"
|
||||||
|
#include "mqtt.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "ir.h"
|
#include "ir.h"
|
||||||
@ -27,12 +28,10 @@ void setup() {
|
|||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
Display::setup();
|
Display::setup();
|
||||||
|
|
||||||
Ota::setup();
|
Ota::setup();
|
||||||
|
|
||||||
Ntp::setup();
|
Ntp::setup();
|
||||||
|
|
||||||
Ir::setup();
|
Ir::setup();
|
||||||
|
Mqtt::setup();
|
||||||
|
|
||||||
hourChanged.setWaiting();
|
hourChanged.setWaiting();
|
||||||
dayChanged.setWaiting();
|
dayChanged.setWaiting();
|
||||||
@ -56,6 +55,7 @@ void wifiConnectedCallback() {
|
|||||||
Ntp::lastConnectedTime = newTime;
|
Ntp::lastConnectedTime = newTime;
|
||||||
}
|
}
|
||||||
tOta.enable();
|
tOta.enable();
|
||||||
|
Mqtt::client.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void otaCallback() {
|
void otaCallback() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user