refactor NetworkDevice interface to work with new esp mqtt lib
This commit is contained in:
@@ -49,6 +49,8 @@ file(GLOB SRCFILES
|
|||||||
networkDevices/NetworkDevice.h
|
networkDevices/NetworkDevice.h
|
||||||
networkDevices/WifiDevice.cpp
|
networkDevices/WifiDevice.cpp
|
||||||
networkDevices/W5500Device.cpp
|
networkDevices/W5500Device.cpp
|
||||||
|
networkDevices/ClientSyncEthernet.cpp
|
||||||
|
networkDevices/espMqttClientEthernet.cpp
|
||||||
NukiWrapper.cpp
|
NukiWrapper.cpp
|
||||||
NukiOpenerWrapper.cpp
|
NukiOpenerWrapper.cpp
|
||||||
MqttTopics.h
|
MqttTopics.h
|
||||||
|
|||||||
2
Config.h
2
Config.h
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define NUKI_HUB_VERSION "7.3-esp-mqtt-5"
|
#define NUKI_HUB_VERSION "7.3-esp-mqtt-6"
|
||||||
|
|
||||||
#define MQTT_QOS_LEVEL 1
|
#define MQTT_QOS_LEVEL 1
|
||||||
#define MQTT_CLEAN_SESSIONS false
|
#define MQTT_CLEAN_SESSIONS false
|
||||||
88
Network.cpp
88
Network.cpp
@@ -136,8 +136,8 @@ void Network::initialize()
|
|||||||
Log->print(F(":"));
|
Log->print(F(":"));
|
||||||
Log->println(port);
|
Log->println(port);
|
||||||
|
|
||||||
_device->mqttClient()->setClientId(_hostnameArr);
|
_device->mqttSetClientId(_hostnameArr);
|
||||||
_device->mqttClient()->setCleanSession(MQTT_CLEAN_SESSIONS);
|
_device->mqttSetCleanSession(MQTT_CLEAN_SESSIONS);
|
||||||
|
|
||||||
_networkTimeout = _preferences->getInt(preference_network_timeout);
|
_networkTimeout = _preferences->getInt(preference_network_timeout);
|
||||||
if(_networkTimeout == 0)
|
if(_networkTimeout == 0)
|
||||||
@@ -182,7 +182,7 @@ bool Network::update()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_device->mqttClient()->connected())
|
if(!_device->mqttConnected())
|
||||||
{
|
{
|
||||||
if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000)
|
if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000)
|
||||||
{
|
{
|
||||||
@@ -238,7 +238,7 @@ bool Network::reconnect()
|
|||||||
_mqttConnectionState = 0;
|
_mqttConnectionState = 0;
|
||||||
int port = _preferences->getInt(preference_mqtt_broker_port);
|
int port = _preferences->getInt(preference_mqtt_broker_port);
|
||||||
|
|
||||||
while (!_device->mqttClient()->connected() && millis() > _nextReconnect)
|
while (!_device->mqttConnected() && millis() > _nextReconnect)
|
||||||
{
|
{
|
||||||
if(strcmp(_mqttBrokerAddr, "") == 0)
|
if(strcmp(_mqttBrokerAddr, "") == 0)
|
||||||
{
|
{
|
||||||
@@ -252,23 +252,23 @@ bool Network::reconnect()
|
|||||||
if(strlen(_mqttUser) == 0)
|
if(strlen(_mqttUser) == 0)
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT: Connecting without credentials"));
|
Log->println(F("MQTT: Connecting without credentials"));
|
||||||
_device->mqttClient()->setServer(_mqttBrokerAddr, port);
|
_device->mqttSetServer(_mqttBrokerAddr, port);
|
||||||
_device->mqttClient()->connect();
|
_device->mqttConnect();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser);
|
Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser);
|
||||||
_device->mqttClient()->setCredentials(_mqttUser, _mqttPass);
|
_device->mqttSetCredentials(_mqttUser, _mqttPass);
|
||||||
_device->mqttClient()->setServer(_mqttBrokerAddr, port);
|
_device->mqttSetServer(_mqttBrokerAddr, port);
|
||||||
_device->mqttClient()->connect();
|
_device->mqttConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool connected = _device->mqttClient()->connected();
|
bool connected = _device->mqttConnected();
|
||||||
unsigned long timeout = millis() + 5000;
|
unsigned long timeout = millis() + 5000;
|
||||||
|
|
||||||
while(!connected && millis() < timeout)
|
while(!connected && millis() < timeout)
|
||||||
{
|
{
|
||||||
connected = _device->mqttClient()->connected();
|
connected = _device->mqttConnected();
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,17 +279,17 @@ bool Network::reconnect()
|
|||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
_device->mqttClient()->onMessage(Network::onMqttDataReceivedCallback);
|
_device->mqttOnMessage(Network::onMqttDataReceivedCallback);
|
||||||
for(const String& topic : _subscribedTopics)
|
for(const String& topic : _subscribedTopics)
|
||||||
{
|
{
|
||||||
_device->mqttClient()->subscribe(topic.c_str(), MQTT_QOS_LEVEL);
|
_device->mqttSubscribe(topic.c_str(), MQTT_QOS_LEVEL);
|
||||||
}
|
}
|
||||||
if(_firstConnect)
|
if(_firstConnect)
|
||||||
{
|
{
|
||||||
_firstConnect = false;
|
_firstConnect = false;
|
||||||
for(const auto& it : _initTopics)
|
for(const auto& it : _initTopics)
|
||||||
{
|
{
|
||||||
_device->mqttClient()->publish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str());
|
_device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delay(1000);
|
delay(1000);
|
||||||
@@ -371,11 +371,6 @@ void Network::onMqttDataReceived(const espMqttClientTypes::MessageProperties& pr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttClientSetup *Network::mqttClient()
|
|
||||||
{
|
|
||||||
return _device->mqttClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Network::reconfigureDevice()
|
void Network::reconfigureDevice()
|
||||||
{
|
{
|
||||||
_device->reconfigure();
|
_device->reconfigure();
|
||||||
@@ -405,7 +400,7 @@ void Network::publishFloat(const char* prefix, const char* topic, const float va
|
|||||||
dtostrf(value, 0, precision, str);
|
dtostrf(value, 0, precision, str);
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
_device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str);
|
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::publishInt(const char* prefix, const char *topic, const int value)
|
void Network::publishInt(const char* prefix, const char *topic, const int value)
|
||||||
@@ -414,7 +409,7 @@ void Network::publishInt(const char* prefix, const char *topic, const int value)
|
|||||||
itoa(value, str, 10);
|
itoa(value, str, 10);
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
_device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str);
|
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::publishUInt(const char* prefix, const char *topic, const unsigned int value)
|
void Network::publishUInt(const char* prefix, const char *topic, const unsigned int value)
|
||||||
@@ -423,7 +418,7 @@ void Network::publishUInt(const char* prefix, const char *topic, const unsigned
|
|||||||
utoa(value, str, 10);
|
utoa(value, str, 10);
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
_device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str);
|
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::publishULong(const char* prefix, const char *topic, const unsigned long value)
|
void Network::publishULong(const char* prefix, const char *topic, const unsigned long value)
|
||||||
@@ -432,7 +427,7 @@ void Network::publishULong(const char* prefix, const char *topic, const unsigned
|
|||||||
utoa(value, str, 10);
|
utoa(value, str, 10);
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
_device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str);
|
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::publishBool(const char* prefix, const char *topic, const bool value)
|
void Network::publishBool(const char* prefix, const char *topic, const bool value)
|
||||||
@@ -441,14 +436,14 @@ void Network::publishBool(const char* prefix, const char *topic, const bool valu
|
|||||||
str[0] = value ? '1' : '0';
|
str[0] = value ? '1' : '0';
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
_device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str);
|
_device->mqttPublish(path, MQTT_QOS_LEVEL, true, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Network::publishString(const char* prefix, const char *topic, const char *value)
|
bool Network::publishString(const char* prefix, const char *topic, const char *value)
|
||||||
{
|
{
|
||||||
char path[200] = {0};
|
char path[200] = {0};
|
||||||
buildMqttPath(prefix, topic, path);
|
buildMqttPath(prefix, topic, path);
|
||||||
return _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, value) > 0;
|
return _device->mqttPublish(path, MQTT_QOS_LEVEL, true, value) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState)
|
void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState)
|
||||||
@@ -490,7 +485,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/smartlock/config");
|
path.concat("/smartlock/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
|
|
||||||
// Battery critical
|
// Battery critical
|
||||||
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
||||||
@@ -514,7 +509,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_low/config");
|
path.concat("/battery_low/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
|
|
||||||
// Keypad battery critical
|
// Keypad battery critical
|
||||||
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
||||||
@@ -538,7 +533,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/keypad_battery_low/config");
|
path.concat("/keypad_battery_low/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
|
|
||||||
// Battery voltage
|
// Battery voltage
|
||||||
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
||||||
@@ -564,7 +559,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_voltage/config");
|
path.concat("/battery_voltage/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
|
|
||||||
// Trigger
|
// Trigger
|
||||||
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
|
||||||
@@ -589,7 +584,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/trigger/config");
|
path.concat("/trigger/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -625,7 +620,7 @@ void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic,
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_level/config");
|
path.concat("/battery_level/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,7 +655,7 @@ void Network::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopi
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/door_sensor/config");
|
path.concat("/door_sensor/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -694,7 +689,7 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/ring/config");
|
path.concat("/ring/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -732,7 +727,7 @@ void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic,
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/wifi_signal_strength/config");
|
path.concat("/wifi_signal_strength/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -764,7 +759,7 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic,
|
|||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/bluetooth_signal_strength/config");
|
path.concat("/bluetooth_signal_strength/config");
|
||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -778,55 +773,55 @@ void Network::removeHASSConfig(char* uidString)
|
|||||||
path.concat("/lock/");
|
path.concat("/lock/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/smartlock/config");
|
path.concat("/smartlock/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/binary_sensor/");
|
path.concat("/binary_sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_low/config");
|
path.concat("/battery_low/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_voltage/config");
|
path.concat("/battery_voltage/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/trigger/config");
|
path.concat("/trigger/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/battery_level/config");
|
path.concat("/battery_level/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/binary_sensor/");
|
path.concat("/binary_sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/door_sensor/config");
|
path.concat("/door_sensor/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/binary_sensor/");
|
path.concat("/binary_sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/ring/config");
|
path.concat("/ring/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/wifi_signal_strength/config");
|
path.concat("/wifi_signal_strength/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
|
|
||||||
path = discoveryTopic;
|
path = discoveryTopic;
|
||||||
path.concat("/sensor/");
|
path.concat("/sensor/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/bluetooth_signal_strength/config");
|
path.concat("/bluetooth_signal_strength/config");
|
||||||
_device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -839,3 +834,8 @@ const NetworkDeviceType Network::networkDeviceType()
|
|||||||
{
|
{
|
||||||
return _networkDeviceType;
|
return _networkDeviceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t Network::subscribe(const char *topic, uint8_t qos)
|
||||||
|
{
|
||||||
|
return _device->mqttSubscribe(topic, qos);
|
||||||
|
}
|
||||||
|
|||||||
@@ -43,11 +43,12 @@ public:
|
|||||||
|
|
||||||
void publishPresenceDetection(char* csv);
|
void publishPresenceDetection(char* csv);
|
||||||
|
|
||||||
MqttClientSetup* mqttClient();
|
|
||||||
int mqttConnectionState(); // 0 = not connected; 1 = connected; 2 = connected and mqtt processed
|
int mqttConnectionState(); // 0 = not connected; 1 = connected; 2 = connected and mqtt processed
|
||||||
|
|
||||||
const NetworkDeviceType networkDeviceType();
|
const NetworkDeviceType networkDeviceType();
|
||||||
|
|
||||||
|
uint16_t subscribe(const char* topic, uint8_t qos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
||||||
void onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
void onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
||||||
|
|||||||
@@ -476,7 +476,7 @@ void NetworkOpener::subscribe(const char *path)
|
|||||||
{
|
{
|
||||||
char prefixedPath[500];
|
char prefixedPath[500];
|
||||||
buildMqttPath(path, prefixedPath);
|
buildMqttPath(path, prefixedPath);
|
||||||
_network->mqttClient()->subscribe(prefixedPath, MQTT_QOS_LEVEL);
|
_network->subscribe(prefixedPath, MQTT_QOS_LEVEL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkOpener::comparePrefixedPath(const char *fullPath, const char *subPath)
|
bool NetworkOpener::comparePrefixedPath(const char *fullPath, const char *subPath)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode)
|
|||||||
this->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
this->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttLogger::MqttLogger(MqttClientSetup& client, const char* topic, MqttLoggerMode mode)
|
MqttLogger::MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode)
|
||||||
{
|
{
|
||||||
this->setClient(client);
|
this->setClient(client);
|
||||||
this->setTopic(topic);
|
this->setTopic(topic);
|
||||||
@@ -19,9 +19,9 @@ MqttLogger::~MqttLogger()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttLogger::setClient(MqttClientSetup& client)
|
void MqttLogger::setClient(NetworkDevice* client)
|
||||||
{
|
{
|
||||||
this->client = &client;
|
this->client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttLogger::setTopic(const char* topic)
|
void MqttLogger::setTopic(const char* topic)
|
||||||
@@ -74,9 +74,9 @@ void MqttLogger::sendBuffer()
|
|||||||
if (this->bufferCnt > 0)
|
if (this->bufferCnt > 0)
|
||||||
{
|
{
|
||||||
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial;
|
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial;
|
||||||
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected())
|
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->mqttConnected())
|
||||||
{
|
{
|
||||||
this->client->publish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt);
|
this->client->mqttPublish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt);
|
||||||
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
|
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
|
||||||
{
|
{
|
||||||
doSerial = true;
|
doSerial = true;
|
||||||
|
|||||||
@@ -11,8 +11,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Print.h>
|
#include <Print.h>
|
||||||
#include "MqttClient.h"
|
#include "../../../networkDevices/NetworkDevice.h"
|
||||||
#include "MqttClientSetup.h"
|
|
||||||
|
|
||||||
#define MQTT_MAX_PACKET_SIZE 1024
|
#define MQTT_MAX_PACKET_SIZE 1024
|
||||||
|
|
||||||
@@ -30,16 +29,16 @@ private:
|
|||||||
uint8_t* buffer;
|
uint8_t* buffer;
|
||||||
uint8_t* bufferEnd;
|
uint8_t* bufferEnd;
|
||||||
uint16_t bufferCnt = 0, bufferSize = 0;
|
uint16_t bufferCnt = 0, bufferSize = 0;
|
||||||
MqttClientSetup* client;
|
NetworkDevice* client;
|
||||||
MqttLoggerMode mode;
|
MqttLoggerMode mode;
|
||||||
void sendBuffer();
|
void sendBuffer();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
||||||
MqttLogger(MqttClientSetup& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
||||||
~MqttLogger();
|
~MqttLogger();
|
||||||
|
|
||||||
void setClient(MqttClientSetup& client);
|
void setClient(NetworkDevice* client);
|
||||||
void setTopic(const char* topic);
|
void setTopic(const char* topic);
|
||||||
void setMode(MqttLoggerMode mode);
|
void setMode(MqttLoggerMode mode);
|
||||||
void setRetained(boolean retained);
|
void setRetained(boolean retained);
|
||||||
|
|||||||
@@ -13,103 +13,104 @@ the LICENSE file.
|
|||||||
|
|
||||||
#include "MqttClient.h"
|
#include "MqttClient.h"
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
class MqttClientSetup : public MqttClient {
|
class MqttClientSetup : public MqttClient {
|
||||||
public:
|
public:
|
||||||
void setKeepAlive(uint16_t keepAlive) {
|
T& setKeepAlive(uint16_t keepAlive) {
|
||||||
_keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
|
_keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
T& setClientId(const char* clientId) {
|
||||||
|
_clientId = clientId;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
T& setCleanSession(bool cleanSession) {
|
||||||
|
_cleanSession = cleanSession;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
T& setCredentials(const char* username, const char* password) {
|
||||||
|
_username = username;
|
||||||
|
_password = password;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
T& setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) {
|
||||||
|
_willTopic = topic;
|
||||||
|
_willQos = qos;
|
||||||
|
_willRetain = retain;
|
||||||
|
_willPayload = payload;
|
||||||
|
if (!_willPayload) {
|
||||||
|
_willPayloadLength = 0;
|
||||||
|
} else {
|
||||||
|
_willPayloadLength = length;
|
||||||
}
|
}
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setClientId(const char* clientId) {
|
T& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) {
|
||||||
_clientId = clientId;
|
return setWill(topic, qos, retain, reinterpret_cast<const uint8_t*>(payload), strlen(payload));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
T& setServer(IPAddress ip, uint16_t port) {
|
||||||
|
_ip = ip;
|
||||||
|
_port = port;
|
||||||
|
_useIp = true;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setCleanSession(bool cleanSession) {
|
T& setServer(const char* host, uint16_t port) {
|
||||||
_cleanSession = cleanSession;
|
_host = host;
|
||||||
|
_port = port;
|
||||||
|
_useIp = false;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
T& onConnect(espMqttClientTypes::OnConnectCallback callback) {
|
||||||
|
_onConnectCallback = callback;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setCredentials(const char* username, const char* password) {
|
T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
|
||||||
_username = username;
|
_onDisconnectCallback = callback;
|
||||||
_password = password;
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
|
||||||
|
_onSubscribeCallback = callback;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) {
|
T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
|
||||||
_willTopic = topic;
|
_onUnsubscribeCallback = callback;
|
||||||
_willQos = qos;
|
return static_cast<T&>(*this);
|
||||||
_willRetain = retain;
|
}
|
||||||
_willPayload = payload;
|
|
||||||
if (!_willPayload) {
|
|
||||||
_willPayloadLength = 0;
|
|
||||||
} else {
|
|
||||||
_willPayloadLength = length;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
T& onMessage(espMqttClientTypes::OnMessageCallback callback) {
|
||||||
|
_onMessageCallback = callback;
|
||||||
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) {
|
T& onPublish(espMqttClientTypes::OnPublishCallback callback) {
|
||||||
return setWill(topic, qos, retain, reinterpret_cast<const uint8_t*>(payload), strlen(payload));
|
_onPublishCallback = callback;
|
||||||
}
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
void setServer(IPAddress ip, uint16_t port) {
|
/*
|
||||||
_ip = ip;
|
T& onError(espMqttClientTypes::OnErrorCallback callback) {
|
||||||
_port = port;
|
_onErrorCallback = callback;
|
||||||
_useIp = true;
|
return static_cast<T&>(*this);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
protected:
|
||||||
|
|
||||||
void setServer(const char* host, uint16_t port) {
|
|
||||||
_host = host;
|
|
||||||
_port = port;
|
|
||||||
_useIp = false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onConnect(espMqttClientTypes::OnConnectCallback callback) {
|
|
||||||
_onConnectCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
|
|
||||||
_onDisconnectCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
|
|
||||||
_onSubscribeCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
|
|
||||||
_onUnsubscribeCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onMessage(espMqttClientTypes::OnMessageCallback callback) {
|
|
||||||
_onMessageCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onPublish(espMqttClientTypes::OnPublishCallback callback) {
|
|
||||||
_onPublishCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
void onError(espMqttClientTypes::OnErrorCallback callback) {
|
|
||||||
_onErrorCallback = callback;
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
protected:
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1)
|
explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1)
|
||||||
: MqttClient(useTask, priority, core) {}
|
: MqttClient(useTask, priority, core) {}
|
||||||
#else
|
#else
|
||||||
MqttClientSetup()
|
MqttClientSetup()
|
||||||
: MqttClient() {}
|
: MqttClient() {}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,61 +13,61 @@ the LICENSE file.
|
|||||||
|
|
||||||
namespace espMqttClientInternals {
|
namespace espMqttClientInternals {
|
||||||
|
|
||||||
ClientSecureSync::ClientSecureSync(WiFiClientSecure* wiFiClient)
|
ClientSecureSync::ClientSecureSync()
|
||||||
: client(wiFiClient) {
|
: client() {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSecureSync::connect(IPAddress ip, uint16_t port) {
|
bool ClientSecureSync::connect(IPAddress ip, uint16_t port) {
|
||||||
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
|
||||||
int val = true;
|
int val = true;
|
||||||
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSecureSync::connect(const char* host, uint16_t port) {
|
bool ClientSecureSync::connect(const char* host, uint16_t port) {
|
||||||
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
|
||||||
int val = true;
|
int val = true;
|
||||||
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ClientSecureSync::write(const uint8_t* buf, size_t size) {
|
size_t ClientSecureSync::write(const uint8_t* buf, size_t size) {
|
||||||
return client->write(buf, size);
|
return client.write(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSecureSync::available() {
|
int ClientSecureSync::available() {
|
||||||
return client->available();
|
return client.available();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSecureSync::read(uint8_t* buf, size_t size) {
|
int ClientSecureSync::read(uint8_t* buf, size_t size) {
|
||||||
return client->read(buf, size);
|
return client.read(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientSecureSync::stop() {
|
void ClientSecureSync::stop() {
|
||||||
client->stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSecureSync::connected() {
|
bool ClientSecureSync::connected() {
|
||||||
return client->connected();
|
return client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSecureSync::disconnected() {
|
bool ClientSecureSync::disconnected() {
|
||||||
return !client->connected();
|
return !client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace espMqttClientInternals {
|
|||||||
|
|
||||||
class ClientSecureSync : public Transport {
|
class ClientSecureSync : public Transport {
|
||||||
public:
|
public:
|
||||||
ClientSecureSync(WiFiClientSecure* wiFiClient);
|
ClientSecureSync();
|
||||||
bool connect(IPAddress ip, uint16_t port) override;
|
bool connect(IPAddress ip, uint16_t port) override;
|
||||||
bool connect(const char* host, uint16_t port) override;
|
bool connect(const char* host, uint16_t port) override;
|
||||||
size_t write(const uint8_t* buf, size_t size) override;
|
size_t write(const uint8_t* buf, size_t size) override;
|
||||||
@@ -27,7 +27,7 @@ class ClientSecureSync : public Transport {
|
|||||||
void stop() override;
|
void stop() override;
|
||||||
bool connected() override;
|
bool connected() override;
|
||||||
bool disconnected() override;
|
bool disconnected() override;
|
||||||
WiFiClientSecure* client;
|
WiFiClientSecure client;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
|
|||||||
@@ -13,61 +13,61 @@ the LICENSE file.
|
|||||||
|
|
||||||
namespace espMqttClientInternals {
|
namespace espMqttClientInternals {
|
||||||
|
|
||||||
ClientSync::ClientSync(WiFiClient* wiFiClient)
|
ClientSync::ClientSync()
|
||||||
: client(wiFiClient) {
|
: client() {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSync::connect(IPAddress ip, uint16_t port) {
|
bool ClientSync::connect(IPAddress ip, uint16_t port) {
|
||||||
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||||
int val = true;
|
int val = true;
|
||||||
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSync::connect(const char* host, uint16_t port) {
|
bool ClientSync::connect(const char* host, uint16_t port) {
|
||||||
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||||
int val = true;
|
int val = true;
|
||||||
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ClientSync::write(const uint8_t* buf, size_t size) {
|
size_t ClientSync::write(const uint8_t* buf, size_t size) {
|
||||||
return client->write(buf, size);
|
return client.write(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSync::available() {
|
int ClientSync::available() {
|
||||||
return client->available();
|
return client.available();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSync::read(uint8_t* buf, size_t size) {
|
int ClientSync::read(uint8_t* buf, size_t size) {
|
||||||
return client->read(buf, size);
|
return client.read(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientSync::stop() {
|
void ClientSync::stop() {
|
||||||
client->stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSync::connected() {
|
bool ClientSync::connected() {
|
||||||
return client->connected();
|
return client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSync::disconnected() {
|
bool ClientSync::disconnected() {
|
||||||
return !client->connected();
|
return !client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace espMqttClientInternals {
|
|||||||
|
|
||||||
class ClientSync : public Transport {
|
class ClientSync : public Transport {
|
||||||
public:
|
public:
|
||||||
ClientSync(WiFiClient* wiFiClient);
|
ClientSync();
|
||||||
bool connect(IPAddress ip, uint16_t port) override;
|
bool connect(IPAddress ip, uint16_t port) override;
|
||||||
bool connect(const char* host, uint16_t port) override;
|
bool connect(const char* host, uint16_t port) override;
|
||||||
size_t write(const uint8_t* buf, size_t size) override;
|
size_t write(const uint8_t* buf, size_t size) override;
|
||||||
@@ -27,7 +27,7 @@ class ClientSync : public Transport {
|
|||||||
void stop() override;
|
void stop() override;
|
||||||
bool connected() override;
|
bool connected() override;
|
||||||
bool disconnected() override;
|
bool disconnected() override;
|
||||||
WiFiClient* client;
|
WiFiClient client;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ the LICENSE file.
|
|||||||
#include "espMqttClient.h"
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClient::espMqttClient(WiFiClient* wiFiClient, uint8_t priority, uint8_t core)
|
espMqttClient::espMqttClient(uint8_t priority, uint8_t core)
|
||||||
: MqttClientSetup(true, priority, core)
|
: MqttClientSetup(true, priority, core)
|
||||||
, _client(wiFiClient) {
|
, _client() {
|
||||||
#else
|
#else
|
||||||
espMqttClient::espMqttClient()
|
espMqttClient::espMqttClient()
|
||||||
: _client() {
|
: _client() {
|
||||||
@@ -21,9 +21,9 @@ espMqttClient::espMqttClient()
|
|||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClientSecure::espMqttClientSecure(WiFiClientSecure* wiFiClient, uint8_t priority, uint8_t core)
|
espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
|
||||||
: MqttClientSetup(priority, core)
|
: MqttClientSetup(priority, core)
|
||||||
, _client(wiFiClient) {
|
, _client() {
|
||||||
#else
|
#else
|
||||||
espMqttClientSecure::espMqttClientSecure()
|
espMqttClientSecure::espMqttClientSecure()
|
||||||
: _client() {
|
: _client() {
|
||||||
@@ -32,66 +32,55 @@ espMqttClientSecure::espMqttClientSecure()
|
|||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setInsecure() {
|
espMqttClientSecure& espMqttClientSecure::setInsecure() {
|
||||||
_client.client->setInsecure();
|
_client.client.setInsecure();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
|
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
|
||||||
_client.client->setCACert(rootCA);
|
_client.client.setCACert(rootCA);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
|
espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
|
||||||
_client.client->setCertificate(clientCa);
|
_client.client.setCertificate(clientCa);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
|
espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
|
||||||
_client.client->setPrivateKey(privateKey);
|
_client.client.setPrivateKey(privateKey);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
|
espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
|
||||||
_client.client->setPreSharedKey(pskIdent, psKey);
|
_client.client.setPreSharedKey(pskIdent, psKey);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||||
espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) {
|
espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) {
|
||||||
_client.client->setFingerprint(fingerprint);
|
_client.client.setFingerprint(fingerprint);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) {
|
espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) {
|
||||||
_client.client->setTrustAnchors(ta);
|
_client.client.setTrustAnchors(ta);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
|
espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
|
||||||
_client.client->setClientRSACert(cert, sk);
|
_client.client.setClientRSACert(cert, sk);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) {
|
espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) {
|
||||||
_client.client->setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type);
|
_client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) {
|
espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) {
|
||||||
_client.client->setCertStore(certStore);
|
_client.client.setCertStore(certStore);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientEthernet::espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority, uint8_t core)
|
|
||||||
: MqttClientSetup(true, priority, core)
|
|
||||||
, _client(ethernetClient) {
|
|
||||||
#else
|
|
||||||
espMqttClientEthernet::espMqttClientEthernet()
|
|
||||||
: _client() {
|
|
||||||
#endif
|
|
||||||
_transport = &_client;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -19,64 +19,47 @@ the LICENSE file.
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "MqttClientSetup.h"
|
#include "MqttClientSetup.h"
|
||||||
#include "Transport/ClientSyncEthernet.h"
|
|
||||||
|
|
||||||
class espMqttClient : public MqttClientSetup {
|
class espMqttClient : public MqttClientSetup<espMqttClient> {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
explicit espMqttClient(WiFiClient* wiFiClient, uint8_t priority = 1, uint8_t core = 1);
|
explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1);
|
||||||
#else
|
#else
|
||||||
espMqttClient();
|
espMqttClient();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClientInternals::ClientSync _client;
|
espMqttClientInternals::ClientSync _client;
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
espMqttClientInternals::ClientPosix _client;
|
espMqttClientInternals::ClientPosix _client;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
class espMqttClientSecure : public MqttClientSetup {
|
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
explicit espMqttClientSecure(WiFiClientSecure* wiFiClient, uint8_t priority = 1, uint8_t core = 1);
|
explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1);
|
||||||
#else
|
#else
|
||||||
espMqttClientSecure();
|
espMqttClientSecure();
|
||||||
#endif
|
#endif
|
||||||
espMqttClientSecure& setInsecure();
|
espMqttClientSecure& setInsecure();
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClientSecure& setCACert(const char* rootCA);
|
espMqttClientSecure& setCACert(const char* rootCA);
|
||||||
espMqttClientSecure& setCertificate(const char* clientCa);
|
espMqttClientSecure& setCertificate(const char* clientCa);
|
||||||
espMqttClientSecure& setPrivateKey(const char* privateKey);
|
espMqttClientSecure& setPrivateKey(const char* privateKey);
|
||||||
espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey);
|
espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey);
|
||||||
#else
|
#else
|
||||||
espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]);
|
espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]);
|
||||||
espMqttClientSecure& setTrustAnchors(const X509List *ta);
|
espMqttClientSecure& setTrustAnchors(const X509List *ta);
|
||||||
espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk);
|
espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk);
|
||||||
espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type);
|
espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type);
|
||||||
espMqttClientSecure& setCertStore(CertStoreBase *certStore);
|
espMqttClientSecure& setCertStore(CertStoreBase *certStore);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
espMqttClientInternals::ClientSecureSync _client;
|
espMqttClientInternals::ClientSecureSync _client;
|
||||||
};
|
|
||||||
|
|
||||||
class espMqttClientEthernet : public MqttClientSetup {
|
|
||||||
public:
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
explicit espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority = 1, uint8_t core = 1);
|
|
||||||
#else
|
|
||||||
espMqttClient();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
protected:
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientInternals::ClientSyncEthernet _client;
|
|
||||||
#elif defined(__linux__)
|
|
||||||
espMqttClientInternals::ClientPosix _client;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ the LICENSE file.
|
|||||||
|
|
||||||
#include "MqttClientSetup.h"
|
#include "MqttClientSetup.h"
|
||||||
|
|
||||||
class espMqttClientAsync : public MqttClientSetup {
|
class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
explicit espMqttClientAsync(uint8_t priority = 1, uint8_t core = 1);
|
explicit espMqttClientAsync(uint8_t priority = 1, uint8_t core = 1);
|
||||||
|
|||||||
@@ -13,65 +13,65 @@ the LICENSE file.
|
|||||||
|
|
||||||
namespace espMqttClientInternals {
|
namespace espMqttClientInternals {
|
||||||
|
|
||||||
ClientSyncEthernet::ClientSyncEthernet(EthernetClient* ethernetClient)
|
ClientSyncEthernet::ClientSyncEthernet()
|
||||||
: client(ethernetClient) {
|
: client() {
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSyncEthernet::connect(IPAddress ip, uint16_t port) {
|
bool ClientSyncEthernet::connect(IPAddress ip, uint16_t port) {
|
||||||
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||||
int val = true;
|
int val = true;
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSyncEthernet::connect(const char* host, uint16_t port) {
|
bool ClientSyncEthernet::connect(const char* host, uint16_t port) {
|
||||||
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
|
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
|
||||||
if (ret) {
|
if (ret) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
client->setNoDelay(true);
|
client.setNoDelay(true);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
|
||||||
int val = true;
|
int val = true;
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ClientSyncEthernet::write(const uint8_t* buf, size_t size) {
|
size_t ClientSyncEthernet::write(const uint8_t* buf, size_t size) {
|
||||||
return client->write(buf, size);
|
return client.write(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSyncEthernet::available() {
|
int ClientSyncEthernet::available() {
|
||||||
return client->available();
|
return client.available();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientSyncEthernet::read(uint8_t* buf, size_t size) {
|
int ClientSyncEthernet::read(uint8_t* buf, size_t size) {
|
||||||
return client->read(buf, size);
|
return client.read(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientSyncEthernet::stop() {
|
void ClientSyncEthernet::stop() {
|
||||||
client->stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSyncEthernet::connected() {
|
bool ClientSyncEthernet::connected() {
|
||||||
return client->connected();
|
return client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientSyncEthernet::disconnected() {
|
bool ClientSyncEthernet::disconnected() {
|
||||||
return !client->connected();
|
return !client.connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
|
|
||||||
#include "Transport.h"
|
#include "Transport/Transport.h"
|
||||||
#include "EthernetClient.h"
|
#include "EthernetClient.h"
|
||||||
|
|
||||||
namespace espMqttClientInternals {
|
namespace espMqttClientInternals {
|
||||||
|
|
||||||
class ClientSyncEthernet : public Transport {
|
class ClientSyncEthernet : public Transport {
|
||||||
public:
|
public:
|
||||||
ClientSyncEthernet(EthernetClient* ethernetClient);
|
ClientSyncEthernet();
|
||||||
bool connect(IPAddress ip, uint16_t port) override;
|
bool connect(IPAddress ip, uint16_t port) override;
|
||||||
bool connect(const char* host, uint16_t port) override;
|
bool connect(const char* host, uint16_t port) override;
|
||||||
size_t write(const uint8_t* buf, size_t size) override;
|
size_t write(const uint8_t* buf, size_t size) override;
|
||||||
@@ -18,7 +18,7 @@ namespace espMqttClientInternals {
|
|||||||
void stop() override;
|
void stop() override;
|
||||||
bool connected() override;
|
bool connected() override;
|
||||||
bool disconnected() override;
|
bool disconnected() override;
|
||||||
EthernetClient* client;
|
EthernetClient client;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace espMqttClientInternals
|
} // namespace espMqttClientInternals
|
||||||
@@ -17,8 +17,6 @@ public:
|
|||||||
: _hostname(hostname)
|
: _hostname(hostname)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual MqttClientSetup* mqttClient() = 0;
|
|
||||||
|
|
||||||
virtual void initialize() = 0;
|
virtual void initialize() = 0;
|
||||||
virtual ReconnectStatus reconnect() = 0;
|
virtual ReconnectStatus reconnect() = 0;
|
||||||
virtual void reconfigure() = 0;
|
virtual void reconfigure() = 0;
|
||||||
@@ -29,6 +27,17 @@ public:
|
|||||||
virtual bool isConnected() = 0;
|
virtual bool isConnected() = 0;
|
||||||
virtual int8_t signalStrength() = 0;
|
virtual int8_t signalStrength() = 0;
|
||||||
|
|
||||||
|
virtual void mqttSetClientId(const char* clientId) = 0;
|
||||||
|
virtual void mqttSetCleanSession(bool cleanSession) = 0;
|
||||||
|
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload) = 0;
|
||||||
|
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) = 0;
|
||||||
|
virtual bool mqttConnected() const = 0;
|
||||||
|
virtual void mqttSetServer(const char* host, uint16_t port) = 0;
|
||||||
|
virtual bool mqttConnect() = 0;
|
||||||
|
virtual void mqttSetCredentials(const char* username, const char* password) = 0;
|
||||||
|
virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) = 0;
|
||||||
|
virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const uint16_t _mqttMaxBufferSize = 6144;
|
const uint16_t _mqttMaxBufferSize = 6144;
|
||||||
const String _hostname;
|
const String _hostname;
|
||||||
|
|||||||
@@ -38,8 +38,6 @@ void W5500Device::initialize()
|
|||||||
resetDevice();
|
resetDevice();
|
||||||
|
|
||||||
Ethernet.init(ETHERNET_CS_PIN);
|
Ethernet.init(ETHERNET_CS_PIN);
|
||||||
_ethClient = new EthernetClient();
|
|
||||||
_mqttClient = new espMqttClientEthernet(_ethClient);
|
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||||
{
|
{
|
||||||
@@ -49,7 +47,7 @@ void W5500Device::initialize()
|
|||||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||||
pathStr.concat(mqtt_topic_log);
|
pathStr.concat(mqtt_topic_log);
|
||||||
strcpy(_path, pathStr.c_str());
|
strcpy(_path, pathStr.c_str());
|
||||||
Log = new MqttLogger(*_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
|
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial);
|
||||||
}
|
}
|
||||||
|
|
||||||
reconnect();
|
reconnect();
|
||||||
@@ -185,8 +183,52 @@ int8_t W5500Device::signalStrength()
|
|||||||
return 127;
|
return 127;
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttClientSetup *W5500Device::mqttClient()
|
void W5500Device::mqttSetClientId(const char *clientId)
|
||||||
{
|
{
|
||||||
return _mqttClient;
|
_mqttClient.setClientId(clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void W5500Device::mqttSetCleanSession(bool cleanSession)
|
||||||
|
{
|
||||||
|
_mqttClient.setCleanSession(cleanSession);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||||
|
{
|
||||||
|
return _mqttClient.publish(topic, qos, retain, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool W5500Device::mqttConnected() const
|
||||||
|
{
|
||||||
|
return _mqttClient.connected();
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500Device::mqttSetServer(const char *host, uint16_t port)
|
||||||
|
{
|
||||||
|
_mqttClient.setServer(host, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool W5500Device::mqttConnect()
|
||||||
|
{
|
||||||
|
return _mqttClient.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500Device::mqttSetCredentials(const char *username, const char *password)
|
||||||
|
{
|
||||||
|
_mqttClient.setCredentials(username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
void W5500Device::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||||
|
{
|
||||||
|
_mqttClient.onMessage(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t W5500Device::mqttSubscribe(const char *topic, uint8_t qos)
|
||||||
|
{
|
||||||
|
return _mqttClient.subscribe(topic, qos);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
|
||||||
|
{
|
||||||
|
return _mqttClient.publish(topic, qos, retain, payload, length);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "espMqttClient.h"
|
#include "espMqttClient.h"
|
||||||
|
#include "espMqttClientEthernet.h"
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
|
|
||||||
@@ -22,14 +23,31 @@ public:
|
|||||||
|
|
||||||
int8_t signalStrength() override;
|
int8_t signalStrength() override;
|
||||||
|
|
||||||
virtual MqttClientSetup* mqttClient();
|
void mqttSetClientId(const char *clientId) override;
|
||||||
|
|
||||||
|
void mqttSetCleanSession(bool cleanSession) override;
|
||||||
|
|
||||||
|
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override;
|
||||||
|
|
||||||
|
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override;
|
||||||
|
|
||||||
|
bool mqttConnected() const override;
|
||||||
|
|
||||||
|
void mqttSetServer(const char *host, uint16_t port) override;
|
||||||
|
|
||||||
|
bool mqttConnect() override;
|
||||||
|
|
||||||
|
void mqttSetCredentials(const char *username, const char *password) override;
|
||||||
|
|
||||||
|
void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;
|
||||||
|
|
||||||
|
uint16_t mqttSubscribe(const char *topic, uint8_t qos) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void resetDevice();
|
void resetDevice();
|
||||||
void initializeMacAddress(byte* mac);
|
void initializeMacAddress(byte* mac);
|
||||||
|
|
||||||
EthernetClient* _ethClient = nullptr;
|
espMqttClientEthernet _mqttClient;
|
||||||
MqttClientSetup* _mqttClient = nullptr;
|
|
||||||
Preferences* _preferences = nullptr;
|
Preferences* _preferences = nullptr;
|
||||||
|
|
||||||
int _maintainResult = 0;
|
int _maintainResult = 0;
|
||||||
|
|||||||
@@ -18,26 +18,26 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
|||||||
size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE);
|
size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE);
|
||||||
size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE);
|
size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE);
|
||||||
|
|
||||||
if(caLength > 1) // length is 1 when empty
|
_useEncryption = caLength > 1;
|
||||||
|
|
||||||
|
if(_useEncryption > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT over TLS."));
|
Log->println(F("MQTT over TLS."));
|
||||||
Log->println(_ca);
|
Log->println(_ca);
|
||||||
_wifiClientSecure = new WiFiClientSecure();
|
_mqttClientSecure = new espMqttClientSecure();
|
||||||
_wifiClientSecure->setCACert(_ca);
|
_mqttClientSecure->setCACert(_ca);
|
||||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT with client certificate."));
|
Log->println(F("MQTT with client certificate."));
|
||||||
Log->println(_cert);
|
Log->println(_cert);
|
||||||
Log->println(_key);
|
Log->println(_key);
|
||||||
_wifiClientSecure->setCertificate(_cert);
|
_mqttClientSecure->setCertificate(_cert);
|
||||||
_wifiClientSecure->setPrivateKey(_key);
|
_mqttClientSecure->setPrivateKey(_key);
|
||||||
}
|
}
|
||||||
_mqttClient = new espMqttClientSecure(_wifiClientSecure);
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT without TLS."));
|
Log->println(F("MQTT without TLS."));
|
||||||
_wifiClient = new WiFiClient();
|
_mqttClient = new espMqttClient();
|
||||||
_mqttClient = new espMqttClient(_wifiClient);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||||
@@ -48,7 +48,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
|||||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||||
pathStr.concat(mqtt_topic_log);
|
pathStr.concat(mqtt_topic_log);
|
||||||
strcpy(_path, pathStr.c_str());
|
strcpy(_path, pathStr.c_str());
|
||||||
Log = new MqttLogger(*_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
|
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,12 +108,12 @@ void WifiDevice::reconfigure()
|
|||||||
|
|
||||||
void WifiDevice::printError()
|
void WifiDevice::printError()
|
||||||
{
|
{
|
||||||
if(_wifiClientSecure != nullptr)
|
// if(_wifiClientSecure != nullptr)
|
||||||
{
|
// {
|
||||||
char lastError[100];
|
// char lastError[100];
|
||||||
_wifiClientSecure->lastError(lastError,100);
|
// _wifiClientSecure->lastError(lastError,100);
|
||||||
Log->println(lastError);
|
// Log->println(lastError);
|
||||||
}
|
// }
|
||||||
Log->print(F("Free Heap: "));
|
Log->print(F("Free Heap: "));
|
||||||
Log->println(ESP.getFreeHeap());
|
Log->println(ESP.getFreeHeap());
|
||||||
}
|
}
|
||||||
@@ -152,7 +152,122 @@ void WifiDevice::clearRtcInitVar(WiFiManager *)
|
|||||||
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttClientSetup *WifiDevice::mqttClient()
|
void WifiDevice::mqttSetClientId(const char *clientId)
|
||||||
{
|
{
|
||||||
return _mqttClient;
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
_mqttClientSecure->setClientId(clientId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mqttClient->setClientId(clientId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiDevice::mqttSetCleanSession(bool cleanSession)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
_mqttClientSecure->setCleanSession(cleanSession);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mqttClient->setCleanSession(cleanSession);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
return _mqttClientSecure->publish(topic, qos, retain, payload);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _mqttClient->publish(topic, qos, retain, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
return _mqttClientSecure->publish(topic, qos, retain, payload, length);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _mqttClient->publish(topic, qos, retain, payload, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WifiDevice::mqttConnected() const
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
return _mqttClientSecure->connected();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _mqttClient->connected();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiDevice::mqttSetServer(const char *host, uint16_t port)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
_mqttClientSecure->setServer(host, port);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mqttClient->setServer(host, port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WifiDevice::mqttConnect()
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
return _mqttClientSecure->connect();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _mqttClient->connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiDevice::mqttSetCredentials(const char *username, const char *password)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
_mqttClientSecure->setCredentials(username, password);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mqttClient->setCredentials(username, password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
_mqttClientSecure->onMessage(callback);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mqttClient->onMessage(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t WifiDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||||
|
{
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
return _mqttClientSecure->subscribe(topic, qos);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _mqttClient->subscribe(topic, qos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "WiFiManager.h"
|
#include "WiFiManager.h"
|
||||||
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
class WifiDevice : public NetworkDevice
|
class WifiDevice : public NetworkDevice
|
||||||
{
|
{
|
||||||
@@ -22,7 +23,25 @@ public:
|
|||||||
|
|
||||||
int8_t signalStrength() override;
|
int8_t signalStrength() override;
|
||||||
|
|
||||||
MqttClientSetup *mqttClient() override;
|
void mqttSetClientId(const char *clientId) override;
|
||||||
|
|
||||||
|
void mqttSetCleanSession(bool cleanSession) override;
|
||||||
|
|
||||||
|
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override;
|
||||||
|
|
||||||
|
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override;
|
||||||
|
|
||||||
|
bool mqttConnected() const override;
|
||||||
|
|
||||||
|
void mqttSetServer(const char *host, uint16_t port) override;
|
||||||
|
|
||||||
|
bool mqttConnect() override;
|
||||||
|
|
||||||
|
void mqttSetCredentials(const char *username, const char *password) override;
|
||||||
|
|
||||||
|
void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;
|
||||||
|
|
||||||
|
uint16_t mqttSubscribe(const char *topic, uint8_t qos) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void clearRtcInitVar(WiFiManager*);
|
static void clearRtcInitVar(WiFiManager*);
|
||||||
@@ -30,13 +49,13 @@ private:
|
|||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
|
|
||||||
WiFiManager _wm;
|
WiFiManager _wm;
|
||||||
WiFiClient* _wifiClient = nullptr;
|
espMqttClient* _mqttClient = nullptr;
|
||||||
WiFiClientSecure* _wifiClientSecure = nullptr;
|
espMqttClientSecure* _mqttClientSecure = nullptr;
|
||||||
MqttClientSetup* _mqttClient = nullptr;
|
|
||||||
// SpiffsCookie _cookie;
|
|
||||||
bool _restartOnDisconnect = false;
|
bool _restartOnDisconnect = false;
|
||||||
bool _startAp = false;
|
bool _startAp = false;
|
||||||
char* _path;
|
char* _path;
|
||||||
|
bool _useEncryption = false;
|
||||||
|
|
||||||
char _ca[TLS_CA_MAX_SIZE];
|
char _ca[TLS_CA_MAX_SIZE];
|
||||||
char _cert[TLS_CERT_MAX_SIZE];
|
char _cert[TLS_CERT_MAX_SIZE];
|
||||||
|
|||||||
8
networkDevices/espMqttClientEthernet.cpp
Normal file
8
networkDevices/espMqttClientEthernet.cpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "espMqttClientEthernet.h"
|
||||||
|
|
||||||
|
espMqttClientEthernet::espMqttClientEthernet(uint8_t priority, uint8_t core)
|
||||||
|
: MqttClientSetup(true, priority, core),
|
||||||
|
_client()
|
||||||
|
{
|
||||||
|
_transport = &_client;
|
||||||
|
}
|
||||||
20
networkDevices/espMqttClientEthernet.h
Normal file
20
networkDevices/espMqttClientEthernet.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "MqttClientSetup.h"
|
||||||
|
#include "ClientSyncEthernet.h"
|
||||||
|
|
||||||
|
class espMqttClientEthernet : public MqttClientSetup<espMqttClientEthernet> {
|
||||||
|
public:
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
explicit espMqttClientEthernet(uint8_t priority = 1, uint8_t core = 1);
|
||||||
|
#else
|
||||||
|
espMqttClient();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
protected:
|
||||||
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
|
espMqttClientInternals::ClientSyncEthernet _client;
|
||||||
|
#elif defined(__linux__)
|
||||||
|
espMqttClientInternals::ClientPosix _client;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user