6.2.1.11 Add MQTT Client

6.2.1.11 20191002
 * Remove support for MQTT Client based on esp-mqtt-arduino by #define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO
 * Add support for MQTT Client based on lwmqtt to be selected by #define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT
This commit is contained in:
Theo Arends 2018-10-02 17:07:30 +02:00
parent 8091dfca11
commit 0ad250c10b
8 changed files with 71 additions and 45 deletions

View File

@ -1,4 +1,8 @@
/* 6.2.1.10 20180930 /* 6.2.1.11 20191002
* Remove support for MQTT Client based on esp-mqtt-arduino by #define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO
* Add support for MQTT Client based on lwmqtt to be selected by #define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT
*
* 6.2.1.10 20180930
* Add command RGBWWTable to support color calibration (#3933) * Add command RGBWWTable to support color calibration (#3933)
* Add support for Michael Haustein ESP Switch * Add support for Michael Haustein ESP Switch
* Add support for EXS Relay V5.0 (#3810) * Add support for EXS Relay V5.0 (#3810)

View File

@ -20,6 +20,11 @@
#ifndef _SONOFF_H_ #ifndef _SONOFF_H_
#define _SONOFF_H_ #define _SONOFF_H_
#define USE_DHT // Default DHT11 sensor needs no external library
#define USE_ENERGY_SENSOR // Use energy sensors (+14k code)
#define USE_HLW8012 // Use energy sensor for Sonoff Pow and WolfBlitz
#define USE_CSE7766 // Use energy sensor for Sonoff S31 and Pow R2
/*********************************************************************************************\ /*********************************************************************************************\
* Power Type * Power Type
\*********************************************************************************************/ \*********************************************************************************************/
@ -136,7 +141,7 @@ typedef unsigned long power_t; // Power (Relay) type
#define MQTT_PUBSUBCLIENT 1 // Mqtt PubSubClient library #define MQTT_PUBSUBCLIENT 1 // Mqtt PubSubClient library
#define MQTT_TASMOTAMQTT 2 // Mqtt TasmotaMqtt library based on esp-mqtt-arduino #define MQTT_TASMOTAMQTT 2 // Mqtt TasmotaMqtt library based on esp-mqtt-arduino
#define MQTT_ESPMQTTARDUINO 3 // Mqtt esp-mqtt-arduino library by Ingo Randolf #define MQTT_ARDUINOMQTT 3 // Mqtt arduino-mqtt library by Joel Gaehwiler (https://github.com/256dpi/arduino-mqtt)
// Sunrise and Sunset DawnType // Sunrise and Sunset DawnType
#define DAWN_NORMAL -0.8333 #define DAWN_NORMAL -0.8333

View File

@ -20,7 +20,7 @@
#ifndef _SONOFF_VERSION_H_ #ifndef _SONOFF_VERSION_H_
#define _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_
#define VERSION 0x0602010A #define VERSION 0x0602010B
#define D_PROGRAMNAME "Sonoff-Tasmota" #define D_PROGRAMNAME "Sonoff-Tasmota"
#define D_AUTHOR "Theo Arends" #define D_AUTHOR "Theo Arends"

View File

@ -876,7 +876,7 @@ void GetFeatures()
#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT) #if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
feature_drv1 |= 0x00000800; // xdrv_01_mqtt.ino feature_drv1 |= 0x00000800; // xdrv_01_mqtt.ino
#endif #endif
#if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) #if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) // Obsolete since 6.2.1.11
feature_drv1 |= 0x00001000; // xdrv_01_mqtt.ino feature_drv1 |= 0x00001000; // xdrv_01_mqtt.ino
#endif #endif
#ifdef MQTT_HOST_DISCOVERY #ifdef MQTT_HOST_DISCOVERY
@ -933,6 +933,9 @@ void GetFeatures()
#ifdef USE_SMARTCONFIG #ifdef USE_SMARTCONFIG
feature_drv1 |= 0x40000000; // support.ino feature_drv1 |= 0x40000000; // support.ino
#endif #endif
#if (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
feature_drv1 |= 0x80000000; // xdrv_01_mqtt.ino
#endif
/*********************************************************************************************/ /*********************************************************************************************/

View File

@ -216,14 +216,14 @@
//#define USE_ARDUINO_OTA // Add optional support for Arduino OTA (+13k code) //#define USE_ARDUINO_OTA // Add optional support for Arduino OTA (+13k code)
/*-------------------------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------------------------*\
* Select ONE of possible three MQTT library types below * Select ONE of possible MQTT library types below
\*-------------------------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------------------------*/
// Default MQTT driver for both non-TLS and TLS connections. Blocks network if MQTT server is unavailable. // Default MQTT driver for both non-TLS and TLS connections. Blocks network if MQTT server is unavailable.
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library #define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support // Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support
//#define MQTT_LIBRARY_TYPE MQTT_TASMOTAMQTT // Use TasmotaMqtt library (+4k4 code, +4k mem) - non-TLS only //#define MQTT_LIBRARY_TYPE MQTT_TASMOTAMQTT // Use TasmotaMqtt library (+4k4 (core 2.3.0), +14k4 (core 2.4.2 lwip2) code, +4k mem) - non-TLS only
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support // Alternative MQTT driver does not block network when MQTT server is unavailable. TLS should work but needs to be tested.
//#define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO // Use (patched) esp-mqtt-arduino library (+4k8 code, +4k mem) - non-TLS only //#define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT // Use arduino-mqtt (lwmqtt) library (+3k3 code, +2k mem)
// -- MQTT ---------------------------------------- // -- MQTT ----------------------------------------
#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on) #define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on)

