startup loop nonresponsive

I worked up an incredibly simple setup() and loop() routine to initialize a MKR GSM 1400 and echo various messages throughout to the Serial port. When I compile and upload the code and open my PuTTy terminal to the appropriate port, the terminal echoes as follows:

SMS Messages Receiver 1
0
test1

…so it looks to be freezing at the GSM begin() function. The documentation for this function specifies in it’s default parameter state, it should return a status text. Nothing is coming back, and the rest of the program is not executing.

I am only using the USB port, which should be mapped to Serial. I have installed GSM(1.0.6) and MKRGSM(1.3.0) Arduino libraries, and have to this point successfully run other code samples such as the LED Blink example.

Code is below. Anything I’m missing here?

Thanks,
R

// include the GSM library
#include <MKRGSM.h>
  
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup() {
 
 pinMode(LED_BUILTIN, OUTPUT); // Init LED_BUILTIN pin as output
 // initialize serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }

 Serial.println("SMS Messages Receiver 1");

 // connection state
 bool connected = false;
 Serial.println(connected);

 // Start GSM connection
 while (!connected) {
   Serial.println("test1");
   Serial.println(gsmAccess.begin()); 
   Serial.println("test1.1");
   
   if (gsmAccess.begin()==GSM_READY) {
     Serial.println("test2");
     connected = true;
   } 
   
   else {
     Serial.println("Not connected");
     delay(1000);
   }
 }

}

void loop() {
int c;
Serial.println("Made it to loop()");
 // Check the sms object to see if any SMS messages are ready to receive.
 if (sms.available()) {
   Serial.println("Message received from:");

   // Get remote number
   sms.remoteNumber(senderNumber, 20);
   Serial.println(senderNumber);

   // An example of message disposal
   // Any messages starting with # should be discarded
   if (sms.peek() == '#') {
     Serial.println("Discarded SMS");
     sms.flush();
   }

   // Read message bytes and print them
   while ((c = sms.read()) != -1) {
     Serial.print((char)c);
   }

   Serial.println("\nEND OF MESSAGE");

   // Delete message from modem memory
   sms.flush();
   Serial.println("MESSAGE DELETED");
 }

 
 Serial.println("Unavailable SMS, but looping.");
 delay(1000);

}

You have used print statements to accurately diagnose where your program gets stuck. I do not know the GSM hardware so I cannot explain WHY it gets stuck there. If you ever get the hardware working, you are going to call begin() twice which is usually not such a good idea. Not knowing this library, I do not know for sure but it seems to be something to avoid.

Code is below. Anything I’m missing here? - code tags

// include the GSM library
#include <MKRGSM.h>

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup() 
{
    pinMode(LED_BUILTIN, OUTPUT); // Init LED_BUILTIN pin as output
    // initialize serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) 
    {
        ; // wait for serial port to connect. Needed for native USB port only
    }

    Serial.println("SMS Messages Receiver 1");

    // connection state
    bool connected = false;
    Serial.println(connected);

    // Start GSM connection
    while (!connected) 
    {
        Serial.println("test1");
        Serial.println(gsmAccess.begin());
        Serial.println("test1.1");

        if (gsmAccess.begin()==GSM_READY) 
        {
            Serial.println("test2");
            connected = true;
        }    
        else 
        {
            Serial.println("Not connected");
            delay(1000);
        }
    }
}

void loop() 
{
    int c;
    Serial.println("Made it to loop()");
    
    // Check the sms object to see if any SMS messages are ready to receive.
    if (sms.available()) 
    {
        Serial.println("Message received from:");
        // Get remote number
        sms.remoteNumber(senderNumber, 20);
        Serial.println(senderNumber);
        // An example of message disposal
        // Any messages starting with # should be discarded
        
        if (sms.peek() == '#') 
        {
            Serial.println("Discarded SMS");
            sms.flush();
        }
        // Read message bytes and print them
        while ((c = sms.read()) != -1) 
        {
            Serial.print((char)c);
        }

        Serial.println("\nEND OF MESSAGE");
        // Delete message from modem memory
        sms.flush();
        Serial.println("MESSAGE DELETED");
    }

    Serial.println("Unavailable SMS, but looping.");
    delay(1000);
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.... :slight_smile:

Thank you for the FYI (and vice versa for my ignorance). I have edited the previous post to include tags.

I have made a small amount of progress on this by placing the GSM instance in debug mode. It’s now echoing some of the AT commands and results that occur:

SMS Messages Receiver 1
0
test1
▒AT
OK
AT+IPR=921600
OK
AT
OK
AT+UPSV=3
OK
AT+CPIN?
ERROR
AT+CPIN?
ERROR
AT+CPIN?
ERROR

A couple things here…

1.) AT+IPR setting:

