mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Committer:
mazgch
Date:
Wed May 28 09:21:55 2014 +0000
Revision:
20:52f0e5de8c3d
Parent:
19:f022ff746eb8
Child:
21:a090a5043e23
use latest lib

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 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 19:f022ff746eb8 67 if (len)
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 5:5366d39d3719 89 if (ip != NOIP)
mazgch 2:b77151f111a9 90 {
mazgch 19:f022ff746eb8 91 mdm.dumpIp(ip);
mazgch 19:f022ff746eb8 92 printf("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 93 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 94 if (socket >= 0)
mazgch 2:b77151f111a9 95 {
mazgch 16:43f6de7bc38b 96 mdm.socketSetBlocking(socket, 10000);
mazgch 4:90ab1ec64b0e 97 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 98 {
mazgch 4:90ab1ec64b0e 99 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 4:90ab1ec64b0e 100 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 101
mazgch 19:f022ff746eb8 102 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 19:f022ff746eb8 103 if (ret > 0)
mazgch 19:f022ff746eb8 104 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 105 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 106 }
mazgch 4:90ab1ec64b0e 107 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 108 }
mazgch 2:b77151f111a9 109
mazgch 16:43f6de7bc38b 110 int port = 7;
mazgch 16:43f6de7bc38b 111 const char* host = "echo.u-blox.com";
mazgch 16:43f6de7bc38b 112 MDMParser::IP ip = mdm.gethostbyname(host);
mazgch 16:43f6de7bc38b 113 char data[] = "\r\nxxx Socket Hello World\r\n"
mazgch 19:f022ff746eb8 114 #ifdef LARGE_DATA
mazgch 17:c293780a40ac 115 "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 116 "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 117 "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 118 "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 119 "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 120
mazgch 17:c293780a40ac 121 "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 122 "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 123 "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 124 "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 125 "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 126
mazgch 17:c293780a40ac 127 "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 128 "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 129 "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 130 "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 131 "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 132
mazgch 17:c293780a40ac 133 "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 134 "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 135 "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 136 "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 137 "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 138 #endif
mazgch 17:c293780a40ac 139 "End\r\n";
mazgch 16:43f6de7bc38b 140
mazgch 19:f022ff746eb8 141 printf("Testing TCP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 142 socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 16:43f6de7bc38b 143 if (socket >= 0)
mazgch 16:43f6de7bc38b 144 {
mazgch 16:43f6de7bc38b 145 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 146 if (mdm.socketConnect(socket, host, port)) {
mazgch 16:43f6de7bc38b 147 memcpy(data, "\r\nTCP", 5);
mazgch 16:43f6de7bc38b 148 ret = mdm.socketSend(socket, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 149 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 150 printf("Socket Send %d \"%s\"\r\n", ret, data);
mazgch 16:43f6de7bc38b 151 }
mazgch 16:43f6de7bc38b 152 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 153 if (ret >= 0) {
mazgch 19:f022ff746eb8 154 printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
mazgch 16:43f6de7bc38b 155 }
mazgch 16:43f6de7bc38b 156 mdm.socketClose(socket);
mazgch 16:43f6de7bc38b 157 }
mazgch 16:43f6de7bc38b 158 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 159 }
mazgch 16:43f6de7bc38b 160
mazgch 19:f022ff746eb8 161 printf("Testing UDP sockets with ECHO server\r\n");
mazgch 16:43f6de7bc38b 162 socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
mazgch 16:43f6de7bc38b 163 if (socket >= 0)
mazgch 16:43f6de7bc38b 164 {
mazgch 16:43f6de7bc38b 165 mdm.socketSetBlocking(socket, 10000);
mazgch 16:43f6de7bc38b 166 memcpy(data, "\r\nUDP", 5);
mazgch 16:43f6de7bc38b 167 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
mazgch 16:43f6de7bc38b 168 if (ret == sizeof(data)-1) {
mazgch 19:f022ff746eb8 169 printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
mazgch 16:43f6de7bc38b 170 }
mazgch 16:43f6de7bc38b 171 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
mazgch 16:43f6de7bc38b 172 if (ret >= 0) {
mazgch 19:f022ff746eb8 173 printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
mazgch 16:43f6de7bc38b 174 }
mazgch 16:43f6de7bc38b 175 mdm.socketFree(socket);
mazgch 16:43f6de7bc38b 176 }
mazgch 16:43f6de7bc38b 177
mazgch 4:90ab1ec64b0e 178 // disconnect
mazgch 4:90ab1ec64b0e 179 mdm.disconnect();
mazgch 2:b77151f111a9 180 }
mazgch 10:d2da2028a233 181 }
mazgch 19:f022ff746eb8 182 printf("SMS and GPS Loop\r\n");
mazgch 10:d2da2028a233 183 char link[128] = "";
mazgch 10:d2da2028a233 184 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 185 const int wait = 100;
mazgch 10:d2da2028a233 186 bool abort = false;
mazgch 11:b8505cbbd55c 187 //DigitalOut led(LED1);
mazgch 10:d2da2028a233 188 while (!abort) {
mazgch 19:f022ff746eb8 189 // led = !led;
mazgch 10:d2da2028a233 190 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 10:d2da2028a233 191 {
mazgch 10:d2da2028a233 192 int len = LENGTH(ret);
mazgch 19:f022ff746eb8 193 //printf("NMEA: %.*s\r\n", len-2, msg);
mazgch 19:f022ff746eb8 194 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
mazgch 4:90ab1ec64b0e 195 {
mazgch 19:f022ff746eb8 196 if (!strncmp("$GPGLL", buf, 6)) {
mazgch 19:f022ff746eb8 197 double la = 0, lo = 0;
mazgch 19:f022ff746eb8 198 char ch;
mazgch 19:f022ff746eb8 199 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 19:f022ff746eb8 200 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 19:f022ff746eb8 201 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 19:f022ff746eb8 202 {
mazgch 19:f022ff746eb8 203 printf("GPS Location: %.5f %.5f\r\n", la, lo);
mazgch 19:f022ff746eb8 204 sprintf(link, "I am here!\n"
mazgch 19:f022ff746eb8 205 "https://maps.google.com/?q=%.5f,%.5f", la, lo);
mazgch 19:f022ff746eb8 206 }
mazgch 19:f022ff746eb8 207 } else if (!strncmp("$GPGGA", buf, 6)) {
mazgch 19:f022ff746eb8 208 double a = 0;
mazgch 19:f022ff746eb8 209 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
mazgch 19:f022ff746eb8 210 printf("GPS Altitude: %.1f\r\n", a);
mazgch 19:f022ff746eb8 211 } else if (!strncmp("$GPVTG", buf, 6)) {
mazgch 19:f022ff746eb8 212 double s = 0;
mazgch 19:f022ff746eb8 213 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
mazgch 19:f022ff746eb8 214 printf("GPS Speed: %.1f\r\n", s);
mazgch 4:90ab1ec64b0e 215 }
mazgch 4:90ab1ec64b0e 216 }
mazgch 10:d2da2028a233 217 }
mazgch 19:f022ff746eb8 218 if (mdmOk && (i++ == 5000/wait)) {
mazgch 10:d2da2028a233 219 i = 0;
mazgch 10:d2da2028a233 220 // check the network status
mazgch 15:ea10b6cf8c85 221 if (mdm.checkNetStatus(&netStatus)) {
mazgch 19:f022ff746eb8 222 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
mazgch 15:ea10b6cf8c85 223 }
mazgch 10:d2da2028a233 224
mazgch 10:d2da2028a233 225 // checking unread sms
mazgch 10:d2da2028a233 226 int ix[8];
mazgch 10:d2da2028a233 227 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 228 if (8 < n) n = 8;
mazgch 10:d2da2028a233 229 while (0 < n--)
mazgch 10:d2da2028a233 230 {
mazgch 10:d2da2028a233 231 char num[32];
mazgch 19:f022ff746eb8 232 printf("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 233 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 19:f022ff746eb8 234 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 19:f022ff746eb8 235 printf("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 236 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 237 // provide a reply
mazgch 10:d2da2028a233 238 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 239 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 240 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 19:f022ff746eb8 241 else if (strstr(buf, /*s*/"hutdown"))
mazgch 19:f022ff746eb8 242 abort = true, reply = "bye bye";
mazgch 19:f022ff746eb8 243 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 244 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 245 }
mazgch 4:90ab1ec64b0e 246 }
mazgch 9:26f694bc31b4 247 }
mazgch 10:d2da2028a233 248 wait_ms(wait);
lawliet 0:4e3cb26f6019 249 }
mazgch 19:f022ff746eb8 250 gps.powerOff();
mazgch 10:d2da2028a233 251 mdm.powerOff();
lawliet 0:4e3cb26f6019 252 return 0;
lawliet 0:4e3cb26f6019 253 }