View File

@ -23,19 +23,29 @@
// Default MQTT driver for both non-TLS and TLS connections. Blocks network if MQTT server is unavailable. // Default MQTT driver for both non-TLS and TLS connections. Blocks network if MQTT server is unavailable.
//#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library //#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support // Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support
//#define MQTT_LIBRARY_TYPE MQTT_TASMOTAMQTT // Use TasmotaMqtt library (+4k4 code, +4k mem) - non-TLS only //#define MQTT_LIBRARY_TYPE MQTT_TASMOTAMQTT // Use TasmotaMqtt library (+4k4 (core 2.3.0), +14k4 (core 2.4.2 lwip2) code, +4k mem) - non-TLS only
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support // Alternative MQTT driver does not block network when MQTT server is unavailable. TLS should work but needs to be tested.
//#define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO // Use (patched) esp-mqtt-arduino library (+4k8 code, +4k mem) - non-TLS only //#define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT // Use arduino-mqtt (lwmqtt) library (+3k3 code, +2k mem)
#ifdef USE_MQTT_TLS #ifdef USE_MQTT_TLS
/*
#ifdef MQTT_LIBRARY_TYPE #ifdef MQTT_LIBRARY_TYPE
#undef MQTT_LIBRARY_TYPE #undef MQTT_LIBRARY_TYPE
#endif #endif
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as it only supports TLS #define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as it only supports TLS
*/
#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
#undef MQTT_LIBRARY_TYPE
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as it only supports TLS
#endif
#else #else
#ifndef MQTT_LIBRARY_TYPE #ifndef MQTT_LIBRARY_TYPE
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as default #define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as default
#endif #endif
#endif #endif
/*********************************************************************************************/ /*********************************************************************************************/
@ -136,48 +146,48 @@ void MqttLoop()
{ {
} }
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) /*******************************************/ #elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT) /**********************************************/
#include <MQTT.h> #include <MQTTClient.h>
MQTT *MqttClient = NULL; MQTTClient MqttClient(MQTT_MAX_PACKET_SIZE);
bool MqttIsConnected() bool MqttIsConnected()
{ {
return mqtt_connected; return MqttClient.connected();
} }
void MqttDisconnect() void MqttDisconnect()
{ {
if (MqttClient) MqttClient->disconnect(); MqttClient.disconnect();
} }
void MqttDisconnectedCb() /*
void MqttMyDataCb(MQTTClient* client, char* topic, char* data, int data_len)
//void MqttMyDataCb(MQTTClient *client, char topic[], char data[], int data_len)
{ {
MqttDisconnected(MqttClient->getState()); // status codes are documented in file mqtt.h as tConnState // MqttDataHandler((char*)topic, (byte*)data, data_len);
} }
*/
void MqttMyDataCb(const char* topic, uint32_t topic_len, const char* data, uint32_t data_len) void MqttMyDataCb(String &topic, String &data)
{ {
char topic_copy[topic_len +1]; MqttDataHandler((char*)topic.c_str(), (byte*)data.c_str(), data.length());
memcpy(topic_copy, topic, topic_len);
topic_copy[topic_len] = 0;
if (0 == data_len) data = (const char*)&topic_copy + topic_len;
MqttDataHandler((char*)topic_copy, (byte*)data, data_len);
} }
void MqttSubscribeLib(char *topic) void MqttSubscribeLib(char *topic)
{ {
MqttClient->subscribe(topic); MqttClient.subscribe(topic, 0);
} }
bool MqttPublishLib(const char* topic, boolean retained) bool MqttPublishLib(const char* topic, boolean retained)
{ {
return MqttClient->publish(topic, mqtt_data, strlen(mqtt_data), 0, retained); return MqttClient.publish(topic, mqtt_data, strlen(mqtt_data), retained, 0);
} }
void MqttLoop() void MqttLoop()
{ {
MqttClient.loop();
// delay(10);
} }
#endif // MQTT_LIBRARY_TYPE #endif // MQTT_LIBRARY_TYPE
@ -467,6 +477,14 @@ void MqttReconnect()
GetTopic_P(stopic, TELE, mqtt_topic, S_LWT); GetTopic_P(stopic, TELE, mqtt_topic, S_LWT);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_OFFLINE); snprintf_P(mqtt_data, sizeof(mqtt_data), S_OFFLINE);
//#ifdef ARDUINO_ESP8266_RELEASE_2_4_1
#ifdef USE_MQTT_TLS
EspClient = WiFiClientSecure(); // Wifi Secure Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497)
#else
EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497)
#endif
//#endif
if (2 == mqtt_initial_connection_state) { // Executed once just after power on and wifi is connected if (2 == mqtt_initial_connection_state) { // Executed once just after power on and wifi is connected
#ifdef USE_MQTT_TLS #ifdef USE_MQTT_TLS
if (!MqttCheckTls()) return; if (!MqttCheckTls()) return;
@ -479,25 +497,17 @@ void MqttReconnect()
MqttClient.OnConnected(MqttConnected); MqttClient.OnConnected(MqttConnected);
MqttClient.OnDisconnected(MqttDisconnectedCb); MqttClient.OnDisconnected(MqttDisconnectedCb);
MqttClient.OnData(MqttDataHandler); MqttClient.OnData(MqttDataHandler);
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) #elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
MqttClient = new MQTT(mqtt_client, Settings.mqtt_host, Settings.mqtt_port, stopic, 1, true, mqtt_data); MqttClient.begin(Settings.mqtt_host, Settings.mqtt_port, EspClient);
MqttClient->setUserPwd(mqtt_user, mqtt_pwd); MqttClient.setWill(stopic, mqtt_data, true, 1);
MqttClient->onConnected(MqttConnected); MqttClient.setOptions(MQTT_KEEPALIVE, true, 1000);
MqttClient->onDisconnected(MqttDisconnectedCb); // MqttClient.onMessageAdvanced(MqttMyDataCb);
MqttClient->onData(MqttMyDataCb); MqttClient.onMessage(MqttMyDataCb);
#endif #endif
mqtt_initial_connection_state = 1; mqtt_initial_connection_state = 1;
} }
//#ifdef ARDUINO_ESP8266_RELEASE_2_4_1
#ifdef USE_MQTT_TLS
EspClient = WiFiClientSecure(); // Wifi Secure Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497)
#else
EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497)
#endif
//#endif
#if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT) #if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT)
MqttClient.setCallback(MqttDataHandler); MqttClient.setCallback(MqttDataHandler);
MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port); MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port);
@ -508,8 +518,12 @@ void MqttReconnect()
} }
#elif (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT) #elif (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
MqttClient.Connect(); MqttClient.Connect();
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) #elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
MqttClient->connect(); if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd)) {
MqttConnected();
} else {
MqttDisconnected(MqttClient.lastError()); // status codes are documented here https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L11
}
#endif // MQTT_LIBRARY_TYPE #endif // MQTT_LIBRARY_TYPE
} }

