インターフェース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
インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラムです。
main.cpp@32:2ad9c1d1afd0, 2014-07-24 (annotated)
- Committer:
- ntaka206
- Date:
- Thu Jul 24 11:08:18 2014 +0000
- Revision:
- 32:2ad9c1d1afd0
- Parent:
- 31:22a4e87217f8
- Child:
- 33:65bcfee03271
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 30:e6c7827dfbe8 | 47 | //#define XI_PATH "POST /v2/feeds/" XI_FEED_ID ".csv?_method=put " |
ntaka206 | 30:e6c7827dfbe8 | 48 | //#define XI_HEADER "X-ApiKey: " XI_API_KEY "\r\nContent-Type: text/csv\r\n" |
ntaka206 | 28:d504a106c124 | 49 | |
ntaka206 | 31:22a4e87217f8 | 50 | #define XI_PATH "/v2/feeds/" XI_FEED_ID ".json" |
ntaka206 | 30:e6c7827dfbe8 | 51 | #define XI_HEADER "X-ApiKey: " XI_API_KEY |
ntaka206 | 30:e6c7827dfbe8 | 52 | |
ntaka206 | 31:22a4e87217f8 | 53 | #define XI_UPLOAD_INTERVAL 2 // sec |
mazgch | 19:f022ff746eb8 | 54 | //------------------------------------------------------------------------------------ |
lawliet | 0:4e3cb26f6019 | 55 | |
lawliet | 0:4e3cb26f6019 | 56 | int main(void) |
lawliet | 0:4e3cb26f6019 | 57 | { |
mazgch | 2:b77151f111a9 | 58 | int ret; |
mazgch | 19:f022ff746eb8 | 59 | #ifdef LARGE_DATA |
mazgch | 16:43f6de7bc38b | 60 | char buf[2048] = ""; |
mazgch | 17:c293780a40ac | 61 | #else |
mazgch | 17:c293780a40ac | 62 | char buf[512] = ""; |
mazgch | 17:c293780a40ac | 63 | #endif |
ntaka206 | 26:712544e993ff | 64 | // ntaka |
ntaka206 | 26:712544e993ff | 65 | Serial pc(USBTX,USBRX); |
ntaka206 | 26:712544e993ff | 66 | pc.baud(115200); |
lawliet | 0:4e3cb26f6019 | 67 | |
mazgch | 19:f022ff746eb8 | 68 | // Create the GPS object |
mazgch | 19:f022ff746eb8 | 69 | #if 1 // use GPSI2C class |
mazgch | 19:f022ff746eb8 | 70 | GPSI2C gps; |
mazgch | 19:f022ff746eb8 | 71 | #else // or GPSSerial class |
mazgch | 19:f022ff746eb8 | 72 | GPSSerial gps; |
mazgch | 10:d2da2028a233 | 73 | #endif |
mazgch | 10:d2da2028a233 | 74 | // Create the modem object |
mazgch | 19:f022ff746eb8 | 75 | MDMSerial mdm; |
mazgch | 22:078f410ef2c7 | 76 | //mdm.setDebug(4); // enable this for debugging issues |
mazgch | 2:b77151f111a9 | 77 | // initialize the modem |
mazgch | 19:f022ff746eb8 | 78 | MDMParser::DevStatus devStatus = {}; |
mazgch | 19:f022ff746eb8 | 79 | MDMParser::NetStatus netStatus = {}; |
mazgch | 10:d2da2028a233 | 80 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
mazgch | 19:f022ff746eb8 | 81 | mdm.dumpDevStatus(&devStatus); |
mazgch | 19:f022ff746eb8 | 82 | if (mdmOk) { |
mazgch | 20:52f0e5de8c3d | 83 | #if 0 |
mazgch | 20:52f0e5de8c3d | 84 | // file system API |
mazgch | 19:f022ff746eb8 | 85 | const char* filename = "File"; |
mazgch | 19:f022ff746eb8 | 86 | char buf[] = "Hello World"; |
mazgch | 19:f022ff746eb8 | 87 | printf("writeFile \"%s\"\r\n", buf); |
mazgch | 19:f022ff746eb8 | 88 | if (mdm.writeFile(filename, buf, sizeof(buf))) |
mazgch | 19:f022ff746eb8 | 89 | { |
mazgch | 19:f022ff746eb8 | 90 | memset(buf, 0, sizeof(buf)); |
mazgch | 19:f022ff746eb8 | 91 | int len = mdm.readFile(filename, buf, sizeof(buf)); |
mazgch | 24:81f5b43a6585 | 92 | if (len >= 0) |
mazgch | 19:f022ff746eb8 | 93 | printf("readFile %d \"%.*s\"\r\n", len, len, buf); |
mazgch | 19:f022ff746eb8 | 94 | mdm.delFile(filename); |
mazgch | 19:f022ff746eb8 | 95 | } |
mazgch | 20:52f0e5de8c3d | 96 | #endif |
mazgch | 20:52f0e5de8c3d | 97 | |
mazgch | 20:52f0e5de8c3d | 98 | // wait until we are connected |
mazgch | 20:52f0e5de8c3d | 99 | mdmOk = mdm.registerNet(&netStatus); |
mazgch | 20:52f0e5de8c3d | 100 | mdm.dumpNetStatus(&netStatus); |
mazgch | 20:52f0e5de8c3d | 101 | } |
ntaka206 | 29:1f18e74ff972 | 102 | #if 1 |
ntaka206 | 28:d504a106c124 | 103 | |
mazgch | 20:52f0e5de8c3d | 104 | if (mdmOk) |
mazgch | 20:52f0e5de8c3d | 105 | { |
mazgch | 19:f022ff746eb8 | 106 | // http://www.geckobeach.com/cellular/secrets/gsmcodes.php |
mazgch | 19:f022ff746eb8 | 107 | // http://de.wikipedia.org/wiki/USSD-Codes |
mazgch | 19:f022ff746eb8 | 108 | const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" |
mazgch | 19:f022ff746eb8 | 109 | printf("Ussd Send Command %s\r\n", ussd); |
mazgch | 19:f022ff746eb8 | 110 | ret = mdm.ussdCommand(ussd, buf); |
mazgch | 19:f022ff746eb8 | 111 | if (ret > 0) |
mazgch | 19:f022ff746eb8 | 112 | printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf); |
mazgch | 19:f022ff746eb8 | 113 | |
mazgch | 4:90ab1ec64b0e | 114 | // join the internet connection |
mazgch | 19:f022ff746eb8 | 115 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
mazgch | 5:5366d39d3719 | 116 | if (ip != NOIP) |
mazgch | 2:b77151f111a9 | 117 | { |
mazgch | 19:f022ff746eb8 | 118 | mdm.dumpIp(ip); |
mazgch | 19:f022ff746eb8 | 119 | printf("Make a Http Post Request\r\n"); |
mazgch | 4:90ab1ec64b0e | 120 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
mazgch | 4:90ab1ec64b0e | 121 | if (socket >= 0) |
mazgch | 2:b77151f111a9 | 122 | { |
mazgch | 16:43f6de7bc38b | 123 | mdm.socketSetBlocking(socket, 10000); |
mazgch | 4:90ab1ec64b0e | 124 | if (mdm.socketConnect(socket, "mbed.org", 80)) |
mazgch | 4:90ab1ec64b0e | 125 | { |
mazgch | 4:90ab1ec64b0e | 126 | const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; |
mazgch | 4:90ab1ec64b0e | 127 | mdm.socketSend(socket, http, sizeof(http)-1); |
mazgch | 4:90ab1ec64b0e | 128 | |
mazgch | 19:f022ff746eb8 | 129 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
mazgch | 19:f022ff746eb8 | 130 | if (ret > 0) |
mazgch | 19:f022ff746eb8 | 131 | printf("Socket Recv \"%*s\"\r\n", ret, buf); |
mazgch | 4:90ab1ec64b0e | 132 | mdm.socketClose(socket); |
mazgch | 4:90ab1ec64b0e | 133 | } |
mazgch | 4:90ab1ec64b0e | 134 | mdm.socketFree(socket); |
mazgch | 4:90ab1ec64b0e | 135 | } |
ntaka206 | 30:e6c7827dfbe8 | 136 | #if 0 |
mazgch | 2:b77151f111a9 | 137 | |
mazgch | 16:43f6de7bc38b | 138 | int port = 7; |
mazgch | 16:43f6de7bc38b | 139 | const char* host = "echo.u-blox.com"; |
mazgch | 16:43f6de7bc38b | 140 | MDMParser::IP ip = mdm.gethostbyname(host); |
mazgch | 16:43f6de7bc38b | 141 | char data[] = "\r\nxxx Socket Hello World\r\n" |
mazgch | 19:f022ff746eb8 | 142 | #ifdef LARGE_DATA |
mazgch | 17:c293780a40ac | 143 | "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 144 | "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 145 | "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 146 | "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 147 | "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 148 | |
mazgch | 17:c293780a40ac | 149 | "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 150 | "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 151 | "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 152 | "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 153 | "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 16:43f6de7bc38b | 154 | |
mazgch | 17:c293780a40ac | 155 | "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 156 | "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 157 | "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 158 | "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 159 | "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 160 | |
mazgch | 17:c293780a40ac | 161 | "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 162 | "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 163 | "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 164 | "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 17:c293780a40ac | 165 | "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" |
mazgch | 16:43f6de7bc38b | 166 | #endif |
mazgch | 17:c293780a40ac | 167 | "End\r\n"; |
mazgch | 16:43f6de7bc38b | 168 | |
mazgch | 19:f022ff746eb8 | 169 | printf("Testing TCP sockets with ECHO server\r\n"); |
mazgch | 16:43f6de7bc38b | 170 | socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
mazgch | 16:43f6de7bc38b | 171 | if (socket >= 0) |
mazgch | 16:43f6de7bc38b | 172 | { |
mazgch | 16:43f6de7bc38b | 173 | mdm.socketSetBlocking(socket, 10000); |
mazgch | 16:43f6de7bc38b | 174 | if (mdm.socketConnect(socket, host, port)) { |
mazgch | 16:43f6de7bc38b | 175 | memcpy(data, "\r\nTCP", 5); |
mazgch | 16:43f6de7bc38b | 176 | ret = mdm.socketSend(socket, data, sizeof(data)-1); |
mazgch | 16:43f6de7bc38b | 177 | if (ret == sizeof(data)-1) { |
mazgch | 19:f022ff746eb8 | 178 | printf("Socket Send %d \"%s\"\r\n", ret, data); |
mazgch | 16:43f6de7bc38b | 179 | } |
mazgch | 16:43f6de7bc38b | 180 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
mazgch | 16:43f6de7bc38b | 181 | if (ret >= 0) { |
mazgch | 19:f022ff746eb8 | 182 | printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf); |
mazgch | 16:43f6de7bc38b | 183 | } |
mazgch | 16:43f6de7bc38b | 184 | mdm.socketClose(socket); |
mazgch | 16:43f6de7bc38b | 185 | } |
mazgch | 16:43f6de7bc38b | 186 | mdm.socketFree(socket); |
mazgch | 16:43f6de7bc38b | 187 | } |
mazgch | 16:43f6de7bc38b | 188 | |
mazgch | 19:f022ff746eb8 | 189 | printf("Testing UDP sockets with ECHO server\r\n"); |
mazgch | 16:43f6de7bc38b | 190 | socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port); |
mazgch | 16:43f6de7bc38b | 191 | if (socket >= 0) |
mazgch | 16:43f6de7bc38b | 192 | { |
mazgch | 16:43f6de7bc38b | 193 | mdm.socketSetBlocking(socket, 10000); |
mazgch | 16:43f6de7bc38b | 194 | memcpy(data, "\r\nUDP", 5); |
mazgch | 16:43f6de7bc38b | 195 | ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1); |
mazgch | 16:43f6de7bc38b | 196 | if (ret == sizeof(data)-1) { |
mazgch | 19:f022ff746eb8 | 197 | printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data); |
mazgch | 16:43f6de7bc38b | 198 | } |
mazgch | 16:43f6de7bc38b | 199 | ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1); |
mazgch | 16:43f6de7bc38b | 200 | if (ret >= 0) { |
mazgch | 19:f022ff746eb8 | 201 | printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf); |
mazgch | 16:43f6de7bc38b | 202 | } |
mazgch | 16:43f6de7bc38b | 203 | mdm.socketFree(socket); |
mazgch | 16:43f6de7bc38b | 204 | } |
ntaka206 | 30:e6c7827dfbe8 | 205 | #endif |
ntaka206 | 30:e6c7827dfbe8 | 206 | |
ntaka206 | 30:e6c7827dfbe8 | 207 | // no disconnect |
mazgch | 4:90ab1ec64b0e | 208 | // disconnect |
ntaka206 | 30:e6c7827dfbe8 | 209 | // mdm.disconnect(); |
mazgch | 2:b77151f111a9 | 210 | } |
mazgch | 10:d2da2028a233 | 211 | } |
ntaka206 | 29:1f18e74ff972 | 212 | #endif |
ntaka206 | 29:1f18e74ff972 | 213 | |
ntaka206 | 29:1f18e74ff972 | 214 | // Xively |
ntaka206 | 29:1f18e74ff972 | 215 | |
mazgch | 19:f022ff746eb8 | 216 | printf("SMS and GPS Loop\r\n"); |
mazgch | 10:d2da2028a233 | 217 | char link[128] = ""; |
mazgch | 10:d2da2028a233 | 218 | unsigned int i = 0xFFFFFFFF; |
mazgch | 10:d2da2028a233 | 219 | const int wait = 100; |
mazgch | 10:d2da2028a233 | 220 | bool abort = false; |
ntaka206 | 30:e6c7827dfbe8 | 221 | int xv_upload_cnt = XI_UPLOAD_INTERVAL; |
mazgch | 11:b8505cbbd55c | 222 | //DigitalOut led(LED1); |
mazgch | 10:d2da2028a233 | 223 | while (!abort) { |
mazgch | 19:f022ff746eb8 | 224 | // led = !led; |
mazgch | 10:d2da2028a233 | 225 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) |
mazgch | 10:d2da2028a233 | 226 | { |
mazgch | 10:d2da2028a233 | 227 | int len = LENGTH(ret); |
mazgch | 19:f022ff746eb8 | 228 | //printf("NMEA: %.*s\r\n", len-2, msg); |
mazgch | 19:f022ff746eb8 | 229 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) |
mazgch | 4:90ab1ec64b0e | 230 | { |
mazgch | 19:f022ff746eb8 | 231 | if (!strncmp("$GPGLL", buf, 6)) { |
mazgch | 19:f022ff746eb8 | 232 | double la = 0, lo = 0; |
mazgch | 19:f022ff746eb8 | 233 | char ch; |
mazgch | 19:f022ff746eb8 | 234 | if (gps.getNmeaAngle(1,buf,len,la) && |
mazgch | 19:f022ff746eb8 | 235 | gps.getNmeaAngle(3,buf,len,lo) && |
mazgch | 19:f022ff746eb8 | 236 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') |
mazgch | 19:f022ff746eb8 | 237 | { |
mazgch | 19:f022ff746eb8 | 238 | printf("GPS Location: %.5f %.5f\r\n", la, lo); |
mazgch | 19:f022ff746eb8 | 239 | sprintf(link, "I am here!\n" |
mazgch | 19:f022ff746eb8 | 240 | "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
ntaka206 | 30:e6c7827dfbe8 | 241 | // Xively |
ntaka206 | 31:22a4e87217f8 | 242 | if (xv_upload_cnt++ >= XI_UPLOAD_INTERVAL) { |
ntaka206 | 31:22a4e87217f8 | 243 | xv_upload_cnt = 0; |
ntaka206 | 31:22a4e87217f8 | 244 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
ntaka206 | 31:22a4e87217f8 | 245 | if (socket >= 0) |
ntaka206 | 31:22a4e87217f8 | 246 | { |
ntaka206 | 31:22a4e87217f8 | 247 | mdm.socketSetBlocking(socket, 10000); |
ntaka206 | 31:22a4e87217f8 | 248 | if (mdm.socketConnect(socket, XI_SERVER, 80)) |
ntaka206 | 31:22a4e87217f8 | 249 | { |
ntaka206 | 31:22a4e87217f8 | 250 | // char data[128]; |
ntaka206 | 31:22a4e87217f8 | 251 | // sprintf(data, "lat,%f\r\nlon,%f\r\n", la, lo); |
ntaka206 | 31:22a4e87217f8 | 252 | // JSON location |
ntaka206 | 31:22a4e87217f8 | 253 | picojson::object v; |
ntaka206 | 31:22a4e87217f8 | 254 | picojson::object loc; |
ntaka206 | 31:22a4e87217f8 | 255 | string mb="mobile"; |
ntaka206 | 31:22a4e87217f8 | 256 | |
ntaka206 | 31:22a4e87217f8 | 257 | loc["disposition"] = picojson::value(mb); |
ntaka206 | 31:22a4e87217f8 | 258 | loc["lat"] = picojson::value(la); |
ntaka206 | 31:22a4e87217f8 | 259 | loc["lon"] = picojson::value(lo); |
ntaka206 | 31:22a4e87217f8 | 260 | v["location"] = picojson::value(loc); |
ntaka206 | 31:22a4e87217f8 | 261 | |
ntaka206 | 31:22a4e87217f8 | 262 | string str = picojson::value(v).serialize(); |
ntaka206 | 31:22a4e87217f8 | 263 | // printf("serialized content = %s\r\n" , str.c_str()); |
ntaka206 | 31:22a4e87217f8 | 264 | |
ntaka206 | 31:22a4e87217f8 | 265 | char http[512]; |
ntaka206 | 31:22a4e87217f8 | 266 | 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 | 267 | printf(http); |
ntaka206 | 29:1f18e74ff972 | 268 | |
ntaka206 | 31:22a4e87217f8 | 269 | mdm.socketSend(socket, http, strlen(http)); |
ntaka206 | 31:22a4e87217f8 | 270 | |
ntaka206 | 31:22a4e87217f8 | 271 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
ntaka206 | 31:22a4e87217f8 | 272 | if (ret > 0) |
ntaka206 | 31:22a4e87217f8 | 273 | printf("Socket Recv \"%*s\"\r\n", ret, buf); |
ntaka206 | 31:22a4e87217f8 | 274 | mdm.socketClose(socket); |
ntaka206 | 31:22a4e87217f8 | 275 | } |
ntaka206 | 31:22a4e87217f8 | 276 | mdm.socketFree(socket); |
ntaka206 | 31:22a4e87217f8 | 277 | } |
ntaka206 | 31:22a4e87217f8 | 278 | } |
mazgch | 19:f022ff746eb8 | 279 | } |
mazgch | 19:f022ff746eb8 | 280 | } else if (!strncmp("$GPGGA", buf, 6)) { |
mazgch | 19:f022ff746eb8 | 281 | double a = 0; |
mazgch | 19:f022ff746eb8 | 282 | if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] |
mazgch | 19:f022ff746eb8 | 283 | printf("GPS Altitude: %.1f\r\n", a); |
mazgch | 19:f022ff746eb8 | 284 | } else if (!strncmp("$GPVTG", buf, 6)) { |
mazgch | 19:f022ff746eb8 | 285 | double s = 0; |
mazgch | 19:f022ff746eb8 | 286 | if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] |
mazgch | 19:f022ff746eb8 | 287 | printf("GPS Speed: %.1f\r\n", s); |
mazgch | 4:90ab1ec64b0e | 288 | } |
mazgch | 4:90ab1ec64b0e | 289 | } |
mazgch | 10:d2da2028a233 | 290 | } |
mazgch | 19:f022ff746eb8 | 291 | if (mdmOk && (i++ == 5000/wait)) { |
mazgch | 10:d2da2028a233 | 292 | i = 0; |
mazgch | 10:d2da2028a233 | 293 | // check the network status |
mazgch | 15:ea10b6cf8c85 | 294 | if (mdm.checkNetStatus(&netStatus)) { |
mazgch | 19:f022ff746eb8 | 295 | mdm.dumpNetStatus(&netStatus, fprintf, stdout); |
mazgch | 15:ea10b6cf8c85 | 296 | } |
mazgch | 10:d2da2028a233 | 297 | |
mazgch | 10:d2da2028a233 | 298 | // checking unread sms |
mazgch | 10:d2da2028a233 | 299 | int ix[8]; |
mazgch | 10:d2da2028a233 | 300 | int n = mdm.smsList("REC UNREAD", ix, 8); |
mazgch | 10:d2da2028a233 | 301 | if (8 < n) n = 8; |
mazgch | 10:d2da2028a233 | 302 | while (0 < n--) |
mazgch | 10:d2da2028a233 | 303 | { |
mazgch | 10:d2da2028a233 | 304 | char num[32]; |
mazgch | 19:f022ff746eb8 | 305 | printf("Unread SMS at index %d\r\n", ix[n]); |
mazgch | 10:d2da2028a233 | 306 | if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) { |
mazgch | 19:f022ff746eb8 | 307 | printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); |
mazgch | 19:f022ff746eb8 | 308 | printf("Delete SMS at index %d\r\n", ix[n]); |
mazgch | 10:d2da2028a233 | 309 | mdm.smsDelete(ix[n]); |
mazgch | 10:d2da2028a233 | 310 | // provide a reply |
mazgch | 10:d2da2028a233 | 311 | const char* reply = "Hello my friend"; |
mazgch | 10:d2da2028a233 | 312 | if (strstr(buf, /*w*/"here are you")) |
mazgch | 10:d2da2028a233 | 313 | reply = *link ? link : "I don't know"; // reply wil location link |
mazgch | 19:f022ff746eb8 | 314 | else if (strstr(buf, /*s*/"hutdown")) |
mazgch | 19:f022ff746eb8 | 315 | abort = true, reply = "bye bye"; |
mazgch | 19:f022ff746eb8 | 316 | printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); |
mazgch | 10:d2da2028a233 | 317 | mdm.smsSend(num, reply); |
mazgch | 4:90ab1ec64b0e | 318 | } |
mazgch | 4:90ab1ec64b0e | 319 | } |
mazgch | 9:26f694bc31b4 | 320 | } |
mazgch | 10:d2da2028a233 | 321 | wait_ms(wait); |
lawliet | 0:4e3cb26f6019 | 322 | } |
mazgch | 19:f022ff746eb8 | 323 | gps.powerOff(); |
mazgch | 10:d2da2028a233 | 324 | mdm.powerOff(); |
lawliet | 0:4e3cb26f6019 | 325 | return 0; |
lawliet | 0:4e3cb26f6019 | 326 | } |