vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.91k stars 709 forks source link

SIM800C - MQTTS connection return status -2 #696

Open mbone opened 1 year ago

mbone commented 1 year ago

[x] I have read the Troubleshooting section of the ReadMe

What type of issues is this?

[ ] Request to support a new module

[ ] Bug or problem compiling the library [x] Bug or issue with library functionality (ie, sending data over TCP/IP) [x] Question or request for help

What are you working with?

Modem: SIM900C Main processor board: ESP32 TinyGSM version: 0.11.5

Code:

include

include

include

include

include

include

include

define SerialMon Serial

define SerialAT Serial1

define TINY_GSM_MODEM_SIM800

define TINY_GSM_RX_BUFFER 256

define TINY_GSM_USE_GPRS true

// Define the serial console for debug prints, if needed

define TINY_GSM_DEBUG SerialMon

// Add a reception delay, if needed. // This may be needed for a fast processor at a slow baud rate.

define TINY_GSM_YIELD() { delay(2); }

include

// Your GPRS credentials, if any const char apn[] = "iot.1nce.net"; const char gprsUser[] = ""; const char gprsPass[] = "";

// MQTT details const char* broker = "xxx.s1.eu.hivemq.cloud";

const char topicLed = "GsmClientTest/led"; const char topicInit = "GsmClientTest/init"; const char* topicLedStatus = "GsmClientTest/ledStatus";

define LED_PIN 2

int ledStatus = LOW;

static const char *root_ca PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54r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dwJx4nLCgdNbOhdjsnvzqvHu7Ur TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq 4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc= -----END CERTIFICATE----- )EOF";

WiFiManager wm; WiFiClient espClient; SSLClient wSSLclient(&espClient); PubSubClient wmqtt(wSSLclient);

TinyGsm modem(SerialAT); TinyGsmClient gclient(modem); SSLClient SSLclient(&gclient); PubSubClient mqtt(SSLclient);

//========================================== void setup_wifi() { wm.setDebugOutput(false);
//wm.resetSettings();

bool res; res = wm.autoConnect("AutoConnectAP","password"); // password protected ap

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println("\nWiFi connected\nIP address: "); Serial.println(WiFi.localIP()); }

void setDateTime() { // You can use your own timezone, but the exact time is not used at all. // Only the date is needed for validating the certificates. //configTime(TZ_Europe_Berlin, "pool.ntp.org", "time.nist.gov"); NTP.setTimeZone (TZ_Etc_UTC); NTP.begin ();

Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 3600 2) { delay(100); Serial.print("."); now = time(nullptr); } Serial.println();

struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.printf("%s %s", tzname[0], asctime(&timeinfo)); }

void mqttCallback(char topic, byte payload, unsigned int len) { SerialMon.print("Message arrived ["); SerialMon.print(topic); SerialMon.print("]: "); SerialMon.write(payload, len); SerialMon.println();

// Only proceed if incoming message's topic matches if (String(topic) == topicLed) { ledStatus = !ledStatus; digitalWrite(LED_PIN, ledStatus); mqtt.publish(topicLedStatus, ledStatus ? "1" : "0"); } }

void wmqttCallback(char topic, byte payload, unsigned int len) { SerialMon.print("Message arrived ["); SerialMon.print(topic); SerialMon.print("]: "); SerialMon.write(payload, len); SerialMon.println();

// Only proceed if incoming message's topic matches if (String(topic) == topicLed) { ledStatus = !ledStatus; digitalWrite(LED_PIN, ledStatus); mqtt.publish(topicLedStatus, ledStatus ? "1" : "0"); } }

boolean mqttConnect() { SerialMon.print("Connecting via GSM to "); SerialMon.print(broker);

// Connect to MQTT Broker

if (mqtt.connect("nmGsmClientTest","xxx","xxx")) { SerialMon.println(" - Success"); mqtt.publish(topicInit, "GsmClientTest started"); mqtt.subscribe(topicLed); return mqtt.connected();
} else { SerialMon.print(" - Fail (rc="); SerialMon.print(mqtt.state()); SerialMon.println(")"); delay(5000); return false; }
}

boolean wmqttConnect() { SerialMon.print("Connecting via WiFi to "); SerialMon.print(broker);

// Connect to MQTT Broker

if (wmqtt.connect("nmGsmClientTest2","xxx","xxx")) { SerialMon.println(" - Success"); wmqtt.publish(topicInit, "GsmClientTest started"); wmqtt.subscribe(topicLed); return wmqtt.connected();
} else { SerialMon.print(" - Fail (rc="); SerialMon.print(wmqtt.state()); SerialMon.println(")"); delay(5000); return false; }

}

void setup() {

pinMode(LED_PIN, OUTPUT);

// Set console baud rate SerialMon.begin(115200);

setup_wifi(); setDateTime();

SerialAT.begin(9600, SERIAL_8N1,16,17, false); SerialMon.println("Initializing modem..."); modem.init(); //modem.restart(); delay(3000);

SSLclient.setCACert(root_ca); //mqtt = new PubSubClient(SSLclient); mqtt.setServer(broker, 8883); mqtt.setCallback(mqttCallback);

wSSLclient.setCACert(root_ca); wmqtt.setServer(broker, 8883); wmqtt.setCallback(wmqttCallback); }

void loop() {

// Make sure we're still registered on the network if (!modem.isNetworkConnected()) { SerialMon.print("Connecting Network"); if (!modem.waitForNetwork(240000L)) { SerialMon.println(" - Fail"); delay(10000); return; } if (modem.isNetworkConnected()) { SerialMon.println(" - Success"); }

// and make sure GPRS/EPS is still connected
if (!modem.isGprsConnected()) {
  //SerialMon.println("GPRS disconnected!");
  SerialMon.print(F("Connecting to "));
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    SerialMon.println(" - Fail");
    delay(10000);
    return;
  }
  if (modem.isGprsConnected()) { SerialMon.println(" - Success"); }
}

}

if (!mqtt.connected()) { mqttConnect(); } if (!wmqtt.connected()) { wmqttConnect(); }

mqtt.loop(); wmqtt.loop(); }

Scenario, steps to reproduce

GSM will not connect to broker using SSL wrapper, but will connect with the a unsecure connection

Expected result

Connecting via GSM to xxx.s1.eu.hivemq.cloud - Success

Actual result

Connecting via GSM to xxx.s1.eu.hivemq.cloud[1539842] ### Unhandled: +CME ERROR: operation not allowed

Debug and AT command log

WiFi connected IP address: 192.168.3.80 Waiting for NTP time sync: .............................................. UTC Wed Nov 23 10:33:24 2022 Initializing modem... [5749] ### TinyGSM Version: 0.11.5 [5749] ### TinyGSM Compiled Module: TinyGsmClientSIM800 [5948] ### Modem: SIMCOM SIM800C [5948] ### Modem: SIMCOM SIM800C Connecting via GSM to xxx.s1.eu.hivemq.cloud[84411] ### Unhandled: +CME ERROR: operation not allowed