Skip to content

Commit

Permalink
Update TinyGSM
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Oct 25, 2024
1 parent 1f49a65 commit ec0b412
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/TinyGSM/src/TinyGsmHttpsA76xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
}
Expand Down
87 changes: 86 additions & 1 deletion lib/TinyGSM/src/TinyGsmMqttA76xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -334,6 +354,9 @@ class TinyGsmMqttA76xx
return false;
}




bool mqtt_set_rx_buffer_size(uint32_t size)
{
if (size == 0) {
Expand Down Expand Up @@ -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: <client_index>,<req_length>
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: <client_index>,<req_length>,<qos>
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
*/
Expand Down

0 comments on commit ec0b412

Please sign in to comment.