Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck at [ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0 #104

Closed
cloakedch opened this issue Aug 16, 2024 · 2 comments
Closed

Comments

@cloakedch
Copy link

Hello

I'm using TinyGSM with a A7670E-FASE modem and try to connect to a MQTT broker (for instance, broker.hivemq.com) using TLS, which fails at the handshake:

[ 14972][V][SSLClient.cpp:404] read(): This is the iClient->read() implementation
[ 14991][V][SSLClient.cpp:404] read(): This is the iClient->read() implementation
[ 14998][V][SSLClient.cpp:168] connect(): connect with CA
[ 15003][V][SSLClient.cpp:226] connect(): Connecting to broker.hivemq.com:8883
[ 15010][V][SSLClient.cpp:227] connect(): Timeout value: 0
[ 15015][V][SSLClient.cpp:228] connect(): CA Certificate: Provided
[ 15021][V][SSLClient.cpp:229] connect(): Client Certificate: Not Provided
[ 15028][V][SSLClient.cpp:230] connect(): Private Key: Not Provided
[ 15034][V][ssl__client.cpp:301] start_ssl_client(): Free internal heap before TLS 345352
[ 15042][V][ssl__client.cpp:302] start_ssl_client(): Connecting to broker.hivemq.com:8883
[ 15050][V][ssl__client.cpp:396] init_tcp_connection(): Client pointer: 0x3ffc15cc
[ 15204][V][ssl__client.cpp:421] seed_random_number_generator(): Seeding the random number generator
[ 15213][V][ssl__client.cpp:423] seed_random_number_generator(): Entropy context initialized
[ 15222][V][ssl__client.cpp:318] start_ssl_client(): Random number generator seeded, ret: 0
[ 15230][V][ssl__client.cpp:443] set_up_tls_defaults(): Setting up the SSL/TLS defaults...
[ 15239][V][ssl__client.cpp:329] start_ssl_client(): SSL config defaults set, ret: 0
[ 15246][V][ssl__client.cpp:483] auth_root_ca_buff(): Loading CA cert
[ 15256][V][ssl__client.cpp:334] start_ssl_client(): SSL auth mode set, ret: 0
[ 15263][V][ssl__client.cpp:339] start_ssl_client(): SSL client cert and key set, ret: 0
[ 15271][V][ssl__client.cpp:630] set_hostname_for_tls(): Setting hostname for TLS session...
[ 15279][V][ssl__client.cpp:344] start_ssl_client(): SSL hostname set, ret: 0
[ 15286][V][ssl__client.cpp:682] set_io_callbacks_and_timeout(): Setting up IO callbacks...
[ 15294][V][ssl__client.cpp:685] set_io_callbacks_and_timeout(): Setting timeout to 0
[ 15302][V][ssl__client.cpp:349] start_ssl_client(): SSL IO callbacks and timeout set, ret: 0
[ 15310][V][ssl__client.cpp:728] perform_ssl_handshake(): Performing the SSL/TLS handshake, timeout 120000 ms
[ 15320][D][ssl__client.cpp:730] perform_ssl_handshake(): calling mbedtls_ssl_handshake with ssl_ctx address 0x3ffb9d14
[ 15379][V][ssl__client.cpp:198] client_net_send(): SSL client TX res=296 len=296
[ 15386][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0
[ 15403][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0
[15410] ### Closed: 0
[ 15434][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0
[ 15451][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0
[ 15467][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0
[ 15483][V][ssl__client.cpp:118] client_net_recv_timeout(): Timeout set to 0

Those last lines are then repeated infinitely.
I realize that the line "[15410] ### Closed: 0" indicates a closed TCP connection, but I don't know if it's being closed by the SSLClient or by TinyGSM.

Initialization seems to look good:

Initializing modem...
[5344] ### TinyGSM Version: 0.12.0
[5344] ### TinyGSM Compiled Module: TinyGsmClientSIM7600
[8371] ### Modem: SIMCOM INCORPORATED A7670E-FASE
[8371] ### Modem: SIMCOM INCORPORATED A7670E-FASE
done
Setting network mode to 38... done
Modem Info: Manufacturer: SIMCOM INCORPORATED Model: A7670E-FASE Revision: A7670M7_V1.11.1 IMEI: redacted
'=========== After Setup Start ============
INTERNAL Memory Info:
'------------------------------------------
Total Size : 381516 B ( 372.6 KB)
Free Bytes : 345352 B ( 337.3 KB)
Allocated Bytes : 28568 B ( 27.9 KB)
Minimum Free Bytes: 339404 B ( 331.4 KB)
Largest Free Block: 114676 B ( 112.0 KB)
'------------------------------------------
GPIO Info:
'------------------------------------------
GPIO : BUS_TYPE[bus/unit][chan]
'--------------------------------------
1 : UART_TX[0]
3 : UART_RX[0]
4 : GPIO
25 : GPIO
26 : UART_RX[1]
27 : UART_TX[1]
'============ After Setup End =============
13652] ### Unhandled: +NETOPEN: 0

OK

+CGEV: ME PDN ACT 8,0
Waiting for network... done
Connecting to network... connected
[14943] CCID: redacted
[14954] IMEI: redacted
[14960] Operator: redacted
[14967] Local IP: redacted
[14972] Signal quality: 20

Can you point me in the right direction where to start looking for mistakes?

My sketch:

// ----------------------------------------------------------------------------
// Serial
// ----------------------------------------------------------------------------

// Set serial for debugging
#define SerialMon Serial
#define SerialMonBaud 115200

// Set serial for AT commands (SIM7600)
#define SerialAT Serial1

// ----------------------------------------------------------------------------
// Modem
// ----------------------------------------------------------------------------

#define MODEM_PIN_POWER_KEY 4
#define MODEM_PIN_POWER_ON 25
#define MODEM_PIN_TX 27
#define MODEM_PIN_RX 26
#define MODEM_UART_BAUD 115200

#define TINY_GSM_MODEM_SIM7600
#define TINY_GSM_RX_BUFFER 1024

#define DUMP_AT_COMMANDS
#define TINY_GSM_DEBUG Serial

typedef enum {
  AUTOMATIC = 2,
  GSM = 13,
  WCDMA = 14,
  LTE = 38
} NetworkMode;

#include <TinyGsmClient.h>
#include <SSLClient.h>
#include <PubSubClient.h>

TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
SSLClient sslClient(&client);
PubSubClient mqttClient(sslClient);

const char modemApn[] = "<apn>"; // redacted
const char modemSimPin[] = "<simpin>"; // redacted


// ----------------------------------------------------------------------------
// MQTT
// ----------------------------------------------------------------------------
const char *mqttPrintingTopic = nullptr;
const char mqttBroker[] = "broker.hivemq.com";
const int mqttPort = 8883;
const char mqttClientName[] = "<clientName>"; // redacted
const char mqttUser[] = "<user>"; // redacted
const char mqttPassword[] = "<password>"; // redacted
const char mqttTopicBase[] = "<base>"; // redacted

// Forward declarations
bool startModem();
void setNetworkMode(NetworkMode networkMode);
bool connectToGsm();
void setupMqtt();
void mqttCallback(char* topic, byte* payload, unsigned int len);
bool connectToMqtt();
void mqttPublish(const char* topic, const char* payload);

void setup() {
  SerialMon.begin(SerialMonBaud);
  delay(10);

  sslClient.setCACert(rootCA);
  //sslClient.setTimeout(10000); // tried this, doesn't help.
  //sslClient.setInsecure(); // also tried this (and removed setting the CA), doesn't help.

  if (!startModem()) {
    ESP.restart();
  }

  setupMqtt();
}

void loop() {
  // ensure GPRS connection
  if (!modem.isNetworkConnected() || !modem.isGprsConnected()) {
    if (!connectToGsm()) {
      delay(2000);
      return;
    }
  } else {
    Serial.print("signal strength: ");
    Serial.println(modem.getSignalQuality());
  }

  // ensure MQTT connection
  if (!mqttClient.connected()) {
    if (!connectToMqtt()) {
      delay(2000);
      return;
    }
  }

  mqttClient.loop();

  // Example of publishing a message every 10 seconds
  static unsigned long lastPublish = 0;
  if (millis() - lastPublish > 10000) {
    lastPublish = millis();
    mqttPublish(mqttPrintingTopic, "Hello, MQTT!");
  }
}

bool startModem() {
  // Power the modem
  pinMode(MODEM_PIN_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PIN_POWER_ON, HIGH);

  // Turn on the modem
  pinMode(MODEM_PIN_POWER_KEY, OUTPUT);
  digitalWrite(MODEM_PIN_POWER_KEY, HIGH);
  delay(500);
  digitalWrite(MODEM_PIN_POWER_KEY, LOW);
  delay(3000); 

  // Begin serial communication with the modem
  SerialAT.begin(MODEM_UART_BAUD, SERIAL_8N1, MODEM_PIN_RX, MODEM_PIN_TX);

  // Initialize the modem
  SerialMon.println("Initializing modem...");
  bool initialized = modem.isNetworkConnected() ? modem.restart(modemSimPin) : modem.init(modemSimPin);
  if (!initialized) {
    SerialMon.println("failed");
    return false;
  }

  SerialMon.println("done");

  // set network mode
  setNetworkMode(LTE);

  // Check modem info
  SerialMon.print("Modem Info: ");
  SerialMon.println(modem.getModemInfo());

  return true;
}

void setNetworkMode(NetworkMode networkMode) {
  SerialMon.print("Setting network mode to ");
  SerialMon.print(networkMode);
  SerialMon.print("...");
  modem.setNetworkMode(networkMode);
  modem.waitResponse();
  SerialMon.println(" done");
}

bool connectToGsm() {
  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork(30000L)) {
    SerialMon.println(" failed");
    return false;
  }
  SerialMon.println(" done");

  // Connect to the network
  SerialMon.print("Connecting to network...");
  modem.gprsConnect(modemApn);
  modem.waitResponse();

  if (modem.isGprsConnected()) {
    SerialMon.println(" connected");
    
    String ccid = modem.getSimCCID();
    DBG("CCID:", ccid);

    String imei = modem.getIMEI();
    DBG("IMEI:", imei);

    String cop = modem.getOperator();
    DBG("Operator:", cop);

    IPAddress local = modem.localIP();
    DBG("Local IP:", local);

    int csq = modem.getSignalQuality();
    DBG("Signal quality:", csq);

    return true;
  } else {
    SerialMon.println(" failed to connect");
    return false;
  }
}

void setupMqtt() {
  mqttClient.setServer(mqttBroker, mqttPort);
  mqttClient.setCallback(mqttCallback);

  // set mqtt topic to consist of a base and the IMEI
  String imei = modem.getIMEI();
  String concatenatedText = String(mqttTopicBase) + imei;
  mqttPrintingTopic = concatenatedText.c_str();
}

void mqttCallback(char* topic, byte* payload, unsigned int len) {
  SerialMon.print("Message arrived [");
  SerialMon.print(topic);
  SerialMon.print("] ");
  for (unsigned int i = 0; i < len; i++) {
    SerialMon.print((char)payload[i]);
  }
  SerialMon.println();
}

bool connectToMqtt() {
  SerialMon.print("Connecting to MQTT broker...");
  if (mqttClient.connect(mqttClientName, mqttUser, mqttPassword)) {
    SerialMon.println(" connected.");
    // Subscribe to a topic after connecting
    SerialMon.print("Subscribing to ");
    SerialMon.print(mqttPrintingTopic);
    SerialMon.print("...");
    if (mqttClient.subscribe(mqttPrintingTopic)) {
      SerialMon.println(" done");
      return true;
    } else {
      SerialMon.println(" failed");
      return false;
    }
  } else {
    SerialMon.print(" failed, rc=");
    SerialMon.print(mqttClient.state());
    SerialMon.println(" try again in 5 seconds");
    return false;
  }
}

void mqttPublish(const char* topic, const char* payload) {
  if (mqttClient.connected()) {
    mqttClient.publish(topic, payload);
    SerialMon.print("Published message: ");
    SerialMon.println(payload);
  } else {
    SerialMon.println("Cannot publish, MQTT client not connected.");
  }
}
Copy link

The following fields are missing: Description, Steps to Reproduce, Expected Behavior, Actual Behavior, Environment, IDE. Please update the issue with this information.

@cloakedch
Copy link
Author

Turned out my broker wasn't properly configured and it wasn't a client error after all.
Closing as fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant