ok
gprs.h@3:48ee24a4b0f3, 2014-01-10 (annotated)
- Committer:
- lawliet
- Date:
- Fri Jan 10 05:59:36 2014 +0000
- Revision:
- 3:48ee24a4b0f3
- Parent:
- 2:16985da3a446
- Child:
- 4:63a2619b423b
Version 1.3(fix some bugs)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:a5ae94727346 | 1 | /* |
lawliet | 3:48ee24a4b0f3 | 2 | gprs.h |
lawliet | 0:a5ae94727346 | 3 | 2013 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 0:a5ae94727346 | 4 | |
lawliet | 0:a5ae94727346 | 5 | Author:lawliet.zou@gmail.com |
lawliet | 0:a5ae94727346 | 6 | 2013-11-14 |
lawliet | 0:a5ae94727346 | 7 | |
lawliet | 0:a5ae94727346 | 8 | This library is free software; you can redistribute it and/or |
lawliet | 0:a5ae94727346 | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 0:a5ae94727346 | 10 | License as published by the Free Software Foundation; either |
lawliet | 0:a5ae94727346 | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 0:a5ae94727346 | 12 | |
lawliet | 0:a5ae94727346 | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 0:a5ae94727346 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 0:a5ae94727346 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 0:a5ae94727346 | 16 | Lesser General Public License for more details. |
lawliet | 0:a5ae94727346 | 17 | |
lawliet | 0:a5ae94727346 | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 0:a5ae94727346 | 19 | License along with this library; if not, write to the Free Software |
lawliet | 0:a5ae94727346 | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 0:a5ae94727346 | 21 | */ |
lawliet | 0:a5ae94727346 | 22 | |
lawliet | 0:a5ae94727346 | 23 | #ifndef __GPRS_H__ |
lawliet | 0:a5ae94727346 | 24 | #define __GPRS_H__ |
lawliet | 0:a5ae94727346 | 25 | |
lawliet | 3:48ee24a4b0f3 | 26 | #include <stdio.h> |
lawliet | 0:a5ae94727346 | 27 | #include "mbed.h" |
lawliet | 3:48ee24a4b0f3 | 28 | |
lawliet | 3:48ee24a4b0f3 | 29 | #define DEFAULT_TIMEOUT 5 |
lawliet | 3:48ee24a4b0f3 | 30 | |
lawliet | 2:16985da3a446 | 31 | /** GPRS class. |
lawliet | 2:16985da3a446 | 32 | * Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol |
lawliet | 2:16985da3a446 | 33 | */ |
lawliet | 2:16985da3a446 | 34 | class GPRS |
lawliet | 1:642a8dbe076c | 35 | { |
lawliet | 1:642a8dbe076c | 36 | public: |
lawliet | 2:16985da3a446 | 37 | /** Create GPRS instance |
lawliet | 2:16985da3a446 | 38 | * @param tx uart transmit pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 39 | * @param rx uart receive pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 40 | * @param baudRate baud rate of uart communication |
lawliet | 2:16985da3a446 | 41 | * @param number default phone number during mobile communication |
lawliet | 2:16985da3a446 | 42 | */ |
lawliet | 2:16985da3a446 | 43 | GPRS(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) { |
lawliet | 2:16985da3a446 | 44 | gprsSerial.baud(baudRate); |
lawliet | 2:16985da3a446 | 45 | phoneNumber = number; |
lawliet | 1:642a8dbe076c | 46 | }; |
lawliet | 2:16985da3a446 | 47 | |
lawliet | 2:16985da3a446 | 48 | /** init GPRS module including SIM card check & signal strength & network check |
lawliet | 2:16985da3a446 | 49 | * @returns |
lawliet | 2:16985da3a446 | 50 | * 0 on success |
lawliet | 2:16985da3a446 | 51 | * -1 on error |
lawliet | 2:16985da3a446 | 52 | */ |
lawliet | 1:642a8dbe076c | 53 | int init(void); |
lawliet | 2:16985da3a446 | 54 | |
lawliet | 2:16985da3a446 | 55 | /** read from GPRS module and save to buffer array |
lawliet | 2:16985da3a446 | 56 | * @param *buffer buffer array to save what read from GPRS module |
lawliet | 2:16985da3a446 | 57 | * @param *count the maximal bytes number read from GPRS module |
lawliet | 2:16985da3a446 | 58 | * @returns |
lawliet | 2:16985da3a446 | 59 | * 0 on success |
lawliet | 2:16985da3a446 | 60 | * -1 on error |
lawliet | 2:16985da3a446 | 61 | */ |
lawliet | 1:642a8dbe076c | 62 | int readBuffer(char *buffer,int count); |
lawliet | 2:16985da3a446 | 63 | |
lawliet | 2:16985da3a446 | 64 | /** send AT command to GPRS module |
lawliet | 2:16985da3a446 | 65 | * @param *cmd command array which will be send to GPRS module |
lawliet | 2:16985da3a446 | 66 | */ |
lawliet | 1:642a8dbe076c | 67 | void sendCmd(char *cmd); |
lawliet | 2:16985da3a446 | 68 | |
lawliet | 2:16985da3a446 | 69 | /** check GPRS module response before timeout |
lawliet | 2:16985da3a446 | 70 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 71 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 72 | * @returns |
lawliet | 2:16985da3a446 | 73 | * 0 on success |
lawliet | 2:16985da3a446 | 74 | * -1 on error |
lawliet | 2:16985da3a446 | 75 | */ |
lawliet | 1:642a8dbe076c | 76 | int waitForResp(char *resp, int timeout); |
lawliet | 2:16985da3a446 | 77 | |
lawliet | 2:16985da3a446 | 78 | /** send AT command to GPRS module and wait for correct response |
lawliet | 2:16985da3a446 | 79 | * @param *cmd AT command which will be send to GPRS module |
lawliet | 2:16985da3a446 | 80 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 81 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 82 | * @returns |
lawliet | 2:16985da3a446 | 83 | * 0 on success |
lawliet | 2:16985da3a446 | 84 | * -1 on error |
lawliet | 2:16985da3a446 | 85 | */ |
lawliet | 1:642a8dbe076c | 86 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout); |
lawliet | 2:16985da3a446 | 87 | |
lawliet | 2:16985da3a446 | 88 | /** check SIM card' Status |
lawliet | 2:16985da3a446 | 89 | * @returns |
lawliet | 2:16985da3a446 | 90 | * 0 on success |
lawliet | 2:16985da3a446 | 91 | * -1 on error |
lawliet | 2:16985da3a446 | 92 | */ |
lawliet | 1:642a8dbe076c | 93 | int checkSIMStatus(void); |
lawliet | 2:16985da3a446 | 94 | |
lawliet | 2:16985da3a446 | 95 | /** check signal strength |
lawliet | 2:16985da3a446 | 96 | * @returns |
lawliet | 2:16985da3a446 | 97 | * signal strength in number(ex 3,4,5,6,7,8...) on success |
lawliet | 2:16985da3a446 | 98 | * -1 on error |
lawliet | 2:16985da3a446 | 99 | */ |
lawliet | 1:642a8dbe076c | 100 | int checkSignalStrength(void); |
lawliet | 2:16985da3a446 | 101 | |
lawliet | 2:16985da3a446 | 102 | /** check network is ok or not |
lawliet | 2:16985da3a446 | 103 | * @returns |
lawliet | 2:16985da3a446 | 104 | * 0 on success |
lawliet | 2:16985da3a446 | 105 | * -1 on error |
lawliet | 2:16985da3a446 | 106 | */ |
lawliet | 1:642a8dbe076c | 107 | int networkInit(void); |
lawliet | 2:16985da3a446 | 108 | |
lawliet | 2:16985da3a446 | 109 | /** send text SMS |
lawliet | 2:16985da3a446 | 110 | * @param *number phone number which SMS will be send to |
lawliet | 2:16985da3a446 | 111 | * @param *data message that will be send to |
lawliet | 2:16985da3a446 | 112 | * @returns |
lawliet | 2:16985da3a446 | 113 | * 0 on success |
lawliet | 2:16985da3a446 | 114 | * -1 on error |
lawliet | 2:16985da3a446 | 115 | */ |
lawliet | 1:642a8dbe076c | 116 | int sendSMS(char *number, char *data); |
lawliet | 2:16985da3a446 | 117 | |
lawliet | 2:16985da3a446 | 118 | /** read SMS if get a SMS |
lawliet | 2:16985da3a446 | 119 | * @param *buffer buffer that get from GPRS module(when getting a SMS, GPRS module will return a buffer array) |
lawliet | 2:16985da3a446 | 120 | * @param *message buffer used to get SMS message |
lawliet | 2:16985da3a446 | 121 | * @param check whether to check phone number(we may only want to read SMS from specified phone number) |
lawliet | 2:16985da3a446 | 122 | * @returns |
lawliet | 2:16985da3a446 | 123 | * 0 on success |
lawliet | 2:16985da3a446 | 124 | * -1 on error |
lawliet | 2:16985da3a446 | 125 | */ |
lawliet | 1:642a8dbe076c | 126 | int readSMS(char *buffer, char *message, bool check); |
lawliet | 2:16985da3a446 | 127 | |
lawliet | 2:16985da3a446 | 128 | /** delete SMS message on SIM card |
lawliet | 2:16985da3a446 | 129 | * @param *index the index number which SMS message will be delete |
lawliet | 2:16985da3a446 | 130 | * @returns |
lawliet | 2:16985da3a446 | 131 | * 0 on success |
lawliet | 2:16985da3a446 | 132 | * -1 on error |
lawliet | 2:16985da3a446 | 133 | */ |
lawliet | 1:642a8dbe076c | 134 | int deleteSMS(int index); |
lawliet | 2:16985da3a446 | 135 | |
lawliet | 2:16985da3a446 | 136 | /** call someone |
lawliet | 2:16985da3a446 | 137 | * @param *number the phone number which you want to call |
lawliet | 2:16985da3a446 | 138 | * @returns |
lawliet | 2:16985da3a446 | 139 | * 0 on success |
lawliet | 2:16985da3a446 | 140 | * -1 on error |
lawliet | 2:16985da3a446 | 141 | */ |
lawliet | 1:642a8dbe076c | 142 | int callUp(char *number); |
lawliet | 2:16985da3a446 | 143 | |
lawliet | 2:16985da3a446 | 144 | /** auto answer if coming a call |
lawliet | 2:16985da3a446 | 145 | * @returns |
lawliet | 2:16985da3a446 | 146 | * 0 on success |
lawliet | 2:16985da3a446 | 147 | * -1 on error |
lawliet | 2:16985da3a446 | 148 | */ |
lawliet | 1:642a8dbe076c | 149 | int answer(void); |
lawliet | 2:16985da3a446 | 150 | |
lawliet | 2:16985da3a446 | 151 | /** a loop to wait for some event. if a call comes in, it will auto answer it and if a SMS message comes in, it will read the message |
lawliet | 2:16985da3a446 | 152 | * @param *check whether to check phone number when get event |
lawliet | 2:16985da3a446 | 153 | * @returns |
lawliet | 2:16985da3a446 | 154 | * 0 on success |
lawliet | 2:16985da3a446 | 155 | * -1 on error |
lawliet | 2:16985da3a446 | 156 | */ |
lawliet | 1:642a8dbe076c | 157 | int loop(bool check); |
lawliet | 2:16985da3a446 | 158 | |
lawliet | 2:16985da3a446 | 159 | /** build TCP connect |
lawliet | 2:16985da3a446 | 160 | * @param *ip ip address which will connect to |
lawliet | 2:16985da3a446 | 161 | * @param *port TCP server' port number |
lawliet | 2:16985da3a446 | 162 | * @returns |
lawliet | 2:16985da3a446 | 163 | * 0 on success |
lawliet | 2:16985da3a446 | 164 | * -1 on error |
lawliet | 2:16985da3a446 | 165 | */ |
lawliet | 1:642a8dbe076c | 166 | int connectTCP(char *ip, char *port); |
lawliet | 2:16985da3a446 | 167 | |
lawliet | 2:16985da3a446 | 168 | /** send data to TCP server |
lawliet | 2:16985da3a446 | 169 | * @param *data data that will be send to TCP server |
lawliet | 2:16985da3a446 | 170 | * @returns |
lawliet | 2:16985da3a446 | 171 | * 0 on success |
lawliet | 2:16985da3a446 | 172 | * -1 on error |
lawliet | 2:16985da3a446 | 173 | */ |
lawliet | 1:642a8dbe076c | 174 | int sendTCPData(char *data); |
lawliet | 2:16985da3a446 | 175 | |
lawliet | 2:16985da3a446 | 176 | /** close TCP connection |
lawliet | 2:16985da3a446 | 177 | * @returns |
lawliet | 2:16985da3a446 | 178 | * 0 on success |
lawliet | 2:16985da3a446 | 179 | * -1 on error |
lawliet | 2:16985da3a446 | 180 | */ |
lawliet | 1:642a8dbe076c | 181 | int closeTCP(void); |
lawliet | 2:16985da3a446 | 182 | |
lawliet | 2:16985da3a446 | 183 | /** close TCP service |
lawliet | 2:16985da3a446 | 184 | * @returns |
lawliet | 2:16985da3a446 | 185 | * 0 on success |
lawliet | 2:16985da3a446 | 186 | * -1 on error |
lawliet | 2:16985da3a446 | 187 | */ |
lawliet | 1:642a8dbe076c | 188 | int shutTCP(void); |
lawliet | 2:16985da3a446 | 189 | |
lawliet | 2:16985da3a446 | 190 | /** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands |
lawliet | 2:16985da3a446 | 191 | */ |
lawliet | 1:642a8dbe076c | 192 | void serialDebug(PinName tx, PinName rx); |
lawliet | 2:16985da3a446 | 193 | |
lawliet | 1:642a8dbe076c | 194 | private: |
lawliet | 1:642a8dbe076c | 195 | Serial gprsSerial; |
lawliet | 1:642a8dbe076c | 196 | Timer timeCnt; |
lawliet | 1:642a8dbe076c | 197 | char *phoneNumber; |
lawliet | 0:a5ae94727346 | 198 | }; |
lawliet | 0:a5ae94727346 | 199 | |
lawliet | 0:a5ae94727346 | 200 | #endif |