-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduinoCellular.h
306 lines (252 loc) · 9.93 KB
/
ArduinoCellular.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* @file ArduinoCellular.h
* @brief Header file for the ArduinoCellular library.
*
* This library provides methods to interact with the Arduino Pro Modem, such as connecting to the network,
* sending SMS messages, getting GPS location, and more.
*/
#ifndef ARDUINO_CELLULAR_MODEM_H
#define ARDUINO_CELLULAR_MODEM_H
#if defined(ARDUINO_CELLULAR_DEBUG)
#define DUMP_AT_COMMANDS
#endif
#include <Arduino.h>
#include <vector>
#if defined __has_include
#if !__has_include (<ArduinoIoTCloud.h>)
#define ARDUINO_CELLULAR_BEARSSL
#endif
#endif
#if defined(ARDUINO_CELLULAR_BEARSSL)
#include "ArduinoBearSSLConfig.h"
#include <ArduinoBearSSL.h>
#endif
#include <ModemInterface.h>
#include <TimeUtils.h>
/**
* @enum ModemModel
* @brief Represents the model of the modem.
*/
enum ModemModel {
EC200, /**< Quectel EC200 modem. */
EG25, /**< Quectel EG25 modem. */
Unsupported /**< Unsupported modem model. */
};
/**
* Represents an SMS message.
*/
class SMS {
public:
int16_t index; /**< The index of the SMS message. */
String sender; /**< The phone number associated with the SMS. */
String message; /**< The content of the SMS message. */
Time timestamp; /**< The timestamp when the SMS was received. */
/**
* Default constructor for SMS.
* Initializes the number, message, and timestamp to empty values.
*/
SMS() {
this->index = -1;
this->sender = "";
this->message = "";
this->timestamp = Time();
}
/**
* Constructor for SMS.
* @param index The index of the SMS message.
* @param sender The phone number associated with the sender of the SMS.
* @param message The content of the SMS message.
* @param timestamp The timestamp when the SMS was received.
*/
SMS(int16_t index, String sender, String message, Time timestamp) {
this->index = index;
this->sender = sender;
this->message = message;
this->timestamp = timestamp;
}
};
/**
* @struct Geolocation
* @brief Represents a geographic location with latitude and longitude coordinates.
*/
struct Geolocation {
float latitude; /**< The latitude coordinate of the location. */
float longitude; /**< The longitude coordinate of the location. */
};
/**
* @class ArduinoCellular
*
* This class provides methods to interact with the Arduino Pro Modem, such as connecting to the network,
* sending SMS messages, getting GPS location, and more.
*/
class ArduinoCellular {
public:
/**
* @brief Creates an instance of the ArduinoCellular class.
*/
ArduinoCellular();
/**
* @brief Initializes the modem.
* This function must be called before using any other functions in the library.
*/
void begin();
/**
* @brief Unlocks the SIM card using the specified PIN.
* @param pin The SIM card PIN.
* @return True if the SIM card is unlocked, false otherwise.
*/
bool unlockSIM(String pin);
/**
* @brief Registers with the cellular network and connects to the Internet
* if the APN, GPRS username, and GPRS password are provided.
* @param apn The Access Point Name.
* @param username The APN username.
* @param password The APN password.
* @param waitForever The function does not return unless a connection has been established
* @return True if the connection is successful, false otherwise.
*/
bool connect(String apn = "", String username = "", String password = "", bool waitForever = true);
/**
* @brief same as previous, username and password are empty
*/
bool connect(String apn, bool waitForever);
/**
* @brief Checks if the modem is registered on the network.
* @return True if the network is connected, false otherwise.
*/
bool isConnectedToOperator();
/**
* @brief Checks if the GPRS network is connected.
* @return True if the GPRS network is connected, false otherwise.
*/
bool isConnectedToInternet();
/**
* @brief Enables or disables the GPS functionality.
* @param assisted True to enable assisted GPS, false to disable it. Assist GPS uses the network to get the GPS location faster, so cellular needs to be enabled.
* @return True if GPS was enabled successfully, false otherwise.
*/
bool enableGPS(bool assisted = false);
/**
* @brief Gets the GPS location. (Blocking call)
* @param timeout The timeout (In milliseconds) to wait for the GPS location.
* @return The GPS location. If the location is not retrieved, the latitude and longitude will be 0.0.
*/
Geolocation getGPSLocation(unsigned long timeout = 60000);
/**
* @brief Gets the current time from the network.
* @return The current time.
*/
Time getCellularTime();
/**
* @brief Gets the current time from the GPS module.
* @return The current time.
*/
Time getGPSTime();
/**
* @brief Sends an SMS message to the specified number.
* @param number The phone number to send the SMS to.
* @param message The message to send.
*/
void sendSMS(String number, String message);
/**
* @brief Gets the list of read SMS messages.
* @return A vector of SMS messages.
*/
std::vector<SMS> getReadSMS();
/**
* @brief Gets the list of unread SMS messages.
* @return A vector of SMS messages.
*/
std::vector<SMS> getUnreadSMS();
/**
* @brief Deletes an SMS message at the specified index.
*
* @param index The index of the SMS message to delete.
* @return True if the SMS message was successfully deleted, false otherwise.
*/
bool deleteSMS(uint16_t index);
/**
* @brief Sends an AT command to the modem and waits for a response, then returns the response.
* @param command The AT command to send.
* @param timeout The timeout (In milliseconds) to wait for the response. Default is 1000ms.
* @return The response from the modem.
*/
String sendATCommand(const char * command, unsigned long timeout = 1000);
/**
* @brief Sends a USSD command to the network operator and waits for a response.
* @param command The USSD command to send.
* @return The response from the network operator. (Note: The response may be an SMS message or a USSD response)
*/
String sendUSSDCommand(const char * command);
/**
* @brief Gets the Network client. (OSI Layer 3)
* @return The GSM client.
*/
TinyGsmClient getNetworkClient();
/**
* @brief Gets the Transport Layer Security (TLS) client. (OSI Layer 4)
* @return The GSM client.
*/
#if defined(ARDUINO_CELLULAR_BEARSSL)
BearSSLClient getSecureNetworkClient();
#endif
/**
* @brief Gets the HTTP client for the specified server and port.
* @param server The server address.
* @param port The server port.
* @return The HTTP client.
*/
HttpClient getHTTPClient(const char * server, const int port);
/**
* @brief Gets the HTTPS client for the specified server and port.
* @param server The server address.
* @param port The server port.
* @return The HTTPS client.
*/
HttpClient getHTTPSClient(const char * server, const int port);
/**
* @brief Gets the local IP address.
* @return The local IP address.
*/
IPAddress getIPAddress();
/**
* @brief Gets the signal quality.
* @return The signal quality.
*/
int getSignalQuality();
/**
* @brief Sets the debug stream for ArduinoCellular.
*
* This function allows you to set the debug stream for ArduinoCellular.
* The debug stream is used to output debug messages and information.
*
* @param stream A pointer to the Stream object that will be used as the debug stream.
*/
void setDebugStream(Stream& stream);
/**
* @brief Gets the SIM card status.
* @return The SIM card status.
*/
SimStatus getSimStatus();
private:
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);
/**
* @brief Waits for network registration. (Blocking call)
* @param waitForever if true the function does not return until a connection has been established
* @return True if the network registration is successful, false otherwise.
*/
bool awaitNetworkRegistration(bool waitForever);
/**
* @brief Gets the GPS location. (Blocking call)
* @param latitude Pointer to store the latitude. (0.0 if not retrieved)
* @param longitude Pointer to store the longitude. (0.0 if not retrieved)
* @param timeout The timeout (In milliseconds) to wait for the GPS location.
*/
void getGPSLocation(float* latitude, float* longitude, unsigned long timeout = 60000);
TinyGsmClient client; /**< The GSM client. */
ModemModel model; /**< The modem model. */
Stream* debugStream = nullptr; /**< The stream to be used for printing debugging messages. */
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */
};
#endif