インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラム。   CQ publishing Interface 2014.10 issue, C027 3G test program.

Dependencies:   C027_Support C027_SupportTest mbed picojson

Fork of C027_SupportTest by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*
00003  Interface 2014/10
00004  u-blolx C027 3G test sample
00005  2014/07 Naoya Takamura
00006  */
00007 //------------------------------------------------------------------------------------
00008 /* This example was tested on C027-U20 and C027-G35 with the on board modem. 
00009    
00010    Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON 
00011    is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this 
00012    configuration the following platforms were tested (it is likely that others 
00013    will work as well)
00014    - U-BLOX:    C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
00015    - NXP:       LPC1549v2, LPC4088qsb
00016    - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
00017    - STM:       NUCLEO-F401RE, NUCLEO-F030R8
00018                 mount resistors SB13/14 1k, SB62/63 0R
00019 */
00020 #include "GPS.h"
00021 #include "MDM.h"
00022 #include "picojson.h"
00023 
00024 //------------------------------------------------------------------------------------
00025 // You need to configure these cellular modem / SIM parameters.
00026 // These parameters are ignored for LISA-C200 variants and can be left NULL.
00027 //------------------------------------------------------------------------------------
00028 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
00029 #define SIMPIN      NULL
00030 /*! The APN of your network operator SIM, sometimes it is "internet" check your 
00031     contract with the network operator. You can also try to look-up your settings in 
00032     google: https://www.google.de/search?q=APN+list */
00033 //#define APN         NULL
00034 #define APN         "3g-d-2.ocn.ne.jp"
00035 //! Set the user name for your APN, or NULL if not needed
00036 //#define USERNAME    NULL
00037 #define USERNAME    "hoge@one.ocn.ne.jp"
00038 //! Set the password for your APN, or NULL if not needed
00039 //#define PASSWORD    NULL 
00040 #define PASSWORD    "hogehoge"
00041 
00042 //---- Xively --------------------------------------------------------------
00043 #define XI_FEED_ID "SET your Feed ID" // set Xively Feed ID (numerical, no quoutes)
00044 #define XI_API_KEY "SET your Feed API KEY" // set Xively API key (double-quoted string)
00045 
00046 #define XI_SERVER   "api.xively.com"
00047 
00048 #define XI_PATH     "/v2/feeds/" XI_FEED_ID ".json"
00049 #define XI_HEADER   "X-ApiKey: " XI_API_KEY
00050 
00051 #define XI_UPLOAD_INTERVAL   2  // sec
00052 //------------------------------------------------------------------------------------
00053 
00054 int main(void)
00055 {
00056     int ret;
00057     char buf[512] = "";
00058     // Add by ntaka for USB Console
00059     Serial pc(USBTX,USBRX);
00060     pc.baud(115200);
00061 
00062     // Create the GPS object
00063     // use GPSI2C class
00064     GPSI2C gps;
00065     // Create the modem object
00066     MDMSerial mdm;
00067     //mdm.setDebug(4); // enable this for debugging issues 
00068     // initialize the modem 
00069     MDMParser::DevStatus devStatus = {};
00070     MDMParser::NetStatus netStatus = {};
00071     bool mdmOk = mdm.init(SIMPIN, &devStatus);
00072     mdm.dumpDevStatus(&devStatus);
00073     if (mdmOk) {
00074 #if 0
00075         // file system API
00076         const char* filename = "File";
00077         char buf[] = "Hello World";
00078         printf("writeFile \"%s\"\r\n", buf);
00079         if (mdm.writeFile(filename, buf, sizeof(buf)))
00080         {
00081             memset(buf, 0, sizeof(buf));
00082             int len = mdm.readFile(filename, buf, sizeof(buf));
00083             if (len >= 0) 
00084                 printf("readFile %d \"%.*s\"\r\n", len, len, buf);
00085             mdm.delFile(filename);
00086         }
00087 #endif
00088 
00089         // wait until we are connected
00090         mdmOk = mdm.registerNet(&netStatus);
00091         mdm.dumpNetStatus(&netStatus);
00092     }
00093     if (mdmOk)
00094     {
00095         // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
00096         // http://de.wikipedia.org/wiki/USSD-Codes
00097         const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
00098         printf("Ussd Send Command %s\r\n", ussd);
00099         ret = mdm.ussdCommand(ussd, buf);
00100         if (ret > 0) 
00101             printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf);
00102 
00103         // join the internet connection 
00104         MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
00105         if (ip != NOIP)
00106         {
00107             mdm.dumpIp(ip);
00108             printf("Make a Http Post Request\r\n");
00109             int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
00110             if (socket >= 0)
00111             {
00112                 mdm.socketSetBlocking(socket, 10000);
00113                 if (mdm.socketConnect(socket, "mbed.org", 80))
00114                 {
00115                     const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
00116                     mdm.socketSend(socket, http, sizeof(http)-1);
00117                 
00118                     ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
00119                     if (ret > 0)
00120                         printf("Socket Recv \"%*s\"\r\n", ret, buf);
00121                     mdm.socketClose(socket);
00122                 }
00123                 mdm.socketFree(socket);
00124             }
00125 #if 0
00126             
00127             int port = 7;
00128             const char* host = "echo.u-blox.com";
00129             MDMParser::IP ip = mdm.gethostbyname(host);
00130             char data[] = "\r\nxxx Socket Hello World\r\n"
00131                         "End\r\n";
00132                 
00133             printf("Testing TCP sockets with ECHO server\r\n");
00134             socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
00135             if (socket >= 0)
00136             {
00137                 mdm.socketSetBlocking(socket, 10000);
00138                 if (mdm.socketConnect(socket, host, port)) {
00139                     memcpy(data, "\r\nTCP", 5); 
00140                     ret = mdm.socketSend(socket, data, sizeof(data)-1);
00141                     if (ret == sizeof(data)-1) {
00142                         printf("Socket Send %d \"%s\"\r\n", ret, data);
00143                     }
00144                     ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
00145                     if (ret >= 0) {
00146                         printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
00147                     }
00148                     mdm.socketClose(socket);
00149                 }
00150                 mdm.socketFree(socket);
00151             }
00152 
00153             printf("Testing UDP sockets with ECHO server\r\n");
00154             socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
00155             if (socket >= 0)
00156             {
00157                 mdm.socketSetBlocking(socket, 10000);
00158                 memcpy(data, "\r\nUDP", 5); 
00159                 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
00160                 if (ret == sizeof(data)-1) {
00161                     printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
00162                 }
00163                 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
00164                 if (ret >= 0) {
00165                     printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
00166                 }
00167                 mdm.socketFree(socket);
00168             }
00169 #endif
00170            
00171 // no disconnect
00172             // disconnect  
00173 //            mdm.disconnect();
00174         }
00175     }
00176 
00177     // Upload GPS data to Xively
00178     printf("SMS and GPS Loop\r\n");
00179     char link[128] = "";
00180     unsigned int i = 0xFFFFFFFF;
00181     const int wait = 100;
00182     bool abort = false;
00183     int xv_upload_cnt = XI_UPLOAD_INTERVAL;
00184     //DigitalOut led(LED1);
00185     while (!abort) {
00186     //    led = !led;
00187         while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
00188         {
00189             int len = LENGTH(ret);
00190             //printf("NMEA: %.*s\r\n", len-2, msg); 
00191             if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
00192             {
00193                 if (!strncmp("$GPGLL", buf, 6)) {
00194                     double la = 0, lo = 0;
00195                     char ch;
00196                     if (gps.getNmeaAngle(1,buf,len,la) && 
00197                         gps.getNmeaAngle(3,buf,len,lo) && 
00198                         gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
00199                     {
00200                         printf("GPS Location: %.5f %.5f\r\n", la, lo); 
00201                         sprintf(link, "I am here!\n"
00202                                       "https://maps.google.com/?q=%.5f,%.5f", la, lo); 
00203                         // Upload to Xively
00204                         if (xv_upload_cnt++ >= XI_UPLOAD_INTERVAL) {
00205                             xv_upload_cnt = 0;
00206                             int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
00207                             if (socket >= 0)
00208                             {
00209                                 mdm.socketSetBlocking(socket, 10000);
00210                                 if (mdm.socketConnect(socket, XI_SERVER, 80))
00211                                 {
00212                 //                    char data[128];
00213                 //                    sprintf(data, "lat,%f\r\nlon,%f\r\n", la, lo);
00214                                     // Make JSON location
00215                                     picojson::object v;
00216                                     picojson::object loc;
00217                                     string mb="mobile";
00218                                  
00219                                     loc["disposition"] =  picojson::value(mb);
00220                                     loc["lat"] =  picojson::value(la);
00221                                     loc["lon"] =  picojson::value(lo);
00222                                     v["location"] =  picojson::value(loc);
00223                                     // JSON serialize
00224                                     string str = picojson::value(v).serialize();
00225 //                                    printf("serialized content = %s\r\n" ,  str.c_str());
00226                                     // PUT HTTP data to Xively
00227                                     char http[512];
00228                                     sprintf(http, "PUT %s HTTP/1.1\r\nHost: %s\r\n%s\r\nContent-Length: %d\r\n\r\n%s\r\n", XI_PATH, XI_SERVER, XI_HEADER, strlen(str.c_str()), str.c_str());
00229                                     printf(http);
00230 
00231                                     mdm.socketSend(socket, http, strlen(http));
00232                                     // Recv responce
00233                                     ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
00234                                     if (ret > 0)
00235                                         printf("Socket Recv \"%*s\"\r\n", ret, buf);
00236                                     mdm.socketClose(socket);
00237                                 }
00238                                 mdm.socketFree(socket);
00239                             }
00240                         }
00241                     }
00242                 } else if (!strncmp("$GPGGA", buf, 6)) {
00243                     double a = 0; 
00244                     if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
00245                         printf("GPS Altitude: %.1f\r\n", a); 
00246                 } else if (!strncmp("$GPVTG", buf, 6)) {
00247                     double s = 0; 
00248                     if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
00249                         printf("GPS Speed: %.1f\r\n", s); 
00250                 }
00251             }
00252         }
00253         if (mdmOk && (i++ == 5000/wait)) {
00254             i = 0;
00255             // check the network status
00256             if (mdm.checkNetStatus(&netStatus)) {
00257                 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
00258             }
00259                 
00260             // checking unread sms
00261             int ix[8];
00262             int n = mdm.smsList("REC UNREAD", ix, 8);
00263             if (8 < n) n = 8;
00264             while (0 < n--)
00265             {
00266                 char num[32];
00267                 printf("Unread SMS at index %d\r\n", ix[n]);
00268                 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
00269                     printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
00270                     printf("Delete SMS at index %d\r\n", ix[n]);
00271                     mdm.smsDelete(ix[n]);
00272                     // provide a reply
00273                     const char* reply = "Hello my friend";
00274                     if (strstr(buf, /*w*/"here are you"))
00275                         reply = *link ? link : "I don't know"; // reply wil location link
00276                     else if (strstr(buf, /*s*/"hutdown"))
00277                         abort = true, reply = "bye bye";
00278                     printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
00279                     mdm.smsSend(num, reply);
00280                 }
00281             }
00282         }
00283         wait_ms(wait);
00284     }
00285     gps.powerOff();
00286     mdm.powerOff();
00287     return 0;
00288 }