-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMqttBackend.h
67 lines (53 loc) · 2.23 KB
/
MqttBackend.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Copyright (C) 2020 Xavier Lüthi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
MQTT backend with High Level functions.
*/
#ifndef _MQTT_BACKEND_H_
#define _MQTT_BACKEND_H_
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "PubSubClient.h"
#include <ESP8266httpUpdate.h>
#include "FlashConfig.h"
// Specific topic that all hardware on the network must listen to and answer.
// It's a kind of discovery topic to have a view on active hardware.
#define MQTT_INVENTORY_TOPIC "house/hardware/ping_all"
#define MQTT_ROOT "house/hardware"
#define MQTT_SENSOR_ROOT "house/sensors/"
class MqttBackend : public PubSubClient {
public:
MqttBackend(WiFiClient& wifiClient);
// to be called within Arduino setup() global function
void setup(const char *serverIP = "192.168.1.1", int port = 1883, String myId = "ESP-SENSOR");
bool reconnect();
bool forceReconnect(); // explicitly disconnect first
// send message to a specific mqtt log topic
bool sendLog(const char *message);
void setFlashConfig(FlashConfig& config);
private:
IPAddress _serverIPAddress;
WiFiClient* _wifiClient; // reference to WifiClient used for ESPhttpUpdate
int _serverPort;
String _id;
String _rootTopic;
FlashConfig* _config;
bool _doSendLogs = true; // set to false to avoid sending log message via MQTT (/log topic)
void callback(char* topic, byte* payload, unsigned int length);
bool sendBootNotification();
void setRootTopic();
protected:
virtual void onCallback(char* topic, byte* payload, unsigned int length);
// Generic function to send a message to MQTT broker
bool sendMessage(const char *topic, const char *payload);
};
#endif