example-reading-imei-imsi

Revision:
1:63483ec4d0ff
Parent:
0:b51700dcba34
--- a/main.cpp	Fri Oct 13 16:09:30 2017 +0000
+++ b/main.cpp	Thu Feb 15 11:48:51 2018 +0000
@@ -84,16 +84,23 @@
     /* Give modem a little time to settle down */
     wait_ms(250);
 
+    printf("Powering up the modem\r\n");
+    onboard_modem_power_up();
+#ifdef TARGET_UBLOX_C030_N211
+    wait_ms(5000);
+#else
+    wait_ms(500);
+#endif
+
+    // Set AT parser timeout to 1sec for AT OK check
+    at->set_timeout(1000);
+    
+    printf("Checking for AT response from the modem\r\n");
     for (int retry_count = 0; !success && (retry_count < 20); retry_count++) {
         printf("...wait\r\n");
-        onboard_modem_power_up();
-        wait_ms(500);
-        // The modem tends to sends out some garbage during power up. Needs to clean up. 
+        // The modem tends to sends out some garbage during power up.
         at->flush();
         
-        // Set AT parser timeout to 1sec for AT OK check
-        at->set_timeout(1000);
-        
         // AT OK talk to the modem
         if (at->send("AT")) {
             wait_ms(100);
@@ -101,29 +108,38 @@
                 success = true;
             }
         }
-        // Increase the parser time to 8sec       
-        at->set_timeout(8000);
     }
+    
+    // Increase the parser time to 8 sec
+    at->set_timeout(8000);
+    
     if (success) {
+        printf("Configuring the modem\r\n");
+
+#ifdef TARGET_UBLOX_C030_N211
+        // Turn off modem echoing and turn on verbose responses
+        success = at->send("AT+CMEE=1");
+#else
         // Set the final baud rate
         if (at->send("AT+IPR=%d", 115200) && at->recv("OK")) {
             // Need to wait for things to be sorted out on the modem side
             wait_ms(100);
             ((UARTSerial *)fh)->set_baud(115200);
         }
-
+        
         // Turn off modem echoing and turn on verbose responses
         success = at->send("ATE0;+CMEE=2") && at->recv("OK") &&
                   // The following commands are best sent separately
-              
                   // Turn off RTC/CTS handshaking
-                  at->send("AT&K0") && at->recv("OK") && 
+                  at->send("AT&K0") && at->recv("OK") &&
                   // Set DCD circuit(109), changes in accordance with 
                   // the carrier detect status
                   at->send("AT&C1") && at->recv("OK") &&
                   // Set DTR circuit, we ignore the state change of DTR 
                   at->send("AT&D0") && at->recv("OK"); 
+#endif                  
     }
+    
     return success;
 }
 /*
@@ -133,34 +149,54 @@
 
 int main()
 {
-    printf("\n\r\n\ru-blox C030-U201 Reading the modem IMEI and eUICC/SIM IMSI\n\r");
+    bool success = false;
+    
+    printf("\n\r\n\ru-blox C030 reading the modem IMEI and eUICC/SIM IMSI\n\r");
     printf("Initialising UART for modem communication");
-    fh = new UARTSerial(MDMTXD, MDMRXD, 115200);
+    fh = new UARTSerial(MDMTXD, MDMRXD, 9600);
     printf("...DONE\r\n");
     
-    printf("Initialising the Modem AT Command Parser");
+    // NOTE: if you are experiencing problems with the AT command
+    // exchange, change the "false" below to "true" to get debug output
+    // from the AT command parser
+    printf("Initialising the modem AT command parser");
     at = new ATCmdParser(fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE,
             AT_PARSER_TIMEOUT, false);
     printf("...DONE\r\n");
     
     printf("Initializing the modem\r\n");
-    if (setup_modem()){
+    if (setup_modem()) {
         printf("...DONE\r\nThe modem powered up\r\n");
-        char imei[15+1];
+        char imei[25+1];
         char imsi[15+1];
         printf("Reading IMEI and IMSI\r\n");
+        
+#ifdef TARGET_UBLOX_C030_N211
+        if (at->send("AT+CGSN=1") && at->recv("\nOK\n%25[^\n]\nOK\n", imei)){
+            printf("IMEI: %s\r\n",imei + 6); // Avoid the "+CGSN:" prefix
+        }
+#else
         if (at->send("AT+CGSN") && at->recv("%15[^\n]\nOK\n", imei)){
             printf("IMEI: %s\r\n",imei);
         }
-        if (at->send("AT+CIMI") && at->recv("%15[^\n]\nOK\n", imsi)){
-            printf("IMSI: %s\r\n",imsi);
+#endif
+        // Sometimes it takes a little while for the SIM to be initialised,
+        // so retry this until done
+        at->set_timeout(1000);
+        for (int retry_count = 0; !success && (retry_count < 10); retry_count++) {
+            if (at->send("AT+CIMI") && at->recv("%15[^\n]\nOK\n", imsi)){
+                printf("IMSI: %s\r\n",imsi);
+                success = true;
+            }
+        }        
+        if (!success) {
+            printf("Unable to read IMSI: has a SIM been inserted?\r\n");
         }
-    }
-    else {
-        printf("Unable To intialize modem\r\n");
+    } else {
+        printf("Unable to intialize modem\r\n");
     }
     
-    printf("FINISHED...\n\r");
+    printf("FINISHED...\r\n");
     
     // Create threadUserButtonCheck thread
     Thread threadUserButtonCheck(threadBodyUserButtonCheck);