mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Revision:
4:90ab1ec64b0e
Parent:
2:b77151f111a9
Child:
5:5366d39d3719
--- a/main.cpp	Tue Apr 08 12:40:53 2014 +0000
+++ b/main.cpp	Tue Apr 08 15:50:09 2014 +0000
@@ -39,33 +39,11 @@
     printf("Modem Example\n");
 
     c027.mdmPower(true);
+    c027.gpsPower(true);
     wait(2);
 
-#if 0
-    c027.gpsPower(true);
-    GPSI2C gps; // use GPSI2C or GPSSerial class 
-    while (1)
-    {
-        while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
-        {
-            int len = LENGTH(ret);
-            //printf("NMEA: %.*s\n", len-2, msg); 
-            if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
-            {
-                double la = 0, lo = 0;
-                char ch;
-                if (gps.getNmeaAngle(1,buf,len,la) && 
-                    gps.getNmeaAngle(3,buf,len,lo) && 
-                    gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
-                {
-                    printf("GPS: %.5f %.5f\n", la, lo); 
-                }
-            }
-        }
-        wait_ms(100);
-    }
-#endif    
-
+    GPSI2C gps(GPSSDA,GPSSCL,GPSADR);     // use GPSI2C class
+    // GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); // or GPSSerial class 
     MDMSerial mdm(MDMTXD, MDMRXD, MDMBAUD
 #if DEVICE_SERIAL_FC
                  ,MDMRTS,MDMCTS
@@ -74,94 +52,145 @@
                 
     // initialize the modem 
     printf("Init\r\n");
-    mdm.init();
+    MDMParser::DevStatus status;
+    if (mdm.init(NULL/*no SIM PIN*/, &status))
+    {
+        printf("Device Status:\n");
+        const char* txtModel[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
+        if (status.model < sizeof(txtModel)/sizeof(*txtModel) && (status.model != MDMParser::MODEL_UNKNOWN))
+            printf("  Model: %s\n", txtModel[status.model]);
+        const char* txtSim[] = { "Unknown", "Pin", "Ready" };
+        if (status.sim < sizeof(txtSim)/sizeof(*txtSim) && (status.sim != MDMParser::SIM_UNKNOWN))
+            printf("  SIM:   %s\n", txtSim[status.sim]);
+        if (status.ccid)  
+            printf("  CCID:  %s\n", status.ccid);
+        if (status.imei) 
+            printf("  IMEI:  %s\r\n", status.imei);
+        if (status.imsi)  
+            printf("  IMSI:  %s\n", status.imsi);
+            
+        // wait until we are connected
+        printf("Network Check\r\n");
+        while (!mdm.checkNetStatus())
+            wait_ms(1000);
     
-    // wait until we are connected
-    printf("Network Check\r\n");
-    while (!mdm.checkNetStatus())
-        wait_ms(1000);
-
-    printf("Network Join\r\n");
-    // join the internet connection 
-    if (mdm.join("gprs.swisscom.ch"))
-    {
-        printf("Socket Create\r\n");
-        int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
-        if (socket >= 0)
+        printf("Network Join\r\n");
+        // join the internet connection 
+        if (mdm.join("gprs.swisscom.ch"))
         {
-            printf("Socket Connect\r\n");
-            if (mdm.socketConnect(socket, "mbed.org", 80))
+            printf("Socket Create\r\n");
+            int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
+            if (socket >= 0)
             {
-                printf("Make a Http Post Request\r\n");
-                const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
-                printf("Socket Send\r\n");
-                mdm.socketSend(socket, http, sizeof(http)-1);
+                printf("Socket Connect\r\n");
+                if (mdm.socketConnect(socket, "mbed.org", 80))
+                {
+                    printf("Make a Http Post Request\r\n");
+                    const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
+                    printf("Socket Send\r\n");
+                    mdm.socketSend(socket, http, sizeof(http)-1);
+                
+                    printf("Socket Recving\r\n");
+                    while (true) {
+                        ret = mdm.socketReadable(socket);
+                        if (ret > 0)
+                            ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
+                        if (ret < 0)
+                            break;
+                        else if (ret > 0)
+                            printf("Socket Recv \"%*s\"\r\n", ret, buf);
+                    }
+                    printf("Socket Close\r\n");
+                    mdm.socketClose(socket);
+                }
+                printf("Socket Free\r\n");
+                mdm.socketFree(socket);
+            }
             
-                printf("Socket Recving\r\n");
-                while (true) {
-                    ret = mdm.socketReadable(socket);
-                    if (ret > 0)
-                        ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
-                    if (ret < 0)
-                        break;
-                    else if (ret > 0)
-                        printf("Socket Recv \"%*s\"\r\n", ret, buf);
-                }
-                printf("Socket Close\r\n");
-                mdm.socketClose(socket);
-            }
-            printf("Socket Free\r\n");
-            mdm.socketFree(socket);
+            // disconnect  
+            printf("Network Disconnect\r\n");
+            mdm.disconnect();
         }
         
-        // disconnect  
-        printf("Network Disconnect\r\n");
-        mdm.disconnect();
+        const char* ussd = "*#134#";
+        printf("Send Ussd Command %s\r\n", ussd);
+        ret = mdm.ussdCommand(ussd, buf, sizeof(buf));
+        if (ret > 0) 
+            printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
+            
+        printf("Checking SMS and GPS\r\n");
+        char link[128] = "";
+        int i = 0;
+        const int wait = 100;
+        while (1) {
+            
+            while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
+            {
+                int len = LENGTH(ret);
+                //printf("NMEA: %.*s\n", len-2, msg); 
+                if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
+                {
+                    double la = 0, lo = 0;
+                    char ch;
+                    if (gps.getNmeaAngle(1,buf,len,la) && 
+                        gps.getNmeaAngle(3,buf,len,lo) && 
+                        gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
+                    {
+                        printf("GPS Location: %.5f %.5f\n", la, lo); 
+                        sprintf(link, "I am here!\n"
+                                      "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
+                    }
+                }
+            }
+            
+            if (i++ == 10000/wait) {
+                i = 0;
+                
+                // checking incoming sms
+                int cnt = mdm.smsCount();
+                // search through all the messages if there are some
+                for (int ix = 1; (cnt > 0) && (ix <= 350); ix ++) {
+                    char num[32];
+                    if (mdm.smsRead(ix, num, buf, sizeof(buf))) {
+                        printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
+                        printf("Delete SMS at index %d\r\n", ix);
+                        mdm.smsDelete(ix);
+                        cnt --;
+                        // provide a reply
+                        const char* reply = "Hello my friend";
+                        if (strstr(buf, /*w*/"here are you"))
+                            reply = *link ? link : "I don't know"; // reply wil location link
+                        printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
+                        mdm.smsSend(num, reply);
+                    }
+                }
+                
+                // check the network status
+                MDMParser::NetStatus status;
+                if (mdm.checkNetStatus(&status))
+                {
+                    printf("Network Status:\n");
+                    const char* txtNet[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
+                    if (status.net < sizeof(txtNet)/sizeof(*txtNet) && (status.net != MDMParser::NET_UNKNOWN))
+                        printf("  Network:            %s\n", txtNet[status.net]);
+                    const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
+                    if (status.act < sizeof(txtAct)/sizeof(*txtAct) && (status.act != MDMParser::ACT_UNKNOWN))
+                        printf("  Access Technology:  %s\n", txtAct[status.act]);
+                    if (status.rssi) 
+                        printf("  Signal Strength:    %d dBm\r\n", status.rssi);
+                    if (status.opr)  
+                        printf("  Operator:           %s\n", status.opr);
+                    if (status.num)  
+                        printf("  Phone Number:       %s\n", status.num);
+                }
+            }
+            wait_ms(wait);
+        }  
+        mdm.powerOff();
     }
-    
-    const char* ussd = "*#134#";
-    printf("Send Ussd Command %s\r\n", ussd);
-    ret = mdm.ussdCommand(ussd, buf, sizeof(buf));
-    if (ret > 0) 
-        printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
-        
-    printf("Checking SMS\r\n");
-    //int cnt = mdm.smsCount();
-    while (1) {
-        char num[32];
-        for (int ix = 0; ix < 16; ix ++) {
-            if (mdm.smsRead(ix, num, buf, sizeof(buf))) {
-                printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
-                printf("Delete SMS at index %d\r\n", ix);
-                mdm.smsDelete(ix);
-                const char* reply = "Hello my friend";
-                printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
-                mdm.smsSend(num, reply);
-            }
-        }
-        MDMParser::Status info;
-        if (mdm.checkNetStatus(&info))
-        {
-            printf("Network Status:\n");
-            const char* txtNet[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
-            if (info.net < sizeof(txtNet)/sizeof(*txtNet) && (info.net != MDMParser::NET_UNKNOWN))
-                printf("  Network:            %s\n", txtNet[info.net]);
-            const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
-            if (info.act < sizeof(txtAct)/sizeof(*txtAct) && (info.act != MDMParser::ACT_UNKNOWN))
-                printf("  Access Technology:  %s\n", txtAct[info.act]);
-            if (info.rssi) 
-                printf("  Signal Strength:    %d dBm\r\n", info.rssi);
-            if (info.opr)  
-                printf("  Operator:           %s\n", info.opr);
-            if (info.num)  
-                printf("  Phone Number:       %s\n", info.num);
-        }
-        wait_ms(10000);
-    }  
-    
-    mdm.powerOff();
     // now it is safe to switch off
     c027.mdmPower(false);
+    c027.gpsPower(false);
     
     return 0;
 }