mbed 5.4 with sleep mode

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Committer:
mazgch
Date:
Thu Apr 10 13:07:35 2014 +0000
Revision:
7:e000317ddef6
Parent:
6:71f6214d595e
Child:
9:26f694bc31b4
use latest libarary

Who changed what in which revision?

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