-
Notifications
You must be signed in to change notification settings - Fork 0
/
MqttBase.h
51 lines (41 loc) · 1.74 KB
/
MqttBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <vector>
/** Case insensitive string compare */
bool compareChar(char & c1, char & c2);
bool caseInSensStringCompare(const char* in1, const char* in2);
class MqttBase {
private:
std::vector<std::shared_ptr<std::string>> mqtt_topics_;
std::vector<std::function<void(const char*, const char*, int)>> logic_callbacks_;
// const char* mqtt_topic_;
const char* mqtt_server_;
const char* mqtt_client_name_;
int mqtt_port_ = 1883;
StaticJsonDocument<300> doc_;
JsonObject JSONencoder_ = doc_.to<JsonObject>();
// std::unique_ptr<PubSubClient> client;
PubSubClient* pub_client_;
WiFiClient* wifi_client_;
// std::function<void(const char*, const char*, int)> logic_callback_;
void debug_print(const char* str);
void debug_print(int i);
void debug_println(const char* str);
void debug_println(int i);
public:
MqttBase(const char* mqtt_server, const char* mqtt_client_name, uint16_t mqtt_port);
~MqttBase();
void init(const char* ssid, const char* password,
std::vector<std::shared_ptr<std::string>>& mqtt_topics,
std::vector<std::function<void(const char*, const char*, int)>> logic_callbacks);
void reconnect();
virtual void callback(char* topic, byte* message, unsigned int length);
virtual void publish(const char* topic, const char* methode, const char* state, bool retained);
virtual void publish(const char* topic, const char* methode, const char* state, int data,
bool retained);
virtual void publish(const char* topic, const char* methode, const char* state, const char* data,
bool retained);
void loop();
bool get_mqtt_status() { return pub_client_->connected(); }
};