whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Committer:
mazgch
Date:
Tue Apr 08 12:16:23 2014 +0000
Revision:
2:b77151f111a9
Parent:
0:4e3cb26f6019
Child:
4:90ab1ec64b0e
first release of example

Who changed what in which revision?

UserRevisionLine numberNew 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 2:b77151f111a9 42 wait(2);
lawliet 0:4e3cb26f6019 43
mazgch 2:b77151f111a9 44 #if 0
mazgch 2:b77151f111a9 45 c027.gpsPower(true);
mazgch 2:b77151f111a9 46 GPSI2C gps; // use GPSI2C or GPSSerial class
mazgch 2:b77151f111a9 47 while (1)
mazgch 2:b77151f111a9 48 {
mazgch 2:b77151f111a9 49 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
mazgch 2:b77151f111a9 50 {
mazgch 2:b77151f111a9 51 int len = LENGTH(ret);
mazgch 2:b77151f111a9 52 //printf("NMEA: %.*s\n", len-2, msg);
mazgch 2:b77151f111a9 53 if ((PROTOCOL(ret) == NMEA) && (len > 6) && !strncmp("$GPGLL", buf, 6))
mazgch 2:b77151f111a9 54 {
mazgch 2:b77151f111a9 55 double la = 0, lo = 0;
mazgch 2:b77151f111a9 56 char ch;
mazgch 2:b77151f111a9 57 if (gps.getNmeaAngle(1,buf,len,la) &&
mazgch 2:b77151f111a9 58 gps.getNmeaAngle(3,buf,len,lo) &&
mazgch 2:b77151f111a9 59 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
mazgch 2:b77151f111a9 60 {
mazgch 2:b77151f111a9 61 printf("GPS: %.5f %.5f\n", la, lo);
mazgch 2:b77151f111a9 62 }
mazgch 2:b77151f111a9 63 }
mazgch 2:b77151f111a9 64 }
mazgch 2:b77151f111a9 65 wait_ms(100);
mazgch 2:b77151f111a9 66 }
mazgch 2:b77151f111a9 67 #endif
lawliet 0:4e3cb26f6019 68
mazgch 2:b77151f111a9 69 MDMSerial mdm(MDMTXD, MDMRXD, MDMBAUD
mazgch 2:b77151f111a9 70 #if DEVICE_SERIAL_FC
mazgch 2:b77151f111a9 71 ,MDMRTS,MDMCTS
mazgch 2:b77151f111a9 72 #endif
mazgch 2:b77151f111a9 73 );
mazgch 2:b77151f111a9 74
mazgch 2:b77151f111a9 75 // initialize the modem
mazgch 2:b77151f111a9 76 printf("Init\r\n");
mazgch 2:b77151f111a9 77 mdm.init();
mazgch 2:b77151f111a9 78
mazgch 2:b77151f111a9 79 // wait until we are connected
mazgch 2:b77151f111a9 80 printf("Network Check\r\n");
mazgch 2:b77151f111a9 81 while (!mdm.checkNetStatus())
mazgch 2:b77151f111a9 82 wait_ms(1000);
lawliet 0:4e3cb26f6019 83
mazgch 2:b77151f111a9 84 printf("Network Join\r\n");
mazgch 2:b77151f111a9 85 // join the internet connection
mazgch 2:b77151f111a9 86 if (mdm.join("gprs.swisscom.ch"))
mazgch 2:b77151f111a9 87 {
mazgch 2:b77151f111a9 88 printf("Socket Create\r\n");
mazgch 2:b77151f111a9 89 int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP);
mazgch 2:b77151f111a9 90 if (socket >= 0)
mazgch 2:b77151f111a9 91 {
mazgch 2:b77151f111a9 92 printf("Socket Connect\r\n");
mazgch 2:b77151f111a9 93 if (mdm.socketConnect(socket, "mbed.org", 80))
mazgch 2:b77151f111a9 94 {
mazgch 2:b77151f111a9 95 printf("Make a Http Post Request\r\n");
mazgch 2:b77151f111a9 96 const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
mazgch 2:b77151f111a9 97 printf("Socket Send\r\n");
mazgch 2:b77151f111a9 98 mdm.socketSend(socket, http, sizeof(http)-1);
mazgch 2:b77151f111a9 99
mazgch 2:b77151f111a9 100 printf("Socket Recving\r\n");
mazgch 2:b77151f111a9 101 while (true) {
mazgch 2:b77151f111a9 102 ret = mdm.socketReadable(socket);
mazgch 2:b77151f111a9 103 if (ret > 0)
mazgch 2:b77151f111a9 104 ret = mdm.socketRecv(socket, buf, sizeof(buf)-1);
mazgch 2:b77151f111a9 105 if (ret < 0)
mazgch 2:b77151f111a9 106 break;
mazgch 2:b77151f111a9 107 else if (ret > 0)
mazgch 2:b77151f111a9 108 printf("Socket Recv \"%*s\"\r\n", ret, buf);
mazgch 2:b77151f111a9 109 }
mazgch 2:b77151f111a9 110 printf("Socket Close\r\n");
mazgch 2:b77151f111a9 111 mdm.socketClose(socket);
mazgch 2:b77151f111a9 112 }
mazgch 2:b77151f111a9 113 printf("Socket Free\r\n");
mazgch 2:b77151f111a9 114 mdm.socketFree(socket);
mazgch 2:b77151f111a9 115 }
mazgch 2:b77151f111a9 116
mazgch 2:b77151f111a9 117 // disconnect
mazgch 2:b77151f111a9 118 printf("Network Disconnect\r\n");
mazgch 2:b77151f111a9 119 mdm.disconnect();
lawliet 0:4e3cb26f6019 120 }
mazgch 2:b77151f111a9 121
mazgch 2:b77151f111a9 122 const char* ussd = "*#134#";
mazgch 2:b77151f111a9 123 printf("Send Ussd Command %s\r\n", ussd);
mazgch 2:b77151f111a9 124 ret = mdm.ussdCommand(ussd, buf, sizeof(buf));
mazgch 2:b77151f111a9 125 if (ret > 0)
mazgch 2:b77151f111a9 126 printf("Got Ussd Answer: \"%*s\"\r\n", ret, buf);
mazgch 2:b77151f111a9 127
mazgch 2:b77151f111a9 128 printf("Checking SMS\r\n");
mazgch 2:b77151f111a9 129 //int cnt = mdm.smsCount();
mazgch 2:b77151f111a9 130 while (1) {
mazgch 2:b77151f111a9 131 char num[32];
mazgch 2:b77151f111a9 132 for (int ix = 0; ix < 16; ix ++) {
mazgch 2:b77151f111a9 133 if (mdm.smsRead(ix, num, buf, sizeof(buf))) {
mazgch 2:b77151f111a9 134 printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf);
mazgch 2:b77151f111a9 135 printf("Delete SMS at index %d\r\n", ix);
mazgch 2:b77151f111a9 136 mdm.smsDelete(ix);
mazgch 2:b77151f111a9 137 const char* reply = "Hello my friend";
mazgch 2:b77151f111a9 138 printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num);
mazgch 2:b77151f111a9 139 mdm.smsSend(num, reply);
mazgch 2:b77151f111a9 140 }
mazgch 2:b77151f111a9 141 }
mazgch 2:b77151f111a9 142 MDMParser::Status info;
mazgch 2:b77151f111a9 143 if (mdm.checkNetStatus(&info))
mazgch 2:b77151f111a9 144 {
mazgch 2:b77151f111a9 145 printf("Network Status:\n");
mazgch 2:b77151f111a9 146 const char* txtNet[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
mazgch 2:b77151f111a9 147 if (info.net < sizeof(txtNet)/sizeof(*txtNet) && (info.net != MDMParser::NET_UNKNOWN))
mazgch 2:b77151f111a9 148 printf(" Network: %s\n", txtNet[info.net]);
mazgch 2:b77151f111a9 149 const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
mazgch 2:b77151f111a9 150 if (info.act < sizeof(txtAct)/sizeof(*txtAct) && (info.act != MDMParser::ACT_UNKNOWN))
mazgch 2:b77151f111a9 151 printf(" Access Technology: %s\n", txtAct[info.act]);
mazgch 2:b77151f111a9 152 if (info.rssi)
mazgch 2:b77151f111a9 153 printf(" Signal Strength: %d dBm\r\n", info.rssi);
mazgch 2:b77151f111a9 154 if (info.opr)
mazgch 2:b77151f111a9 155 printf(" Operator: %s\n", info.opr);
mazgch 2:b77151f111a9 156 if (info.num)
mazgch 2:b77151f111a9 157 printf(" Phone Number: %s\n", info.num);
mazgch 2:b77151f111a9 158 }
mazgch 2:b77151f111a9 159 wait_ms(10000);
mazgch 2:b77151f111a9 160 }
mazgch 2:b77151f111a9 161
mazgch 2:b77151f111a9 162 mdm.powerOff();
mazgch 2:b77151f111a9 163 // now it is safe to switch off
mazgch 2:b77151f111a9 164 c027.mdmPower(false);
mazgch 2:b77151f111a9 165
lawliet 0:4e3cb26f6019 166 return 0;
lawliet 0:4e3cb26f6019 167 }
mazgch 2:b77151f111a9 168