wip with light and mqtt working
This commit is contained in:
91
lib/communication/mqtt.cpp
Normal file
91
lib/communication/mqtt.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiClient.h>
|
||||
#include "mqtt.h"
|
||||
|
||||
constexpr uint16_t BUFFER_SIZE = 2048;
|
||||
|
||||
static WiFiClient wifiClient = WiFiClient();
|
||||
static PubSubClient mqttClient = PubSubClient(wifiClient);
|
||||
std::string Mqtt::brokerIp;
|
||||
uint16_t Mqtt::brokerPort;
|
||||
std::string Mqtt::clientId;
|
||||
std::string Mqtt::username;
|
||||
std::string Mqtt::password;
|
||||
std::map<std::string, MqttCallback> Mqtt::callbacks;
|
||||
bool Mqtt::initialized = false;
|
||||
bool Mqtt::isConnected = false;
|
||||
|
||||
void Mqtt::mqttCb(char* topic, uint8_t* payload, unsigned int length) {
|
||||
std::string topicStr(topic);
|
||||
if (callbacks.find(topicStr) != callbacks.end()) {
|
||||
callbacks[topicStr](payload, length);
|
||||
}
|
||||
}
|
||||
|
||||
void Mqtt::subscribe(const std::string& topic, MqttCallback callback) {
|
||||
if (mqttClient.connected()) {
|
||||
if (mqttClient.subscribe(topic.c_str())) {
|
||||
callbacks[topic] = callback;
|
||||
Serial.printf("Subscribed to topic: %s\n", topic.c_str());
|
||||
} else {
|
||||
Serial.printf("Failed to subscribe to topic: %s\n", topic.c_str());
|
||||
}
|
||||
} else {
|
||||
Serial.println("MQTT client is not connected. Cannot subscribe.");
|
||||
}
|
||||
}
|
||||
|
||||
void Mqtt::publish(const std::string& topic, const std::string& payload, bool retain) {
|
||||
if (mqttClient.connected()) {
|
||||
if (mqttClient.publish(topic.c_str(), payload.c_str(), retain)) {
|
||||
} else {
|
||||
Serial.printf("Failed to publish to topic: %s\n", topic.c_str(), payload.c_str());
|
||||
}
|
||||
} else {
|
||||
Serial.println("MQTT client is not connected. Cannot publish.");
|
||||
}
|
||||
}
|
||||
|
||||
void Mqtt::poll() {
|
||||
if (mqttClient.connected()) {
|
||||
mqttClient.loop(); // Process incoming messages
|
||||
} else {
|
||||
Serial.println("MQTT client is not connected. Polling skipped.");
|
||||
}
|
||||
}
|
||||
|
||||
void Mqtt::checkConnection() {
|
||||
if (!mqttClient.connected()) {
|
||||
Serial.println("MQTT client is not connected. Attempting to reconnect...");
|
||||
if (mqttClient.connect(Mqtt::clientId.c_str(), Mqtt::username.c_str(), Mqtt::password.c_str())) {
|
||||
Serial.println("Reconnected to MQTT broker successfully.");
|
||||
for (const auto& callback : Mqtt::callbacks) {
|
||||
mqttClient.subscribe(callback.first.c_str());
|
||||
}
|
||||
Mqtt::isConnected = true;
|
||||
} else {
|
||||
Serial.printf("Failed to reconnect to MQTT broker, rc=%d\n", mqttClient.state());
|
||||
Mqtt::isConnected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Mqtt::connect(std::string brokerIp, uint16_t brokerPort, std::string clientId, std::string username, std::string password) {
|
||||
Mqtt::brokerIp = brokerIp;
|
||||
Mqtt::brokerPort = brokerPort;
|
||||
Mqtt::clientId = clientId;
|
||||
Mqtt::username = username;
|
||||
Mqtt::password = password;
|
||||
mqttClient.setServer(Mqtt::brokerIp.c_str(), Mqtt::brokerPort);
|
||||
mqttClient.setKeepAlive(60);
|
||||
mqttClient.setCallback(mqttCb);
|
||||
mqttClient.setBufferSize(BUFFER_SIZE);
|
||||
|
||||
if (mqttClient.connect(Mqtt::clientId.c_str(), Mqtt::username.c_str(), Mqtt::password.c_str())) {
|
||||
Serial.println("Connected to MQTT broker");
|
||||
Mqtt::initialized = true;
|
||||
Mqtt::isConnected = true;
|
||||
} else {
|
||||
Serial.printf("Failed to connect to MQTT broker, rc=%d\n", mqttClient.state());
|
||||
}
|
||||
}
|
||||
26
lib/communication/mqtt.h
Normal file
26
lib/communication/mqtt.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
typedef std::function<void(uint8_t*, int)> MqttCallback;
|
||||
|
||||
class Mqtt {
|
||||
public:
|
||||
static void connect(std::string brokerIp, uint16_t brokerPort, std::string clientId, std::string username="mqtt", std::string password="mqtt");
|
||||
static void poll();
|
||||
static void checkConnection();
|
||||
static void publish(const std::string& topic, const std::string& payload, bool retain = false);
|
||||
static void subscribe(const std::string& topic, MqttCallback callback);
|
||||
static void mqttCb(char* topic, uint8_t* payload, unsigned int length);
|
||||
|
||||
private:
|
||||
static std::string brokerIp;
|
||||
static uint16_t brokerPort;
|
||||
static std::string clientId;
|
||||
static std::string username;
|
||||
static std::string password;
|
||||
static bool initialized;
|
||||
static bool isConnected;
|
||||
static std::map<std::string, MqttCallback> callbacks;
|
||||
};
|
||||
Reference in New Issue
Block a user