インターフェース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

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

Committer:
mazgch
Date:
Tue May 27 09:20:46 2014 +0000
Revision:
19:f022ff746eb8
Parent:
18:50e6c4ed8a4a
Child:
20:52f0e5de8c3d
pull latest libs (use latest API)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:4e3cb26f6019 1 #include "mbed.h"
mazgch 9:26f694bc31b4 2
mazgch 19:f022ff746eb8 3 //------------------------------------------------------------------------------------
mazgch 11:b8505cbbd55c 4 /* This example was tested on C027-U20 and C027-G35 with the on board modem.
mazgch 11:b8505cbbd55c 5
mazgch 18:50e6c4ed8a4a 6 Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON
mazgch 11:b8505cbbd55c 7 is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this
mazgch 11:b8505cbbd55c 8 configuration the following platforms were tested (it is likely that others
mazgch 11:b8505cbbd55c 9 will work as well)
mazgch 19:f022ff746eb8 10 - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
mazgch 18:50e6c4ed8a4a 11 - NXP: LPC1549v2, LPC4088qsb
mazgch 18:50e6c4ed8a4a 12 - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
mazgch 12:96c7b62c7aaf 13 - STM: NUCLEO-F401RE, NUCLEO-F030R8
mazgch 18:50e6c4ed8a4a 14 mount resistors SB13/14 1k, SB62/63 0R
mazgch 11:b8505cbbd55c 15 */
mazgch 19:f022ff746eb8 16 #include "GPS.h"
mazgch 19:f022ff746eb8 17 #include "MDM.h"
mazgch 19:f022ff746eb8 18 //------------------------------------------------------------------------------------
mazgch 19:f022ff746eb8 19 // You need to configure these cellular modem / SIM parameters.
mazgch 19:f022ff746eb8 20 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 19:f022ff746eb8 21 //------------------------------------------------------------------------------------
mazgch 19:f022ff746eb8 22 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 19:f022ff746eb8 23 #define SIMPIN NULL
mazgch 19:f022ff746eb8 24 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 19:f022ff746eb8 25 contract with the network operator. You can also try to look-up your settings in
mazgch 19:f022ff746eb8 26 google: https://www.google.de/search?q=APN+list */
mazgch 19:f022ff746eb8 27 #define APN "gprs.swisscom.ch"
mazgch 19:f022ff746eb8 28 //! Set the user name for your APN, or NULL if not needed
mazgch 19:f022ff746eb8 29 #define USERNAME NULL
mazgch 19:f022ff746eb8 30 //! Set the password for your APN, or NULL if not needed
mazgch 19:f022ff746eb8 31 #define PASSWORD NULL
mazgch 19:f022ff746eb8 32 //------------------------------------------------------------------------------------
lawliet 0:4e3cb26f6019 33
lawliet 0:4e3cb26f6019 34 int main(void)
lawliet 0:4e3cb26f6019 35 {
mazgch 2:b77151f111a9 36 int ret;
mazgch 19:f022ff746eb8 37 #ifdef LARGE_DATA
mazgch 16:43f6de7bc38b 38 char buf[2048] = "";
mazgch 17:c293780a40ac 39 #else
mazgch 17:c293780a40ac 40 char buf[512] = "";
mazgch 17:c293780a40ac 41 #endif
lawliet 0:4e3cb26f6019 42
mazgch 19:f022ff746eb8 43 // Create the GPS object
mazgch 19:f022ff746eb8 44 #if 1 // use GPSI2C class
mazgch 19:f022ff746eb8 45 GPSI2C gps;
mazgch 19:f022ff746eb8 46 #else // or GPSSerial class
mazgch 19:f022ff746eb8 47 GPSSerial gps;
mazgch 10:d2da2028a233 48 #endif
mazgch 10:d2da2028a233 49 // Create the modem object
mazgch 19:f022ff746eb8 50 MDMSerial mdm;
mazgch 19:f022ff746eb8 51 //mdm.setDebug(4); // enable this for debugging issues
mazgch 2:b77151f111a9 52 // initialize the modem
mazgch 19:f022ff746eb8 53 MDMParser::DevStatus devStatus = {};
mazgch 19:f022ff746eb8 54 MDMParser::NetStatus netStatus = {};
mazgch 10:d2da2028a233 55 bool mdmOk = mdm.init(SIMPIN, &devStatus);
mazgch 19:f022ff746eb8 56 mdm.dumpDevStatus(&devStatus);
mazgch 19:f022ff746eb8 57 if (mdmOk) {
mazgch 19:f022ff746eb8 58 // wait until we are connected
mazgch 19:f022ff746eb8 59 mdmOk = mdm.registerNet(&netStatus);
mazgch 19:f022ff746eb8 60 mdm.dumpNetStatus(&netStatus);
mazgch 19:f022ff746eb8 61 }
mazgch 10:d2da2028a233 62 if (mdmOk)
mazgch 4:90ab1ec64b0e 63 {
mazgch 19:f022ff746eb8 64 const char* filename = "File";
mazgch 19:f022ff746eb8 65 char buf[] = "Hello World";
mazgch 19:f022ff746eb8 66 printf("writeFile \"%s\"\r\n", buf);
mazgch 19:f022ff746eb8 67 if (mdm.writeFile(filename, buf, sizeof(buf)))
mazgch 19:f022ff746eb8 68 {
mazgch 19:f022ff746eb8 69 memset(buf, 0, sizeof(buf));
mazgch 19:f022ff746eb8 70 int len = mdm.readFile(filename, buf, sizeof(buf));
mazgch 19:f022ff746eb8 71 if (len)
mazgch 19:f022ff746eb8 72 printf("readFile %d \"%.*s\"\r\n", len, len, buf);
mazgch 19:f022ff746eb8 73 mdm.delFile(filename);
mazgch 19:f022ff746eb8 74 }
mazgch 2:b77151f111a9 75
mazgch 19:f022ff746eb8 76 // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
mazgch 19:f022ff746eb8 77 // http://de.wikipedia.org/wiki/USSD-Codes
mazgch 19:f022ff746eb8 78 const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
mazgch 19:f022ff746eb8 79 printf("Ussd Send Command %s\r\n", ussd);
mazgch 19:f022ff746eb8 80 ret = mdm.ussdCommand(ussd, buf);
mazgch 19:f022ff746eb8 81 if (ret > 0)
mazgch 19:f022ff746eb8 82 printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf);
mazgch 19:f022ff746eb8 83
mazgch 4:90ab1ec64b0e 84 // join the internet connection
mazgch 19:f022ff746eb8 85 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 5:5366d39d3719 86 if (ip != NOIP)
mazgch 2:b77151f111a9 87 {
mazgch 19:f022ff746eb8 88 mdm.dumpIp(ip);
mazgch 19:f022ff746eb8 89 printf("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 90 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 91 if (socket >= 0)
mazgch 2:b77151f111a9 92 {
mazgch 16:43f6de7bc38b 93 mdm.socketSetBlocking(socket, 10000);
mazgch 4:90ab1ec64b0e 94 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 95 {
mazgch 4:90ab1ec64b0e 96 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 4:90ab1ec64b0e 97 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 98
mazgch 19:f022ff746eb8 99 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 19:f022ff746eb8 100 if (ret > 0)
mazgch 19:f022ff746eb8 101 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 102 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 103 }
mazgch 4:90ab1ec64b0e 104 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 105 }
mazgch 2:b77151f111a9 106
mazgch 16:43f6de7bc38b 107 int port = 7;
mazgch 16:43f6de7bc38b 108 const char* host = "echo.u-blox.com";
mazgch 16:43f6de7bc38b 109 MDMParser::IP ip = mdm.gethostbyname(host);
mazgch 16:43f6de7bc38b 110 char data[] = "\r\nxxx Socket Hello World\r\n"
mazgch 19:f022ff746eb8 111 #ifdef LARGE_DATA
mazgch 17:c293780a40ac 112 "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 113 "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 114 "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 115 "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 116 "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 117
mazgch 17:c293780a40ac 118 "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 119 "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 120 "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 121 "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 122 "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 123
mazgch 17:c293780a40ac 124 "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 125 "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 126 "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 127 "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 128 "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 129
mazgch 17:c293780a40ac 130 "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 131 "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 132 "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 133 "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 134 "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 135 #endif
mazgch 17:c293780a40ac 136 "End\r\n";
mazgch 16:43f6de7bc38b 137
mazgch 19:f022ff746eb8 138 printf("Testing TCP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 139 socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 16:43f6de7bc38b 140 if (socket >= 0)
mazgch 16:43f6de7bc38b 141 {
mazgch 16:43f6de7bc38b 142 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 143 if (mdm.socketConnect(socket, host, port)) {
mazgch 16:43f6de7bc38b 144 memcpy(data, "\r\nTCP", 5);
mazgch 16:43f6de7bc38b 145 ret = mdm.socketSend(socket, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 146 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 147 printf("Socket Send %d \"%s\"\r\n", ret, data);
mazgch 16:43f6de7bc38b 148 }
mazgch 16:43f6de7bc38b 149 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 150 if (ret >= 0) {
mazgch 19:f022ff746eb8 151 printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
mazgch 16:43f6de7bc38b 152 }
mazgch 16:43f6de7bc38b 153 mdm.socketClose(socket);
mazgch 16:43f6de7bc38b 154 }
mazgch 16:43f6de7bc38b 155 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 156 }
mazgch 16:43f6de7bc38b 157
mazgch 19:f022ff746eb8 158 printf("Testing UDP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 159 socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
mazgch 16:43f6de7bc38b 160 if (socket >= 0)
mazgch 16:43f6de7bc38b 161 {
mazgch 16:43f6de7bc38b 162 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 163 memcpy(data, "\r\nUDP", 5);
mazgch 16:43f6de7bc38b 164 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 165 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 166 printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
mazgch 16:43f6de7bc38b 167 }
mazgch 16:43f6de7bc38b 168 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 169 if (ret >= 0) {
mazgch 19:f022ff746eb8 170 printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
mazgch 16:43f6de7bc38b 171 }
mazgch 16:43f6de7bc38b 172 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 173 }
mazgch 16:43f6de7bc38b 174
mazgch 4:90ab1ec64b0e 175 // disconnect
mazgch 4:90ab1ec64b0e 176 mdm.disconnect();
mazgch 2:b77151f111a9 177 }
mazgch 10:d2da2028a233 178 }
mazgch 19:f022ff746eb8 179 printf("SMS and GPS Loop\r\n");
mazgch 10:d2da2028a233 180 char link[128] = "";
mazgch 10:d2da2028a233 181 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 182 const int wait = 100;
mazgch 10:d2da2028a233 183 bool abort = false;
mazgch 11:b8505cbbd55c 184 //DigitalOut led(LED1);
mazgch 10:d2da2028a233 185 while (!abort) {
mazgch 19:f022ff746eb8 186 // led = !led;
mazgch 10:d2da2028a233 187 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 10:d2da2028a233 188 {
mazgch 10:d2da2028a233 189 int len = LENGTH(ret);
mazgch 19:f022ff746eb8 190 //printf("NMEA: %.*s\r\n", len-2, msg);
mazgch 19:f022ff746eb8 191 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
mazgch 4:90ab1ec64b0e 192 {
mazgch 19:f022ff746eb8 193 if (!strncmp("$GPGLL", buf, 6)) {
mazgch 19:f022ff746eb8 194 double la = 0, lo = 0;
mazgch 19:f022ff746eb8 195 char ch;
mazgch 19:f022ff746eb8 196 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 19:f022ff746eb8 197 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 19:f022ff746eb8 198 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 19:f022ff746eb8 199 {
mazgch 19:f022ff746eb8 200 printf("GPS Location: %.5f %.5f\r\n", la, lo);
mazgch 19:f022ff746eb8 201 sprintf(link, "I am here!\n"
mazgch 19:f022ff746eb8 202 "https://maps.google.com/?q=%.5f,%.5f", la, lo);
mazgch 19:f022ff746eb8 203 }
mazgch 19:f022ff746eb8 204 } else if (!strncmp("$GPGGA", buf, 6)) {
mazgch 19:f022ff746eb8 205 double a = 0;
mazgch 19:f022ff746eb8 206 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
mazgch 19:f022ff746eb8 207 printf("GPS Altitude: %.1f\r\n", a);
mazgch 19:f022ff746eb8 208 } else if (!strncmp("$GPVTG", buf, 6)) {
mazgch 19:f022ff746eb8 209 double s = 0;
mazgch 19:f022ff746eb8 210 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
mazgch 19:f022ff746eb8 211 printf("GPS Speed: %.1f\r\n", s);
mazgch 4:90ab1ec64b0e 212 }
mazgch 4:90ab1ec64b0e 213 }
mazgch 10:d2da2028a233 214 }
mazgch 19:f022ff746eb8 215 if (mdmOk && (i++ == 5000/wait)) {
mazgch 10:d2da2028a233 216 i = 0;
mazgch 10:d2da2028a233 217 // check the network status
mazgch 15:ea10b6cf8c85 218 if (mdm.checkNetStatus(&netStatus)) {
mazgch 19:f022ff746eb8 219 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
mazgch 15:ea10b6cf8c85 220 }
mazgch 10:d2da2028a233 221
mazgch 10:d2da2028a233 222 // checking unread sms
mazgch 10:d2da2028a233 223 int ix[8];
mazgch 10:d2da2028a233 224 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 225 if (8 < n) n = 8;
mazgch 10:d2da2028a233 226 while (0 < n--)
mazgch 10:d2da2028a233 227 {
mazgch 10:d2da2028a233 228 char num[32];
mazgch 19:f022ff746eb8 229 printf("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 230 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 19:f022ff746eb8 231 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 19:f022ff746eb8 232 printf("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 233 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 234 // provide a reply
mazgch 10:d2da2028a233 235 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 236 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 237 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 19:f022ff746eb8 238 else if (strstr(buf, /*s*/"hutdown"))
mazgch 19:f022ff746eb8 239 abort = true, reply = "bye bye";
mazgch 19:f022ff746eb8 240 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 241 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 242 }
mazgch 4:90ab1ec64b0e 243 }
mazgch 9:26f694bc31b4 244 }
mazgch 10:d2da2028a233 245 wait_ms(wait);
lawliet 0:4e3cb26f6019 246 }
mazgch 19:f022ff746eb8 247 gps.powerOff();
mazgch 10:d2da2028a233 248 mdm.powerOff();
lawliet 0:4e3cb26f6019 249 return 0;
lawliet 0:4e3cb26f6019 250 }