IM920地温観測システム CQ 2017ARMセミナー用サンプルプログラム

Dependencies:   C027_Support_ForIM920

Fork of C027_SupportTest by u-blox

Revision:
19:f022ff746eb8
Parent:
18:50e6c4ed8a4a
Child:
20:52f0e5de8c3d
--- a/main.cpp	Thu May 15 06:16:51 2014 +0000
+++ b/main.cpp	Tue May 27 09:20:46 2014 +0000
@@ -1,160 +1,106 @@
 #include "mbed.h"
-#include "GPS.h"
-#include "MDM.h"
-
-//----------------------------------------------------------------------
-// You may need to configure these parameters
-
-/** Set your secret SIM pin here "1234"
-*/
-#define SIMPIN      NULL
 
-/** The APN of your network operator, sometimes it is "internet" 
-    check your contract with the network operator
-*/
-#define APN         "gprs.swisscom.ch"
-
-/** Set the user name for your APN, or NULL if not needed
-*/
-#define USERNAME    NULL
-
-/** Set the password for your APN, or NULL if not needed
-*/
-#define PASSWORD    NULL 
-
-//----------------------------------------------------------------------
+//------------------------------------------------------------------------------------
 /* This example was tested on C027-U20 and C027-G35 with the on board modem. 
    
    Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON 
    is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this 
    configuration the following platforms were tested (it is likely that others 
    will work as well)
-   - U-BLOX:    C027-G35, C027-U20, C027-C20 (for shield remove define C027_USEONBOARD)
+   - U-BLOX:    C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
    - NXP:       LPC1549v2, LPC4088qsb
    - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
    - STM:       NUCLEO-F401RE, NUCLEO-F030R8
                 mount resistors SB13/14 1k, SB62/63 0R
 */
-#if defined(TARGET_UBLOX_C027)
-  #define C027_USEONBOARD // remove if you have the GSM/GPS shield mounted to the C027
-#endif
-
-#ifdef C027_USEONBOARD
-  #include "C027.h"
-  C027 c027;
-#else 
-  #define GPSSCL    D15
-  #define GPSSDA    D14
-  #define GPSADR   (66<<1) // GPS I2C Address
-  #define MDMPWRON  D4
-  #define MDMTXD    D1
-  #define MDMRXD    D0
-  #define MDMCTS    NC
-  #define MDMRTS    NC
-  #define MDMBAUD   115200
-#endif
-
-//----------------------------------------------------------------------
-// no tracing if serial is shared with the VCP
-#define DOTRACE ((USBRX!=MDMRXD)&&(USBTX!=MDMTXD))
-#define TRACE     (!DOTRACE)?:printf
+#include "GPS.h"
+#include "MDM.h"
+//------------------------------------------------------------------------------------
+// You need to configure these cellular modem / SIM parameters.
+// These parameters are ignored for LISA-C200 variants and can be left NULL.
+//------------------------------------------------------------------------------------
+//! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
+#define SIMPIN      NULL
+/*! The APN of your network operator SIM, sometimes it is "internet" check your 
+    contract with the network operator. You can also try to look-up your settings in 
+    google: https://www.google.de/search?q=APN+list */
+#define APN         "gprs.swisscom.ch"
+//! Set the user name for your APN, or NULL if not needed
+#define USERNAME    NULL
+//! Set the password for your APN, or NULL if not needed
+#define PASSWORD    NULL 
+//------------------------------------------------------------------------------------
 
 int main(void)
 {
     int ret;
-#ifdef TARGET_LPC1768
+#ifdef LARGE_DATA
     char buf[2048] = "";
 #else
     char buf[512] = "";
 #endif
 
-    // only trace if the serial is different from our modem port
-    if (DOTRACE) {
-        Serial pc(USBTX,USBRX);
-        pc.baud(115200);
-    }
-    
-    wait_ms(1000);
-    
-    TRACE("GSM/GPS Support Example\r\n");
-    
-#ifdef C027_USEONBOARD
-    // turn on the supplies of the Modem and the GPS
-    c027.mdmPower(true);
-    c027.gpsPower(true);
-#else
-    // turn on the Modem
-    DigitalOut mdmPwrOn(MDMPWRON, 0);
-    wait_ms(150);
-    mdmPwrOn = 1;
+    // Create the GPS object
+#if 1   // use GPSI2C class
+    GPSI2C gps;
+#else   // or GPSSerial class 
+    GPSSerial gps; 
 #endif
-    wait(2);
-    
-    // Create the GPS object
-#if GPSADR    // use GPSI2C class
-    GPSI2C gps(GPSSDA,GPSSCL,GPSADR); 
-#elif GPSBAUD // or GPSSerial class 
-    GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); 
-#else
-    #warning "please define the pins for the GPS"
-#endif
-    
     // Create the modem object
