Repository navigation
Open
Description
I am using the tiny gsm library from this fork with the esp32 on arduino ide 2:
github link
The code is as follows:
#include <Arduino.h>
#define ENABLE_DEBUG // To enable debugging
#define ENABLE_ERROR_STRING // To show details in error
#define DEBUG_PORT Serial // To define the serial port for debug printing
#define SSLCLIENT_INSECURE_ONLY
#define SSLCLIENT_HALF_DUPLEX
// Choose your modem – for A7670 use this:
#define TINY_GSM_MODEM_A7670
#define SerialMon Serial
#define gsmSerial Serial1
#define TINY_GSM_DEBUG SerialMon
// SIM card PIN (leave empty if no PIN)
#define GSM_PIN ""
// Pin definitions for ESP32
#define MODEM_TX 17
#define MODEM_RX 39
#include <ESP_SSLClient.h>
#include <TinyGsmClient.h>
// APN settings – change to your provider's APN
const char apn[] = "internet";
const char gprsUser[] = "";
const char gprsPass[] = "";
TinyGsm modem(gsmSerial);
TinyGsmClient client(modem);
ESP_SSLClient ssl_client;
void setup() {
Serial.begin(115200);
gsmSerial.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
SerialMon.println("Initializing modem...");
while (!modem.init()) {
SerialMon.println("Modem init failed. Check wiring/power.");
delay(5000);
}
if (strlen(GSM_PIN) > 0 && modem.getSimStatus() != 3) {
SerialMon.print("Unlocking SIM...");
if (modem.simUnlock(GSM_PIN)) {
SerialMon.println(" OK");
} else {
SerialMon.println(" Failed");
while (true) { delay(1000); }
}
}
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
while (true) { delay(1000); }
}
SerialMon.println(" OK");
// Connect to GPRS (internet)
SerialMon.print("Connecting to APN: ");
SerialMon.println(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println("GPRS connect failed");
while (true) { delay(1000); }
}
SerialMon.println("GPRS connected");
// Show local IP address
IPAddress local = modem.localIP();
SerialMon.print("Local IP: ");
SerialMon.println(local);
// Optional: signal quality
int csq = modem.getSignalQuality();
SerialMon.print("Signal quality (CSQ): ");
SerialMon.println(csq);
Serial.print("ESP_Client version ");
Serial.println(ESP_SSLCLIENT_VERSION);
ssl_client.setInsecure();
ssl_client.setBufferSizes(8192, 4096);
ssl_client.setDebugLevel(4);
ssl_client.setHandshakeTimeout(120);
ssl_client.setSessionTimeout(120);
ssl_client.setClient(&client);
}
void loop() {
modem.maintain();
static unsigned long lastTime = 0;
if (millis() - lastTime >= 5000) {
lastTime = millis();
Serial.println("---------------------------------");
Serial.print("Connecting to server...");
if (ssl_client.connect("example.com", 443)) {
Serial.println(" ok");
Serial.println("Send GET request...");
ssl_client.print("GET / HTTP/1.1\r\n");
ssl_client.print("Host: example.com\r\n");
ssl_client.print("Connection: close\r\n");
ssl_client.print("\r\n");
Serial.print("Read response...");
unsigned long ms = millis();
while (!ssl_client.available() && millis() - ms < 3000) {
delay(1);
}
Serial.println();
while (ssl_client.available()) {
Serial.print((char)ssl_client.read());
}
Serial.println();
} else {
Serial.println(" failed\n");
}
ssl_client.stop();
Serial.println();
}
}
The error I see is as follows:
Initializing modem...
[3020] TinyGSM Version: 1.0.0
[3020] TinyGSM Compiled Module: TinyGsmClientA7670
[3048] Modem: A7670SA-FASE
[3048] Modem: A7670SA-FASE
Waiting for network... OK
Connecting to APN: internet
GPRS connected
Local IP: redacted
Signal quality (CSQ): 22
ESP_Client version 3.1.3
---------------------------------
Connecting to server...> INFO.mConnectBasicClient: Basic client connected!
> INFO.mConnectSSL: Start connection.
> INFO.mConnectSSL: Wait for SSL handshake.
> INFO.mUpdateEngine: State SENDREC
> INFO.mUpdateEngine: State RECVREC
> INFO.mRunUntil: SSL state changed.
> INFO.mRunUntil: State RECVREC
> INFO.mRunUntil: Expected bytes count: 5
> ERROR.mUpdateEngine: Error reading bytes from basic client. Write Error: 0
> WARN.mRunUntil: Terminating with write error.
> ERROR.mConnectSSL: Failed to initlalize the SSL layer.
> ERROR.mConnectSSL: Unknown error code.
failed
---------------------------------
Connecting to server...> INFO.mConnectSSL: Start connection.
> INFO.mConnectSSL: Wait for SSL handshake.
> INFO.mUpdateEngine: State SENDREC
> INFO.mUpdateEngine: State RECVREC
> ERROR.mUpdateEngine: Error reading bytes from basic client. Write Error: 0
> WARN.mRunUntil: Terminating with write error.
> ERROR.mConnectSSL: Failed to initlalize the SSL layer.
> ERROR.mConnectSSL: Unknown error code.
failed
[10195] Closed: 0
The connection fails at the ssl handshake as the logs show. Quite a head scratcher for me as this is the wifi example but with wifi client replaced by the gsm client. (the gsm example has included in the library gives the same results)
Metadata
Metadata
Assignees
Labels
No labels
Activity
Fix TLS handshake failure with TinyGSM modem clients issue #34
mobizt commentedon Nov 26, 2026
Thanks for the detailed report and logs.
The handshake fails on the first read of the server's response. With TinyGSM,
available()can report data that the modem hasn't actually transferred to the ESP32 yet. In that caseread()returns0, and the library treated that as a fatal error and closed the connection. That's theError reading bytes from basic clientin your log.This is fixed in the latest version of the library. Please update and try again.
Please also try this on your side
The TLS handshake receives several KB at once (the server certificate chain). The default 256-byte RX buffer of
HardwareSerialcan overflow at 115200 baud and drop bytes.If it still fails, please post the log with
TINY_GSM_DEBUGenabled, especially the+CIPRXGETlines during the handshake.