mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Committer:
mazgch
Date:
Fri Aug 21 09:14:03 2015 +0000
Revision:
30:062717f25e41
Parent:
28:334263983fcd
Child:
33:e27f40fada64
clean-up

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 30:062717f25e41 23 #define SIMPIN "1922"
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 30:062717f25e41 50 MDMSerial mdm(D1,D0);
mazgch 28:334263983fcd 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 4:90ab1ec64b0e 79 // join the internet connection
mazgch 19:f022ff746eb8 80 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 28:334263983fcd 81 if (ip != NOIP)
mazgch 2:b77151f111a9 82 {
mazgch 19:f022ff746eb8 83 mdm.dumpIp(ip);
mazgch 19:f022ff746eb8 84 printf("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 85 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 86 if (socket >= 0)
mazgch 2:b77151f111a9 87 {
mazgch 16:43f6de7bc38b 88 mdm.socketSetBlocking(socket, 10000);
mazgch 4:90ab1ec64b0e 89 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 90 {
mazgch 4:90ab1ec64b0e 91 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 4:90ab1ec64b0e 92 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 93
mazgch 19:f022ff746eb8 94 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 19:f022ff746eb8 95 if (ret > 0)
mazgch 19:f022ff746eb8 96 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 97 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 98 }
mazgch 4:90ab1ec64b0e 99 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 100 }
mazgch 2:b77151f111a9 101
mazgch 16:43f6de7bc38b 102 int port = 7;
mazgch 16:43f6de7bc38b 103 const char* host = "echo.u-blox.com";
mazgch 16:43f6de7bc38b 104 MDMParser::IP ip = mdm.gethostbyname(host);
mazgch 16:43f6de7bc38b 105 char data[] = "\r\nxxx Socket Hello World\r\n"
mazgch 19:f022ff746eb8 106 #ifdef LARGE_DATA
mazgch 17:c293780a40ac 107 "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 108 "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 109 "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 110 "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 111 "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 112
mazgch 17:c293780a40ac 113 "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 114 "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 115 "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 116 "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 117 "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 118
mazgch 17:c293780a40ac 119 "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 120 "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 121 "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 122 "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 123 "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 124
mazgch 17:c293780a40ac 125 "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 126 "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 127 "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 128 "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 17:c293780a40ac 129 "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
mazgch 16:43f6de7bc38b 130 #endif
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 }
mazgch 16:43f6de7bc38b 169
mazgch 4:90ab1ec64b0e 170 // disconnect
mazgch 4:90ab1ec64b0e 171 mdm.disconnect();
mazgch 2:b77151f111a9 172 }
mazgch 30:062717f25e41 173
mazgch 30:062717f25e41 174 // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
mazgch 30:062717f25e41 175 // http://de.wikipedia.org/wiki/USSD-Codes
mazgch 30:062717f25e41 176 const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
mazgch 30:062717f25e41 177 printf("Ussd Send Command %s\r\n", ussd);
mazgch 30:062717f25e41 178 ret = mdm.ussdCommand(ussd, buf);
mazgch 30:062717f25e41 179 if (ret > 0)
mazgch 30:062717f25e41 180 printf("Ussd Got Answer: \"%s\"\r\n", buf);
mazgch 10:d2da2028a233 181 }
mazgch 30:062717f25e41 182
mazgch 19:f022ff746eb8 183 printf("SMS and GPS Loop\r\n");
mazgch 10:d2da2028a233 184 char link[128] = "";
mazgch 10:d2da2028a233 185 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 186 const int wait = 100;
mazgch 10:d2da2028a233 187 bool abort = false;
mazgch 11:b8505cbbd55c 188 //DigitalOut led(LED1);
mazgch 10:d2da2028a233 189 while (!abort) {
mazgch 19:f022ff746eb8 190 // led = !led;
mazgch 10:d2da2028a233 191 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 10:d2da2028a233 192 {
mazgch 10:d2da2028a233 193 int len = LENGTH(ret);
mazgch 19:f022ff746eb8 194 //printf("NMEA: %.*s\r\n", len-2, msg);
mazgch 19:f022ff746eb8 195 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
mazgch 4:90ab1ec64b0e 196 {
mazgch 27:fe3b383dd4ec 197 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
mazgch 27:fe3b383dd4ec 198 if ((buf[0] == '$') || buf[1] == 'G') {
mazgch 27:fe3b383dd4ec 199 #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
mazgch 27:fe3b383dd4ec 200 if (_CHECK_TALKER("GLL")) {
mazgch 27:fe3b383dd4ec 201 double la = 0, lo = 0;
mazgch 27:fe3b383dd4ec 202 char ch;
mazgch 27:fe3b383dd4ec 203 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 27:fe3b383dd4ec 204 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 27:fe3b383dd4ec 205 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 27:fe3b383dd4ec 206 {
mazgch 27:fe3b383dd4ec 207 printf("GPS Location: %.5f %.5f\r\n", la, lo);
mazgch 27:fe3b383dd4ec 208 sprintf(link, "I am here!\n"
mazgch 27:fe3b383dd4ec 209 "https://maps.google.com/?q=%.5f,%.5f", la, lo);
mazgch 27:fe3b383dd4ec 210 }
mazgch 27:fe3b383dd4ec 211 } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) {
mazgch 27:fe3b383dd4ec 212 double a = 0;
mazgch 27:fe3b383dd4ec 213 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
mazgch 27:fe3b383dd4ec 214 printf("GPS Altitude: %.1f\r\n", a);
mazgch 27:fe3b383dd4ec 215 } else if (_CHECK_TALKER("VTG")) {
mazgch 27:fe3b383dd4ec 216 double s = 0;
mazgch 27:fe3b383dd4ec 217 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
mazgch 27:fe3b383dd4ec 218 printf("GPS Speed: %.1f\r\n", s);
mazgch 19:f022ff746eb8 219 }
mazgch 4:90ab1ec64b0e 220 }
mazgch 4:90ab1ec64b0e 221 }
mazgch 10:d2da2028a233 222 }
mazgch 19:f022ff746eb8 223 if (mdmOk && (i++ == 5000/wait)) {
mazgch 10:d2da2028a233 224 i = 0;
mazgch 10:d2da2028a233 225 // check the network status
mazgch 15:ea10b6cf8c85 226 if (mdm.checkNetStatus(&netStatus)) {
mazgch 19:f022ff746eb8 227 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
mazgch 15:ea10b6cf8c85 228 }
mazgch 10:d2da2028a233 229
mazgch 10:d2da2028a233 230 // checking unread sms
mazgch 10:d2da2028a233 231 int ix[8];
mazgch 10:d2da2028a233 232 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 233 if (8 < n) n = 8;
mazgch 10:d2da2028a233 234 while (0 < n--)
mazgch 10:d2da2028a233 235 {
mazgch 10:d2da2028a233 236 char num[32];
mazgch 19:f022ff746eb8 237 printf("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 238 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 19:f022ff746eb8 239 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 19:f022ff746eb8 240 printf("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 241 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 242 // provide a reply
mazgch 10:d2da2028a233 243 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 244 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 245 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 19:f022ff746eb8 246 else if (strstr(buf, /*s*/"hutdown"))
mazgch 19:f022ff746eb8 247 abort = true, reply = "bye bye";
mazgch 19:f022ff746eb8 248 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 249 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 250 }
mazgch 4:90ab1ec64b0e 251 }
mazgch 9:26f694bc31b4 252 }
mazgch 10:d2da2028a233 253 wait_ms(wait);
lawliet 0:4e3cb26f6019 254 }
mazgch 19:f022ff746eb8 255 gps.powerOff();
mazgch 10:d2da2028a233 256 mdm.powerOff();
lawliet 0:4e3cb26f6019 257 return 0;
lawliet 0:4e3cb26f6019 258 }