Example showing how to use the ublox Cellular GPS/GNSS module. The program will use GPS to get location and time, setup TCP connections and finally send and receive SMS messages

Dependencies:   C027_Support mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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