mbed 5.4 with sleep mode
main.cpp@9:26f694bc31b4, 2014-04-17 (annotated)
- Committer:
- mazgch
- Date:
- Thu Apr 17 15:10:09 2014 +0000
- Revision:
- 9:26f694bc31b4
- Parent:
- 7:e000317ddef6
- Child:
- 10:d2da2028a233
Make defines to easier change the user config.
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 "C027.h" |
mazgch | 2:b77151f111a9 | 3 | #include "GPS.h" |
mazgch | 2:b77151f111a9 | 4 | #include "MDM.h" |
lawliet | 0:4e3cb26f6019 | 5 | |
mazgch | 9:26f694bc31b4 | 6 | //---------------------------------------------------------------------- |
mazgch | 9:26f694bc31b4 | 7 | // You may need to configure these parameters |
mazgch | 9:26f694bc31b4 | 8 | |
mazgch | 9:26f694bc31b4 | 9 | /** Set your secret SIM pin here "1234" |
mazgch | 9:26f694bc31b4 | 10 | */ |
mazgch | 9:26f694bc31b4 | 11 | #define SIMPIN NULL |
mazgch | 9:26f694bc31b4 | 12 | |
mazgch | 9:26f694bc31b4 | 13 | /** The APN of your network operator, sometimes it is "internet" |
mazgch | 9:26f694bc31b4 | 14 | check your contract with the network operator |
mazgch | 9:26f694bc31b4 | 15 | */ |
mazgch | 9:26f694bc31b4 | 16 | #define APN "gprs.swisscom.ch" |
mazgch | 9:26f694bc31b4 | 17 | |
mazgch | 9:26f694bc31b4 | 18 | /** Set the user name for your APN, or NULL if not needed |
mazgch | 9:26f694bc31b4 | 19 | */ |
mazgch | 9:26f694bc31b4 | 20 | #define USERNAME NULL |
mazgch | 9:26f694bc31b4 | 21 | |
mazgch | 9:26f694bc31b4 | 22 | /** Set the password for your APN, or NULL if not needed |
mazgch | 9:26f694bc31b4 | 23 | */ |
mazgch | 9:26f694bc31b4 | 24 | #define PASSWORD NULL |
mazgch | 9:26f694bc31b4 | 25 | |
mazgch | 9:26f694bc31b4 | 26 | //---------------------------------------------------------------------- |
mazgch | 9:26f694bc31b4 | 27 | |
mazgch | 2:b77151f111a9 | 28 | C027 c027; |
lawliet | 0:4e3cb26f6019 | 29 | |
mazgch | 5:5366d39d3719 | 30 | void printDeviceStatus(MDMParser::DevStatus* status) { |
mazgch | 5:5366d39d3719 | 31 | printf("Device Status:\r\n"); |
mazgch | 6:71f6214d595e | 32 | const char* txtDev[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" }; |
mazgch | 6:71f6214d595e | 33 | if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != MDMParser::DEV_UNKNOWN)) |
mazgch | 6:71f6214d595e | 34 | printf(" Device: %s\r\n", txtDev[status->dev]); |
mazgch | 7:e000317ddef6 | 35 | const char* txtLpm[] = { "Disabled", "Enabled", "Active" }; |
mazgch | 7:e000317ddef6 | 36 | if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm)) |
mazgch | 7:e000317ddef6 | 37 | printf(" Power Save: %s\r\n", txtLpm[status->lpm]); |
mazgch | 5:5366d39d3719 | 38 | const char* txtSim[] = { "Unknown", "Pin", "Ready" }; |
mazgch | 5:5366d39d3719 | 39 | if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != MDMParser::SIM_UNKNOWN)) |
mazgch | 6:71f6214d595e | 40 | printf(" SIM: %s\r\n", txtSim[status->sim]); |
mazgch | 6:71f6214d595e | 41 | if (*status->ccid) |
mazgch | 6:71f6214d595e | 42 | printf(" CCID: %s\r\n", status->ccid); |
mazgch | 6:71f6214d595e | 43 | if (*status->imei) |
mazgch | 6:71f6214d595e | 44 | printf(" IMEI: %s\r\n", status->imei); |
mazgch | 6:71f6214d595e | 45 | if (*status->imsi) |
mazgch | 6:71f6214d595e | 46 | printf(" IMSI: %s\r\n", status->imsi); |
mazgch | 7:e000317ddef6 | 47 | if (*status->meid) |
mazgch | 7:e000317ddef6 | 48 | printf(" MEID: %s\r\n", status->meid); |
mazgch | 6:71f6214d595e | 49 | if (*status->manu) |
mazgch | 6:71f6214d595e | 50 | printf(" Manufacturer: %s\r\n", status->manu); |
mazgch | 6:71f6214d595e | 51 | if (*status->model) |
mazgch | 6:71f6214d595e | 52 | printf(" Model: %s\r\n", status->model); |
mazgch | 6:71f6214d595e | 53 | if (*status->ver) |
mazgch | 6:71f6214d595e | 54 | printf(" Version: %s\r\n", status->ver); |
mazgch | 5:5366d39d3719 | 55 | } |
mazgch | 5:5366d39d3719 | 56 | |
mazgch | 5:5366d39d3719 | 57 | void printNetStatus(MDMParser::NetStatus *status) |
mazgch | 5:5366d39d3719 | 58 | { |
mazgch | 5:5366d39d3719 | 59 | printf("Network Status:\r\n"); |
mazgch | 5:5366d39d3719 | 60 | const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" }; |
mazgch | 5:5366d39d3719 | 61 | if (status->reg < sizeof(txtReg)/sizeof(*txtReg) && (status->reg != MDMParser::REG_UNKNOWN)) |
mazgch | 5:5366d39d3719 | 62 | printf(" Registration: %s\r\n", txtReg[status->reg]); |
mazgch | 5:5366d39d3719 | 63 | const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" }; |
mazgch | 5:5366d39d3719 | 64 | if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != MDMParser::ACT_UNKNOWN)) |
mazgch | 5:5366d39d3719 | 65 | printf(" Access Technology: %s\r\n", txtAct[status->act]); |
mazgch | 5:5366d39d3719 | 66 | if (status->rssi) |
mazgch | 5:5366d39d3719 | 67 | printf(" Signal Strength: %d dBm\r\n", status->rssi); |
mazgch | 6:71f6214d595e | 68 | if (*status->opr) |
mazgch | 5:5366d39d3719 | 69 | printf(" Operator: %s\r\n", status->opr); |
mazgch | 6:71f6214d595e | 70 | if (*status->num) |
mazgch | 5:5366d39d3719 | 71 | printf(" Phone Number: %s\r\n", status->num); |
mazgch | 5:5366d39d3719 | 72 | } |
lawliet | 0:4e3cb26f6019 | 73 | |
lawliet | 0:4e3cb26f6019 | 74 | int main(void) |
lawliet | 0:4e3cb26f6019 | 75 | { |
mazgch | 2:b77151f111a9 | 76 | int ret; |
mazgch | 2:b77151f111a9 | 77 | char buf[512] = ""; |
lawliet | 0:4e3cb26f6019 | 78 | |
mazgch | 2:b77151f111a9 | 79 | Serial pc(USBTX,USBRX); |
mazgch | 2:b77151f111a9 | 80 | pc.baud(115200); |
mazgch | 2:b77151f111a9 | 81 | |
mazgch | 5:5366d39d3719 | 82 | printf("C027 Support Example\r\n"); |
mazgch | 2:b77151f111a9 | 83 | |
mazgch | 2:b77151f111a9 | 84 | c027.mdmPower(true); |
mazgch | 4:90ab1ec64b0e | 85 | c027.gpsPower(true); |
mazgch | 2:b77151f111a9 | 86 | wait(2); |
lawliet | 0:4e3cb26f6019 | 87 | |
mazgch | 4:90ab1ec64b0e | 88 | GPSI2C gps(GPSSDA,GPSSCL,GPSADR); // use GPSI2C class |
mazgch | 4:90ab1ec64b0e | 89 | // GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); // or GPSSerial class |
mazgch | 7:e000317ddef6 | 90 | MDMSerial mdm(MDMTXD,MDMRXD,MDMBAUD |
mazgch | 7:e000317ddef6 | 91 | #if DEVICE_SERIAL_FC |
mazgch | 7:e000317ddef6 | 92 | ,MDMRTS,MDMCTS |
mazgch | 7:e000317ddef6 | 93 | #endif |
mazgch | 2:b77151f111a9 | 94 | ); |
mazgch | 2:b77151f111a9 | 95 | |
mazgch | 2:b77151f111a9 | 96 | // initialize the modem |
mazgch | 6:71f6214d595e | 97 | printf("Device Init\r\n"); |
mazgch | 5:5366d39d3719 | 98 | MDMParser::DevStatus devStatus; |
mazgch | 5:5366d39d3719 | 99 | MDMParser::NetStatus netStatus; |
mazgch | 9:26f694bc31b4 | 100 | if (mdm.init(SIMPIN, &devStatus)) |
mazgch | 4:90ab1ec64b0e | 101 | { |
mazgch | 5:5366d39d3719 | 102 | printDeviceStatus(&devStatus); |
mazgch | 5:5366d39d3719 | 103 | |
mazgch | 4:90ab1ec64b0e | 104 | // wait until we are connected |
mazgch | 4:90ab1ec64b0e | 105 | printf("Network Check\r\n"); |
mazgch | 5:5366d39d3719 | 106 | while (!mdm.checkNetStatus(&netStatus)) |
mazgch | 4:90ab1ec64b0e | 107 | wait_ms(1000); |
mazgch | 2:b77151f111a9 | 108 | |
mazgch | 5:5366d39d3719 | 109 | printNetStatus(&netStatus); |
mazgch | 4:90ab1ec64b0e | 110 | printf("Network Join\r\n"); |
mazgch | 4:90ab1ec64b0e | 111 | // join the internet connection |
mazgch | 5:5366d39d3719 | 112 | MDMParser::IP ip = |
mazgch | 9:26f694bc31b4 | 113 | mdm.join(APN,USERNAME,PASSWORD); |
mazgch | 5:5366d39d3719 | 114 | if (ip != NOIP) |
mazgch | 2:b77151f111a9 | 115 | { |
mazgch | 6:71f6214d595e | 116 | printf(" IP Address: " IPSTR "\r\n", IPNUM(ip)); |
mazgch | 4:90ab1ec64b0e | 117 | printf("Socket Create\r\n"); |
mazgch | 4:90ab1ec64b0e | 118 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
mazgch | 4:90ab1ec64b0e | 119 | if (socket >= 0) |
mazgch | 2:b77151f111a9 | 120 | { |
mazgch | 4:90ab1ec64b0e | 121 | printf("Socket Connect\r\n"); |
mazgch | 4:90ab1ec64b0e | 122 | if (mdm.socketConnect(socket, "mbed.org", 80)) |
mazgch | 4:90ab1ec64b0e | 123 | { |
mazgch | 4:90ab1ec64b0e | 124 | printf("Make a Http Post Request\r\n"); |
mazgch | 4:90ab1ec64b0e | 125 | const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; |
mazgch | 4:90ab1ec64b0e | 126 | printf("Socket Send\r\n"); |
mazgch | 4:90ab1ec64b0e | 127 | mdm.socketSend(socket, http, sizeof(http)-1); |
mazgch | 4:90ab1ec64b0e | 128 | |
mazgch | 4:90ab1ec64b0e | 129 | printf("Socket Recving\r\n"); |
mazgch | 4:90ab1ec64b0e | 130 | while (true) { |
mazgch | 4:90ab1ec64b0e | 131 | ret = mdm.socketReadable(socket); |
mazgch | 4:90ab1ec64b0e | 132 | if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 133 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
mazgch | 4:90ab1ec64b0e | 134 | if (ret < 0) |
mazgch | 4:90ab1ec64b0e | 135 | break; |
mazgch | 4:90ab1ec64b0e | 136 | else if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 137 | printf("Socket Recv \"%*s\"\r\n", ret, buf); |
mazgch | 4:90ab1ec64b0e | 138 | } |
mazgch | 4:90ab1ec64b0e | 139 | printf("Socket Close\r\n"); |
mazgch | 4:90ab1ec64b0e | 140 | mdm.socketClose(socket); |
mazgch | 4:90ab1ec64b0e | 141 | } |
mazgch | 4:90ab1ec64b0e | 142 | printf("Socket Free\r\n"); |
mazgch | 4:90ab1ec64b0e | 143 | mdm.socketFree(socket); |
mazgch | 4:90ab1ec64b0e | 144 | } |
mazgch | 2:b77151f111a9 | 145 | |
mazgch | 4:90ab1ec64b0e | 146 | // disconnect |
mazgch | 4:90ab1ec64b0e | 147 | printf("Network Disconnect\r\n"); |
mazgch | 4:90ab1ec64b0e | 148 | mdm.disconnect(); |
mazgch | 2:b77151f111a9 | 149 | } |
mazgch | 2:b77151f111a9 | 150 | |
mazgch | 5:5366d39d3719 | 151 | const char* ussd = "*#134#"; // You may get answer "UNKNOWN APPLICATION" |
mazgch | 4:90ab1ec64b0e | 152 | printf("Send Ussd Command %s\r\n", ussd); |
mazgch | 5:5366d39d3719 | 153 | ret = mdm.ussdCommand(ussd, buf); |
mazgch | 4:90ab1ec64b0e | 154 | if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 155 | printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf); |
mazgch | 4:90ab1ec64b0e | 156 | |
mazgch | 4:90ab1ec64b0e | 157 | printf("Checking SMS and GPS\r\n"); |
mazgch | 4:90ab1ec64b0e | 158 | char link[128] = ""; |
mazgch | 5:5366d39d3719 | 159 | unsigned int i = 0xFFFFFFFF; |
mazgch | 4:90ab1ec64b0e | 160 | const int wait = 100; |
mazgch | 4:90ab1ec64b0e | 161 | while (1) { |
mazgch | 4:90ab1ec64b0e | 162 | |
mazgch | 4:90ab1ec64b0e | 163 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) |
mazgch | 4:90ab1ec64b0e | 164 | { |
mazgch | 4:90ab1ec64b0e | 165 | int len = LENGTH(ret); |
mazgch | 5:5366d39d3719 | 166 | //printf("NMEA: %.*s\r\n", len-2, msg); |
mazgch | 4:90ab1ec64b0e | 167 | if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6)) |
mazgch | 4:90ab1ec64b0e | 168 | { |
mazgch | 4:90ab1ec64b0e | 169 | double la = 0, lo = 0; |
mazgch | 4:90ab1ec64b0e | 170 | char ch; |
mazgch | 4:90ab1ec64b0e | 171 | if (gps.getNmeaAngle(1,buf,len,la) && |
mazgch | 4:90ab1ec64b0e | 172 | gps.getNmeaAngle(3,buf,len,lo) && |
mazgch | 4:90ab1ec64b0e | 173 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') |
mazgch | 4:90ab1ec64b0e | 174 | { |
mazgch | 5:5366d39d3719 | 175 | printf("GPS Location: %.5f %.5f\r\n", la, lo); |
mazgch | 4:90ab1ec64b0e | 176 | sprintf(link, "I am here!\n" |
mazgch | 4:90ab1ec64b0e | 177 | "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
mazgch | 4:90ab1ec64b0e | 178 | } |
mazgch | 4:90ab1ec64b0e | 179 | } |
mazgch | 4:90ab1ec64b0e | 180 | } |
mazgch | 4:90ab1ec64b0e | 181 | |
mazgch | 4:90ab1ec64b0e | 182 | if (i++ == 10000/wait) { |
mazgch | 4:90ab1ec64b0e | 183 | i = 0; |
mazgch | 5:5366d39d3719 | 184 | // check the network status |
mazgch | 5:5366d39d3719 | 185 | if (mdm.checkNetStatus(&netStatus)) |
mazgch | 5:5366d39d3719 | 186 | printNetStatus(&netStatus); |
mazgch | 5:5366d39d3719 | 187 | |
mazgch | 5:5366d39d3719 | 188 | // checking unread sms |
mazgch | 5:5366d39d3719 | 189 | int ix[8]; |
mazgch | 5:5366d39d3719 | 190 | int n = mdm.smsList("REC UNREAD", ix, 8); |
mazgch | 5:5366d39d3719 | 191 | if (8 < n) n = 8; |
mazgch | 5:5366d39d3719 | 192 | while (0 < n--) |
mazgch | 5:5366d39d3719 | 193 | { |
mazgch | 4:90ab1ec64b0e | 194 | char num[32]; |
mazgch | 5:5366d39d3719 | 195 | printf("Unread SMS at index %d\r\n", ix[n]); |
mazgch | 5:5366d39d3719 | 196 | if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) { |
mazgch | 4:90ab1ec64b0e | 197 | printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); |
mazgch | 5:5366d39d3719 | 198 | printf("Delete SMS at index %d\r\n", ix[n]); |
mazgch | 5:5366d39d3719 | 199 | mdm.smsDelete(ix[n]); |
mazgch | 4:90ab1ec64b0e | 200 | // provide a reply |
mazgch | 4:90ab1ec64b0e | 201 | const char* reply = "Hello my friend"; |
mazgch | 4:90ab1ec64b0e | 202 | if (strstr(buf, /*w*/"here are you")) |
mazgch | 4:90ab1ec64b0e | 203 | reply = *link ? link : "I don't know"; // reply wil location link |
mazgch | 4:90ab1ec64b0e | 204 | printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); |
mazgch | 4:90ab1ec64b0e | 205 | mdm.smsSend(num, reply); |
mazgch | 4:90ab1ec64b0e | 206 | } |
mazgch | 4:90ab1ec64b0e | 207 | } |
mazgch | 4:90ab1ec64b0e | 208 | } |
mazgch | 4:90ab1ec64b0e | 209 | wait_ms(wait); |
mazgch | 9:26f694bc31b4 | 210 | } |
mazgch | 4:90ab1ec64b0e | 211 | mdm.powerOff(); |
mazgch | 5:5366d39d3719 | 212 | gps.powerOff(); |
lawliet | 0:4e3cb26f6019 | 213 | } |
mazgch | 7:e000317ddef6 | 214 | printf("Shutdown\r\n"); |
mazgch | 2:b77151f111a9 | 215 | // now it is safe to switch off |
mazgch | 2:b77151f111a9 | 216 | c027.mdmPower(false); |
mazgch | 4:90ab1ec64b0e | 217 | c027.gpsPower(false); |
mazgch | 2:b77151f111a9 | 218 | |
lawliet | 0:4e3cb26f6019 | 219 | return 0; |
lawliet | 0:4e3cb26f6019 | 220 | } |
mazgch | 2:b77151f111a9 | 221 |