mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Committer:
mazgch
Date:
Mon May 12 07:29:13 2014 +0000
Revision:
14:ab7c8627f950
Parent:
13:662bd1df9a72
Child:
15:ea10b6cf8c85
pull latest lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:4e3cb26f6019 1 #include "mbed.h"
mazgch 2:b77151f111a9 2 #include "GPS.h"
mazgch 2:b77151f111a9 3 #include "MDM.h"
lawliet 0:4e3cb26f6019 4
mazgch 9:26f694bc31b4 5 //----------------------------------------------------------------------
mazgch 9:26f694bc31b4 6 // You may need to configure these parameters
mazgch 9:26f694bc31b4 7
mazgch 9:26f694bc31b4 8 /** Set your secret SIM pin here "1234"
mazgch 9:26f694bc31b4 9 */
mazgch 9:26f694bc31b4 10 #define SIMPIN NULL
mazgch 9:26f694bc31b4 11
mazgch 9:26f694bc31b4 12 /** The APN of your network operator, sometimes it is "internet"
mazgch 9:26f694bc31b4 13 check your contract with the network operator
mazgch 9:26f694bc31b4 14 */
mazgch 9:26f694bc31b4 15 #define APN "gprs.swisscom.ch"
mazgch 9:26f694bc31b4 16
mazgch 9:26f694bc31b4 17 /** Set the user name for your APN, or NULL if not needed
mazgch 9:26f694bc31b4 18 */
mazgch 9:26f694bc31b4 19 #define USERNAME NULL
mazgch 9:26f694bc31b4 20
mazgch 9:26f694bc31b4 21 /** Set the password for your APN, or NULL if not needed
mazgch 9:26f694bc31b4 22 */
mazgch 9:26f694bc31b4 23 #define PASSWORD NULL
mazgch 9:26f694bc31b4 24
mazgch 10:d2da2028a233 25 //----------------------------------------------------------------------
mazgch 11:b8505cbbd55c 26 /* This example was tested on C027-U20 and C027-G35 with the on board modem.
mazgch 11:b8505cbbd55c 27
mazgch 11:b8505cbbd55c 28 Additionally it was tested with a shield where the SARA-G350 RX/TX/PWRON
mazgch 11:b8505cbbd55c 29 is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this
mazgch 11:b8505cbbd55c 30 configuration the following platforms were tested (it is likely that others
mazgch 11:b8505cbbd55c 31 will work as well)
mazgch 12:96c7b62c7aaf 32 - U-BLOX: C027-XX.
mazgch 12:96c7b62c7aaf 33 - NXP: LPC1549v2
mazgch 12:96c7b62c7aaf 34 - Freescale: FRDM-KL25Z, FRDM-KL46Z
mazgch 12:96c7b62c7aaf 35 - STM: NUCLEO-F401RE, NUCLEO-F030R8
mazgch 11:b8505cbbd55c 36 */
mazgch 13:662bd1df9a72 37 #if defined(TARGET_UBLOX_C027)
mazgch 13:662bd1df9a72 38 #define C027_USEONBOARD // remove this if you have the GSM/GPS shield mounted
mazgch 10:d2da2028a233 39 #endif
mazgch 10:d2da2028a233 40
mazgch 13:662bd1df9a72 41 #ifdef C027_USEONBOARD
mazgch 13:662bd1df9a72 42 #include "C027.h"
mazgch 13:662bd1df9a72 43 C027 c027;
mazgch 13:662bd1df9a72 44 #else
mazgch 10:d2da2028a233 45 #define GPSSCL D15
mazgch 10:d2da2028a233 46 #define GPSSDA D14
mazgch 10:d2da2028a233 47 #define GPSADR (66<<1) // GPS I2C Address
mazgch 10:d2da2028a233 48 #define MDMPWRON D4
mazgch 10:d2da2028a233 49 #define MDMTXD D1
mazgch 10:d2da2028a233 50 #define MDMRXD D0
mazgch 10:d2da2028a233 51 #define MDMCTS NC
mazgch 10:d2da2028a233 52 #define MDMRTS NC
mazgch 10:d2da2028a233 53 #define MDMBAUD 115200
mazgch 10:d2da2028a233 54 #endif
mazgch 10:d2da2028a233 55
mazgch 10:d2da2028a233 56 // no tracing if serial is shared with the VCP
mazgch 10:d2da2028a233 57 #define TRACE ((USBRX==MDMRXD)||(USBTX==MDMTXD))?:printf
lawliet 0:4e3cb26f6019 58
mazgch 5:5366d39d3719 59 void printDeviceStatus(MDMParser::DevStatus* status) {
mazgch 10:d2da2028a233 60 TRACE("Device Status:\r\n");
mazgch 6:71f6214d595e 61 const char* txtDev[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
mazgch 6:71f6214d595e 62 if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != MDMParser::DEV_UNKNOWN))
mazgch 10:d2da2028a233 63 TRACE(" Device: %s\r\n", txtDev[status->dev]);
mazgch 7:e000317ddef6 64 const char* txtLpm[] = { "Disabled", "Enabled", "Active" };
mazgch 7:e000317ddef6 65 if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm))
mazgch 10:d2da2028a233 66 TRACE(" Power Save: %s\r\n", txtLpm[status->lpm]);
mazgch 5:5366d39d3719 67 const char* txtSim[] = { "Unknown", "Pin", "Ready" };
mazgch 5:5366d39d3719 68 if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != MDMParser::SIM_UNKNOWN))
mazgch 10:d2da2028a233 69 TRACE(" SIM: %s\r\n", txtSim[status->sim]);
mazgch 6:71f6214d595e 70 if (*status->ccid)
mazgch 10:d2da2028a233 71 TRACE(" CCID: %s\r\n", status->ccid);
mazgch 6:71f6214d595e 72 if (*status->imei)
mazgch 10:d2da2028a233 73 TRACE(" IMEI: %s\r\n", status->imei);
mazgch 6:71f6214d595e 74 if (*status->imsi)
mazgch 10:d2da2028a233 75 TRACE(" IMSI: %s\r\n", status->imsi);
mazgch 7:e000317ddef6 76 if (*status->meid)
mazgch 10:d2da2028a233 77 TRACE(" MEID: %s\r\n", status->meid);
mazgch 6:71f6214d595e 78 if (*status->manu)
mazgch 10:d2da2028a233 79 TRACE(" Manufacturer: %s\r\n", status->manu);
mazgch 6:71f6214d595e 80 if (*status->model)
mazgch 10:d2da2028a233 81 TRACE(" Model: %s\r\n", status->model);
mazgch 6:71f6214d595e 82 if (*status->ver)
mazgch 10:d2da2028a233 83 TRACE(" Version: %s\r\n", status->ver);
mazgch 5:5366d39d3719 84 }
mazgch 5:5366d39d3719 85
mazgch 5:5366d39d3719 86 void printNetStatus(MDMParser::NetStatus *status)
mazgch 5:5366d39d3719 87 {
mazgch 10:d2da2028a233 88 TRACE("Network Status:\r\n");
mazgch 5:5366d39d3719 89 const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
mazgch 5:5366d39d3719 90 if (status->reg < sizeof(txtReg)/sizeof(*txtReg) && (status->reg != MDMParser::REG_UNKNOWN))
mazgch 10:d2da2028a233 91 TRACE(" Registration: %s\r\n", txtReg[status->reg]);
mazgch 5:5366d39d3719 92 const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
mazgch 5:5366d39d3719 93 if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != MDMParser::ACT_UNKNOWN))
mazgch 10:d2da2028a233 94 TRACE(" Access Technology: %s\r\n", txtAct[status->act]);
mazgch 5:5366d39d3719 95 if (status->rssi)
mazgch 10:d2da2028a233 96 TRACE(" Signal Strength: %d dBm\r\n", status->rssi);
mazgch 6:71f6214d595e 97 if (*status->opr)
mazgch 10:d2da2028a233 98 TRACE(" Operator: %s\r\n", status->opr);
mazgch 6:71f6214d595e 99 if (*status->num)
mazgch 10:d2da2028a233 100 TRACE(" Phone Number: %s\r\n", status->num);
mazgch 5:5366d39d3719 101 }
lawliet 0:4e3cb26f6019 102
lawliet 0:4e3cb26f6019 103 int main(void)
lawliet 0:4e3cb26f6019 104 {
mazgch 2:b77151f111a9 105 int ret;
mazgch 2:b77151f111a9 106 char buf[512] = "";
lawliet 0:4e3cb26f6019 107
mazgch 13:662bd1df9a72 108 // only trace if the serial is different from our modem port
mazgch 11:b8505cbbd55c 109 if ((USBRX!=MDMRXD)&&(USBTX!=MDMTXD)) {
mazgch 11:b8505cbbd55c 110 Serial pc(USBTX,USBRX);
mazgch 11:b8505cbbd55c 111 pc.baud(115200);
mazgch 11:b8505cbbd55c 112 }
mazgch 11:b8505cbbd55c 113
mazgch 11:b8505cbbd55c 114 wait_ms(1000);
mazgch 2:b77151f111a9 115
mazgch 10:d2da2028a233 116 TRACE("GSM/GPS Support Example\r\n");
mazgch 11:b8505cbbd55c 117
mazgch 10:d2da2028a233 118 #ifdef C027_USEONBOARD
mazgch 10:d2da2028a233 119 // turn on the supplies of the Modem and the GPS
mazgch 2:b77151f111a9 120 c027.mdmPower(true);
mazgch 4:90ab1ec64b0e 121 c027.gpsPower(true);
mazgch 10:d2da2028a233 122 #else
mazgch 10:d2da2028a233 123 // turn on the Modem
mazgch 10:d2da2028a233 124 DigitalOut mdmPwrOn(MDMPWRON, 0);
mazgch 10:d2da2028a233 125 wait_ms(150);
mazgch 10:d2da2028a233 126 mdmPwrOn = 1;
mazgch 10:d2da2028a233 127 #endif
mazgch 2:b77151f111a9 128 wait(2);
mazgch 11:b8505cbbd55c 129
mazgch 10:d2da2028a233 130 // Create the GPS object
mazgch 13:662bd1df9a72 131 #if GPSADR // use GPSI2C class
mazgch 13:662bd1df9a72 132 GPSI2C gps(GPSSDA,GPSSCL,GPSADR);
mazgch 13:662bd1df9a72 133 #elif GPSBAUD // or GPSSerial class
mazgch 13:662bd1df9a72 134 GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD);
mazgch 10:d2da2028a233 135 #else
mazgch 10:d2da2028a233 136 #warning "please define the pins for the GPS"
mazgch 10:d2da2028a233 137 #endif
mazgch 11:b8505cbbd55c 138
mazgch 10:d2da2028a233 139 // Create the modem object
mazgch 7:e000317ddef6 140 MDMSerial mdm(MDMTXD,MDMRXD,MDMBAUD
mazgch 10:d2da2028a233 141 #if DEVICE_SERIAL_FC
mazgch 7:e000317ddef6 142 ,MDMRTS,MDMCTS
mazgch 10:d2da2028a233 143 #endif
mazgch 2:b77151f111a9 144 );
mazgch 11:b8505cbbd55c 145
mazgch 2:b77151f111a9 146 // initialize the modem
mazgch 10:d2da2028a233 147 TRACE("Device Init\r\n");
mazgch 5:5366d39d3719 148 MDMParser::DevStatus devStatus;
mazgch 5:5366d39d3719 149 MDMParser::NetStatus netStatus;
mazgch 10:d2da2028a233 150 bool mdmOk = mdm.init(SIMPIN, &devStatus);
mazgch 10:d2da2028a233 151 if (mdmOk)
mazgch 4:90ab1ec64b0e 152 {
mazgch 5:5366d39d3719 153 printDeviceStatus(&devStatus);
mazgch 5:5366d39d3719 154
mazgch 4:90ab1ec64b0e 155 // wait until we are connected
mazgch 10:d2da2028a233 156 TRACE("Network Check\r\n");
mazgch 5:5366d39d3719 157 while (!mdm.checkNetStatus(&netStatus))
mazgch 4:90ab1ec64b0e 158 wait_ms(1000);
mazgch 2:b77151f111a9 159
mazgch 5:5366d39d3719 160 printNetStatus(&netStatus);
mazgch 10:d2da2028a233 161 TRACE("Network Join\r\n");
mazgch 4:90ab1ec64b0e 162 // join the internet connection
mazgch 5:5366d39d3719 163 MDMParser::IP ip =
mazgch 9:26f694bc31b4 164 mdm.join(APN,USERNAME,PASSWORD);
mazgch 5:5366d39d3719 165 if (ip != NOIP)
mazgch 2:b77151f111a9 166 {
mazgch 10:d2da2028a233 167 TRACE(" IP Address: " IPSTR "\r\n", IPNUM(ip));
mazgch 10:d2da2028a233 168 TRACE("Socket Create\r\n");
mazgch 4:90ab1ec64b0e 169 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 4:90ab1ec64b0e 170 if (socket >= 0)
mazgch 2:b77151f111a9 171 {
mazgch 10:d2da2028a233 172 TRACE("Socket Connect\r\n");
mazgch 4:90ab1ec64b0e 173 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 4:90ab1ec64b0e 174 {
mazgch 10:d2da2028a233 175 TRACE("Make a Http Post Request\r\n");
mazgch 4:90ab1ec64b0e 176 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 10:d2da2028a233 177 TRACE("Socket Send\r\n");
mazgch 4:90ab1ec64b0e 178 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 4:90ab1ec64b0e 179
mazgch 10:d2da2028a233 180 TRACE("Socket Recving\r\n");
mazgch 4:90ab1ec64b0e 181 while (true) {
mazgch 4:90ab1ec64b0e 182 ret = mdm.socketReadable(socket);
mazgch 4:90ab1ec64b0e 183 if (ret > 0)
mazgch 4:90ab1ec64b0e 184 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 4:90ab1ec64b0e 185 if (ret < 0)
mazgch 4:90ab1ec64b0e 186 break;
mazgch 4:90ab1ec64b0e 187 else if (ret > 0)
mazgch 10:d2da2028a233 188 TRACE("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 4:90ab1ec64b0e 189 }
mazgch 10:d2da2028a233 190 TRACE("Socket Close\r\n");
mazgch 4:90ab1ec64b0e 191 mdm.socketClose(socket);
mazgch 4:90ab1ec64b0e 192 }
mazgch 10:d2da2028a233 193 TRACE("Socket Free\r\n");
mazgch 4:90ab1ec64b0e 194 mdm.socketFree(socket);
mazgch 4:90ab1ec64b0e 195 }
mazgch 2:b77151f111a9 196
mazgch 4:90ab1ec64b0e 197 // disconnect
mazgch 10:d2da2028a233 198 TRACE("Network Disconnect\r\n");
mazgch 4:90ab1ec64b0e 199 mdm.disconnect();
mazgch 2:b77151f111a9 200 }
mazgch 2:b77151f111a9 201
mazgch 5:5366d39d3719 202 const char* ussd = "*#134#"; // You may get answer "UNKNOWN APPLICATION"
mazgch 10:d2da2028a233 203 TRACE("Send Ussd Command %s\r\n", ussd);
mazgch 5:5366d39d3719 204 ret = mdm.ussdCommand(ussd, buf);
mazgch 4:90ab1ec64b0e 205 if (ret > 0)
mazgch 10:d2da2028a233 206 TRACE("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
mazgch 10:d2da2028a233 207 }
mazgch 10:d2da2028a233 208 TRACE("Checking SMS and GPS\r\n");
mazgch 10:d2da2028a233 209 char link[128] = "";
mazgch 10:d2da2028a233 210 unsigned int i = 0xFFFFFFFF;
mazgch 10:d2da2028a233 211 const int wait = 100;
mazgch 10:d2da2028a233 212 bool abort = false;
mazgch 11:b8505cbbd55c 213 //DigitalOut led(LED1);
mazgch 10:d2da2028a233 214 while (!abort) {
mazgch 11:b8505cbbd55c 215 // led = !led;
mazgch 10:d2da2028a233 216 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 10:d2da2028a233 217 {
mazgch 10:d2da2028a233 218 int len = LENGTH(ret);
mazgch 10:d2da2028a233 219 //TRACE("NMEA: %.*s\r\n", len-2, msg);
mazgch 14:ab7c8627f950 220 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
mazgch 4:90ab1ec64b0e 221 {
mazgch 10:d2da2028a233 222 double la = 0, lo = 0;
mazgch 10:d2da2028a233 223 char ch;
mazgch 10:d2da2028a233 224 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 10:d2da2028a233 225 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 10:d2da2028a233 226 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 4:90ab1ec64b0e 227 {
mazgch 10:d2da2028a233 228 TRACE("GPS Location: %.5f %.5f\r\n", la, lo);
mazgch 10:d2da2028a233 229 sprintf(link, "I am here!\n"
mazgch 10:d2da2028a233 230 "https://maps.google.com/?q=%.5f,%.5f", la, lo);
mazgch 4:90ab1ec64b0e 231 }
mazgch 4:90ab1ec64b0e 232 }
mazgch 10:d2da2028a233 233 }
mazgch 10:d2da2028a233 234 if (mdmOk && (i++ == 10000/wait)) {
mazgch 10:d2da2028a233 235 i = 0;
mazgch 10:d2da2028a233 236 // check the network status
mazgch 10:d2da2028a233 237 if (mdm.checkNetStatus(&netStatus))
mazgch 10:d2da2028a233 238 printNetStatus(&netStatus);
mazgch 10:d2da2028a233 239
mazgch 10:d2da2028a233 240 // checking unread sms
mazgch 10:d2da2028a233 241 int ix[8];
mazgch 10:d2da2028a233 242 int n = mdm.smsList("REC UNREAD", ix, 8);
mazgch 10:d2da2028a233 243 if (8 < n) n = 8;
mazgch 10:d2da2028a233 244 while (0 < n--)
mazgch 10:d2da2028a233 245 {
mazgch 10:d2da2028a233 246 char num[32];
mazgch 10:d2da2028a233 247 TRACE("Unread SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 248 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
mazgch 10:d2da2028a233 249 TRACE("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 10:d2da2028a233 250 TRACE("Delete SMS at index %d\r\n", ix[n]);
mazgch 10:d2da2028a233 251 mdm.smsDelete(ix[n]);
mazgch 10:d2da2028a233 252 // provide a reply
mazgch 10:d2da2028a233 253 const char* reply = "Hello my friend";
mazgch 10:d2da2028a233 254 if (strstr(buf, /*w*/"here are you"))
mazgch 10:d2da2028a233 255 reply = *link ? link : "I don't know"; // reply wil location link
mazgch 10:d2da2028a233 256 TRACE("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 10:d2da2028a233 257 mdm.smsSend(num, reply);
mazgch 4:90ab1ec64b0e 258 }
mazgch 4:90ab1ec64b0e 259 }
mazgch 9:26f694bc31b4 260 }
mazgch 10:d2da2028a233 261 wait_ms(wait);
lawliet 0:4e3cb26f6019 262 }
mazgch 10:d2da2028a233 263 mdm.powerOff();
mazgch 10:d2da2028a233 264 gps.powerOff();
mazgch 10:d2da2028a233 265 TRACE("Shutdown\r\n");
mazgch 10:d2da2028a233 266 #ifdef C027_USEONBOARD
mazgch 10:d2da2028a233 267 // now it is safe to switch LDOs off
mazgch 2:b77151f111a9 268 c027.mdmPower(false);
mazgch 4:90ab1ec64b0e 269 c027.gpsPower(false);
mazgch 10:d2da2028a233 270 #endif
lawliet 0:4e3cb26f6019 271 return 0;
lawliet 0:4e3cb26f6019 272 }
mazgch 2:b77151f111a9 273