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