rhr
gprs.h@2:16985da3a446, 2013-11-18 (annotated)
- Committer:
- lawliet
- Date:
- Mon Nov 18 06:05:17 2013 +0000
- Revision:
- 2:16985da3a446
- Parent:
- 1:642a8dbe076c
- Child:
- 3:48ee24a4b0f3
Version 0.3?add document note for GPRS class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:a5ae94727346 | 1 | /* |
lawliet | 0:a5ae94727346 | 2 | IOT_Mbed.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 | 0:a5ae94727346 | 26 | #include "mbed.h" |
lawliet | 2:16985da3a446 | 27 | /** GPRS class. |
lawliet | 2:16985da3a446 | 28 | * Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol |
lawliet | 2:16985da3a446 | 29 | */ |
lawliet | 2:16985da3a446 | 30 | class GPRS |
lawliet | 1:642a8dbe076c | 31 | { |
lawliet | 1:642a8dbe076c | 32 | public: |
lawliet | 2:16985da3a446 | 33 | /** Create GPRS instance |
lawliet | 2:16985da3a446 | 34 | * @param tx uart transmit pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 35 | * @param rx uart receive pin to communicate with GPRS module |
lawliet | 2:16985da3a446 | 36 | * @param baudRate baud rate of uart communication |
lawliet | 2:16985da3a446 | 37 | * @param number default phone number during mobile communication |
lawliet | 2:16985da3a446 | 38 | */ |
lawliet | 2:16985da3a446 | 39 | GPRS(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) { |
lawliet | 2:16985da3a446 | 40 | gprsSerial.baud(baudRate); |
lawliet | 2:16985da3a446 | 41 | phoneNumber = number; |
lawliet | 1:642a8dbe076c | 42 | }; |
lawliet | 2:16985da3a446 | 43 | |
lawliet | 2:16985da3a446 | 44 | /** init GPRS module including SIM card check & signal strength & network check |
lawliet | 2:16985da3a446 | 45 | * @returns |
lawliet | 2:16985da3a446 | 46 | * 0 on success |
lawliet | 2:16985da3a446 | 47 | * -1 on error |
lawliet | 2:16985da3a446 | 48 | */ |
lawliet | 1:642a8dbe076c | 49 | int init(void); |
lawliet | 2:16985da3a446 | 50 | |
lawliet | 2:16985da3a446 | 51 | /** read from GPRS module and save to buffer array |
lawliet | 2:16985da3a446 | 52 | * @param *buffer buffer array to save what read from GPRS module |
lawliet | 2:16985da3a446 | 53 | * @param *count the maximal bytes number read from GPRS module |
lawliet | 2:16985da3a446 | 54 | * @returns |
lawliet | 2:16985da3a446 | 55 | * 0 on success |
lawliet | 2:16985da3a446 | 56 | * -1 on error |
lawliet | 2:16985da3a446 | 57 | */ |
lawliet | 1:642a8dbe076c | 58 | int readBuffer(char *buffer,int count); |
lawliet | 2:16985da3a446 | 59 | |
lawliet | 2:16985da3a446 | 60 | /** send AT command to GPRS module |
lawliet | 2:16985da3a446 | 61 | * @param *cmd command array which will be send to GPRS module |
lawliet | 2:16985da3a446 | 62 | */ |
lawliet | 1:642a8dbe076c | 63 | void sendCmd(char *cmd); |
lawliet | 2:16985da3a446 | 64 | |
lawliet | 2:16985da3a446 | 65 | /** check GPRS module response before timeout |
lawliet | 2:16985da3a446 | 66 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 67 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 68 | * @returns |
lawliet | 2:16985da3a446 | 69 | * 0 on success |
lawliet | 2:16985da3a446 | 70 | * -1 on error |
lawliet | 2:16985da3a446 | 71 | */ |
lawliet | 1:642a8dbe076c | 72 | int waitForResp(char *resp, int timeout); |
lawliet | 2:16985da3a446 | 73 | |
lawliet | 2:16985da3a446 | 74 | /** send AT command to GPRS module and wait for correct response |
lawliet | 2:16985da3a446 | 75 | * @param *cmd AT command which will be send to GPRS module |
lawliet | 2:16985da3a446 | 76 | * @param *resp correct response which GPRS module will return |
lawliet | 2:16985da3a446 | 77 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 78 | * @returns |
lawliet | 2:16985da3a446 | 79 | * 0 on success |
lawliet | 2:16985da3a446 | 80 | * -1 on error |
lawliet | 2:16985da3a446 | 81 | */ |
lawliet | 1:642a8dbe076c | 82 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout); |
lawliet | 2:16985da3a446 | 83 | |
lawliet | 2:16985da3a446 | 84 | /** check SIM card' Status |
lawliet | 2:16985da3a446 | 85 | * @returns |
lawliet | 2:16985da3a446 | 86 | * 0 on success |
lawliet | 2:16985da3a446 | 87 | * -1 on error |
lawliet | 2:16985da3a446 | 88 | */ |
lawliet | 1:642a8dbe076c | 89 | int checkSIMStatus(void); |
lawliet | 2:16985da3a446 | 90 | |
lawliet | 2:16985da3a446 | 91 | /** check signal strength |
lawliet | 2:16985da3a446 | 92 | * @returns |
lawliet | 2:16985da3a446 | 93 | * signal strength in number(ex 3,4,5,6,7,8...) on success |
lawliet | 2:16985da3a446 | 94 | * -1 on error |
lawliet | 2:16985da3a446 | 95 | */ |
lawliet | 1:642a8dbe076c | 96 | int checkSignalStrength(void); |
lawliet | 2:16985da3a446 | 97 | |
lawliet | 2:16985da3a446 | 98 | /** check network is ok or not |
lawliet | 2:16985da3a446 | 99 | * @returns |
lawliet | 2:16985da3a446 | 100 | * 0 on success |
lawliet | 2:16985da3a446 | 101 | * -1 on error |
lawliet | 2:16985da3a446 | 102 | */ |
lawliet | 1:642a8dbe076c | 103 | int networkInit(void); |
lawliet | 2:16985da3a446 | 104 | |
lawliet | 2:16985da3a446 | 105 | /** send text SMS |
lawliet | 2:16985da3a446 | 106 | * @param *number phone number which SMS will be send to |
lawliet | 2:16985da3a446 | 107 | * @param *data message that will be send to |
lawliet | 2:16985da3a446 | 108 | * @returns |
lawliet | 2:16985da3a446 | 109 | * 0 on success |
lawliet | 2:16985da3a446 | 110 | * -1 on error |
lawliet | 2:16985da3a446 | 111 | */ |
lawliet | 1:642a8dbe076c | 112 | int sendSMS(char *number, char *data); |
lawliet | 2:16985da3a446 | 113 | |
lawliet | 2:16985da3a446 | 114 | /** read SMS if get a SMS |
lawliet | 2:16985da3a446 | 115 | * @param *buffer buffer that get from GPRS module(when getting a SMS, GPRS module will return a buffer array) |
lawliet | 2:16985da3a446 | 116 | * @param *message buffer used to get SMS message |
lawliet | 2:16985da3a446 | 117 | * @param check whether to check phone number(we may only want to read SMS from specified phone number) |
lawliet | 2:16985da3a446 | 118 | * @returns |
lawliet | 2:16985da3a446 | 119 | * 0 on success |
lawliet | 2:16985da3a446 | 120 | * -1 on error |
lawliet | 2:16985da3a446 | 121 | */ |
lawliet | 1:642a8dbe076c | 122 | int readSMS(char *buffer, char *message, bool check); |
lawliet | 2:16985da3a446 | 123 | |
lawliet | 2:16985da3a446 | 124 | /** delete SMS message on SIM card |
lawliet | 2:16985da3a446 | 125 | * @param *index the index number which SMS message will be delete |
lawliet | 2:16985da3a446 | 126 | * @returns |
lawliet | 2:16985da3a446 | 127 | * 0 on success |
lawliet | 2:16985da3a446 | 128 | * -1 on error |
lawliet | 2:16985da3a446 | 129 | */ |
lawliet | 1:642a8dbe076c | 130 | int deleteSMS(int index); |
lawliet | 2:16985da3a446 | 131 | |
lawliet | 2:16985da3a446 | 132 | /** call someone |
lawliet | 2:16985da3a446 | 133 | * @param *number the phone number which you want to call |
lawliet | 2:16985da3a446 | 134 | * @returns |
lawliet | 2:16985da3a446 | 135 | * 0 on success |
lawliet | 2:16985da3a446 | 136 | * -1 on error |
lawliet | 2:16985da3a446 | 137 | */ |
lawliet | 1:642a8dbe076c | 138 | int callUp(char *number); |
lawliet | 2:16985da3a446 | 139 | |
lawliet | 2:16985da3a446 | 140 | /** auto answer if coming a call |
lawliet | 2:16985da3a446 | 141 | * @returns |
lawliet | 2:16985da3a446 | 142 | * 0 on success |
lawliet | 2:16985da3a446 | 143 | * -1 on error |
lawliet | 2:16985da3a446 | 144 | */ |
lawliet | 1:642a8dbe076c | 145 | int answer(void); |
lawliet | 2:16985da3a446 | 146 | |
lawliet | 2:16985da3a446 | 147 | /** 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 | 148 | * @param *check whether to check phone number when get event |
lawliet | 2:16985da3a446 | 149 | * @returns |
lawliet | 2:16985da3a446 | 150 | * 0 on success |
lawliet | 2:16985da3a446 | 151 | * -1 on error |
lawliet | 2:16985da3a446 | 152 | */ |
lawliet | 1:642a8dbe076c | 153 | int loop(bool check); |
lawliet | 2:16985da3a446 | 154 | |
lawliet | 2:16985da3a446 | 155 | /** build TCP connect |
lawliet | 2:16985da3a446 | 156 | * @param *ip ip address which will connect to |
lawliet | 2:16985da3a446 | 157 | * @param *port TCP server' port number |
lawliet | 2:16985da3a446 | 158 | * @returns |
lawliet | 2:16985da3a446 | 159 | * 0 on success |
lawliet | 2:16985da3a446 | 160 | * -1 on error |
lawliet | 2:16985da3a446 | 161 | */ |
lawliet | 1:642a8dbe076c | 162 | int connectTCP(char *ip, char *port); |
lawliet | 2:16985da3a446 | 163 | |
lawliet | 2:16985da3a446 | 164 | /** send data to TCP server |
lawliet | 2:16985da3a446 | 165 | * @param *data data that will be send to TCP server |
lawliet | 2:16985da3a446 | 166 | * @returns |
lawliet | 2:16985da3a446 | 167 | * 0 on success |
lawliet | 2:16985da3a446 | 168 | * -1 on error |
lawliet | 2:16985da3a446 | 169 | */ |
lawliet | 1:642a8dbe076c | 170 | int sendTCPData(char *data); |
lawliet | 2:16985da3a446 | 171 | |
lawliet | 2:16985da3a446 | 172 | /** close TCP connection |
lawliet | 2:16985da3a446 | 173 | * @returns |
lawliet | 2:16985da3a446 | 174 | * 0 on success |
lawliet | 2:16985da3a446 | 175 | * -1 on error |
lawliet | 2:16985da3a446 | 176 | */ |
lawliet | 1:642a8dbe076c | 177 | int closeTCP(void); |
lawliet | 2:16985da3a446 | 178 | |
lawliet | 2:16985da3a446 | 179 | /** close TCP service |
lawliet | 2:16985da3a446 | 180 | * @returns |
lawliet | 2:16985da3a446 | 181 | * 0 on success |
lawliet | 2:16985da3a446 | 182 | * -1 on error |
lawliet | 2:16985da3a446 | 183 | */ |
lawliet | 1:642a8dbe076c | 184 | int shutTCP(void); |
lawliet | 2:16985da3a446 | 185 | |
lawliet | 2:16985da3a446 | 186 | /** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands |
lawliet | 2:16985da3a446 | 187 | */ |
lawliet | 1:642a8dbe076c | 188 | void serialDebug(PinName tx, PinName rx); |
lawliet | 2:16985da3a446 | 189 | |
lawliet | 1:642a8dbe076c | 190 | private: |
lawliet | 1:642a8dbe076c | 191 | Serial gprsSerial; |
lawliet | 1:642a8dbe076c | 192 | Timer timeCnt; |
lawliet | 1:642a8dbe076c | 193 | char *phoneNumber; |
lawliet | 0:a5ae94727346 | 194 | }; |
lawliet | 0:a5ae94727346 | 195 | |
lawliet | 0:a5ae94727346 | 196 | #endif |