From ec0b412b21e94472f76bc0b3ce098201b7ce5038 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Fri, 25 Oct 2024 14:09:05 +0800 Subject: [PATCH] Update TinyGSM --- lib/TinyGSM/src/TinyGsmHttpsA76xx.h | 6 +- lib/TinyGSM/src/TinyGsmMqttA76xx.h | 87 ++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/lib/TinyGSM/src/TinyGsmHttpsA76xx.h b/lib/TinyGSM/src/TinyGsmHttpsA76xx.h index 01cf816..36dbf7c 100644 --- a/lib/TinyGSM/src/TinyGsmHttpsA76xx.h +++ b/lib/TinyGSM/src/TinyGsmHttpsA76xx.h @@ -25,6 +25,10 @@ class TinyGsmHttpsA76xx if (thisModem().waitResponse(10000UL) != 1) { return false; } + // set sni + thisModem().sendAT("+CSSLCFG=\"enableSNI\",0,1"); + thisModem().waitResponse(); + return true; } @@ -269,7 +273,7 @@ class TinyGsmHttpsA76xx return -1; } - int https_post(const String& payload) + int https_post(const String &payload) { return https_post((uint8_t *) payload.c_str(), payload.length()); } diff --git a/lib/TinyGSM/src/TinyGsmMqttA76xx.h b/lib/TinyGSM/src/TinyGsmMqttA76xx.h index 2a88102..758c49f 100644 --- a/lib/TinyGSM/src/TinyGsmMqttA76xx.h +++ b/lib/TinyGSM/src/TinyGsmMqttA76xx.h @@ -25,6 +25,10 @@ class TinyGsmMqttA76xx const char *cert_pem; /*!< SSL server certification, PEM format as string, if the client requires to verify server */ const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */ const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */ + const char *will_topic; + const char *will_msg; + uint8_t will_qos = 0; + public: /* @@ -72,6 +76,13 @@ class TinyGsmMqttA76xx this->client_key_pem = clientCertKey; } + void setWillMessage(const char *topic, const char *msg, uint8_t qos) + { + will_msg = msg; + will_topic = topic; + will_qos = qos; + } + bool mqtt_connect( uint8_t clientIndex, const char *server, uint16_t port, @@ -168,7 +179,16 @@ class TinyGsmMqttA76xx // Set MQTT3.1.1 , Default use MQTT 3.1 thisModem().sendAT("+CMQTTCFG=\"version\",", clientIndex, ",4"); thisModem().waitResponse(30000UL); - + + if (will_msg && will_topic) { + if (!mqttWillTopic(clientIndex, will_topic)) { + return false; + } + if (!mqttWillMessage(clientIndex, will_msg, will_qos)) { + return false; + } + } + if (username && password) { thisModem().sendAT("+CMQTTCONNECT=", clientIndex, ',', "\"tcp://", server, ':', port, "\",", keepalive_time, ',', 1, ",\"", username, "\",\"", password, "\""); } else { @@ -334,6 +354,9 @@ class TinyGsmMqttA76xx return false; } + + + bool mqtt_set_rx_buffer_size(uint32_t size) { if (size == 0) { @@ -431,6 +454,68 @@ class TinyGsmMqttA76xx } return false; } + + +protected: + bool mqttWillTopic(uint8_t clientIndex, const char *topic) + { + if (clientIndex > muxCount) { + DBG("Error: Client index out of bounds"); + return false; + } + + // Set the Will topic + // +CMQTTWILLTOPIC: , + thisModem().sendAT("+CMQTTWILLTOPIC=", clientIndex, ',', strlen(topic)); + + int response = thisModem().waitResponse(10000UL, ">"); + if (response != 1) { + DBG("Error: Did not receive expected '>' prompt, response: ", response); + return false; + } + + // Send the actual topic + thisModem().stream.write(topic); + thisModem().stream.println(); + + response = thisModem().waitResponse(); + if (response != 1) { + DBG("Error: Did not receive 'OK' after sending the Will topic, response: ", response); + return false; + } + + return true; + } + + bool mqttWillMessage(uint8_t clientIndex, const char *message, uint8_t qos) + { + if (clientIndex > muxCount) { + DBG("Error: Client index out of bounds"); + return false; + } + + // Set the Will message + // +CMQTTWILLMSG: ,, + thisModem().sendAT("+CMQTTWILLMSG=", clientIndex, ',', strlen(message), ',', qos); + + int response = thisModem().waitResponse(10000UL, ">"); + if (response != 1) { + DBG("Error: Did not receive expected '>' prompt, response: ", response); + return false; + } + + // Send the actual message + thisModem().stream.write(message); + thisModem().stream.println(); + + response = thisModem().waitResponse(); + if (response != 1) { + DBG("Error: Did not receive 'OK' after sending the Will message, response: ", response); + return false; + } + + return true; + } /* * CRTP Helper */