Program to demonstrate basic C027_Support functionality

Dependencies:   C027_Support mbed-dev

Committer:
michaelVisimid
Date:
Fri Jan 27 00:52:20 2017 +0000
Revision:
1:2421820cab10
Parent:
0:624b906de247
Pre-compiled mbed libraries don't work correctly when exported

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaelVisimid 0:624b906de247 1 #include "mbed.h"
michaelVisimid 0:624b906de247 2
michaelVisimid 0:624b906de247 3
michaelVisimid 0:624b906de247 4 //------------------------------------------------------------------------------------
michaelVisimid 0:624b906de247 5 /* This example was tested on C027-U20 and C027-G35 with the on board modem.
michaelVisimid 0:624b906de247 6
michaelVisimid 0:624b906de247 7 Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON
michaelVisimid 0:624b906de247 8 is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this
michaelVisimid 0:624b906de247 9 configuration the following platforms were tested (it is likely that others
michaelVisimid 0:624b906de247 10 will work as well)
michaelVisimid 0:624b906de247 11 - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
michaelVisimid 0:624b906de247 12 - NXP: LPC1549v2, LPC4088qsb
michaelVisimid 0:624b906de247 13 - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
michaelVisimid 0:624b906de247 14 - STM: NUCLEO-F401RE, NUCLEO-F030R8
michaelVisimid 0:624b906de247 15 mount resistors SB13/14 1k, SB62/63 0R
michaelVisimid 0:624b906de247 16 */
michaelVisimid 0:624b906de247 17 //#include "GPS.h"
michaelVisimid 0:624b906de247 18 #include "MDM.h"
michaelVisimid 0:624b906de247 19 //------------------------------------------------------------------------------------
michaelVisimid 0:624b906de247 20 // You need to configure these cellular modem / SIM parameters.
michaelVisimid 0:624b906de247 21 // These parameters are ignored for LISA-C200 variants and can be left NULL.
michaelVisimid 0:624b906de247 22 //------------------------------------------------------------------------------------
michaelVisimid 0:624b906de247 23 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
michaelVisimid 0:624b906de247 24 #define SIMPIN "1922"
michaelVisimid 0:624b906de247 25 /*! The APN of your network operator SIM, sometimes it is "internet" check your
michaelVisimid 0:624b906de247 26 contract with the network operator. You can also try to look-up your settings in
michaelVisimid 0:624b906de247 27 google: https://www.google.de/search?q=APN+list */
michaelVisimid 0:624b906de247 28 #define APN "epc.tmobile.com"
michaelVisimid 0:624b906de247 29 //! Set the user name for your APN, or NULL if not needed
michaelVisimid 0:624b906de247 30 #define USERNAME NULL
michaelVisimid 0:624b906de247 31 //! Set the password for your APN, or NULL if not needed
michaelVisimid 0:624b906de247 32 #define PASSWORD NULL
michaelVisimid 0:624b906de247 33 //------------------------------------------------------------------------------------
michaelVisimid 0:624b906de247 34 DigitalIn button(USER_BUTTON);
michaelVisimid 0:624b906de247 35 Serial pc(USBTX,USBRX);
michaelVisimid 0:624b906de247 36 int main(void)
michaelVisimid 0:624b906de247 37 {
michaelVisimid 0:624b906de247 38 pc.baud(9600);
michaelVisimid 0:624b906de247 39 int ret;
michaelVisimid 0:624b906de247 40 #ifdef LARGE_DATA
michaelVisimid 0:624b906de247 41 char buf[2048] = "";
michaelVisimid 0:624b906de247 42 #else
michaelVisimid 0:624b906de247 43 char buf[512] = "";
michaelVisimid 0:624b906de247 44 #endif
michaelVisimid 0:624b906de247 45 MDMSerial mdm(PC_10,PC_11);
michaelVisimid 0:624b906de247 46 #if 0
michaelVisimid 0:624b906de247 47 // for debugging formatted output through
michaelVisimid 0:624b906de247 48
michaelVisimid 0:624b906de247 49 MDMParser::DevStatus devStatus = {};
michaelVisimid 0:624b906de247 50 MDMParser::NetStatus netStatus = {};
michaelVisimid 0:624b906de247 51 bool mdmOk = mdm.init(SIMPIN, &devStatus);
michaelVisimid 0:624b906de247 52 if (mdmOk) {
michaelVisimid 0:624b906de247 53 // wait until we are connected
michaelVisimid 0:624b906de247 54 mdmOk = mdm.registerNet(&netStatus);
michaelVisimid 0:624b906de247 55 mdm.dumpNetStatus(&netStatus);
michaelVisimid 0:624b906de247 56 }else{
michaelVisimid 0:624b906de247 57 printf("reg failed\r\n");
michaelVisimid 0:624b906de247 58 }
michaelVisimid 0:624b906de247 59 printf("start\r\n");
michaelVisimid 0:624b906de247 60 printf("array size: %d\r\n",sizeof(someKeyArray));
michaelVisimid 0:624b906de247 61 mdm.DebugFunction();
michaelVisimid 0:624b906de247 62 while(1){
michaelVisimid 0:624b906de247 63
michaelVisimid 0:624b906de247 64 }*/
michaelVisimid 0:624b906de247 65 #else
michaelVisimid 0:624b906de247 66 //mdm.setDebug(4); // enable this for debugging issues
michaelVisimid 0:624b906de247 67 // initialize the modem
michaelVisimid 0:624b906de247 68 MDMParser::DevStatus devStatus = {};
michaelVisimid 0:624b906de247 69 MDMParser::NetStatus netStatus = {};
michaelVisimid 0:624b906de247 70 bool mdmOk = mdm.init(SIMPIN, &devStatus);
michaelVisimid 0:624b906de247 71 mdm.dumpDevStatus(&devStatus);
michaelVisimid 0:624b906de247 72 if (mdmOk) {
michaelVisimid 0:624b906de247 73
michaelVisimid 0:624b906de247 74
michaelVisimid 0:624b906de247 75 // wait until we are connected
michaelVisimid 0:624b906de247 76 mdmOk = mdm.registerNet(&netStatus);
michaelVisimid 0:624b906de247 77 mdm.dumpNetStatus(&netStatus);
michaelVisimid 0:624b906de247 78 }else{
michaelVisimid 0:624b906de247 79 printf("reg failed\r\n");
michaelVisimid 0:624b906de247 80 }
michaelVisimid 0:624b906de247 81 if (mdmOk)
michaelVisimid 0:624b906de247 82 {
michaelVisimid 0:624b906de247 83 // join the internet connection
michaelVisimid 0:624b906de247 84 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
michaelVisimid 0:624b906de247 85 if (ip == NOIP)
michaelVisimid 0:624b906de247 86 printf("Not able to join network");
michaelVisimid 0:624b906de247 87 else
michaelVisimid 0:624b906de247 88 {
michaelVisimid 0:624b906de247 89 mdm.dumpIp(ip);
michaelVisimid 0:624b906de247 90 printf("Make a Http Post Request\r\n");
michaelVisimid 0:624b906de247 91 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
michaelVisimid 0:624b906de247 92 if (socket >= 0)
michaelVisimid 0:624b906de247 93 {
michaelVisimid 0:624b906de247 94 mdm.socketSetBlocking(socket, 10000);
michaelVisimid 0:624b906de247 95 if (mdm.socketConnect(socket, "mbed.org", 80))
michaelVisimid 0:624b906de247 96 {
michaelVisimid 0:624b906de247 97 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
michaelVisimid 0:624b906de247 98 mdm.socketSend(socket, http, sizeof(http)-1);
michaelVisimid 0:624b906de247 99
michaelVisimid 0:624b906de247 100 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
michaelVisimid 0:624b906de247 101 if (ret > 0)
michaelVisimid 0:624b906de247 102 printf("Socket Recv \"%*s\"\r\n", ret, buf);
michaelVisimid 0:624b906de247 103 mdm.socketClose(socket);
michaelVisimid 0:624b906de247 104 }
michaelVisimid 0:624b906de247 105 mdm.socketFree(socket);
michaelVisimid 0:624b906de247 106 }
michaelVisimid 0:624b906de247 107
michaelVisimid 0:624b906de247 108 int port = 7;
michaelVisimid 0:624b906de247 109 const char* host = "echo.u-blox.com";
michaelVisimid 0:624b906de247 110 MDMParser::IP ip = mdm.gethostbyname(host);
michaelVisimid 0:624b906de247 111 char data[] = "\r\nxxx Socket Hello World\r\n"
michaelVisimid 0:624b906de247 112 #ifdef LARGE_DATA
michaelVisimid 0:624b906de247 113 "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 114 "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 115 "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 116 "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 117 "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 118
michaelVisimid 0:624b906de247 119 "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 120 "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 121 "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 122 "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 123 "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 124
michaelVisimid 0:624b906de247 125 "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 126 "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 127 "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 128 "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 129 "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 130
michaelVisimid 0:624b906de247 131 "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 132 "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 133 "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 134 "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 135 "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n"
michaelVisimid 0:624b906de247 136 #endif
michaelVisimid 0:624b906de247 137 "End\r\n";
michaelVisimid 0:624b906de247 138
michaelVisimid 0:624b906de247 139 printf("Testing TCP sockets with ECHO server\r\n");
michaelVisimid 0:624b906de247 140 socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
michaelVisimid 0:624b906de247 141 if (socket >= 0)
michaelVisimid 0:624b906de247 142 {
michaelVisimid 0:624b906de247 143 mdm.socketSetBlocking(socket, 10000);
michaelVisimid 0:624b906de247 144 if (mdm.socketConnect(socket, host, port)) {
michaelVisimid 0:624b906de247 145 memcpy(data, "\r\nTCP", 5);
michaelVisimid 0:624b906de247 146 ret = mdm.socketSend(socket, data, sizeof(data)-1);
michaelVisimid 0:624b906de247 147 if (ret == sizeof(data)-1) {
michaelVisimid 0:624b906de247 148 printf("Socket Send %d \"%s\"\r\n", ret, data);
michaelVisimid 0:624b906de247 149 }
michaelVisimid 0:624b906de247 150 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
michaelVisimid 0:624b906de247 151 if (ret >= 0) {
michaelVisimid 0:624b906de247 152 printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf);
michaelVisimid 0:624b906de247 153 }
michaelVisimid 0:624b906de247 154 mdm.socketClose(socket);
michaelVisimid 0:624b906de247 155 }
michaelVisimid 0:624b906de247 156 mdm.socketFree(socket);
michaelVisimid 0:624b906de247 157 }
michaelVisimid 0:624b906de247 158
michaelVisimid 0:624b906de247 159 printf("Testing UDP sockets with ECHO server\r\n");
michaelVisimid 0:624b906de247 160 socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port);
michaelVisimid 0:624b906de247 161 if (socket >= 0)
michaelVisimid 0:624b906de247 162 {
michaelVisimid 0:624b906de247 163 mdm.socketSetBlocking(socket, 10000);
michaelVisimid 0:624b906de247 164 memcpy(data, "\r\nUDP", 5);
michaelVisimid 0:624b906de247 165 ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1);
michaelVisimid 0:624b906de247 166 if (ret == sizeof(data)-1) {
michaelVisimid 0:624b906de247 167 printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data);
michaelVisimid 0:624b906de247 168 }
michaelVisimid 0:624b906de247 169 ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1);
michaelVisimid 0:624b906de247 170 if (ret >= 0) {
michaelVisimid 0:624b906de247 171 printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf);
michaelVisimid 0:624b906de247 172 }
michaelVisimid 0:624b906de247 173 mdm.socketFree(socket);
michaelVisimid 0:624b906de247 174 }
michaelVisimid 0:624b906de247 175
michaelVisimid 0:624b906de247 176 // disconnect
michaelVisimid 0:624b906de247 177 mdm.disconnect();
michaelVisimid 0:624b906de247 178 }
michaelVisimid 0:624b906de247 179
michaelVisimid 0:624b906de247 180 // http://www.geckobeach.com/cellular/secrets/gsmcodes.php
michaelVisimid 0:624b906de247 181 // http://de.wikipedia.org/wiki/USSD-Codes
michaelVisimid 0:624b906de247 182 const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION"
michaelVisimid 0:624b906de247 183 printf("Ussd Send Command %s\r\n", ussd);
michaelVisimid 0:624b906de247 184 ret = mdm.ussdCommand(ussd, buf);
michaelVisimid 0:624b906de247 185 if (ret > 0)
michaelVisimid 0:624b906de247 186 printf("Ussd Got Answer: \"%s\"\r\n", buf);
michaelVisimid 0:624b906de247 187 }
michaelVisimid 0:624b906de247 188
michaelVisimid 0:624b906de247 189 printf("SMS and GPS Loop\r\n");
michaelVisimid 0:624b906de247 190 char link[128] = "";
michaelVisimid 0:624b906de247 191 unsigned int i = 0xFFFFFFFF;
michaelVisimid 0:624b906de247 192 const int wait = 100;
michaelVisimid 0:624b906de247 193 bool abort = false;
michaelVisimid 0:624b906de247 194 #ifdef CELLOCATE
michaelVisimid 0:624b906de247 195 const int sensorMask = 3; // Hybrid: GNSS + CellLocate
michaelVisimid 0:624b906de247 196 const int timeoutMargin = 5; // seconds
michaelVisimid 0:624b906de247 197 const int submitPeriod = 60; // 1 minutes in seconds
michaelVisimid 0:624b906de247 198 const int targetAccuracy = 1; // meters
michaelVisimid 0:624b906de247 199 unsigned int j = submitPeriod * 1000/wait;
michaelVisimid 0:624b906de247 200 bool cellLocWait = false;
michaelVisimid 0:624b906de247 201 MDMParser::CellLocData loc;
michaelVisimid 0:624b906de247 202
michaelVisimid 0:624b906de247 203 //Token can be released from u-blox site, when you got one replace "TOKEN" below
michaelVisimid 0:624b906de247 204 //if (!mdm.cellLocSrvHttp("TOKEN"))
michaelVisimid 0:624b906de247 205 if (!mdm.cellLocSrvTcp("TOKEN"))
michaelVisimid 0:624b906de247 206 mdm.cellLocSrvUdp();
michaelVisimid 0:624b906de247 207 mdm.cellLocConfig(1); // Deep scan mode
michaelVisimid 0:624b906de247 208 //mdm.cellUnsolIndication(1);
michaelVisimid 0:624b906de247 209 #endif
michaelVisimid 0:624b906de247 210 //DigitalOut led(LED1);
michaelVisimid 0:624b906de247 211 while (!abort) {
michaelVisimid 0:624b906de247 212 // led = !led;
michaelVisimid 0:624b906de247 213
michaelVisimid 0:624b906de247 214 #ifdef CELLOCATE
michaelVisimid 0:624b906de247 215 if (mdmOk && (j++ == submitPeriod * 1000/wait)) {
michaelVisimid 0:624b906de247 216 j=0;
michaelVisimid 0:624b906de247 217 printf("CellLocate Request\r\n");
michaelVisimid 0:624b906de247 218 mdm.cellLocRequest(MDMParser::CELL_LOCATE, 15, 1, MDMParser::CELL_DETAILED, 1);
michaelVisimid 0:624b906de247 219 //mdm.cellLocRequest(sensorMask, submitPeriod-timeoutMargin, targetAccuracy);
michaelVisimid 0:624b906de247 220 cellLocWait = true;
michaelVisimid 0:624b906de247 221 }
michaelVisimid 0:624b906de247 222 if (cellLocWait && mdm.cellLocGetData(&loc)){
michaelVisimid 0:624b906de247 223 cellLocWait = false;
michaelVisimid 0:624b906de247 224 printf("CellLocate position received, sensor_used: %d, \r\n", loc.sensor );
michaelVisimid 0:624b906de247 225 printf(" latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", loc.latitude, loc.longitude, loc.altitutude);
michaelVisimid 0:624b906de247 226 if (loc.sensor == 1)
michaelVisimid 0:624b906de247 227 printf(" uncertainty: %d, speed: %d, direction: %d, vertical_acc: %d, satellite used: %d \r\n", loc.uncertainty,loc.speed,loc.direction,loc.verticalAcc,loc.svUsed);
michaelVisimid 0:624b906de247 228 if (loc.sensor == 1 || loc.sensor == 2)
michaelVisimid 0:624b906de247 229 sprintf(link, "I am here!\n"
michaelVisimid 0:624b906de247 230 "https://maps.google.com/?q=%.5f,%.5f", loc.latitude, loc.longitude);
michaelVisimid 0:624b906de247 231 }
michaelVisimid 0:624b906de247 232 if (cellLocWait && (j%100 == 0 ))
michaelVisimid 0:624b906de247 233 printf("Waiting for CellLocate...\r\n");
michaelVisimid 0:624b906de247 234 #endif
michaelVisimid 0:624b906de247 235 if (mdmOk && (i++ == 5000/wait)) {
michaelVisimid 0:624b906de247 236 i = 0;
michaelVisimid 0:624b906de247 237 // check the network status
michaelVisimid 0:624b906de247 238 if (mdm.checkNetStatus(&netStatus)) {
michaelVisimid 0:624b906de247 239 mdm.dumpNetStatus(&netStatus, fprintf, stdout);
michaelVisimid 0:624b906de247 240 }
michaelVisimid 0:624b906de247 241
michaelVisimid 0:624b906de247 242 // checking unread sms
michaelVisimid 0:624b906de247 243 int ix[8];
michaelVisimid 0:624b906de247 244 int n = mdm.smsList("REC UNREAD", ix, 8);
michaelVisimid 0:624b906de247 245 if (8 < n) n = 8;
michaelVisimid 0:624b906de247 246 while (0 < n--)
michaelVisimid 0:624b906de247 247 {
michaelVisimid 0:624b906de247 248 char num[32];
michaelVisimid 0:624b906de247 249 printf("Unread SMS at index %d\r\n", ix[n]);
michaelVisimid 0:624b906de247 250 if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) {
michaelVisimid 0:624b906de247 251 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
michaelVisimid 0:624b906de247 252 printf("Delete SMS at index %d\r\n", ix[n]);
michaelVisimid 0:624b906de247 253 mdm.smsDelete(ix[n]);
michaelVisimid 0:624b906de247 254 // provide a reply
michaelVisimid 0:624b906de247 255 const char* reply = "Hello my friend";
michaelVisimid 0:624b906de247 256 if (strstr(buf, /*w*/"here are you"))
michaelVisimid 0:624b906de247 257 reply = *link ? link : "I don't know"; // reply wil location link
michaelVisimid 0:624b906de247 258 else if (strstr(buf, /*s*/"hutdown"))
michaelVisimid 0:624b906de247 259 abort = true, reply = "bye bye";
michaelVisimid 0:624b906de247 260 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
michaelVisimid 0:624b906de247 261 mdm.smsSend(num, reply);
michaelVisimid 0:624b906de247 262 }
michaelVisimid 0:624b906de247 263 }
michaelVisimid 0:624b906de247 264 }
michaelVisimid 0:624b906de247 265 #ifdef RTOS_H
michaelVisimid 0:624b906de247 266 Thread::wait(wait);
michaelVisimid 0:624b906de247 267 #else
michaelVisimid 0:624b906de247 268 ::wait_ms(wait);
michaelVisimid 0:624b906de247 269 #endif
michaelVisimid 0:624b906de247 270 }
michaelVisimid 0:624b906de247 271 //gps.powerOff();
michaelVisimid 0:624b906de247 272
michaelVisimid 0:624b906de247 273 mdm.powerOff();
michaelVisimid 0:624b906de247 274 #endif
michaelVisimid 0:624b906de247 275 return 0;
michaelVisimid 0:624b906de247 276
michaelVisimid 0:624b906de247 277 }
michaelVisimid 0:624b906de247 278