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