mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Committer:
mazgch
Date:
Fri Dec 12 07:50:45 2014 +0000
Revision:
27:fe3b383dd4ec
Parent:
24:81f5b43a6585
Child:
28:334263983fcd
update for multi GNSS parsing

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 21:a090a5043e23 27 #define APN NULL
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 27:fe3b383dd4ec 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 20:52f0e5de8c3d 58 #if 0
mazgch 20:52f0e5de8c3d 59 // file system API
mazgch 19:f022ff746eb8 60 const char* filename = "File";
mazgch 19:f022ff746eb8 61 char buf[] = "Hello World";
mazgch 19:f022ff746eb8 62 printf("writeFile \"%s\"\r\n", buf);
mazgch 19:f022ff746eb8 63 if (mdm.writeFile(filename, buf, sizeof(buf)))
mazgch 19:f022ff746eb8 64 {
mazgch 19:f022ff746eb8 65 memset(buf, 0, sizeof(buf));
mazgch 19:f022ff746eb8 66 int len = mdm.readFile(filename, buf, sizeof(buf));
mazgch 24:81f5b43a6585 67 if (len >= 0)
mazgch 19:f022ff746eb8 68 printf("readFile %d \"%.*s\"\r\n", len, len, buf);
mazgch 19:f022ff746eb8 69 mdm.delFile(filename);
mazgch 19:f022ff746eb8 70 }
mazgch 20:52f0e5de8c3d 71 #endif
mazgch 20:52f0e5de8c3d 72
mazgch 20:52f0e5de8c3d 73 // wait until we are connected
mazgch 20:52f0e5de8c3d 74 mdmOk = mdm.registerNet(&netStatus);
mazgch 20:52f0e5de8c3d 75 mdm.dumpNetStatus(&netStatus);
mazgch 20:52f0e5de8c3d 76 }
mazgch 20:52f0e5de8c3d 77 if (mdmOk)
mazgch 20:52f0e5de8c3d 78 {
mazgch 19:f022ff746eb8 79 // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
mazgch 19:f022ff746eb8 80 // http://de.wikipedia.org/wiki/USSD-Codes
mazgch 19:f022ff746eb8 81 const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
mazgch 19:f022ff746eb8 82 printf("Ussd Send Command %s\r\n", ussd);
mazgch 19:f022ff746eb8 83 ret = mdm.ussdCommand(ussd, buf);
mazgch 19:f022ff746eb8 84 if (ret > 0)
mazgch 19:f022ff746eb8 85 printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf);
mazgch 19:f022ff746eb8 86
mazgch 4:90ab1ec64b0e 87 // join the internet connection
mazgch 19:f022ff746eb8 88 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 27:fe3b383dd4ec 89 if (ip == NOIP)
mazgch 27:fe3b383dd4ec 90 printf("Not joined");
mazgch 27:fe3b383dd4ec 91 else
mazgch 2:b77151f111a9 92 {
mazgch 19:f022ff746eb8 93 mdm.dumpIp(ip);
mazgch 19:f022ff746eb8 94 printf("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 95 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 96 if (socket >= 0)
mazgch 2:b77151f111a9 97 {
mazgch 16:43f6de7bc38b 98 mdm.socketSetBlocking(socket, 10000);
mazgch 4:90ab1ec64b0e 99 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 100 {
mazgch 4:90ab1ec64b0e 101 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 4:90ab1ec64b0e 102 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 103
mazgch 19:f022ff746eb8 104 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 19:f022ff746eb8 105 if (ret > 0)
mazgch 19:f022ff746eb8 106 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 107 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 108 }
mazgch 4:90ab1ec64b0e 109 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 110 }
mazgch 2:b77151f111a9 111
mazgch 16:43f6de7bc38b 112 int port = 7;
mazgch 16:43f6de7bc38b 113 const char* host = "echo.u-blox.com";
mazgch 16:43f6de7bc38b 114 MDMParser::IP ip = mdm.gethostbyname(host);
mazgch 16:43f6de7bc38b 115 char data[] = "\r\nxxx Socket Hello World\r\n"
mazgch 19:f022ff746eb8 116 #ifdef LARGE_DATA
mazgch 17:c293780a40ac 117 "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 118 "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 119 "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 120 "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 121 "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 122
mazgch 17:c293780a40ac 123 "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 124 "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 125 "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 126 "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 127 "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 128
mazgch 17:c293780a40ac 129 "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 130 "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 131 "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 132 "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 133 "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 134
mazgch 17:c293780a40ac 135 "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 136 "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 137 "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 138 "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 139 "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 140 #endif
mazgch 17:c293780a40ac 141 "End\r\n";
mazgch 16:43f6de7bc38b 142
mazgch 19:f022ff746eb8 143 printf("Testing TCP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 144 socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 16:43f6de7bc38b 145 if (socket >= 0)
mazgch 16:43f6de7bc38b 146 {
mazgch 16:43f6de7bc38b 147 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 148 if (mdm.socketConnect(socket, host, port)) {
mazgch 16:43f6de7bc38b 149 memcpy(data, "\r\nTCP", 5);
mazgch 16:43f6de7bc38b 150 ret = mdm.socketSend(socket, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 151 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 152 printf("Socket Send %d \"%s\"\r\n", ret, data);
mazgch 16:43f6de7bc38b 153 }
mazgch 16:43f6de7bc38b 154 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 155 if (ret >= 0) {
mazgch 19:f022ff746eb8 156 printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
mazgch 16:43f6de7bc38b 157 }
mazgch 16:43f6de7bc38b 158 mdm.socketClose(socket);
mazgch 16:43f6de7bc38b 159 }
mazgch 16:43f6de7bc38b 160 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 161 }
mazgch 16:43f6de7bc38b 162
mazgch 19:f022ff746eb8 163 printf("Testing UDP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 164 socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
mazgch 16:43f6de7bc38b 165 if (socket >= 0)
mazgch 16:43f6de7bc38b 166 {
mazgch 16:43f6de7bc38b 167 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 168 memcpy(data, "\r\nUDP", 5);
mazgch 16:43f6de7bc38b 169 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 170 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 171 printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
mazgch 16:43f6de7bc38b 172 }
mazgch 16:43f6de7bc38b 173 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 174 if (ret >= 0) {
mazgch 19:f022ff746eb8 175 printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
mazgch 16:43f6de7bc38b 176 }
mazgch 16:43f6de7bc38b 177 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 178 }
mazgch 16:43f6de7bc38b 179
mazgch 4:90ab1ec64b0e 180 // disconnect
mazgch 4:90ab1ec64b0e 181 mdm.disconnect();
mazgch 2:b77151f111a9 182 }
mazgch 10:d2da2028a233 183 }
mazgch 19:f022ff746eb8 184 printf("SMS and GPS Loop\r\n");
mazgch 10:d2da2028a233 185 char link[128] = "";
mazgch 10:d2da2028a233 186 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 187 const int wait = 100;
mazgch 10:d2da2028a233 188 bool abort = false;
mazgch 11:b8505cbbd55c 189 //DigitalOut led(LED1);
mazgch 10:d2da2028a233 190 while (!abort) {
mazgch 19:f022ff746eb8 191 // led = !led;
mazgch 10:d2da2028a233 192 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 10:d2da2028a233 193 {
mazgch 10:d2da2028a233 194 int len = LENGTH(ret);
mazgch 19:f022ff746eb8 195 //printf("NMEA: %.*s\r\n", len-2, msg);
mazgch 19:f022ff746eb8 196 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
mazgch 4:90ab1ec64b0e 197 {
mazgch 27:fe3b383dd4ec 198 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
mazgch 27:fe3b383dd4ec 199 if ((buf[0] == '$') || buf[1] == 'G') {
mazgch 27:fe3b383dd4ec 200 #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
mazgch 27:fe3b383dd4ec 201 if (_CHECK_TALKER("GLL")) {
mazgch 27:fe3b383dd4ec 202 double la = 0, lo = 0;
mazgch 27:fe3b383dd4ec 203 char ch;
mazgch 27:fe3b383dd4ec 204 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 27:fe3b383dd4ec 205 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 27:fe3b383dd4ec 206 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 27:fe3b383dd4ec 207 {
mazgch 27:fe3b383dd4ec 208 printf("GPS Location: %.5f %.5f\r\n", la, lo);
mazgch 27:fe3b383dd4ec 209 sprintf(link, "I am here!\n"
mazgch 27:fe3b383dd4ec 210 "https://maps.google.com/?q=%.5f,%.5f", la, lo);
mazgch 27:fe3b383dd4ec 211 }
mazgch 27:fe3b383dd4ec 212 } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) {
mazgch 27:fe3b383dd4ec 213 double a = 0;
mazgch 27:fe3b383dd4ec 214 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
mazgch 27:fe3b383dd4ec 215 printf("GPS Altitude: %.1f\r\n", a);
mazgch 27:fe3b383dd4ec 216 } else if (_CHECK_TALKER("VTG")) {
mazgch 27:fe3b383dd4ec 217 double s = 0;
mazgch 27:fe3b383dd4ec 218 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
mazgch 27:fe3b383dd4ec 219 printf("GPS Speed: %.1f\r\n", s);
mazgch 19:f022ff746eb8 220 }
mazgch 4:90ab1ec64b0e 221 }
mazgch 4:90ab1ec64b0e 222 }
mazgch 10:d2da2028a233 223 }
mazgch 19:f022ff746eb8 224 if (mdmOk && (i++ == 5000/wait)) {
mazgch 10:d2da2028a233 225 i = 0;
mazgch 10:d2da2028a233 226 // check the network status
mazgch 15:ea10b6cf8c85 227 if (mdm.checkNetStatus(&netStatus)) {
mazgch 19:f022ff746eb8 228 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
mazgch 15:ea10b6cf8c85 229 }
mazgch 10:d2da2028a233 230
mazgch 10:d2da2028a233 231 // checking unread sms
mazgch 10:d2da2028a233 232 int ix[8];
mazgch 10:d2da2028a233 233 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 234 if (8 < n) n = 8;
mazgch 10:d2da2028a233 235 while (0 < n--)
mazgch 10:d2da2028a233 236 {
mazgch 10:d2da2028a233 237 char num[32];
mazgch 19:f022ff746eb8 238 printf("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 239 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 19:f022ff746eb8 240 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 19:f022ff746eb8 241 printf("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 242 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 243 // provide a reply
mazgch 10:d2da2028a233 244 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 245 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 246 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 19:f022ff746eb8 247 else if (strstr(buf, /*s*/"hutdown"))
mazgch 19:f022ff746eb8 248 abort = true, reply = "bye bye";
mazgch 19:f022ff746eb8 249 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 250 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 251 }
mazgch 4:90ab1ec64b0e 252 }
mazgch 9:26f694bc31b4 253 }
mazgch 10:d2da2028a233 254 wait_ms(wait);
lawliet 0:4e3cb26f6019 255 }
mazgch 19:f022ff746eb8 256 gps.powerOff();
mazgch 10:d2da2028a233 257 mdm.powerOff();
lawliet 0:4e3cb26f6019 258 return 0;
lawliet 0:4e3cb26f6019 259 }