-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#ifndef MQTTMANAGER_H | ||
#define MQTTMANAGER_H | ||
|
||
#include <Arduino.h> | ||
#include <ESP8266WiFi.h> | ||
|
||
#undef min | ||
#undef max | ||
|
||
#include <AsyncMqttClient.h> | ||
|
||
#include "SimpleTimer.h" | ||
#include "MqttDiscoveryComponent.h" | ||
|
||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
||
class MqttManager | ||
{ | ||
private: | ||
String m_mqttServer; | ||
uint16_t m_mqttPort; | ||
String m_mqttUsername; | ||
String m_mqttPassword; | ||
|
||
String m_deviceName; | ||
String m_deviceIP; | ||
String m_deviceMac; | ||
String m_hardware; | ||
String m_firmware; | ||
String m_firmwareVersion; | ||
|
||
String m_deviceDataTopic; | ||
|
||
MqttDiscoveryComponent* m_deviceNameSensor; | ||
MqttDiscoveryComponent* m_deviceIpSensor; | ||
MqttDiscoveryComponent* m_deviceMacSensor; | ||
MqttDiscoveryComponent* m_deviceHardwareSensor; | ||
MqttDiscoveryComponent* m_deviceFirmwareSensor; | ||
MqttDiscoveryComponent* m_deviceFirmwareVersionSensor; | ||
|
||
std::map<String, String> m_statusTopics; | ||
std::vector<String> m_subscribeTopics; | ||
std::vector<MqttDiscoveryComponent*> m_discoveryComponents; | ||
|
||
AsyncMqttClient m_mqttClient; | ||
|
||
bool m_connected; | ||
bool m_mqttDiscoveryEnabled{false}; | ||
|
||
SimpleTimer m_deviceStatusInfoTimer; | ||
SimpleTimer m_checkConnectivityTimer; | ||
unsigned long m_checkConnectivityTimeOnline; | ||
unsigned long m_checkConnectivityTimeOffline; | ||
|
||
void setDeviceMac(); | ||
void publishDeviceStatusInfo(); | ||
void publishDiscoveryInfo(); | ||
void refreshStatusTopics(); | ||
void checkConnectivity(); | ||
|
||
public: | ||
MqttManager(); | ||
|
||
void setup(String mqttServer, String mqttPort, String mqttUsername, String mqttPassword, bool mqttDiscoveryEnabled = false); | ||
|
||
void setDeviceData(String deviceName, String hardware, String deviceIP, String firmware, String firmwareVersion); | ||
void setCallback(void (*callback)(String , String)); | ||
void setLastWillMQTT(String topic, const char* payload); | ||
|
||
void setDeviceStatusInfoTime(unsigned long deviceStatusInfoTime); | ||
|
||
void enableDiscovery(bool enable); | ||
void addDiscoveryComponent(MqttDiscoveryComponent* component); | ||
|
||
void addSubscribeTopic(String subscribeTopic); | ||
void clearSubscribeTopics(); | ||
|
||
void addStatusTopic(String statusTopic); | ||
void clearStatusTopics(); | ||
|
||
void startConnection(); | ||
void stopConnection(); | ||
|
||
void publishMQTT(String topic, String payload); | ||
void publishMQTT(String topic, float payload); | ||
|
||
bool connected(); | ||
|
||
void loop(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters