インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラム。   CQ publishing Interface 2014.10 issue, C027 3G test program.

Dependencies:   C027_Support C027_SupportTest mbed picojson

Fork of C027_SupportTest by u-blox

インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラムです。

Committer:
mazgch
Date:
Wed Apr 09 11:48:52 2014 +0000
Revision:
5:5366d39d3719
Parent:
4:90ab1ec64b0e
Child:
6:71f6214d595e
Improved the example and use latest library

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