-
Notifications
You must be signed in to change notification settings - Fork 20
/
GPRS_Shield_Arduino.h
288 lines (241 loc) · 8.62 KB
/
GPRS_Shield_Arduino.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* GPRS_Shield_Arduino.h
* A library for SeeedStudio seeeduino GPRS shield
*
* Copyright (c) 2015 seeed technology inc.
* Website : www.seeed.cc
* Author : lawliet zou
* Create Time: April 2015
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __GPRS_SHIELD_ARDUINO_H__
#define __GPRS_SHIELD_ARDUINO_H__
#include "sim900.h"
/** GPRS class.
* used to realize GPRS communication
*/
enum Protocol {
CLOSED = 0,
TCP = 1,
UDP = 2,
};
class GPRS
{
public:
/** Create GPRS instance
*/
GPRS(Stream& serial, uint8_t pkPin = 2, uint8_t stPin = 3);
/** get instance of GPRS class
*/
static GPRS* getInstance() {
return inst;
};
/** initialize GPRS module including SIM card check & signal strength
* @return true if connected, false otherwise
*/
bool init(void);
/** check if GPRS module is powered on or not
* @returns
* true on success
* false on error
*/
bool checkPowerUp(void);
/** power Up GPRS module (JP has to be soldered)
* @param pin pin 9 connected to JP jumper so we can power up and down through software
* @returns
*
*/
void powerOn();
void powerOff();
/** send text SMS
* @param *number phone number which SMS will be send to
* @param *data message that will be send to
* @returns
* false on success
* true on error
*/
bool sendSMS(char* number, char* data);
/** Check if there is any UNREAD SMS: this function DOESN'T change the UNREAD status of the SMS
* @returns
* 1..20 on success, position/index where SMS is stored, suitable for the function ReadSMS
* -1 on error
* 0 - there is no SMS with specified status (UNREAD)
*/
char isSMSunread();
/** read SMS, phone and date if getting a SMS message. It changes SMS status to READ
* @param messageIndex SIM position to read
* @param message buffer used to get SMS message
* @param length length of message buffer
* @param phone buffer used to get SMS's sender phone number
* @param datetime buffer used to get SMS's send datetime
* @returns
* true on success
* false on error
*/
void readSMS(char *message, char *phone, char *datetime);
/** read SMS if getting a SMS message
* @param buffer buffer that get from GPRS module(when getting a SMS, GPRS module will return a buffer array)
* @param message buffer used to get SMS message
* @param check whether to check phone number(we may only want to read SMS from specified phone number)
* @returns
* true on success
* false on error
*/
bool readSMS(int messageIndex, char *message, int length);
/** delete SMS message on SIM card
* @param index the index number which SMS message will be delete
* @returns
* true on success
* false on error
*/
void readSMS();
bool deleteSMS(int index);
/** call someone
* @param number the phone number which you want to call
* @returns
* true on success
* false on error
*/
bool callUp(char* number);
/** auto answer if coming a call
* @returns
*/
void answer(void);
/** hang up if coming a call
* @returns
* true on success
* false on error
*/
bool hangup(void);
/** Disable +CLIP notification when an incoming call is active, RING text is always shown. See isCallActive function
* This is done in order no to overload serial outputCheck if there is a call active and get the phone number in that case
* @returns
* true on success
* false on error
*/
bool ifcallNow(void);
bool ifSMSNow(void);
bool ifcallEnd(void);
void callEnd(void);
bool disableCLIPring(void);
/** Check if there is a call active and get the phone number in that case
* @returns
* true on success
* false on error
*/
bool isCallActive(char *number);
/** get DateTime from SIM900 (see AT command: AT+CLTS=1) as string
* @param
* @returns
* true on success
* false on error
*/
bool getDateTime(char *buffer);
/** getSignalStrength from SIM900 (see AT command: AT+CSQ)
* @returns
0 — 113 dBm or less
1 — 111 dBm
2...30 — 109... 53 dBm
31 — 51 dBm or greater
99 — not known or not detectable
*/
byte getSignalStrength();
//////////////////////////////////////////////////////
/// GPRS
//////////////////////////////////////////////////////
/** Connect the GPRS module to the network.
* @return true if connected, false otherwise
*/
// bool join(const __FlashStringHelper *apn = 0, const __FlashStringHelper *userName = 0, const __FlashStringHelper *passWord = 0);
bool join(char* apn = 0, char* = 0, char* = 0, int timeout = 2 * DEFAULT_TIMEOUT);
/** Disconnect the GPRS module from the network
* @returns
*/
void disconnect(void);
/** Open a tcp/udp connection with the specified host on the specified port
* @param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6]
* @param ptl protocol for socket, TCP/UDP can be choosen
* @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
* @param port port
* @param timeout wait seconds till connected
* @returns true if successful
*/
bool connect(Protocol ptl, const char * host, int port, int timeout = 2 * DEFAULT_TIMEOUT);
bool connect(Protocol ptl, const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout = 2 * DEFAULT_TIMEOUT);
/** Check if a tcp link is active
* @returns true if successful
*/
bool is_connected(void);
/** Close a tcp connection
* @returns true if successful
*/
bool close(void);
/** check if GPRS module is readable or not
* @returns true if readable
*/
int readable(void);
/** wait a few time to check if GPRS module is readable or not
* @param socket socket
* @param wait_time time of waiting
*/
int wait_readable(int wait_time);
/** wait a few time to check if GPRS module is writeable or not
* @param socket socket
* @param wait_time time of waiting
*/
int wait_writeable(int req_size);
int send(const char * str);
/** send data to socket
* @param socket socket
* @param str string to be sent
* @param len string length
* @returns return bytes that actually been send
*/
int send(const char * str, int len);
/** read data from socket
* @param socket socket
* @param buf buffer that will store the data read from socket
* @param len string length need to read from socket
* @returns bytes that actually read
*/
int recv(char* buf, int len);
/** convert the host to ip
* @param host host ip string, ex. 10.11.12.13
* @param ip long int ip address, ex. 0x11223344
* @returns true if successful
*/
//NOT USED bool gethostbyname(const char* host, uint32_t* ip);
char* getIPAddress();
unsigned long getIPnumber();
private:
bool checkSIMStatus(void);
uint8_t _stPin = 3;
uint8_t _pkPin = 2;
uint32_t str_to_ip(const char* str);
// SoftwareSerial gprsSerial;
Stream* stream;
static GPRS* inst;
uint32_t _ip;
char ip_string[16]; //XXX.YYY.ZZZ.WWW + \0
};
#endif