Hi everyone,
I'm having big problems connecting to AWS with MQTT protocol at servers using GSM.
At the moment I have created a generic code to use MQTT and I pass it the type of client previously created.
I can use WIFI, ETH or GSM.
With WIFI and ETH it works very well (especially with ETH).
Now I'm implementing GSM with the use of SIM7000E. I've been on it day and night for a week and I finally managed to do something but it's not stable enough.
This is that write on Serial.monitor when it's working well:
Avvio connessione GSM....
/nModem Info: SIM7000E R1351
SIM ICCID request: **************************f
Waiting for network... success
Connecting to iot.truphone.com
[36116] ### Network time and time zone updated.
Network connected
success
GPRS connected
10.135.245.228
Qualità segnale: 31
Imposto l'ora sul RTC del ESP con connessione GSM...
Unix time ricavato da connessione GSM: 1682515544
Stampo unix time: 1682508344
[ 35673][I][AbiotMQTT.cpp:21] setup(): Connessione al broker MQTT *************-ats.iot.eu-central-1.amazonaws.com ... topic $aws/*******************************************************************************************************
[ 47847][E][AbiotMQTT.cpp:31] setup(): Iscrizione al topic *********************************
[ 47958][I][AbiotMQTT.cpp:39] setup(): Connesso al broker: 1
Ora attuale: 2023-04-26 14:46:01
Then it works fine for a few minutes and then it starts giving SSL problems like:
(SSLClient)(SSL_ERROR)(m_update_engine): Error writing to m_client
(SSLClient)(SSL_ERROR)(m_update_engine): 0
(SSLClient)(SSL_WARN)(connected): Socket was dropped unexpectedly (this can be an alternative to closing the connection)
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
(SSLClient)(SSL_ERROR)(available): SSL engine failed to update.
[459065] ### Unhandled: 0, SEND FAIL
[459065][I][AbiotClient.cpp:565] loop(): Network disconnected
[459175][I][AbiotClient.cpp:574] loop(): Network re-connected
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400556b4 PS : 0x00060830 A0 : 0x820a83b4 A1 : 0x3fce2830
A2 : 0x00000001 A3 : 0xfffffffd A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000000
A10 : 0x00000000 A11 : 0x3c0d3732 A12 : 0x00000000 A13 : 0x00000000
A14 : 0x3c0d32eb A15 : 0x00000000 SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000001 LBEG : 0x400556d5 LEND : 0x400556e5 LCOUNT : 0xffffffff
An other:
(SSLClient)(SSL_ERROR)(m_update_engine): Error writing to m_client
(SSLClient)(SSL_ERROR)(m_update_engine): 0
(SSLClient)(SSL_WARN)(connected): Socket was dropped unexpectedly (this can be an alternative to closing the connection)
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
(SSLClient)(SSL_ERROR)(available): SSL engine failed to update.
[1071342] ### Unhandled: 0, SEND FAIL
[1071342][I][AbiotClient.cpp:565] loop(): Network disconnected
[1088793][I][AbiotClient.cpp:574] loop(): Network re-connected
[1088857][I][AbiotClient.cpp:580] loop(): GPRS disconnected!
[1088858][I][AbiotClient.cpp:581] loop(): Connecting to
[1088858][I][AbiotClient.cpp:582] loop(): iot.truphone.com
[1089468][I][AbiotClient.cpp:585] loop(): fail
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400556b4 PS : 0x00060830 A0 : 0x820a83b4 A1 : 0x3fce2830
A2 : 0x00000001 A3 : 0xfffffffd A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000000
A10 : 0x00000000 A11 : 0x3c0d3732 A12 : 0x00000000 A13 : 0x00000000
A14 : 0x3c0d32eb A15 : 0x00000000 SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000001 LBEG : 0x400556d5 LEND : 0x400556e5 LCOUNT : 0xffffffff
still...
(SSLClient)(SSL_ERROR)(available): SSL engine failed to update.
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400556b4 PS : 0x00060830 A0 : 0x820a83b4 A1 : 0x3fce2830
A2 : 0x00000001 A3 : 0xfffffffd A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000000
A10 : 0x00000000 A11 : 0x3c0d3732 A12 : 0x00000000 A13 : 0x00000000
A14 : 0x3c0d32eb A15 : 0x00000000 SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000001 LBEG : 0x400556d5 LEND : 0x400556e5 LCOUNT : 0xffffffff
Here even did not even make the first connection:
Avvio connessione GSM....
/nModem Info: SIM7000E R1351
SIM ICCID request: 8944477200000824889f
Waiting for network... success
Connecting to iot.truphone.com
[46921] ### Network time and time zone updated.
Network connected
success
GPRS connected
10.140.124.9
Qualità segnale: 18
Imposto l'ora sul RTC del ESP con connessione GSM...
Unix time ricavato da connessione GSM: 1682520078
Stampo unix time: 1682512878
[ 48295][I][AbiotMQTT.cpp:21] setup(): Connessione al broker MQTT a12d3wtrd05i30-ats.iot.eu-central-1.amazonaws.com ... topic $aws/rules/IoTInboundProcessingRuleCDKdev/6e3dadf2-0edf-4ac2-b94d-5128aff877d0
(SSLClient)(SSL_ERROR)(m_update_engine): Error writing to m_client
(SSLClient)(SSL_ERROR)(m_update_engine): 0
(SSLClient)(SSL_ERROR)(connected): Not connected because write error is set
(SSLClient)(SSL_ERROR)(m_print_ssl_error): SSL_CLIENT_WRITE_FAIL
(SSLClient)(SSL_WARN)(m_run_until): Terminating with write error:
(SSLClient)(SSL_WARN)(m_run_until): 4
(SSLClient)(SSL_ERROR)(m_start_ssl): Failed to initlalize the SSL layer
(SSLClient)(SSL_ERROR)(m_print_br_error): Unknown error code: 0
.[ 94096][E][AbiotMQTT.cpp:27] setup(): Impossibile connettersi al broker MQTT
Ora attuale: 2023-04-26 14:42:04
So, my code is (I put only GSM part)..... in setup() function I have:
this->sslClient->setMutualAuthParams(mTLS);
Serial.println("Avvio connessione GSM....");
pinMode(ALIM_GSM, OUTPUT);
digitalWrite(ALIM_GSM, HIGH);
pinMode(GSM_ON, OUTPUT);
digitalWrite(GSM_ON, LOW);
pinMode(GSM_RESET, OUTPUT);
digitalWrite(GSM_RESET, LOW);
SerialAT->begin(9600);
delay(100);
// verifico se il modem è spento se lo è lo accendo il modem utilizzando il PIN PWKEY
while (!gsmModem->testAT())
{
Serial.println("Il modem è spento, lo accendo.");
digitalWrite(GSM_ON, HIGH);
delay(1200);
Serial.println("Ho tenuto su il pin ON per 1200ms");
digitalWrite(GSM_ON, LOW);
delay(2000);
}
Serial.print("/nModem Info: ");
Serial.println(gsmModem->getModemInfo());
delay(100);
Serial.print("SIM ICCID request: ");
Serial.println(gsmModem->getSimCCID());
delay(100);
gsmModem->setNetworkMode(38);
delay(100);
gsmModem->setPreferredMode(1);
delay(100);
Serial.print("Waiting for network...");
while (!gsmModem->waitForNetwork())
{
Serial.println(" fail");
delay(10000);
}
Serial.println(" success");
// GPRS connection parameters are usually set after network registration
Serial.print(F("Connecting to "));
Serial.println(GSM_APN);
delay(100);
while (!gsmModem->gprsConnect(GSM_APN, GSM_USER, GSM_PASS))
{
Serial.println("GPRS Connect: fail");
delay(10000);
}
delay(100);
if (gsmModem->isNetworkConnected())
{
Serial.println("Network connected");
}
Serial.println(" success");
delay(100);
if (gsmModem->isGprsConnected())
{
Serial.println("GPRS connected");
}
Serial.println(gsmModem->getLocalIP());
delay(100);
Serial.print("Qualità segnale: ");
Serial.println(gsmModem->getSignalQuality());
delay(100);
/*
SerialAT->println("AT+CPSI?"); // Get connection type and band
delay(500);
if (SerialAT->available())
{
String r = SerialAT->readString();
Serial.println(r);
}
*/
Serial.println("Imposto l'ora sul RTC del ESP con connessione GSM...");
getGSMTIME();
break;
this->mqtt = new PubSubClient(AWS_IOT_ENDPOINT, 8883, AbiotMQTT::subscribeHandler, this->client);
delay(500);
log_i("Connessione al broker MQTT %s ... topic %s", AWS_IOT_ENDPOINT, TOPIC_INBOUND);
// Connect to MQTT Broker
while (!mqtt->connect(CONSORTIUM_ID))
{
Serial.print(".");
log_e("Impossibile connettersi al broker MQTT");
return false;
}
log_e("Iscrizione al topic %s", TOPIC_SUB);
while (!mqtt->subscribe(TOPIC_SUB))
{
Serial.print(".");
delay(500);
}
delay(100);
log_i("Connesso al broker: %s ", String(mqtt->connected()).c_str());
return mqtt->connected();
In loop() function:
// Make sure we're still registered on the network
if (!gsmModem->isNetworkConnected())
{
log_i("Network disconnected");
if (!gsmModem->waitForNetwork(180000L, true))
{
log_i(" fail");
delay(10000);
return;
}
if (gsmModem->isNetworkConnected())
{
log_i("Network re-connected");
}
// and make sure GPRS/EPS is still connected
if (!gsmModem->isGprsConnected())
{
log_i("GPRS disconnected!");
log_i("Connecting to ");
log_i(GSM_APN);
if (!gsmModem->gprsConnect(GSM_APN, GSM_USER, GSM_PASS))
{
log_i(" fail");
delay(10000);
return;
}
if (gsmModem->isGprsConnected())
{
log_i("GPRS reconnected");
}
}
}
break;
if (!mqtt->connected())
{
lostConnection++;
log_i("Connessione persa %s volte", lostConnection);
log_i("Connessione con broker MQTT disconnesso");
// Reconnect every 10 seconds
uint32_t t = millis();
if (t - lastReconnectAttempt > 1000L)
{
while (!mqtt->connect(CONSORTIUM_ID))
{
log_e("Impossibile connettersi al broker MQTT");
lastReconnectAttempt = 0;
return;
}
log_e("Iscrizione al topic %s", TOPIC_SUB);
while (!mqtt->subscribe(TOPIC_SUB))
{
delay(500);
}
delay(100);
log_i("Connesso al broker: %s ", String(mqtt->connected()).c_str());
}
}
this->mqtt->loop();
From what I understand I have problems with SSL but I don't understand what I'm doing wrong.
I saw that the TinyGsm library has two objects:
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_MODEM_SIM7000SSL
And I use the first of this.
My include is:
#include <TinyGsmClient.h>
And the object is:
TinyGsm *gsmModem;