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