mbed 5.4 with sleep mode
main.cpp@4:90ab1ec64b0e, 2014-04-08 (annotated)
- Committer:
- mazgch
- Date:
- Tue Apr 08 15:50:09 2014 +0000
- Revision:
- 4:90ab1ec64b0e
- Parent:
- 2:b77151f111a9
- Child:
- 5:5366d39d3719
use new library improved sms processing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:4e3cb26f6019 | 1 | /* |
lawliet | 0:4e3cb26f6019 | 2 | main.cpp |
lawliet | 0:4e3cb26f6019 | 3 | 2013 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 0:4e3cb26f6019 | 4 | |
lawliet | 0:4e3cb26f6019 | 5 | Author:lawliet zou(lawliet.zou@gmail.com) |
lawliet | 0:4e3cb26f6019 | 6 | 2014-02-18 |
lawliet | 0:4e3cb26f6019 | 7 | |
lawliet | 0:4e3cb26f6019 | 8 | This library is free software; you can redistribute it and/or |
lawliet | 0:4e3cb26f6019 | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 0:4e3cb26f6019 | 10 | License as published by the Free Software Foundation; either |
lawliet | 0:4e3cb26f6019 | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 0:4e3cb26f6019 | 12 | |
lawliet | 0:4e3cb26f6019 | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 0:4e3cb26f6019 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 0:4e3cb26f6019 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 0:4e3cb26f6019 | 16 | Lesser General Public License for more details. |
lawliet | 0:4e3cb26f6019 | 17 | |
lawliet | 0:4e3cb26f6019 | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 0:4e3cb26f6019 | 19 | License along with this library; if not, write to the Free Software |
lawliet | 0:4e3cb26f6019 | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 0:4e3cb26f6019 | 21 | */ |
lawliet | 0:4e3cb26f6019 | 22 | |
lawliet | 0:4e3cb26f6019 | 23 | #include "mbed.h" |
mazgch | 2:b77151f111a9 | 24 | #include "C027.h" |
mazgch | 2:b77151f111a9 | 25 | #include "GPS.h" |
mazgch | 2:b77151f111a9 | 26 | #include "MDM.h" |
lawliet | 0:4e3cb26f6019 | 27 | |
mazgch | 2:b77151f111a9 | 28 | C027 c027; |
lawliet | 0:4e3cb26f6019 | 29 | |
lawliet | 0:4e3cb26f6019 | 30 | |
lawliet | 0:4e3cb26f6019 | 31 | int main(void) |
lawliet | 0:4e3cb26f6019 | 32 | { |
mazgch | 2:b77151f111a9 | 33 | int ret; |
mazgch | 2:b77151f111a9 | 34 | char buf[512] = ""; |
lawliet | 0:4e3cb26f6019 | 35 | |
mazgch | 2:b77151f111a9 | 36 | Serial pc(USBTX,USBRX); |
mazgch | 2:b77151f111a9 | 37 | pc.baud(115200); |
mazgch | 2:b77151f111a9 | 38 | |
mazgch | 2:b77151f111a9 | 39 | printf("Modem Example\n"); |
mazgch | 2:b77151f111a9 | 40 | |
mazgch | 2:b77151f111a9 | 41 | c027.mdmPower(true); |
mazgch | 4:90ab1ec64b0e | 42 | c027.gpsPower(true); |
mazgch | 2:b77151f111a9 | 43 | wait(2); |
lawliet | 0:4e3cb26f6019 | 44 | |
mazgch | 4:90ab1ec64b0e | 45 | GPSI2C gps(GPSSDA,GPSSCL,GPSADR); // use GPSI2C class |
mazgch | 4:90ab1ec64b0e | 46 | // GPSSerial gps(GPSTXD,GPSRXD,GPSBAUD); // or GPSSerial class |
mazgch | 2:b77151f111a9 | 47 | MDMSerial mdm(MDMTXD, MDMRXD, MDMBAUD |
mazgch | 2:b77151f111a9 | 48 | #if DEVICE_SERIAL_FC |
mazgch | 2:b77151f111a9 | 49 | ,MDMRTS,MDMCTS |
mazgch | 2:b77151f111a9 | 50 | #endif |
mazgch | 2:b77151f111a9 | 51 | ); |
mazgch | 2:b77151f111a9 | 52 | |
mazgch | 2:b77151f111a9 | 53 | // initialize the modem |
mazgch | 2:b77151f111a9 | 54 | printf("Init\r\n"); |
mazgch | 4:90ab1ec64b0e | 55 | MDMParser::DevStatus status; |
mazgch | 4:90ab1ec64b0e | 56 | if (mdm.init(NULL/*no SIM PIN*/, &status)) |
mazgch | 4:90ab1ec64b0e | 57 | { |
mazgch | 4:90ab1ec64b0e | 58 | printf("Device Status:\n"); |
mazgch | 4:90ab1ec64b0e | 59 | const char* txtModel[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" }; |
mazgch | 4:90ab1ec64b0e | 60 | if (status.model < sizeof(txtModel)/sizeof(*txtModel) && (status.model != MDMParser::MODEL_UNKNOWN)) |
mazgch | 4:90ab1ec64b0e | 61 | printf(" Model: %s\n", txtModel[status.model]); |
mazgch | 4:90ab1ec64b0e | 62 | const char* txtSim[] = { "Unknown", "Pin", "Ready" }; |
mazgch | 4:90ab1ec64b0e | 63 | if (status.sim < sizeof(txtSim)/sizeof(*txtSim) && (status.sim != MDMParser::SIM_UNKNOWN)) |
mazgch | 4:90ab1ec64b0e | 64 | printf(" SIM: %s\n", txtSim[status.sim]); |
mazgch | 4:90ab1ec64b0e | 65 | if (status.ccid) |
mazgch | 4:90ab1ec64b0e | 66 | printf(" CCID: %s\n", status.ccid); |
mazgch | 4:90ab1ec64b0e | 67 | if (status.imei) |
mazgch | 4:90ab1ec64b0e | 68 | printf(" IMEI: %s\r\n", status.imei); |
mazgch | 4:90ab1ec64b0e | 69 | if (status.imsi) |
mazgch | 4:90ab1ec64b0e | 70 | printf(" IMSI: %s\n", status.imsi); |
mazgch | 4:90ab1ec64b0e | 71 | |
mazgch | 4:90ab1ec64b0e | 72 | // wait until we are connected |
mazgch | 4:90ab1ec64b0e | 73 | printf("Network Check\r\n"); |
mazgch | 4:90ab1ec64b0e | 74 | while (!mdm.checkNetStatus()) |
mazgch | 4:90ab1ec64b0e | 75 | wait_ms(1000); |
mazgch | 2:b77151f111a9 | 76 | |
mazgch | 4:90ab1ec64b0e | 77 | printf("Network Join\r\n"); |
mazgch | 4:90ab1ec64b0e | 78 | // join the internet connection |
mazgch | 4:90ab1ec64b0e | 79 | if (mdm.join("gprs.swisscom.ch")) |
mazgch | 2:b77151f111a9 | 80 | { |
mazgch | 4:90ab1ec64b0e | 81 | printf("Socket Create\r\n"); |
mazgch | 4:90ab1ec64b0e | 82 | int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); |
mazgch | 4:90ab1ec64b0e | 83 | if (socket >= 0) |
mazgch | 2:b77151f111a9 | 84 | { |
mazgch | 4:90ab1ec64b0e | 85 | printf("Socket Connect\r\n"); |
mazgch | 4:90ab1ec64b0e | 86 | if (mdm.socketConnect(socket, "mbed.org", 80)) |
mazgch | 4:90ab1ec64b0e | 87 | { |
mazgch | 4:90ab1ec64b0e | 88 | printf("Make a Http Post Request\r\n"); |
mazgch | 4:90ab1ec64b0e | 89 | const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; |
mazgch | 4:90ab1ec64b0e | 90 | printf("Socket Send\r\n"); |
mazgch | 4:90ab1ec64b0e | 91 | mdm.socketSend(socket, http, sizeof(http)-1); |
mazgch | 4:90ab1ec64b0e | 92 | |
mazgch | 4:90ab1ec64b0e | 93 | printf("Socket Recving\r\n"); |
mazgch | 4:90ab1ec64b0e | 94 | while (true) { |
mazgch | 4:90ab1ec64b0e | 95 | ret = mdm.socketReadable(socket); |
mazgch | 4:90ab1ec64b0e | 96 | if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 97 | ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); |
mazgch | 4:90ab1ec64b0e | 98 | if (ret < 0) |
mazgch | 4:90ab1ec64b0e | 99 | break; |
mazgch | 4:90ab1ec64b0e | 100 | else if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 101 | printf("Socket Recv \"%*s\"\r\n", ret, buf); |
mazgch | 4:90ab1ec64b0e | 102 | } |
mazgch | 4:90ab1ec64b0e | 103 | printf("Socket Close\r\n"); |
mazgch | 4:90ab1ec64b0e | 104 | mdm.socketClose(socket); |
mazgch | 4:90ab1ec64b0e | 105 | } |
mazgch | 4:90ab1ec64b0e | 106 | printf("Socket Free\r\n"); |
mazgch | 4:90ab1ec64b0e | 107 | mdm.socketFree(socket); |
mazgch | 4:90ab1ec64b0e | 108 | } |
mazgch | 2:b77151f111a9 | 109 | |
mazgch | 4:90ab1ec64b0e | 110 | // disconnect |
mazgch | 4:90ab1ec64b0e | 111 | printf("Network Disconnect\r\n"); |
mazgch | 4:90ab1ec64b0e | 112 | mdm.disconnect(); |
mazgch | 2:b77151f111a9 | 113 | } |
mazgch | 2:b77151f111a9 | 114 | |
mazgch | 4:90ab1ec64b0e | 115 | const char* ussd = "*#134#"; |
mazgch | 4:90ab1ec64b0e | 116 | printf("Send Ussd Command %s\r\n", ussd); |
mazgch | 4:90ab1ec64b0e | 117 | ret = mdm.ussdCommand(ussd, buf, sizeof(buf)); |
mazgch | 4:90ab1ec64b0e | 118 | if (ret > 0) |
mazgch | 4:90ab1ec64b0e | 119 | printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf); |
mazgch | 4:90ab1ec64b0e | 120 | |
mazgch | 4:90ab1ec64b0e | 121 | printf("Checking SMS and GPS\r\n"); |
mazgch | 4:90ab1ec64b0e | 122 | char link[128] = ""; |
mazgch | 4:90ab1ec64b0e | 123 | int i = 0; |
mazgch | 4:90ab1ec64b0e | 124 | const int wait = 100; |
mazgch | 4:90ab1ec64b0e | 125 | while (1) { |
mazgch | 4:90ab1ec64b0e | 126 | |
mazgch | 4:90ab1ec64b0e | 127 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) |
mazgch | 4:90ab1ec64b0e | 128 | { |
mazgch | 4:90ab1ec64b0e | 129 | int len = LENGTH(ret); |
mazgch | 4:90ab1ec64b0e | 130 | //printf("NMEA: %.*s\n", len-2, msg); |
mazgch | 4:90ab1ec64b0e | 131 | if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6)) |
mazgch | 4:90ab1ec64b0e | 132 | { |
mazgch | 4:90ab1ec64b0e | 133 | double la = 0, lo = 0; |
mazgch | 4:90ab1ec64b0e | 134 | char ch; |
mazgch | 4:90ab1ec64b0e | 135 | if (gps.getNmeaAngle(1,buf,len,la) && |
mazgch | 4:90ab1ec64b0e | 136 | gps.getNmeaAngle(3,buf,len,lo) && |
mazgch | 4:90ab1ec64b0e | 137 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') |
mazgch | 4:90ab1ec64b0e | 138 | { |
mazgch | 4:90ab1ec64b0e | 139 | printf("GPS Location: %.5f %.5f\n", la, lo); |
mazgch | 4:90ab1ec64b0e | 140 | sprintf(link, "I am here!\n" |
mazgch | 4:90ab1ec64b0e | 141 | "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
mazgch | 4:90ab1ec64b0e | 142 | } |
mazgch | 4:90ab1ec64b0e | 143 | } |
mazgch | 4:90ab1ec64b0e | 144 | } |
mazgch | 4:90ab1ec64b0e | 145 | |
mazgch | 4:90ab1ec64b0e | 146 | if (i++ == 10000/wait) { |
mazgch | 4:90ab1ec64b0e | 147 | i = 0; |
mazgch | 4:90ab1ec64b0e | 148 | |
mazgch | 4:90ab1ec64b0e | 149 | // checking incoming sms |
mazgch | 4:90ab1ec64b0e | 150 | int cnt = mdm.smsCount(); |
mazgch | 4:90ab1ec64b0e | 151 | // search through all the messages if there are some |
mazgch | 4:90ab1ec64b0e | 152 | for (int ix = 1; (cnt > 0) && (ix <= 350); ix ++) { |
mazgch | 4:90ab1ec64b0e | 153 | char num[32]; |
mazgch | 4:90ab1ec64b0e | 154 | if (mdm.smsRead(ix, num, buf, sizeof(buf))) { |
mazgch | 4:90ab1ec64b0e | 155 | printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); |
mazgch | 4:90ab1ec64b0e | 156 | printf("Delete SMS at index %d\r\n", ix); |
mazgch | 4:90ab1ec64b0e | 157 | mdm.smsDelete(ix); |
mazgch | 4:90ab1ec64b0e | 158 | cnt --; |
mazgch | 4:90ab1ec64b0e | 159 | // provide a reply |
mazgch | 4:90ab1ec64b0e | 160 | const char* reply = "Hello my friend"; |
mazgch | 4:90ab1ec64b0e | 161 | if (strstr(buf, /*w*/"here are you")) |
mazgch | 4:90ab1ec64b0e | 162 | reply = *link ? link : "I don't know"; // reply wil location link |
mazgch | 4:90ab1ec64b0e | 163 | printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); |
mazgch | 4:90ab1ec64b0e | 164 | mdm.smsSend(num, reply); |
mazgch | 4:90ab1ec64b0e | 165 | } |
mazgch | 4:90ab1ec64b0e | 166 | } |
mazgch | 4:90ab1ec64b0e | 167 | |
mazgch | 4:90ab1ec64b0e | 168 | // check the network status |
mazgch | 4:90ab1ec64b0e | 169 | MDMParser::NetStatus status; |
mazgch | 4:90ab1ec64b0e | 170 | if (mdm.checkNetStatus(&status)) |
mazgch | 4:90ab1ec64b0e | 171 | { |
mazgch | 4:90ab1ec64b0e | 172 | printf("Network Status:\n"); |
mazgch | 4:90ab1ec64b0e | 173 | const char* txtNet[] = { "Unknown", "Denied", "None", "Home", "Roaming" }; |
mazgch | 4:90ab1ec64b0e | 174 | if (status.net < sizeof(txtNet)/sizeof(*txtNet) && (status.net != MDMParser::NET_UNKNOWN)) |
mazgch | 4:90ab1ec64b0e | 175 | printf(" Network: %s\n", txtNet[status.net]); |
mazgch | 4:90ab1ec64b0e | 176 | const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" }; |
mazgch | 4:90ab1ec64b0e | 177 | if (status.act < sizeof(txtAct)/sizeof(*txtAct) && (status.act != MDMParser::ACT_UNKNOWN)) |
mazgch | 4:90ab1ec64b0e | 178 | printf(" Access Technology: %s\n", txtAct[status.act]); |
mazgch | 4:90ab1ec64b0e | 179 | if (status.rssi) |
mazgch | 4:90ab1ec64b0e | 180 | printf(" Signal Strength: %d dBm\r\n", status.rssi); |
mazgch | 4:90ab1ec64b0e | 181 | if (status.opr) |
mazgch | 4:90ab1ec64b0e | 182 | printf(" Operator: %s\n", status.opr); |
mazgch | 4:90ab1ec64b0e | 183 | if (status.num) |
mazgch | 4:90ab1ec64b0e | 184 | printf(" Phone Number: %s\n", status.num); |
mazgch | 4:90ab1ec64b0e | 185 | } |
mazgch | 4:90ab1ec64b0e | 186 | } |
mazgch | 4:90ab1ec64b0e | 187 | wait_ms(wait); |
mazgch | 4:90ab1ec64b0e | 188 | } |
mazgch | 4:90ab1ec64b0e | 189 | mdm.powerOff(); |
lawliet | 0:4e3cb26f6019 | 190 | } |
mazgch | 2:b77151f111a9 | 191 | // now it is safe to switch off |
mazgch | 2:b77151f111a9 | 192 | c027.mdmPower(false); |
mazgch | 4:90ab1ec64b0e | 193 | c027.gpsPower(false); |
mazgch | 2:b77151f111a9 | 194 | |
lawliet | 0:4e3cb26f6019 | 195 | return 0; |
lawliet | 0:4e3cb26f6019 | 196 | } |
mazgch | 2:b77151f111a9 | 197 |