View File

@ -33,7 +33,7 @@
#define HLW_IREF 4545 // 4.545A #define HLW_IREF 4545 // 4.545A
#define HLW_SEL_VOLTAGE 1 #define HLW_SEL_VOLTAGE 1
// HJL-01 based (BlitzWolf, Homecube, Gosund) // HJL-01 based (BlitzWolf, Homecube, Gosund, Teckin)
#define HJL_PREF 1362 #define HJL_PREF 1362
#define HJL_UREF 822 #define HJL_UREF 822
#define HJL_IREF 3300 #define HJL_IREF 3300

View File

@ -105,7 +105,7 @@ a_features = [[
"USE_WS2812_DMA","USE_IR_REMOTE","USE_IR_HVAC","USE_IR_RECEIVE", "USE_WS2812_DMA","USE_IR_REMOTE","USE_IR_HVAC","USE_IR_RECEIVE",
"USE_DOMOTICZ","USE_DISPLAY","USE_HOME_ASSISTANT","USE_SERIAL_BRIDGE", "USE_DOMOTICZ","USE_DISPLAY","USE_HOME_ASSISTANT","USE_SERIAL_BRIDGE",
"USE_TIMERS","USE_SUNRISE","USE_TIMERS_WEB","USE_RULES", "USE_TIMERS","USE_SUNRISE","USE_TIMERS_WEB","USE_RULES",
"USE_KNX","USE_WPS","USE_SMARTCONFIG","" "USE_KNX","USE_WPS","USE_SMARTCONFIG","MQTT_ARDUINOMQTT"
],[ ],[
"USE_CONFIG_OVERRIDE","BE_MINIMAL","USE_SENSORS","USE_CLASSIC", "USE_CONFIG_OVERRIDE","BE_MINIMAL","USE_SENSORS","USE_CLASSIC",
"USE_KNX_NO_EMULATION","USE_DISPLAY_MODES1TO5","USE_DISPLAY_GRAPH","USE_DISPLAY_LCD", "USE_KNX_NO_EMULATION","USE_DISPLAY_MODES1TO5","USE_DISPLAY_GRAPH","USE_DISPLAY_LCD",