-    MDMSerial mdm(MDMTXD,MDMRXD,MDMBAUD
-#if DEVICE_SERIAL_FC
-                ,MDMRTS,MDMCTS
-#endif
-                );
-    
+    MDMSerial mdm;
+    //mdm.setDebug(4); // enable this for debugging issues 
     // initialize the modem 
-    TRACE("Device Init\r\n");
-    MDMParser::DevStatus devStatus;
-    MDMParser::NetStatus netStatus;
+    MDMParser::DevStatus devStatus = {};
+    MDMParser::NetStatus netStatus = {};
     bool mdmOk = mdm.init(SIMPIN, &devStatus);
+    mdm.dumpDevStatus(&devStatus);
+    if (mdmOk) {
+        // wait until we are connected
+        mdmOk = mdm.registerNet(&netStatus);
+        mdm.dumpNetStatus(&netStatus);
+    }
     if (mdmOk)
     {
-        if (DOTRACE) mdm.dumpDevStatus(&devStatus);
-        
-        // wait until we are connected
-        TRACE("Network Check\r\n");
-        while (!mdm.checkNetStatus(&netStatus))
-            wait_ms(1000);
+        const char* filename = "File";
+        char buf[] = "Hello World";
+        printf("writeFile \"%s\"\r\n", buf);
+        if (mdm.writeFile(filename, buf, sizeof(buf)))
+        {
+            memset(buf, 0, sizeof(buf));
+            int len = mdm.readFile(filename, buf, sizeof(buf));
+            if (len) 
+                printf("readFile %d \"%.*s\"\r\n", len, len, buf);
+            mdm.delFile(filename);
+        }
     
-        if (DOTRACE) mdm.dumpNetStatus(&netStatus);
-        TRACE("Network Join\r\n");
+        // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
+        // http://de.wikipedia.org/wiki/USSD-Codes
+        const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
+        printf("Ussd Send Command %s\r\n", ussd);
+        ret = mdm.ussdCommand(ussd, buf);
+        if (ret > 0) 
+            printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf);
+
         // join the internet connection 
