Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
gprs.h@5:ac2342f162fa, 2014-01-22 (annotated)
- Committer:
 - lawliet
 - Date:
 - Wed Jan 22 02:04:57 2014 +0000
 - Revision:
 - 5:ac2342f162fa
 - Parent:
 - 4:63a2619b423b
 - Child:
 - 6:e508f9ee7922
 
version 2.1 (add network APN setting)
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 | 4:63a2619b423b | 30 | #define SMS_MAX_LENGTH 16 | 
| lawliet | 4:63a2619b423b | 31 | |
| lawliet | 4:63a2619b423b | 32 | enum GPRS_MESSAGE { | 
| lawliet | 4:63a2619b423b | 33 | MESSAGE_RING = 0, | 
| lawliet | 4:63a2619b423b | 34 | MESSAGE_SMS = 1, | 
| lawliet | 4:63a2619b423b | 35 | MESSAGE_ERROR | 
| lawliet | 4:63a2619b423b | 36 | }; | 
| lawliet | 4:63a2619b423b | 37 | |
| lawliet | 3:48ee24a4b0f3 | 38 | |
| lawliet | 2:16985da3a446 | 39 | /** GPRS class. | 
| lawliet | 2:16985da3a446 | 40 | * Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol | 
| lawliet | 2:16985da3a446 | 41 | */ | 
| lawliet | 2:16985da3a446 | 42 | class GPRS | 
| lawliet | 1:642a8dbe076c | 43 | { | 
| lawliet | 1:642a8dbe076c | 44 | public: | 
| lawliet | 2:16985da3a446 | 45 | /** Create GPRS instance | 
| lawliet | 2:16985da3a446 | 46 | * @param tx uart transmit pin to communicate with GPRS module | 
| lawliet | 2:16985da3a446 | 47 | * @param rx uart receive pin to communicate with GPRS module | 
| lawliet | 2:16985da3a446 | 48 | * @param baudRate baud rate of uart communication | 
| lawliet | 2:16985da3a446 | 49 | * @param number default phone number during mobile communication | 
| lawliet | 2:16985da3a446 | 50 | */ | 
| lawliet | 2:16985da3a446 | 51 | GPRS(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) { | 
| lawliet | 2:16985da3a446 | 52 | gprsSerial.baud(baudRate); | 
| lawliet | 2:16985da3a446 | 53 | phoneNumber = number; | 
| lawliet | 1:642a8dbe076c | 54 | }; | 
| lawliet | 2:16985da3a446 | 55 | |
| lawliet | 2:16985da3a446 | 56 | /** init GPRS module including SIM card check & signal strength & network check | 
| lawliet | 2:16985da3a446 | 57 | * @returns | 
| lawliet | 2:16985da3a446 | 58 | * 0 on success | 
| lawliet | 2:16985da3a446 | 59 | * -1 on error | 
| lawliet | 2:16985da3a446 | 60 | */ | 
| lawliet | 1:642a8dbe076c | 61 | int init(void); | 
| lawliet | 2:16985da3a446 | 62 | |
| lawliet | 4:63a2619b423b | 63 | /** check SIM card' Status | 
| lawliet | 4:63a2619b423b | 64 | * @returns | 
| lawliet | 4:63a2619b423b | 65 | * 0 on success | 
| lawliet | 4:63a2619b423b | 66 | * -1 on error | 
| lawliet | 4:63a2619b423b | 67 | */ | 
| lawliet | 4:63a2619b423b | 68 | int checkSIMStatus(void); | 
| lawliet | 4:63a2619b423b | 69 | |
| lawliet | 4:63a2619b423b | 70 | /** check signal strength | 
| lawliet | 4:63a2619b423b | 71 | * @returns | 
| lawliet | 4:63a2619b423b | 72 | * signal strength in number(ex 3,4,5,6,7,8...) on success | 
| lawliet | 4:63a2619b423b | 73 | * -1 on error | 
| lawliet | 4:63a2619b423b | 74 | */ | 
| lawliet | 4:63a2619b423b | 75 | int checkSignalStrength(void); | 
| lawliet | 4:63a2619b423b | 76 | |
| lawliet | 4:63a2619b423b | 77 | /** set SMS format and processing mode | 
| lawliet | 4:63a2619b423b | 78 | * @returns | 
| lawliet | 4:63a2619b423b | 79 | * 0 on success | 
| lawliet | 4:63a2619b423b | 80 | * -1 on error | 
| lawliet | 4:63a2619b423b | 81 | */ | 
| lawliet | 4:63a2619b423b | 82 | int settingSMS(void); | 
| lawliet | 4:63a2619b423b | 83 | |
| lawliet | 4:63a2619b423b | 84 | /** send text SMS | 
| lawliet | 4:63a2619b423b | 85 | * @param *number phone number which SMS will be send to | 
| lawliet | 4:63a2619b423b | 86 | * @param *data message that will be send to | 
| lawliet | 4:63a2619b423b | 87 | * @returns | 
| lawliet | 4:63a2619b423b | 88 | * 0 on success | 
| lawliet | 4:63a2619b423b | 89 | * -1 on error | 
| lawliet | 4:63a2619b423b | 90 | */ | 
| lawliet | 4:63a2619b423b | 91 | int sendSMS(char *number, char *data); | 
| lawliet | 4:63a2619b423b | 92 | |
| lawliet | 4:63a2619b423b | 93 | /** read SMS by index | 
| lawliet | 4:63a2619b423b | 94 | * @param *message buffer used to get SMS message | 
| lawliet | 4:63a2619b423b | 95 | * @param index which SMS message to read | 
| lawliet | 4:63a2619b423b | 96 | * @returns | 
| lawliet | 4:63a2619b423b | 97 | * 0 on success | 
| lawliet | 4:63a2619b423b | 98 | * -1 on error | 
| lawliet | 4:63a2619b423b | 99 | */ | 
| lawliet | 4:63a2619b423b | 100 | int readSMS(char *message, int index); | 
| lawliet | 4:63a2619b423b | 101 | |
| lawliet | 4:63a2619b423b | 102 | /** delete SMS message on SIM card | 
| lawliet | 4:63a2619b423b | 103 | * @param *index the index number which SMS message will be delete | 
| lawliet | 4:63a2619b423b | 104 | * @returns | 
| lawliet | 4:63a2619b423b | 105 | * 0 on success | 
| lawliet | 4:63a2619b423b | 106 | * -1 on error | 
| lawliet | 4:63a2619b423b | 107 | */ | 
| lawliet | 4:63a2619b423b | 108 | int deleteSMS(int index); | 
| lawliet | 4:63a2619b423b | 109 | |
| lawliet | 4:63a2619b423b | 110 | /** read SMS when coming a message,it will be store in messageBuffer. | 
| lawliet | 4:63a2619b423b | 111 | * @param message buffer used to get SMS message | 
| lawliet | 4:63a2619b423b | 112 | */ | 
| lawliet | 4:63a2619b423b | 113 | int getSMS(char* message); | 
| lawliet | 4:63a2619b423b | 114 | |
| lawliet | 4:63a2619b423b | 115 | /** call someone | 
| lawliet | 4:63a2619b423b | 116 | * @param *number the phone number which you want to call | 
| lawliet | 4:63a2619b423b | 117 | * @returns | 
| lawliet | 4:63a2619b423b | 118 | * 0 on success | 
| lawliet | 4:63a2619b423b | 119 | * -1 on error | 
| lawliet | 4:63a2619b423b | 120 | */ | 
| lawliet | 4:63a2619b423b | 121 | int callUp(char *number); | 
| lawliet | 4:63a2619b423b | 122 | |
| lawliet | 4:63a2619b423b | 123 | /** auto answer if coming a call | 
| lawliet | 4:63a2619b423b | 124 | * @returns | 
| lawliet | 4:63a2619b423b | 125 | * 0 on success | 
| lawliet | 4:63a2619b423b | 126 | * -1 on error | 
| lawliet | 4:63a2619b423b | 127 | */ | 
| lawliet | 4:63a2619b423b | 128 | int answer(void); | 
| lawliet | 4:63a2619b423b | 129 | |
| lawliet | 4:63a2619b423b | 130 | /** 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 | 4:63a2619b423b | 131 | * @param *check whether to check phone number when get event | 
| lawliet | 4:63a2619b423b | 132 | * @returns | 
| lawliet | 4:63a2619b423b | 133 | * 0 on success | 
| lawliet | 4:63a2619b423b | 134 | * -1 on error | 
| lawliet | 4:63a2619b423b | 135 | */ | 
| lawliet | 4:63a2619b423b | 136 | int loopHandle(void); | 
| lawliet | 4:63a2619b423b | 137 | |
| lawliet | 5:ac2342f162fa | 138 | /** gprs network init | 
| lawliet | 5:ac2342f162fa | 139 | * @param *apn Access Point Name to connect network | 
| lawliet | 5:ac2342f162fa | 140 | * @param *userName general is empty | 
| lawliet | 5:ac2342f162fa | 141 | * @param *passWord general is empty | 
| lawliet | 5:ac2342f162fa | 142 | */ | 
| lawliet | 5:ac2342f162fa | 143 | int networkInit(char* apn, char* userName = NULL, char* passWord = NULL); | 
| lawliet | 4:63a2619b423b | 144 | /** build TCP connect | 
| lawliet | 4:63a2619b423b | 145 | * @param *ip ip address which will connect to | 
| lawliet | 4:63a2619b423b | 146 | * @param *port TCP server' port number | 
| lawliet | 4:63a2619b423b | 147 | * @returns | 
| lawliet | 4:63a2619b423b | 148 | * 0 on success | 
| lawliet | 4:63a2619b423b | 149 | * -1 on error | 
| lawliet | 4:63a2619b423b | 150 | */ | 
| lawliet | 4:63a2619b423b | 151 | int connectTCP(char *ip, char *port); | 
| lawliet | 4:63a2619b423b | 152 | |
| lawliet | 4:63a2619b423b | 153 | /** send data to TCP server | 
| lawliet | 4:63a2619b423b | 154 | * @param *data data that will be send to TCP server | 
| lawliet | 4:63a2619b423b | 155 | * @returns | 
| lawliet | 4:63a2619b423b | 156 | * 0 on success | 
| lawliet | 4:63a2619b423b | 157 | * -1 on error | 
| lawliet | 4:63a2619b423b | 158 | */ | 
| lawliet | 4:63a2619b423b | 159 | int sendTCPData(char *data); | 
| lawliet | 4:63a2619b423b | 160 | |
| lawliet | 4:63a2619b423b | 161 | /** close TCP connection | 
| lawliet | 4:63a2619b423b | 162 | * @returns | 
| lawliet | 4:63a2619b423b | 163 | * 0 on success | 
| lawliet | 4:63a2619b423b | 164 | * -1 on error | 
| lawliet | 4:63a2619b423b | 165 | */ | 
| lawliet | 4:63a2619b423b | 166 | int closeTCP(void); | 
| lawliet | 4:63a2619b423b | 167 | |
| lawliet | 4:63a2619b423b | 168 | /** close TCP service | 
| lawliet | 4:63a2619b423b | 169 | * @returns | 
| lawliet | 4:63a2619b423b | 170 | * 0 on success | 
| lawliet | 4:63a2619b423b | 171 | * -1 on error | 
| lawliet | 4:63a2619b423b | 172 | */ | 
| lawliet | 4:63a2619b423b | 173 | int shutTCP(void); | 
| lawliet | 4:63a2619b423b | 174 | |
| lawliet | 4:63a2619b423b | 175 | |
| lawliet | 4:63a2619b423b | 176 | //USBSerial pc; | 
| lawliet | 4:63a2619b423b | 177 | private: | 
| lawliet | 2:16985da3a446 | 178 | /** read from GPRS module and save to buffer array | 
| lawliet | 2:16985da3a446 | 179 | * @param *buffer buffer array to save what read from GPRS module | 
| lawliet | 2:16985da3a446 | 180 | * @param *count the maximal bytes number read from GPRS module | 
| lawliet | 2:16985da3a446 | 181 | * @returns | 
| lawliet | 2:16985da3a446 | 182 | * 0 on success | 
| lawliet | 2:16985da3a446 | 183 | * -1 on error | 
| lawliet | 2:16985da3a446 | 184 | */ | 
| lawliet | 1:642a8dbe076c | 185 | int readBuffer(char *buffer,int count); | 
| lawliet | 2:16985da3a446 | 186 | |
| lawliet | 2:16985da3a446 | 187 | /** send AT command to GPRS module | 
| lawliet | 2:16985da3a446 | 188 | * @param *cmd command array which will be send to GPRS module | 
| lawliet | 2:16985da3a446 | 189 | */ | 
| lawliet | 1:642a8dbe076c | 190 | void sendCmd(char *cmd); | 
| lawliet | 2:16985da3a446 | 191 | |
| lawliet | 2:16985da3a446 | 192 | /** check GPRS module response before timeout | 
| lawliet | 2:16985da3a446 | 193 | * @param *resp correct response which GPRS module will return | 
| lawliet | 2:16985da3a446 | 194 | * @param *timeout waiting seconds till timeout | 
| lawliet | 2:16985da3a446 | 195 | * @returns | 
| lawliet | 2:16985da3a446 | 196 | * 0 on success | 
| lawliet | 2:16985da3a446 | 197 | * -1 on error | 
| lawliet | 2:16985da3a446 | 198 | */ | 
| lawliet | 1:642a8dbe076c | 199 | int waitForResp(char *resp, int timeout); | 
| lawliet | 2:16985da3a446 | 200 | |
| lawliet | 2:16985da3a446 | 201 | /** send AT command to GPRS module and wait for correct response | 
| lawliet | 2:16985da3a446 | 202 | * @param *cmd AT command which will be send to GPRS module | 
| lawliet | 2:16985da3a446 | 203 | * @param *resp correct response which GPRS module will return | 
| lawliet | 2:16985da3a446 | 204 | * @param *timeout waiting seconds till timeout | 
| lawliet | 2:16985da3a446 | 205 | * @returns | 
| lawliet | 2:16985da3a446 | 206 | * 0 on success | 
| lawliet | 2:16985da3a446 | 207 | * -1 on error | 
| lawliet | 2:16985da3a446 | 208 | */ | 
| lawliet | 1:642a8dbe076c | 209 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout); | 
| lawliet | 2:16985da3a446 | 210 | |
| lawliet | 1:642a8dbe076c | 211 | Serial gprsSerial; | 
| lawliet | 1:642a8dbe076c | 212 | Timer timeCnt; | 
| lawliet | 1:642a8dbe076c | 213 | char *phoneNumber; | 
| lawliet | 4:63a2619b423b | 214 | char messageBuffer[SMS_MAX_LENGTH]; | 
| lawliet | 0:a5ae94727346 | 215 | }; | 
| lawliet | 0:a5ae94727346 | 216 | |
| lawliet | 0:a5ae94727346 | 217 | #endif | 
| lawliet | 4:63a2619b423b | 218 |