25 lines
697 B
C++
25 lines
697 B
C++
#pragma once
|
|
#include <functional>
|
|
#include <PubSubClient.h>
|
|
#include <WiFiClient.h>
|
|
|
|
class MqttManager {
|
|
public:
|
|
using MessageCallback = std::function<void(char*, uint8_t*, unsigned int)>;
|
|
MqttManager();
|
|
void begin(const char* server, uint16_t port, MessageCallback cb);
|
|
void loop();
|
|
bool publish(const char* topic, const char* payload);
|
|
bool subscribe(const char* topic);
|
|
bool connected();
|
|
private:
|
|
WiFiClient espClient;
|
|
PubSubClient client{espClient};
|
|
MessageCallback messageCb;
|
|
const char* serverAddr = nullptr;
|
|
uint16_t serverPort = 0;
|
|
static MqttManager* instance;
|
|
static void staticCallback(char* topic, byte* payload, unsigned int length);
|
|
void connect();
|
|
};
|