-        MDMParser::IP ip = 
-            mdm.join(APN,USERNAME,PASSWORD);
+        MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
         if (ip != NOIP)
         {
-            if (DOTRACE) mdm.dumpIp(ip);
-            TRACE("Socket Create\r\n");
+            mdm.dumpIp(ip);
+            printf("Make a Http Post Request\r\n");
             int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
             if (socket >= 0)
             {
-                TRACE("Socket Connect\r\n");
                 mdm.socketSetBlocking(socket, 10000);
                 if (mdm.socketConnect(socket, "mbed.org", 80))
                 {
-                    TRACE("Make a Http Post Request\r\n");
                     const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
-                    TRACE("Socket Send\r\n");
                     mdm.socketSend(socket, http, sizeof(http)-1);
                 
-                    TRACE("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)
-                            TRACE("Socket Recv \"%*s\"\r\n", ret, buf);
-                    }
-                    TRACE("Socket Close\r\n");
+                    ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
+                    if (ret > 0)
+                        printf("Socket Recv \"%*s\"\r\n", ret, buf);
                     mdm.socketClose(socket);
                 }
-                TRACE("Socket Free\r\n");
                 mdm.socketFree(socket);
             }
             
@@ -162,7 +108,7 @@
             const char* host = "echo.u-blox.com";
             MDMParser::IP ip = mdm.gethostbyname(host);
             char data[] = "\r\nxxx Socket Hello World\r\n"
-#ifdef TARGET_LPC1768
+#ifdef LARGE_DATA
                         "00  0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
                         "01  0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
                         "02  0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
@@ -189,7 +135,7 @@
 #endif            
                         "End\r\n";
                 
-            TRACE("Testing TCP sockets with ECHO server\r\n");
+            printf("Testing TCP sockets with ECHO server\r\n");
             socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
             if (socket >= 0)
             {
@@ -198,18 +144,18 @@
                     memcpy(data, "\r\nTCP", 5); 
                     ret = mdm.socketSend(socket, data, sizeof(data)-1);
                     if (ret == sizeof(data)-1) {
-                        TRACE("Socket Send %d \"%s\"\r\n", ret, data);
+                        printf("Socket Send %d \"%s\"\r\n", ret, data);
                     }
                     ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
                     if (ret >= 0) {
-                        TRACE("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
+                        printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
                     }
                     mdm.socketClose(socket);
                 }
                 mdm.socketFree(socket);
             }
 
-            TRACE("Testing UDP sockets with ECHO server\r\n");
+            printf("Testing UDP sockets with ECHO server\r\n");
             socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
             if (socket >= 0)
             {
@@ -217,57 +163,60 @@
                 memcpy(data, "\r\nUDP", 5); 
                 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
                 if (ret == sizeof(data)-1) {
-                    TRACE("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
+                    printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
                 }
                 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
                 if (ret >= 0) {
-                    TRACE("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
+                    printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
                 }
                 mdm.socketFree(socket);
             }
             
             // disconnect  
-            TRACE("Network Disconnect\r\n");
             mdm.disconnect();
         }
-        
-        const char* ussd = "*#134#"; // You may get answer "UNKNOWN APPLICATION"
-        TRACE("Send Ussd Command %s\r\n", ussd);
-        ret = mdm.ussdCommand(ussd, buf);
-        if (ret > 0) 
-            TRACE("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
     }
-    TRACE("Checking SMS and GPS\r\n");
+    printf("SMS and GPS Loop\r\n");
     char link[128] = "";
     unsigned int i = 0xFFFFFFFF;
     const int wait = 100;
     bool abort = false;
     //DigitalOut led(LED1);
     while (!abort) {
-    //    led = !led;        
+    //    led = !led;
         while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
         {
             int len = LENGTH(ret);
-            //TRACE("NMEA: %.*s\r\n", len-2, msg); 
-            if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
+            //printf("NMEA: %.*s\r\n", len-2, msg); 
+            if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 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')
-                {
-                    TRACE("GPS Location: %.5f %.5f\r\n", la, lo); 
-                    sprintf(link, "I am here!\n"
-                                  "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
+                if (!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\r\n", la, lo); 
+                        sprintf(link, "I am here!\n"
+                                      "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
+                    }
+                } else if (!strncmp("$GPGGA", buf, 6)) {
+                    double a = 0; 
+                    if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
+                        printf("GPS Altitude: %.1f\r\n", a); 
+                } else if (!strncmp("$GPVTG", buf, 6)) {
+                    double s = 0; 
+                    if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
+                        printf("GPS Speed: %.1f\r\n", s); 
                 }
             }
         }
-        if (mdmOk && (i++ == 10000/wait)) {
+        if (mdmOk && (i++ == 5000/wait)) {
             i = 0;
             // check the network status
             if (mdm.checkNetStatus(&netStatus)) {
-                if (DOTRACE) mdm.dumpNetStatus(&netStatus);
+                mdm.dumpNetStatus(&netStatus, fprintf, stdout);
             }
                 
             // checking unread sms
@@ -277,30 +226,25 @@
             while (0 < n--)
             {
                 char num[32];
-                TRACE("Unread SMS at index %d\r\n", ix[n]);
+                printf("Unread SMS at index %d\r\n", ix[n]);
                 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
-                    TRACE("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
-                    TRACE("Delete SMS at index %d\r\n", ix[n]);
+                    printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
+                    printf("Delete SMS at index %d\r\n", ix[n]);
                     mdm.smsDelete(ix[n]);
                     // 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
-                    TRACE("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
+                    else if (strstr(buf, /*s*/"hutdown"))
+                        abort = true, reply = "bye bye";
+                    printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
                     mdm.smsSend(num, reply);
                 }
             }
         }
         wait_ms(wait);
     }
+    gps.powerOff();
     mdm.powerOff();
-    gps.powerOff();
-    TRACE("Shutdown\r\n");
-#ifdef C027_USEONBOARD
-    // now it is safe to switch LDOs off
-    c027.mdmPower(false);
-    c027.gpsPower(false);
-#endif    
     return 0;
 }
-