ghrh
GSM.h@14:b8ec670dfcf4, 2020-10-05 (annotated)
- Committer:
- Michelbylette
- Date:
- Mon Oct 05 09:27:27 2020 +0000
- Revision:
- 14:b8ec670dfcf4
- Parent:
- 13:d05bf1e0237e
gg;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:a5ae94727346 | 1 | /* |
screamer | 10:24671d8aa0c9 | 2 | GSM.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 | |
screamer | 10:24671d8aa0c9 | 23 | #ifndef __GSM_H__ |
screamer | 10:24671d8aa0c9 | 24 | #define __GSM_H__ |
lawliet | 0:a5ae94727346 | 25 | |
lawliet | 3:48ee24a4b0f3 | 26 | #include <stdio.h> |
lawliet | 0:a5ae94727346 | 27 | #include "mbed.h" |
lawliet | 3:48ee24a4b0f3 | 28 | |
lawliet | 7:1278e9dde24e | 29 | #define DEFAULT_TIMEOUT 5 |
lawliet | 7:1278e9dde24e | 30 | #define SMS_MAX_LENGTH 16 |
lawliet | 7:1278e9dde24e | 31 | |
lawliet | 4:63a2619b423b | 32 | |
screamer | 10:24671d8aa0c9 | 33 | enum GSM_MESSAGE { |
lawliet | 4:63a2619b423b | 34 | MESSAGE_RING = 0, |
lawliet | 4:63a2619b423b | 35 | MESSAGE_SMS = 1, |
lawliet | 4:63a2619b423b | 36 | MESSAGE_ERROR |
lawliet | 4:63a2619b423b | 37 | }; |
lawliet | 4:63a2619b423b | 38 | |
screamer | 10:24671d8aa0c9 | 39 | /** GSM class. |
screamer | 10:24671d8aa0c9 | 40 | * Used for mobile communication. attention that GSM module communicate with MCU in serial protocol |
lawliet | 2:16985da3a446 | 41 | */ |
screamer | 10:24671d8aa0c9 | 42 | class GSM |
lawliet | 1:642a8dbe076c | 43 | { |
lawliet | 1:642a8dbe076c | 44 | public: |
screamer | 10:24671d8aa0c9 | 45 | /** Create GSM instance |
Michelbylette | 14:b8ec670dfcf4 | 46 | * @param tx uart transmit pin to communicate with GSM module |
Michelbylette | 14:b8ec670dfcf4 | 47 | * @param rx uart receive pin to communicate with GSM module |
lawliet | 2:16985da3a446 | 48 | * @param baudRate baud rate of uart communication |
lawliet | 2:16985da3a446 | 49 | * @param number default phone number during mobile communication |
Michelbylette | 14:b8ec670dfcf4 | 50 | * @param[out] PWK_pin power key pin to turn the module on |
Michelbylette | 14:b8ec670dfcf4 | 51 | * @param[in] Status_pin status pin to hardware check power status |
Michelbylette | 14:b8ec670dfcf4 | 52 | * @param[in] Sonnette_pin ring indicator to detect incoming messages or calls |
lawliet | 2:16985da3a446 | 53 | */ |
Michelbylette | 11:c93524a12e92 | 54 | GSM(PinName tx, PinName rx, int baudRate, char *number, PinName PWK_pin = NC, PinName Status_pin = NC, PinName Sonnette_pin = NC) : PWK(PWK_pin), Status(Status_pin), Sonnette(Sonnette_pin), gprsSerial(tx, rx) { |
lawliet | 7:1278e9dde24e | 55 | //gprsSerial.baud(baudRate); |
lawliet | 2:16985da3a446 | 56 | phoneNumber = number; |
Michelbylette | 11:c93524a12e92 | 57 | //_reset(reset) |
lawliet | 1:642a8dbe076c | 58 | }; |
lawliet | 7:1278e9dde24e | 59 | |
Michelbylette | 11:c93524a12e92 | 60 | /** Unlock the SIM card |
Michelbylette | 11:c93524a12e92 | 61 | * @param PIN PIN code to unlock the SIM card |
Michelbylette | 11:c93524a12e92 | 62 | * @returns |
Michelbylette | 11:c93524a12e92 | 63 | * 0 on success, |
Michelbylette | 11:c93524a12e92 | 64 | * -1 on error |
Michelbylette | 11:c93524a12e92 | 65 | */ |
Michelbylette | 11:c93524a12e92 | 66 | int unlock(char *PIN); |
Michelbylette | 11:c93524a12e92 | 67 | |
Michelbylette | 11:c93524a12e92 | 68 | /** Power the GSM module |
Michelbylette | 11:c93524a12e92 | 69 | * @returns |
Michelbylette | 11:c93524a12e92 | 70 | * 0 on success |
Michelbylette | 11:c93524a12e92 | 71 | * -1 on error |
Michelbylette | 11:c93524a12e92 | 72 | * |
Michelbylette | 11:c93524a12e92 | 73 | */ |
Michelbylette | 11:c93524a12e92 | 74 | int powerOn(void); |
Michelbylette | 11:c93524a12e92 | 75 | |
Michelbylette | 11:c93524a12e92 | 76 | /** Turn the GSM module off |
Michelbylette | 11:c93524a12e92 | 77 | * @returns |
Michelbylette | 11:c93524a12e92 | 78 | * 0 on success |
Michelbylette | 11:c93524a12e92 | 79 | * -1 on error |
Michelbylette | 11:c93524a12e92 | 80 | */ |
Michelbylette | 11:c93524a12e92 | 81 | int powerOff(void); |
Michelbylette | 11:c93524a12e92 | 82 | |
Michelbylette | 11:c93524a12e92 | 83 | /** Check via an AT command if the module is turned on |
Michelbylette | 11:c93524a12e92 | 84 | * @returns |
Michelbylette | 11:c93524a12e92 | 85 | * 0 : module is on |
Michelbylette | 11:c93524a12e92 | 86 | * -1 : the module is off |
Michelbylette | 11:c93524a12e92 | 87 | */ |
lawliet | 7:1278e9dde24e | 88 | int powerCheck(void); |
Michelbylette | 11:c93524a12e92 | 89 | |
screamer | 10:24671d8aa0c9 | 90 | /** init GSM module including SIM card check & signal strength & network check |
lawliet | 2:16985da3a446 | 91 | * @returns |
screamer | 8:1ee8ba65061a | 92 | * 0 on success, |
lawliet | 2:16985da3a446 | 93 | * -1 on error |
lawliet | 2:16985da3a446 | 94 | */ |
lawliet | 1:642a8dbe076c | 95 | int init(void); |
lawliet | 2:16985da3a446 | 96 | |
Michelbylette | 11:c93524a12e92 | 97 | /** Check SIM card's Status |
lawliet | 4:63a2619b423b | 98 | * @returns |
screamer | 8:1ee8ba65061a | 99 | * 0 on success, |
lawliet | 4:63a2619b423b | 100 | * -1 on error |
lawliet | 4:63a2619b423b | 101 | */ |
lawliet | 4:63a2619b423b | 102 | int checkSIMStatus(void); |
lawliet | 4:63a2619b423b | 103 | |
screamer | 8:1ee8ba65061a | 104 | /** Check signal strength |
lawliet | 4:63a2619b423b | 105 | * @returns |
screamer | 8:1ee8ba65061a | 106 | * signal strength in number(ex 3,4,5,6,7,8...) on success, |
lawliet | 4:63a2619b423b | 107 | * -1 on error |
lawliet | 4:63a2619b423b | 108 | */ |
lawliet | 4:63a2619b423b | 109 | int checkSignalStrength(void); |
lawliet | 4:63a2619b423b | 110 | |
screamer | 8:1ee8ba65061a | 111 | /** Set SMS format and processing mode |
lawliet | 4:63a2619b423b | 112 | * @returns |
screamer | 8:1ee8ba65061a | 113 | * 0 on success, |
lawliet | 4:63a2619b423b | 114 | * -1 on error |
lawliet | 4:63a2619b423b | 115 | */ |
lawliet | 4:63a2619b423b | 116 | int settingSMS(void); |
lawliet | 4:63a2619b423b | 117 | |
screamer | 8:1ee8ba65061a | 118 | /** Send text SMS |
lawliet | 4:63a2619b423b | 119 | * @param *number phone number which SMS will be send to |
lawliet | 4:63a2619b423b | 120 | * @param *data message that will be send to |
lawliet | 4:63a2619b423b | 121 | * @returns |
screamer | 8:1ee8ba65061a | 122 | * 0 on success, |
lawliet | 4:63a2619b423b | 123 | * -1 on error |
lawliet | 4:63a2619b423b | 124 | */ |
lawliet | 4:63a2619b423b | 125 | int sendSMS(char *number, char *data); |
lawliet | 4:63a2619b423b | 126 | |
screamer | 8:1ee8ba65061a | 127 | /** Read SMS by index |
lawliet | 4:63a2619b423b | 128 | * @param *message buffer used to get SMS message |
screamer | 8:1ee8ba65061a | 129 | * @param index which SMS message to read |
lawliet | 4:63a2619b423b | 130 | * @returns |
screamer | 8:1ee8ba65061a | 131 | * 0 on success, |
lawliet | 4:63a2619b423b | 132 | * -1 on error |
lawliet | 4:63a2619b423b | 133 | */ |
lawliet | 4:63a2619b423b | 134 | int readSMS(char *message, int index); |
lawliet | 4:63a2619b423b | 135 | |
screamer | 8:1ee8ba65061a | 136 | /** Delete SMS message on SIM card |
lawliet | 4:63a2619b423b | 137 | * @param *index the index number which SMS message will be delete |
lawliet | 4:63a2619b423b | 138 | * @returns |
screamer | 8:1ee8ba65061a | 139 | * 0 on success, |
lawliet | 4:63a2619b423b | 140 | * -1 on error |
lawliet | 4:63a2619b423b | 141 | */ |
lawliet | 4:63a2619b423b | 142 | int deleteSMS(int index); |
lawliet | 4:63a2619b423b | 143 | |
screamer | 8:1ee8ba65061a | 144 | /** Read SMS when coming a message,it will be store in messageBuffer. |
lawliet | 4:63a2619b423b | 145 | * @param message buffer used to get SMS message |
lawliet | 4:63a2619b423b | 146 | */ |
lawliet | 4:63a2619b423b | 147 | int getSMS(char* message); |
lawliet | 4:63a2619b423b | 148 | |
screamer | 8:1ee8ba65061a | 149 | /** Call someone |
lawliet | 4:63a2619b423b | 150 | * @param *number the phone number which you want to call |
lawliet | 4:63a2619b423b | 151 | * @returns |
screamer | 8:1ee8ba65061a | 152 | * 0 on success, |
lawliet | 4:63a2619b423b | 153 | * -1 on error |
lawliet | 4:63a2619b423b | 154 | */ |
lawliet | 4:63a2619b423b | 155 | int callUp(char *number); |
lawliet | 4:63a2619b423b | 156 | |
screamer | 8:1ee8ba65061a | 157 | /** Auto answer if coming a call |
lawliet | 4:63a2619b423b | 158 | * @returns |
screamer | 8:1ee8ba65061a | 159 | * 0 on success, |
lawliet | 4:63a2619b423b | 160 | * -1 on error |
lawliet | 4:63a2619b423b | 161 | */ |
lawliet | 4:63a2619b423b | 162 | int answer(void); |
lawliet | 4:63a2619b423b | 163 | |
screamer | 8:1ee8ba65061a | 164 | /** 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 | 165 | * @param *check whether to check phone number when get event |
lawliet | 4:63a2619b423b | 166 | * @returns |
screamer | 8:1ee8ba65061a | 167 | * 0 on success, |
lawliet | 4:63a2619b423b | 168 | * -1 on error |
lawliet | 4:63a2619b423b | 169 | */ |
lawliet | 4:63a2619b423b | 170 | int loopHandle(void); |
Michelbylette | 11:c93524a12e92 | 171 | |
screamer | 10:24671d8aa0c9 | 172 | /** GSM network init |
lawliet | 5:ac2342f162fa | 173 | * @param *apn Access Point Name to connect network |
lawliet | 5:ac2342f162fa | 174 | * @param *userName general is empty |
lawliet | 5:ac2342f162fa | 175 | * @param *passWord general is empty |
lawliet | 5:ac2342f162fa | 176 | */ |
lawliet | 5:ac2342f162fa | 177 | int networkInit(char* apn, char* userName = NULL, char* passWord = NULL); |
Michelbylette | 11:c93524a12e92 | 178 | |
screamer | 8:1ee8ba65061a | 179 | /** Build TCP connect |
lawliet | 4:63a2619b423b | 180 | * @param *ip ip address which will connect to |
lawliet | 4:63a2619b423b | 181 | * @param *port TCP server' port number |
lawliet | 4:63a2619b423b | 182 | * @returns |
screamer | 8:1ee8ba65061a | 183 | * 0 on success, |
lawliet | 4:63a2619b423b | 184 | * -1 on error |
lawliet | 4:63a2619b423b | 185 | */ |
lawliet | 4:63a2619b423b | 186 | int connectTCP(char *ip, char *port); |
lawliet | 4:63a2619b423b | 187 | |
screamer | 8:1ee8ba65061a | 188 | /** Send data to TCP server |
lawliet | 4:63a2619b423b | 189 | * @param *data data that will be send to TCP server |
lawliet | 4:63a2619b423b | 190 | * @returns |
screamer | 8:1ee8ba65061a | 191 | * 0 on success, |
lawliet | 4:63a2619b423b | 192 | * -1 on error |
lawliet | 4:63a2619b423b | 193 | */ |
lawliet | 4:63a2619b423b | 194 | int sendTCPData(char *data); |
lawliet | 4:63a2619b423b | 195 | |
screamer | 8:1ee8ba65061a | 196 | /** Close TCP connection |
lawliet | 4:63a2619b423b | 197 | * @returns |
screamer | 8:1ee8ba65061a | 198 | * 0 on success, |
lawliet | 4:63a2619b423b | 199 | * -1 on error |
lawliet | 4:63a2619b423b | 200 | */ |
lawliet | 4:63a2619b423b | 201 | int closeTCP(void); |
lawliet | 4:63a2619b423b | 202 | |
screamer | 8:1ee8ba65061a | 203 | /** Close TCP service |
lawliet | 4:63a2619b423b | 204 | * @returns |
screamer | 8:1ee8ba65061a | 205 | * 0 on success, |
lawliet | 4:63a2619b423b | 206 | * -1 on error |
lawliet | 4:63a2619b423b | 207 | */ |
lawliet | 4:63a2619b423b | 208 | int shutTCP(void); |
lawliet | 4:63a2619b423b | 209 | |
Michelbylette | 11:c93524a12e92 | 210 | |
Michelbylette | 11:c93524a12e92 | 211 | DigitalOut PWK; |
Michelbylette | 11:c93524a12e92 | 212 | DigitalIn Status; |
Michelbylette | 11:c93524a12e92 | 213 | InterruptIn Sonnette; |
lawliet | 6:e508f9ee7922 | 214 | Serial gprsSerial; |
lawliet | 4:63a2619b423b | 215 | //USBSerial pc; |
screamer | 8:1ee8ba65061a | 216 | |
lawliet | 4:63a2619b423b | 217 | private: |
screamer | 10:24671d8aa0c9 | 218 | /** Read from GSM module and save to buffer array |
screamer | 10:24671d8aa0c9 | 219 | * @param *buffer buffer array to save what read from GSM module |
screamer | 10:24671d8aa0c9 | 220 | * @param *count the maximal bytes number read from GSM module |
lawliet | 2:16985da3a446 | 221 | * @returns |
screamer | 8:1ee8ba65061a | 222 | * 0 on success, |
lawliet | 2:16985da3a446 | 223 | * -1 on error |
lawliet | 2:16985da3a446 | 224 | */ |
lawliet | 1:642a8dbe076c | 225 | int readBuffer(char *buffer,int count); |
lawliet | 2:16985da3a446 | 226 | |
screamer | 10:24671d8aa0c9 | 227 | /** Send AT command to GSM module |
screamer | 10:24671d8aa0c9 | 228 | * @param *cmd command array which will be send to GSM module |
lawliet | 2:16985da3a446 | 229 | */ |
lawliet | 1:642a8dbe076c | 230 | void sendCmd(char *cmd); |
lawliet | 2:16985da3a446 | 231 | |
screamer | 10:24671d8aa0c9 | 232 | /** Check GSM module response before timeout |
screamer | 10:24671d8aa0c9 | 233 | * @param *resp correct response which GSM module will return |
lawliet | 2:16985da3a446 | 234 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 235 | * @returns |
screamer | 8:1ee8ba65061a | 236 | * 0 on success, |
lawliet | 2:16985da3a446 | 237 | * -1 on error |
lawliet | 2:16985da3a446 | 238 | */ |
Michelbylette | 11:c93524a12e92 | 239 | int waitForResp(char *resp, int timeout = DEFAULT_TIMEOUT); |
lawliet | 2:16985da3a446 | 240 | |
screamer | 10:24671d8aa0c9 | 241 | /** Send AT command to GSM module and wait for correct response |
screamer | 10:24671d8aa0c9 | 242 | * @param *cmd AT command which will be send to GSM module |
screamer | 10:24671d8aa0c9 | 243 | * @param *resp correct response which GSM module will return |
lawliet | 2:16985da3a446 | 244 | * @param *timeout waiting seconds till timeout |
lawliet | 2:16985da3a446 | 245 | * @returns |
screamer | 8:1ee8ba65061a | 246 | * 0 on success, |
lawliet | 2:16985da3a446 | 247 | * -1 on error |
lawliet | 2:16985da3a446 | 248 | */ |
Michelbylette | 11:c93524a12e92 | 249 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout = DEFAULT_TIMEOUT); |
lawliet | 2:16985da3a446 | 250 | |
lawliet | 1:642a8dbe076c | 251 | Timer timeCnt; |
lawliet | 1:642a8dbe076c | 252 | char *phoneNumber; |
lawliet | 4:63a2619b423b | 253 | char messageBuffer[SMS_MAX_LENGTH]; |
lawliet | 0:a5ae94727346 | 254 | }; |
lawliet | 0:a5ae94727346 | 255 | |
lawliet | 0:a5ae94727346 | 256 | #endif |
lawliet | 4:63a2619b423b | 257 |