mirror of https://github.com/arendst/Tasmota.git
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:
parent
8091dfca11
commit
0ad250c10b
|
@ -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 support for Michael Haustein ESP Switch
|
||||
* Add support for EXS Relay V5.0 (#3810)
|
||||
|
|
|
@ -20,6 +20,11 @@
|
|||
#ifndef _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
|
||||
\*********************************************************************************************/
|
||||
|
@ -136,7 +141,7 @@ typedef unsigned long power_t; // Power (Relay) type
|
|||
|
||||
#define MQTT_PUBSUBCLIENT 1 // Mqtt PubSubClient library
|
||||
#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
|
||||
#define DAWN_NORMAL -0.8333
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#ifndef _SONOFF_VERSION_H_
|
||||
#define _SONOFF_VERSION_H_
|
||||
|
||||
#define VERSION 0x0602010A
|
||||
#define VERSION 0x0602010B
|
||||
|
||||
#define D_PROGRAMNAME "Sonoff-Tasmota"
|
||||
#define D_AUTHOR "Theo Arends"
|
||||
|
|
|
@ -876,7 +876,7 @@ void GetFeatures()
|
|||
#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
|
||||
feature_drv1 |= 0x00000800; // xdrv_01_mqtt.ino
|
||||
#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
|
||||
#endif
|
||||
#ifdef MQTT_HOST_DISCOVERY
|
||||
|
@ -933,6 +933,9 @@ void GetFeatures()
|
|||
#ifdef USE_SMARTCONFIG
|
||||
feature_drv1 |= 0x40000000; // support.ino
|
||||
#endif
|
||||
#if (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
|
||||
feature_drv1 |= 0x80000000; // xdrv_01_mqtt.ino
|
||||
#endif
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
||||
|
|
|
@ -216,14 +216,14 @@
|
|||
//#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.
|
||||
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library
|
||||
// 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
|
||||
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support
|
||||
//#define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO // Use (patched) esp-mqtt-arduino library (+4k8 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. TLS should work but needs to be tested.
|
||||
//#define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT // Use arduino-mqtt (lwmqtt) library (+3k3 code, +2k mem)
|
||||
|
||||
// -- MQTT ----------------------------------------
|
||||
#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on)
|
||||
|
|
|
@ -23,19 +23,29 @@
|
|||
// 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
|
||||
// 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
|
||||
// Alternative MQTT driver does not block network when MQTT server is unavailable. No TLS support
|
||||
//#define MQTT_LIBRARY_TYPE MQTT_ESPMQTTARDUINO // Use (patched) esp-mqtt-arduino library (+4k8 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. TLS should work but needs to be tested.
|
||||
//#define MQTT_LIBRARY_TYPE MQTT_ARDUINOMQTT // Use arduino-mqtt (lwmqtt) library (+3k3 code, +2k mem)
|
||||
|
||||
#ifdef USE_MQTT_TLS
|
||||
|
||||
/*
|
||||
#ifdef MQTT_LIBRARY_TYPE
|
||||
#undef MQTT_LIBRARY_TYPE
|
||||
#endif
|
||||
#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
|
||||
|
||||
#ifndef MQTT_LIBRARY_TYPE
|
||||
#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Use PubSubClient library as default
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
@ -136,48 +146,48 @@ void MqttLoop()
|
|||
{
|
||||
}
|
||||
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) /*******************************************/
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT) /**********************************************/
|
||||
|
||||
#include <MQTT.h>
|
||||
MQTT *MqttClient = NULL;
|
||||
#include <MQTTClient.h>
|
||||
MQTTClient MqttClient(MQTT_MAX_PACKET_SIZE);
|
||||
|
||||
bool MqttIsConnected()
|
||||
{
|
||||
return mqtt_connected;
|
||||
return MqttClient.connected();
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
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);
|
||||
MqttDataHandler((char*)topic.c_str(), (byte*)data.c_str(), data.length());
|
||||
}
|
||||
|
||||
void MqttSubscribeLib(char *topic)
|
||||
{
|
||||
MqttClient->subscribe(topic);
|
||||
MqttClient.subscribe(topic, 0);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
MqttClient.loop();
|
||||
// delay(10);
|
||||
}
|
||||
|
||||
#endif // MQTT_LIBRARY_TYPE
|
||||
|
@ -467,6 +477,14 @@ void MqttReconnect()
|
|||
GetTopic_P(stopic, TELE, mqtt_topic, S_LWT);
|
||||
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
|
||||
#ifdef USE_MQTT_TLS
|
||||
if (!MqttCheckTls()) return;
|
||||
|
@ -479,25 +497,17 @@ void MqttReconnect()
|
|||
MqttClient.OnConnected(MqttConnected);
|
||||
MqttClient.OnDisconnected(MqttDisconnectedCb);
|
||||
MqttClient.OnData(MqttDataHandler);
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO)
|
||||
MqttClient = new MQTT(mqtt_client, Settings.mqtt_host, Settings.mqtt_port, stopic, 1, true, mqtt_data);
|
||||
MqttClient->setUserPwd(mqtt_user, mqtt_pwd);
|
||||
MqttClient->onConnected(MqttConnected);
|
||||
MqttClient->onDisconnected(MqttDisconnectedCb);
|
||||
MqttClient->onData(MqttMyDataCb);
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
|
||||
MqttClient.begin(Settings.mqtt_host, Settings.mqtt_port, EspClient);
|
||||
MqttClient.setWill(stopic, mqtt_data, true, 1);
|
||||
MqttClient.setOptions(MQTT_KEEPALIVE, true, 1000);
|
||||
// MqttClient.onMessageAdvanced(MqttMyDataCb);
|
||||
MqttClient.onMessage(MqttMyDataCb);
|
||||
#endif
|
||||
|
||||
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)
|
||||
MqttClient.setCallback(MqttDataHandler);
|
||||
MqttClient.setServer(Settings.mqtt_host, Settings.mqtt_port);
|
||||
|
@ -508,8 +518,12 @@ void MqttReconnect()
|
|||
}
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
|
||||
MqttClient.Connect();
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO)
|
||||
MqttClient->connect();
|
||||
#elif (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
#define HLW_IREF 4545 // 4.545A
|
||||
#define HLW_SEL_VOLTAGE 1
|
||||
|
||||
// HJL-01 based (BlitzWolf, Homecube, Gosund)
|
||||
// HJL-01 based (BlitzWolf, Homecube, Gosund, Teckin)
|
||||
#define HJL_PREF 1362
|
||||
#define HJL_UREF 822
|
||||
#define HJL_IREF 3300
|
||||
|
|
|
@ -105,7 +105,7 @@ a_features = [[
|
|||
"USE_WS2812_DMA","USE_IR_REMOTE","USE_IR_HVAC","USE_IR_RECEIVE",
|
||||
"USE_DOMOTICZ","USE_DISPLAY","USE_HOME_ASSISTANT","USE_SERIAL_BRIDGE",
|
||||
"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_KNX_NO_EMULATION","USE_DISPLAY_MODES1TO5","USE_DISPLAY_GRAPH","USE_DISPLAY_LCD",
|
||||
|
|
Loading…
Reference in New Issue