Add support for MQTT using Azure IoT Hub

Add support for MQTT using Azure IoT Hub by Kevin Saye (#11906)
This commit is contained in:
Theo Arends 2021-04-27 11:23:17 +02:00
parent 9595e9b759
commit 74156c9965
4 changed files with 53 additions and 51 deletions

View File

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added ### Added
- Initial support for optional ``Template`` JSON fieldpair ``"CMND":"<any template related command>;<any template related command>;..."`` (#11788) - Initial support for optional ``Template`` JSON fieldpair ``"CMND":"<any template related command>;<any template related command>;..."`` (#11788)
- ESP32 pulldown buttons ``Button_d`` and ``Button_id`` (#10814) - ESP32 pulldown buttons ``Button_d`` and ``Button_id`` (#10814)
- Support for MQTT using Azure IoT Hub by Kevin Saye (#11906)
## [Released] ## [Released]

View File

@ -80,6 +80,7 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota
### Added ### Added
- Initial support for optional ``Template`` JSON fieldpair ``"CMND":"<any template related command>;<any template related command>;..."`` [#11788](https://github.com/arendst/Tasmota/issues/11788) - Initial support for optional ``Template`` JSON fieldpair ``"CMND":"<any template related command>;<any template related command>;..."`` [#11788](https://github.com/arendst/Tasmota/issues/11788)
- ESP32 pulldown buttons ``Button_d`` and ``Button_id`` [#10814](https://github.com/arendst/Tasmota/issues/10814) - ESP32 pulldown buttons ``Button_d`` and ``Button_id`` [#10814](https://github.com/arendst/Tasmota/issues/10814)
- Support for MQTT using Azure IoT Hub by Kevin Saye [#11906](https://github.com/arendst/Tasmota/issues/11906)
### Breaking Changed ### Breaking Changed

View File

@ -419,6 +419,7 @@
// Any valid fingerprint with the old algo will be automatically updated to the new algo. // Any valid fingerprint with the old algo will be automatically updated to the new algo.
// Enable this if you want to disable the old algo check, which should be more secure // Enable this if you want to disable the old algo check, which should be more secure
// for USE_4K_RSA (support for 4096 bits certificates, instead of 2048), you need to uncommend `-DUSE_4K_RSA` in `build_flags` from `platform.ini` or `platform_override.ini` // for USE_4K_RSA (support for 4096 bits certificates, instead of 2048), you need to uncommend `-DUSE_4K_RSA` in `build_flags` from `platform.ini` or `platform_override.ini`
// #define USE_MQTT_AZURE_IOT // Enable MQTT for Azure IoT Hub (+1k code)
// -- Telegram Protocol --------------------------- // -- Telegram Protocol ---------------------------
//#define USE_TELEGRAM // Support for Telegram protocol (+49k code, +7.0k mem and +4.8k additional during connection handshake) //#define USE_TELEGRAM // Support for Telegram protocol (+49k code, +7.0k mem and +4.8k additional during connection handshake)

View File

@ -23,11 +23,11 @@
#define MQTT_WIFI_CLIENT_TIMEOUT 200 // Wifi TCP connection timeout (default is 5000 mSec) #define MQTT_WIFI_CLIENT_TIMEOUT 200 // Wifi TCP connection timeout (default is 5000 mSec)
#endif #endif
#if defined(USE_MQTT_AZURE_IOT) #ifdef USE_MQTT_AZURE_IOT
#include <JsonParser.h> #include <JsonParser.h>
#undef MQTT_PORT #undef MQTT_PORT
#define MQTT_PORT 8883 #define MQTT_PORT 8883
#endif //USE_MQTT_AZURE_IOT #endif // USE_MQTT_AZURE_IOT
#define USE_MQTT_NEW_PUBSUBCLIENT #define USE_MQTT_NEW_PUBSUBCLIENT
@ -231,14 +231,14 @@ void MqttDisconnect(void) {
} }
void MqttSubscribeLib(const char *topic) { void MqttSubscribeLib(const char *topic) {
#if defined(USE_MQTT_AZURE_IOT) #ifdef USE_MQTT_AZURE_IOT
// Azure IoT Hub currently does not support custom topics: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support // Azure IoT Hub currently does not support custom topics: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support
String realTopicString = "devices/" + String(SettingsText(SET_MQTT_CLIENT)); String realTopicString = "devices/" + String(SettingsText(SET_MQTT_CLIENT));
realTopicString += "/messages/devicebound/#"; realTopicString += "/messages/devicebound/#";
MqttClient.subscribe(realTopicString.c_str()); MqttClient.subscribe(realTopicString.c_str());
#else //USE_MQTT_AZURE_IOT #else
MqttClient.subscribe(topic); MqttClient.subscribe(topic);
#endif //USE_MQTT_AZURE_IOT #endif // USE_MQTT_AZURE_IOT
MqttClient.loop(); // Solve LmacRxBlk:1 messages MqttClient.loop(); // Solve LmacRxBlk:1 messages
} }
@ -258,7 +258,7 @@ bool MqttPublishLib(const char* topic, bool retained) {
} }
bool result; bool result;
#if defined(USE_MQTT_AZURE_IOT) #ifdef USE_MQTT_AZURE_IOT
String sourceTopicString = String(topic); String sourceTopicString = String(topic);
sourceTopicString.replace("/", "%2F"); sourceTopicString.replace("/", "%2F");
String topicString = "devices/" + String(SettingsText(SET_MQTT_CLIENT)); String topicString = "devices/" + String(SettingsText(SET_MQTT_CLIENT));
@ -273,9 +273,9 @@ bool MqttPublishLib(const char* topic, bool retained) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "Invalid JSON, '%s' for topic '%s', not sending to Azure IoT Hub"), TasmotaGlobal.mqtt_data, topic); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "Invalid JSON, '%s' for topic '%s', not sending to Azure IoT Hub"), TasmotaGlobal.mqtt_data, topic);
result = true; result = true;
} }
#else //USE_MQTT_AZURE_IOT #else
result = MqttClient.publish(topic, TasmotaGlobal.mqtt_data, retained); result = MqttClient.publish(topic, TasmotaGlobal.mqtt_data, retained);
#endif //USE_MQTT_AZURE_IOT #endif // USE_MQTT_AZURE_IOT
yield(); // #3313 yield(); // #3313
return result; return result;
} }
@ -307,7 +307,7 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len
// Save MQTT data ASAP as it's data is discarded by PubSubClient with next publish as used in MQTTlog // Save MQTT data ASAP as it's data is discarded by PubSubClient with next publish as used in MQTTlog
char topic[TOPSZ]; char topic[TOPSZ];
#if defined(USE_MQTT_AZURE_IOT) #ifdef USE_MQTT_AZURE_IOT
// for Azure, we read the topic from the property of the message // for Azure, we read the topic from the property of the message
String fullTopicString = String(mqtt_topic); String fullTopicString = String(mqtt_topic);
int startOfTopic = fullTopicString.indexOf("TOPIC="); int startOfTopic = fullTopicString.indexOf("TOPIC=");
@ -322,9 +322,9 @@ void MqttDataHandler(char* mqtt_topic, uint8_t* mqtt_data, unsigned int data_len
return; return;
} }
strlcpy(topic, newTopic.c_str(), sizeof(topic)); strlcpy(topic, newTopic.c_str(), sizeof(topic));
#else //USE_MQTT_AZURE_IOT #else
strlcpy(topic, mqtt_topic, sizeof(topic)); strlcpy(topic, mqtt_topic, sizeof(topic));
#endif //USE_MQTT_AZURE_IOT #endif // USE_MQTT_AZURE_IOT
mqtt_data[data_len] = 0; mqtt_data[data_len] = 0;
char data[data_len +1]; char data[data_len +1];
memcpy(data, mqtt_data, sizeof(data)); memcpy(data, mqtt_data, sizeof(data));
@ -753,10 +753,9 @@ void MqttReconnect(void) {
} }
String azureMqtt_userString = String(SettingsText(SET_MQTT_HOST)) + "/" + String(SettingsText(SET_MQTT_CLIENT)); + "/?api-version=2018-06-30"; String azureMqtt_userString = String(SettingsText(SET_MQTT_HOST)) + "/" + String(SettingsText(SET_MQTT_CLIENT)); + "/?api-version=2018-06-30";
if (MqttClient.connect(TasmotaGlobal.mqtt_client, azureMqtt_userString.c_str(), mqtt_pwd, stopic, 1, lwt_retain, TasmotaGlobal.mqtt_data, MQTT_CLEAN_SESSION)) { if (MqttClient.connect(TasmotaGlobal.mqtt_client, azureMqtt_userString.c_str(), mqtt_pwd, stopic, 1, lwt_retain, TasmotaGlobal.mqtt_data, MQTT_CLEAN_SESSION)) {
#else //USE_MQTT_AZURE_IOT #else
if (MqttClient.connect(TasmotaGlobal.mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, lwt_retain, TasmotaGlobal.mqtt_data, MQTT_CLEAN_SESSION)) { if (MqttClient.connect(TasmotaGlobal.mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, lwt_retain, TasmotaGlobal.mqtt_data, MQTT_CLEAN_SESSION)) {
#endif //USE_MQTT_AZURE_IOT #endif // USE_MQTT_AZURE_IOT
#ifdef USE_MQTT_TLS #ifdef USE_MQTT_TLS
if (Mqtt.mqtt_tls) { if (Mqtt.mqtt_tls) {
#ifdef ESP8266 #ifdef ESP8266