vshymanskyy / TinyGSM

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

Serial2 stop working after SIM registration if something goes wrong #556

Closed gbconnet closed 3 years ago

gbconnet commented 3 years ago

What type of issues is this?

[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: SIM800L R14.18 TinyGSM version: 0.11.3 Code:

//
// ESP32GPRSSIMProblem.ino
//
// Serial2 stop during SIM initialization
//
/*##############################################################################
                              Core Libraries
##############################################################################*/
#include <WiFi.h>

/*##############################################################################
                              Defines
##############################################################################*/
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_DEBUG        Serial2
#define GSM_AUTOBAUD_MIN      9600
#define GSM_AUTOBAUD_MAX      115200

// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS

#include <TinyGsmClient.h>

#define SMOD Serial1   // GPRS
#define SDBG Serial2   // DEBUG

// SERIAL 1 PINS (In my board)
#define RX1_PIN               4
#define TX1_PIN               2

// LED PIN       (In my board)
#define LEDPIN                32

/*##############################################################################
                        Prototypes
##############################################################################*/
bool GPRSInit(int Mode);

/*##############################################################################
                        Global Variables
##############################################################################*/
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SMOD, SDBG);
TinyGsm modem(debugger);
#else
TinyGsm modem(SMOD);
#endif
TinyGsmClient client(modem);

// Try to use the right and a wrong APN to see the problem
//const char* APN = "m2m.vodafone.it";
const char* APN = "ibox.tim.it";
const char* Username = "";
const char* Password = "";

/*##############################################################################
                        Main Program  (Setup and Loop)
##############################################################################*/
/******************************************************************************
* setup : Initialize ESP32
******************************************************************************/
void setup() {

    SDBG.begin(115200,SERIAL_8N1);
    SDBG.setDebugOutput(false);
    delay(1000);

    SDBG.println();
    SDBG.println("-------------------------------------------------------");
    SDBG.println("   ESP32 GPRS Example");
    SDBG.println("-------------------------------------------------------");

    pinMode     (LEDPIN, OUTPUT);
    digitalWrite(LEDPIN, LOW);

    //GPRS Initialization
    delay(5000);            // Waiting for SIM800L wake up

    for (int i=0; i<2; i++)  // Try to Initialize GPRS
        if (GPRSInit(i))
            break;

    digitalWrite(LEDPIN, HIGH);
    SDBG.println("SETUP: END");
}

/******************************************************************************
* Loop : Manage ESP32
******************************************************************************/
void loop() 
{
} // loop()

/******************************************************************************
* GPRSInit     : Joins GPRS (HOME)
******************************************************************************/
bool GPRSInit(int Mode) 
{
#if SMOD == Serial1
    SMOD.begin(115200, SERIAL_8N1, RX1_PIN, TX1_PIN);
#else
    SMOD.begin(115200, SERIAL_8N1);
#endif

    SDBG.println("MOD: Initializing modem...");

    // Restart takes quite some time
    // To skip it, call init() instead of restart()
    if (Mode)
        modem.restart();
    else
        modem.init();

    String modemInfo = modem.getModemInfo();
    SDBG.print("MOD: Modem Info - ");
    SDBG.println(modemInfo);

    if (!modem.isNetworkConnected()) {
        SDBG.print("MOD: Waiting for network...");
        if (!modem.waitForNetwork()) {
            SDBG.println(" fail");
            delay(5000);
            return(false);
        }
        SDBG.println(" success");
    }

    if (!modem.isGprsConnected()) {
        SDBG.printf("MOD: Connecting to '%s'... ", APN);
        if (!modem.gprsConnect(APN, Username, Password)) {
            SDBG.println("fail");
            delay(5000);
            return(false);
        }
        SDBG.println("success");
    }

    SDBG.println("MOD: modem Initialized");
    return(true);
}

Scenario, steps to reproduce

I have a problem using TinyGSM with SIM800L module. If you set Serial2 as debug, in SIM registration if something goes wrong (modem.waitForNetwork() or modem.gprsConnect(...)) Serial2 stop working (but the program continue to work)

Actual result

In the example if you try to modify APN with a wrong one, Serial2 stop working. If APN is right all is OK

Debug and AT command log

-------------------------------------------------------
   ESP32 GPRS Example
-------------------------------------------------------
MOD: Initializing modem...
[6054] ### TinyGSM Version: 0.10.9
[6054] ### TinyGSM Compiled Module:  TinyGsmClientSIM800
AT

OK
ATE0

OK
AT+CMEE=2

OK
AT+GMM

SIMCOM_SIM800F

OK
[6073] ### Modem: SIMCOM SIM800F
[6073] ### Modem: SIMCOM SIM800F
AT+CLTS=1

OK
AT+CBATCHK=1

OK
AT+CPIN?

+CPIN: READY

OK
ATI

SIM800 R14.18

OK
MOD: Modem Info - SIM800 R14.18
AT+CREG?

+CREG: 0,2

OK
MOD: Waiting for network...AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,2

OK
AT+CREG?

+CREG: 0,1

OK
 success
AT+CGATT?

+CGATT: 0

OK
MOD: Connecting to 'ibox.tim.it'... AT+CIPSHUT

SHUT OK
AT+CGATT=0

OK
AT+SAPBR=3,1,"Contype","GPRS"

OK
AT+SAPBR=3,1,"APN","ibox.tim.it"

OK
AT+CGDCONT=1,"IP","ibox.tim.it"

OK
AT+CGACT=1,1
AT+SAPBR=1,1

+CME ERROR: unknown
AT+SAPBR=2,1

+SAPBR: 1,3,"0.0.0.0"

OK
AT+CGATT=1

+CME ERROR: unknown
fail
gbconnet commented 3 years ago

I forgot to write that if you use Serial port as debug, all is OK

gbconnet commented 3 years ago

I have found the problem.It seems to be the instruction:

#if SMOD == Serial1
    SMOD.begin(115200, SERIAL_8N1, RX1_PIN, TX1_PIN);
#else
    SMOD.begin(115200, SERIAL_8N1);
#endif

Program fail to valuate "#if SMOD == Serial1", and Serial2 port crash!!!