I am using the uBlox SARA-U201 module for connectivity. According to section 15.9 in the spec sheet - “UART data rate configuration +IPR” - the 921600 baud that appears here is not a fluke and is apparently a common setting. This is the data rate that the DCE accepts commands on the UART interface. Does me utilizing my Serial connection (usb) at 9600 baud have anything to do with this, or is this terminal session completely decoupled from the uBlox’s UART interface? I am probably confusing my Serial session with the actual radio receive rate.

2.) AT+CPIN? query:

Rather than receiving back "+CPIN: " and “OK” I am erroring out with no indication as to why. At this point I don’t know if it’s my baud rate causing the issue or my permissions to access the uBlox. The only reason why I lean towards the latter is because it accepted the UPSV setting prior to this error in my log above.

Update after flipping the physical orientation of the SIM card >:( >:( >:(

SMS Messages Receiver 1
0
test1
▒AT
OK
AT+IPR=921600
OK
Not connected
test1
▒AT
OK
AT+IPR=921600
OK
Not connected
test1
▒AT
OK
AT+IPR=921600
OK
AT
OK
AT+UPSV=3
OK
AT+CPIN?
ERROR
AT+CPIN?
ERROR
AT+CPIN?
ERROR
AT+CPIN?
ERROR
AT+CPIN?
+CPIN: READY

OK
AT+CMGF=1		// Indicates to MT which input and output format of messages to use (1=text)
OK
AT+UDCONF=1,1	// Enable hex mode configuration for +USOWR, +USOST, +USORD and +USORF
OK

+PACSP0		        // Response with network selection control setting (0=auto network select)
AT+CTZU=1		// Automatic time zone update (1=auto update via NITZ if supported)
OK
AT+UDTMFD=1,2	// DTMF detector; (1=Enable URC generation on terminal, 2=robust mode)
OK
AT+CREG?		// Network registration status? (0=Network registration URC disabled,
+CREG: 0,0		// … 0= not registered, MT not currently searching a new operator.)

I guess I need to figure out if this AT&T account is active, as it appears this is what CREG would reflect.

All, as an update to this thread. I have verified the IMEI and service contract with AT&T to ensure there are no issues on the server side accessing a data plan.

Right now, the uBlox is just getting hung up on the following:

OK
AT+CREG?
+CREG: 0,2

OK
AT+CREG?
+CREG: 0,2

OK
AT+CREG?
+CREG: 0,2

OK
AT+CREG?
+CREG: 0,2

I have read in the uBlox commands manual that having a ‘0’ as the first parameter like I have is a default and factory-programmed value. Network registration URC disabled. I’m wondering if I must first send a command first to initialize this thing to register?

This is rather nonsensical:

    // initialize serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for native USB port only
    }

The whole point of the if (!Serial) is to wait until the Serial object has been created (i.e. - it actually exists), BEFORE trying to access any of its features. Calling Serial.begin() while !Serial is true, WILL crash the Arduino.

Regards,
Ray L.

No it won't. Calling begin() and then checking !Serial is the normal way it's done for Due, Leonardo, Teensy etc.

Which Arduinos will crash this way?

Sorry to pile on, but the example at if(Serial) - Arduino Reference has this:

void setup() {
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

void loop() {
 //proceed normally
}

One of these days I have to figure out how this works.

What's the best way to issue AT commands manually at the serial connection through the terminal rather than just seeing whatever is echoed by running library methods such as GSM.begin()? I'd like to be able to read the returned value of some individual commands (AT+COPS? for example)

File->Examples->04. Communication->Serial Passthru