インターフェース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:
ntaka206
Date:
Mon Jul 28 03:25:18 2014 +0000
Revision:
33:65bcfee03271
Parent:
32:2ad9c1d1afd0
Modify comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:4e3cb26f6019 1 #include "mbed.h"
ntaka206 32:2ad9c1d1afd0 2 /*
ntaka206 32:2ad9c1d1afd0 3 Interface 2014/10
ntaka206 32:2ad9c1d1afd0 4 u-blolx C027 3G test sample
ntaka206 32:2ad9c1d1afd0 5 2014/07 Naoya Takamura
ntaka206 32:2ad9c1d1afd0 6 */
mazgch 19:f022ff746eb8 7 //------------------------------------------------------------------------------------
mazgch 11:b8505cbbd55c 8 /* This example was tested on C027-U20 and C027-G35 with the on board modem.
mazgch 11:b8505cbbd55c 9
mazgch 18:50e6c4ed8a4a 10 Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON
mazgch 11:b8505cbbd55c 11 is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this
mazgch 11:b8505cbbd55c 12 configuration the following platforms were tested (it is likely that others
mazgch 11:b8505cbbd55c 13 will work as well)
mazgch 19:f022ff746eb8 14 - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
mazgch 18:50e6c4ed8a4a 15 - NXP: LPC1549v2, LPC4088qsb
mazgch 18:50e6c4ed8a4a 16 - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
mazgch 12:96c7b62c7aaf 17 - STM: NUCLEO-F401RE, NUCLEO-F030R8
mazgch 18:50e6c4ed8a4a 18 mount resistors SB13/14 1k, SB62/63 0R
mazgch 11:b8505cbbd55c 19 */
mazgch 19:f022ff746eb8 20 #include "GPS.h"
mazgch 19:f022ff746eb8 21 #include "MDM.h"
ntaka206 31:22a4e87217f8 22 #include "picojson.h"
ntaka206 31:22a4e87217f8 23
mazgch 19:f022ff746eb8 24 //------------------------------------------------------------------------------------
mazgch 19:f022ff746eb8 25 // You need to configure these cellular modem / SIM parameters.
mazgch 19:f022ff746eb8 26 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 19:f022ff746eb8 27 //------------------------------------------------------------------------------------
mazgch 19:f022ff746eb8 28 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 19:f022ff746eb8 29 #define SIMPIN NULL
mazgch 19:f022ff746eb8 30 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 19:f022ff746eb8 31 contract with the network operator. You can also try to look-up your settings in
mazgch 19:f022ff746eb8 32 google: https://www.google.de/search?q=APN+list */
ntaka206 26:712544e993ff 33 //#define APN NULL
ntaka206 26:712544e993ff 34 #define APN "3g-d-2.ocn.ne.jp"
mazgch 19:f022ff746eb8 35 //! Set the user name for your APN, or NULL if not needed
ntaka206 26:712544e993ff 36 //#define USERNAME NULL
ntaka206 32:2ad9c1d1afd0 37 #define USERNAME "hoge@one.ocn.ne.jp"
mazgch 19:f022ff746eb8 38 //! Set the password for your APN, or NULL if not needed
ntaka206 26:712544e993ff 39 //#define PASSWORD NULL
ntaka206 32:2ad9c1d1afd0 40 #define PASSWORD "hogehoge"
ntaka206 26:712544e993ff 41
ntaka206 28:d504a106c124 42 //---- Xively --------------------------------------------------------------
ntaka206 32:2ad9c1d1afd0 43 #define XI_FEED_ID "SET your Feed ID" // set Xively Feed ID (numerical, no quoutes)
ntaka206 32:2ad9c1d1afd0 44 #define XI_API_KEY "SET your Feed API KEY" // set Xively API key (double-quoted string)
ntaka206 28:d504a106c124 45
ntaka206 30:e6c7827dfbe8 46 #define XI_SERVER "api.xively.com"
ntaka206 28:d504a106c124 47
ntaka206 31:22a4e87217f8 48 #define XI_PATH "/v2/feeds/" XI_FEED_ID ".json"
ntaka206 30:e6c7827dfbe8 49 #define XI_HEADER "X-ApiKey: " XI_API_KEY
ntaka206 30:e6c7827dfbe8 50
ntaka206 31:22a4e87217f8 51 #define XI_UPLOAD_INTERVAL 2 // sec
mazgch 19:f022ff746eb8 52 //------------------------------------------------------------------------------------
lawliet 0:4e3cb26f6019 53
lawliet 0:4e3cb26f6019 54 int main(void)
lawliet 0:4e3cb26f6019 55 {
mazgch 2:b77151f111a9 56 int ret;
mazgch 17:c293780a40ac 57 char buf[512] = "";
ntaka206 33:65bcfee03271 58 // Add by ntaka for USB Console
ntaka206 33:65bcfee03271 59 Serial pc(USBTX,USBRX);
ntaka206 33:65bcfee03271 60 pc.baud(115200);
lawliet 0:4e3cb26f6019 61
mazgch 19:f022ff746eb8 62 // Create the GPS object
ntaka206 33:65bcfee03271 63 // use GPSI2C class
mazgch 19:f022ff746eb8 64 GPSI2C gps;
mazgch 10:d2da2028a233 65 // Create the modem object
mazgch 19:f022ff746eb8 66 MDMSerial mdm;
mazgch 22:078f410ef2c7 67 //mdm.setDebug(4); // enable this for debugging issues
mazgch 2:b77151f111a9 68 // initialize the modem
mazgch 19:f022ff746eb8 69 MDMParser::DevStatus devStatus = {};
mazgch 19:f022ff746eb8 70 MDMParser::NetStatus netStatus = {};
mazgch 10:d2da2028a233 71 bool mdmOk = mdm.init(SIMPIN, &devStatus);
mazgch 19:f022ff746eb8 72 mdm.dumpDevStatus(&devStatus);
mazgch 19:f022ff746eb8 73 if (mdmOk) {
mazgch 20:52f0e5de8c3d 74 #if 0
mazgch 20:52f0e5de8c3d 75 // file system API
mazgch 19:f022ff746eb8 76 const char* filename = "File";
mazgch 19:f022ff746eb8 77 char buf[] = "Hello World";
mazgch 19:f022ff746eb8 78 printf("writeFile \"%s\"\r\n", buf);
mazgch 19:f022ff746eb8 79 if (mdm.writeFile(filename, buf, sizeof(buf)))
mazgch 19:f022ff746eb8 80 {
mazgch 19:f022ff746eb8 81 memset(buf, 0, sizeof(buf));
mazgch 19:f022ff746eb8 82 int len = mdm.readFile(filename, buf, sizeof(buf));
mazgch 24:81f5b43a6585 83 if (len >= 0)
mazgch 19:f022ff746eb8 84 printf("readFile %d \"%.*s\"\r\n", len, len, buf);
mazgch 19:f022ff746eb8 85 mdm.delFile(filename);
mazgch 19:f022ff746eb8 86 }
mazgch 20:52f0e5de8c3d 87 #endif
mazgch 20:52f0e5de8c3d 88
mazgch 20:52f0e5de8c3d 89 // wait until we are connected
mazgch 20:52f0e5de8c3d 90 mdmOk = mdm.registerNet(&netStatus);
mazgch 20:52f0e5de8c3d 91 mdm.dumpNetStatus(&netStatus);
mazgch 20:52f0e5de8c3d 92 }
mazgch 20:52f0e5de8c3d 93 if (mdmOk)
mazgch 20:52f0e5de8c3d 94 {
mazgch 19:f022ff746eb8 95 // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
mazgch 19:f022ff746eb8 96 // http://de.wikipedia.org/wiki/USSD-Codes
mazgch 19:f022ff746eb8 97 const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
mazgch 19:f022ff746eb8 98 printf("Ussd Send Command %s\r\n", ussd);
mazgch 19:f022ff746eb8 99 ret = mdm.ussdCommand(ussd, buf);
mazgch 19:f022ff746eb8 100 if (ret > 0)
mazgch 19:f022ff746eb8 101 printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf);
mazgch 19:f022ff746eb8 102
mazgch 4:90ab1ec64b0e 103 // join the internet connection
mazgch 19:f022ff746eb8 104 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 5:5366d39d3719 105 if (ip != NOIP)
mazgch 2:b77151f111a9 106 {
mazgch 19:f022ff746eb8 107 mdm.dumpIp(ip);
mazgch 19:f022ff746eb8 108 printf("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 109 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 110 if (socket >= 0)
mazgch 2:b77151f111a9 111 {
mazgch 16:43f6de7bc38b 112 mdm.socketSetBlocking(socket, 10000);
mazgch 4:90ab1ec64b0e 113 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 114 {
mazgch 4:90ab1ec64b0e 115 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 4:90ab1ec64b0e 116 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 117
mazgch 19:f022ff746eb8 118 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 19:f022ff746eb8 119 if (ret > 0)
mazgch 19:f022ff746eb8 120 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 121 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 122 }
mazgch 4:90ab1ec64b0e 123 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 124 }
ntaka206 30:e6c7827dfbe8 125 #if 0
mazgch 2:b77151f111a9 126
mazgch 16:43f6de7bc38b 127 int port = 7;
mazgch 16:43f6de7bc38b 128 const char* host = "echo.u-blox.com";
mazgch 16:43f6de7bc38b 129 MDMParser::IP ip = mdm.gethostbyname(host);
mazgch 16:43f6de7bc38b 130 char data[] = "\r\nxxx Socket Hello World\r\n"
mazgch 17:c293780a40ac 131 "End\r\n";
mazgch 16:43f6de7bc38b 132
mazgch 19:f022ff746eb8 133 printf("Testing TCP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 134 socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 16:43f6de7bc38b 135 if (socket >= 0)
mazgch 16:43f6de7bc38b 136 {
mazgch 16:43f6de7bc38b 137 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 138 if (mdm.socketConnect(socket, host, port)) {
mazgch 16:43f6de7bc38b 139 memcpy(data, "\r\nTCP", 5);
mazgch 16:43f6de7bc38b 140 ret = mdm.socketSend(socket, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 141 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 142 printf("Socket Send %d \"%s\"\r\n", ret, data);
mazgch 16:43f6de7bc38b 143 }
mazgch 16:43f6de7bc38b 144 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 145 if (ret >= 0) {
mazgch 19:f022ff746eb8 146 printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
mazgch 16:43f6de7bc38b 147 }
mazgch 16:43f6de7bc38b 148 mdm.socketClose(socket);
mazgch 16:43f6de7bc38b 149 }
mazgch 16:43f6de7bc38b 150 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 151 }
mazgch 16:43f6de7bc38b 152
mazgch 19:f022ff746eb8 153 printf("Testing UDP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 154 socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
mazgch 16:43f6de7bc38b 155 if (socket >= 0)
mazgch 16:43f6de7bc38b 156 {
mazgch 16:43f6de7bc38b 157 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 158 memcpy(data, "\r\nUDP", 5);
mazgch 16:43f6de7bc38b 159 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 160 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 161 printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
mazgch 16:43f6de7bc38b 162 }
mazgch 16:43f6de7bc38b 163 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 164 if (ret >= 0) {
mazgch 19:f022ff746eb8 165 printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
mazgch 16:43f6de7bc38b 166 }
mazgch 16:43f6de7bc38b 167 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 168 }
ntaka206 30:e6c7827dfbe8 169 #endif
ntaka206 30:e6c7827dfbe8 170
ntaka206 30:e6c7827dfbe8 171 // no disconnect
mazgch 4:90ab1ec64b0e 172 // disconnect
ntaka206 30:e6c7827dfbe8 173 // mdm.disconnect();
mazgch 2:b77151f111a9 174 }
mazgch 10:d2da2028a233 175 }
ntaka206 29:1f18e74ff972 176
ntaka206 33:65bcfee03271 177 // Upload GPS data to Xively
mazgch 19:f022ff746eb8 178 printf("SMS and GPS Loop\r\n");
mazgch 10:d2da2028a233 179 char link[128] = "";
mazgch 10:d2da2028a233 180 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 181 const int wait = 100;
mazgch 10:d2da2028a233 182 bool abort = false;
ntaka206 30:e6c7827dfbe8 183 int xv_upload_cnt = XI_UPLOAD_INTERVAL;
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);
ntaka206 33:65bcfee03271 203 // Upload to Xively
ntaka206 31:22a4e87217f8 204 if (xv_upload_cnt++ >= XI_UPLOAD_INTERVAL) {
ntaka206 31:22a4e87217f8 205 xv_upload_cnt = 0;
ntaka206 31:22a4e87217f8 206 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
ntaka206 31:22a4e87217f8 207 if (socket >= 0)
ntaka206 31:22a4e87217f8 208 {
ntaka206 31:22a4e87217f8 209 mdm.socketSetBlocking(socket, 10000);
ntaka206 31:22a4e87217f8 210 if (mdm.socketConnect(socket, XI_SERVER, 80))
ntaka206 31:22a4e87217f8 211 {
ntaka206 31:22a4e87217f8 212 // char data[128];
ntaka206 31:22a4e87217f8 213 // sprintf(data, "lat,%f\r\nlon,%f\r\n", la, lo);
ntaka206 33:65bcfee03271 214 // Make JSON location
ntaka206 31:22a4e87217f8 215 picojson::object v;
ntaka206 31:22a4e87217f8 216 picojson::object loc;
ntaka206 31:22a4e87217f8 217 string mb="mobile";
ntaka206 31:22a4e87217f8 218
ntaka206 31:22a4e87217f8 219 loc["disposition"] = picojson::value(mb);
ntaka206 31:22a4e87217f8 220 loc["lat"] = picojson::value(la);
ntaka206 31:22a4e87217f8 221 loc["lon"] = picojson::value(lo);
ntaka206 31:22a4e87217f8 222 v["location"] = picojson::value(loc);
ntaka206 33:65bcfee03271 223 // JSON serialize
ntaka206 31:22a4e87217f8 224 string str = picojson::value(v).serialize();
ntaka206 31:22a4e87217f8 225 // printf("serialized content = %s\r\n" , str.c_str());
ntaka206 33:65bcfee03271 226 // PUT HTTP data to Xively
ntaka206 31:22a4e87217f8 227 char http[512];
ntaka206 31:22a4e87217f8 228 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());
ntaka206 31:22a4e87217f8 229 printf(http);
ntaka206 29:1f18e74ff972 230
ntaka206 31:22a4e87217f8 231 mdm.socketSend(socket, http, strlen(http));
ntaka206 33:65bcfee03271 232 // Recv responce
ntaka206 31:22a4e87217f8 233 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
ntaka206 31:22a4e87217f8 234 if (ret > 0)
ntaka206 31:22a4e87217f8 235 printf("Socket Recv \"%*s\"\r\n", ret, buf);
ntaka206 31:22a4e87217f8 236 mdm.socketClose(socket);
ntaka206 31:22a4e87217f8 237 }
ntaka206 31:22a4e87217f8 238 mdm.socketFree(socket);
ntaka206 31:22a4e87217f8 239 }
ntaka206 31:22a4e87217f8 240 }
mazgch 19:f022ff746eb8 241 }
mazgch 19:f022ff746eb8 242 } else if (!strncmp("$GPGGA", buf, 6)) {
mazgch 19:f022ff746eb8 243 double a = 0;
mazgch 19:f022ff746eb8 244 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
mazgch 19:f022ff746eb8 245 printf("GPS Altitude: %.1f\r\n", a);
mazgch 19:f022ff746eb8 246 } else if (!strncmp("$GPVTG", buf, 6)) {
mazgch 19:f022ff746eb8 247 double s = 0;
mazgch 19:f022ff746eb8 248 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
mazgch 19:f022ff746eb8 249 printf("GPS Speed: %.1f\r\n", s);
mazgch 4:90ab1ec64b0e 250 }
mazgch 4:90ab1ec64b0e 251 }
mazgch 10:d2da2028a233 252 }
mazgch 19:f022ff746eb8 253 if (mdmOk && (i++ == 5000/wait)) {
mazgch 10:d2da2028a233 254 i = 0;
mazgch 10:d2da2028a233 255 // check the network status
mazgch 15:ea10b6cf8c85 256 if (mdm.checkNetStatus(&netStatus)) {
mazgch 19:f022ff746eb8 257 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
mazgch 15:ea10b6cf8c85 258 }
mazgch 10:d2da2028a233 259
mazgch 10:d2da2028a233 260 // checking unread sms
mazgch 10:d2da2028a233 261 int ix[8];
mazgch 10:d2da2028a233 262 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 263 if (8 < n) n = 8;
mazgch 10:d2da2028a233 264 while (0 < n--)
mazgch 10:d2da2028a233 265 {
mazgch 10:d2da2028a233 266 char num[32];
mazgch 19:f022ff746eb8 267 printf("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 268 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 19:f022ff746eb8 269 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 19:f022ff746eb8 270 printf("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 271 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 272 // provide a reply
mazgch 10:d2da2028a233 273 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 274 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 275 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 19:f022ff746eb8 276 else if (strstr(buf, /*s*/"hutdown"))
mazgch 19:f022ff746eb8 277 abort = true, reply = "bye bye";
mazgch 19:f022ff746eb8 278 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 279 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 280 }
mazgch 4:90ab1ec64b0e 281 }
mazgch 9:26f694bc31b4 282 }
mazgch 10:d2da2028a233 283 wait_ms(wait);
lawliet 0:4e3cb26f6019 284 }
mazgch 19:f022ff746eb8 285 gps.powerOff();
mazgch 10:d2da2028a233 286 mdm.powerOff();
lawliet 0:4e3cb26f6019 287 return 0;
lawliet 0:4e3cb26f6019 288 }