-
Notifications
You must be signed in to change notification settings - Fork 725
/
AllFunctions.ino
524 lines (452 loc) · 15 KB
/
AllFunctions.ino
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**************************************************************
*
* TinyGSM Getting Started guide:
* https://tiny.cc/tinygsm-readme
*
* NOTE:
* Some of the functions may be unavailable for your modem.
* Just comment them out.
*
**************************************************************/
// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM868
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_SIM7000
// #define TINY_GSM_MODEM_SIM7000SSL
// #define TINY_GSM_MODEM_SIM7080
// #define TINY_GSM_MODEM_SIM5360
// #define TINY_GSM_MODEM_SIM7600
// #define TINY_GSM_MODEM_A7672X
// #define TINY_GSM_MODEM_UBLOX
// #define TINY_GSM_MODEM_SARAR4
// #define TINY_GSM_MODEM_SARAR5
// #define TINY_GSM_MODEM_M95
// #define TINY_GSM_MODEM_BG95
// #define TINY_GSM_MODEM_BG96
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_MC60
// #define TINY_GSM_MODEM_MC60E
// #define TINY_GSM_MODEM_ESP8266
// #define TINY_GSM_MODEM_ESP32
// #define TINY_GSM_MODEM_XBEE
// #define TINY_GSM_MODEM_SEQUANS_MONARCH
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#ifndef __AVR_ATmega328P__
#define SerialAT Serial1
// or Software Serial on Uno, Nano
#else
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3); // RX, TX
#endif
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// Range to attempt to autobaud
// NOTE: DO NOT AUTOBAUD in production code. Once you've established
// communication, set a fixed baud rate using modem.setBaud(#).
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 57600
// Add a reception delay, if needed.
// This may be needed for a fast processor at a slow baud rate.
// #define TINY_GSM_YIELD() { delay(2); }
/*
* Tests enabled
*/
#define TINY_GSM_TEST_GPRS true
#define TINY_GSM_TEST_WIFI false
#define TINY_GSM_TEST_TCP true
#define TINY_GSM_TEST_SSL true
#define TINY_GSM_TEST_CALL true
#define TINY_GSM_TEST_SMS true
#define TINY_GSM_TEST_USSD true
#define TINY_GSM_TEST_BATTERY true
#define TINY_GSM_TEST_TEMPERATURE true
#define TINY_GSM_TEST_GSM_LOCATION true
#define TINY_GSM_TEST_GPS true
#define TINY_GSM_TEST_NTP true
#define TINY_GSM_TEST_TIME true
// disconnect and power down modem after tests
#define TINY_GSM_POWERDOWN true
// set GSM PIN, if any
#define GSM_PIN ""
// Set phone numbers, if you want to test SMS and Calls
// #define SMS_TARGET "+380xxxxxxxxx"
// #define CALL_TARGET "+380xxxxxxxxx"
// Your GPRS credentials, if any
const char apn[] = "YourAPN";
// const char apn[] = "ibasis.iot";
const char gprsUser[] = "";
const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass";
// Server details to test TCP/SSL
const char server[] = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
#include <TinyGsmClient.h>
#if TINY_GSM_TEST_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#undef TINY_GSM_TEST_GPRS
#undef TINY_GSM_TEST_WIFI
#define TINY_GSM_TEST_GPRS false
#define TINY_GSM_TEST_WIFI true
#endif
#if TINY_GSM_TEST_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#endif
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// !!!!!!!!!!!
// Set your reset, enable, power pins here
// !!!!!!!!!!!
DBG("Wait...");
delay(6000L);
// Set GSM module baud rate
TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
// SerialAT.begin(9600);
}
void loop() {
// Restart takes quite some time
// To skip it, call init() instead of restart()
DBG("Initializing modem...");
if (!modem.restart()) {
// if (!modem.init()) {
DBG("Failed to restart modem, delaying 10s and retrying");
// restart autobaud in case GSM just rebooted
// TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
return;
}
String modemInfo = modem.getModemInfo();
DBG("Modem Info:", modemInfo);
String name = modem.getModemName();
DBG("Modem Name:", name);
String manufacturer = modem.getModemManufacturer();
DBG("Modem Manufacturer:", manufacturer);
String hw_ver = modem.getModemModel();
DBG("Modem Hardware Version:", hw_ver);
String fv_ver = modem.getModemRevision();
DBG("Modem Firware Version:", fv_ver);
#if not defined(TINY_GSM_MODEM_ESP8266) && not defined(TINY_GSM_MODEM_ESP32)
String mod_sn = modem.getModemSerialNumber();
DBG("Modem Serial Number (may be SIM CCID):", mod_sn);
#endif
#if TINY_GSM_TEST_GPRS
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
#endif
#if TINY_GSM_TEST_WIFI && defined(TINY_GSM_MODEM_HAS_WIFI)
DBG("Setting SSID/password...");
if (!modem.networkConnect(wifiSSID, wifiPass)) {
DBG(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
#endif
#if TINY_GSM_TEST_GPRS && defined(TINY_GSM_MODEM_XBEE)
// The XBee must run the gprsConnect function BEFORE waiting for network!
modem.gprsConnect(apn, gprsUser, gprsPass);
#endif
DBG("Waiting for network...");
if (!modem.waitForNetwork(600000L, true)) {
delay(10000);
return;
}
if (modem.isNetworkConnected()) { DBG("Network connected"); }
#if TINY_GSM_TEST_GPRS
DBG("Connecting to", apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
bool res = modem.isGprsConnected();
DBG("GPRS status:", res ? "connected" : "not connected");
String ccid = modem.getSimCCID();
DBG("CCID:", ccid);
String imei = modem.getIMEI();
DBG("IMEI:", imei);
String imsi = modem.getIMSI();
DBG("IMSI:", imsi);
String cop = modem.getOperator();
DBG("Operator:", cop);
// String prov = modem.getProvider();
// DBG("Provider:", prov);
IPAddress local = modem.localIP();
DBG("Local IP:", local);
int csq = modem.getSignalQuality();
DBG("Signal quality:", csq);
#endif
#if TINY_GSM_TEST_USSD && defined TINY_GSM_MODEM_HAS_SMS
String ussd_balance = modem.sendUSSD("*111#");
DBG("Balance (USSD):", ussd_balance);
String ussd_phone_num = modem.sendUSSD("*161#");
DBG("Phone number (USSD):", ussd_phone_num);
#endif
#if TINY_GSM_TEST_TCP && defined TINY_GSM_MODEM_HAS_TCP
TinyGsmClient client(modem, 0);
const int port = 80;
DBG("Connecting to", server);
if (!client.connect(server, port)) {
DBG("... failed");
} else {
// Make a HTTP GET request:
client.print(String("GET ") + resource + " HTTP/1.0\r\n");
client.print(String("Host: ") + server + "\r\n");
client.print("Connection: close\r\n\r\n");
// Wait for data to arrive
uint32_t start = millis();
while (client.connected() && !client.available() &&
millis() - start < 30000L) {
delay(100);
};
// Read data
start = millis();
char logo[640] = {
'\0',
};
int read_chars = 0;
while (client.connected() && millis() - start < 10000L) {
while (client.available()) {
logo[read_chars] = client.read();
logo[read_chars + 1] = '\0';
read_chars++;
start = millis();
}
}
SerialMon.println(logo);
DBG("##### RECEIVED:", strlen(logo), "CHARACTERS");
client.stop();
}
#endif
#if TINY_GSM_TEST_SSL && defined TINY_GSM_MODEM_HAS_SSL
// TODO: Add test of adding certificcate
TinyGsmClientSecure secureClient(modem, 1);
const int securePort = 443;
DBG("Connecting securely to", server);
if (!secureClient.connect(server, securePort)) {
DBG("... failed");
} else {
// Make a HTTP GET request:
secureClient.print(String("GET ") + resource + " HTTP/1.0\r\n");
secureClient.print(String("Host: ") + server + "\r\n");
secureClient.print("Connection: close\r\n\r\n");
// Wait for data to arrive
uint32_t startS = millis();
while (secureClient.connected() && !secureClient.available() &&
millis() - startS < 30000L) {
delay(100);
};
// Read data
startS = millis();
char logoS[640] = {
'\0',
};
int read_charsS = 0;
while (secureClient.connected() && millis() - startS < 10000L) {
while (secureClient.available()) {
logoS[read_charsS] = secureClient.read();
logoS[read_charsS + 1] = '\0';
read_charsS++;
startS = millis();
}
}
SerialMon.println(logoS);
DBG("##### RECEIVED:", strlen(logoS), "CHARACTERS");
secureClient.stop();
}
#endif
#if TINY_GSM_TEST_CALL && defined(TINY_GSM_MODEM_HAS_CALLING) && \
defined(CALL_TARGET)
DBG("Calling:", CALL_TARGET);
// This is NOT supported on M590
res = modem.callNumber(CALL_TARGET);
DBG("Call:", res ? "OK" : "fail");
if (res) {
delay(1000L);
// Play DTMF A, duration 1000ms
modem.dtmfSend('A', 1000);
// Play DTMF 0..4, default duration (100ms)
for (char tone = '0'; tone <= '4'; tone++) { modem.dtmfSend(tone); }
delay(5000);
res = modem.callHangup();
DBG("Hang up:", res ? "OK" : "fail");
}
#endif
// Test the SMS functions
#if TINY_GSM_TEST_SMS && defined TINY_GSM_MODEM_HAS_SMS && defined SMS_TARGET
res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
DBG("SMS:", res ? "OK" : "fail");
// This is only supported on SIMxxx series
res = modem.sendSMS_UTF8_begin(SMS_TARGET);
if (res) {
auto stream = modem.sendSMS_UTF8_stream();
stream.print(F("Привіііт! Print number: "));
stream.print(595);
res = modem.sendSMS_UTF8_end();
}
DBG("UTF8 SMS:", res ? "OK" : "fail");
#endif
// Test the GSM location functions
#if TINY_GSM_TEST_GSM_LOCATION && defined TINY_GSM_MODEM_HAS_GSM_LOCATION
float gsm_latitude = 0;
float gsm_longitude = 0;
float gsm_accuracy = 0;
int gsm_year = 0;
int gsm_month = 0;
int gsm_day = 0;
int gsm_hour = 0;
int gsm_minute = 0;
int gsm_second = 0;
for (int8_t i = 15; i; i--) {
DBG("Requesting current GSM location");
if (modem.getGsmLocation(&gsm_latitude, &gsm_longitude, &gsm_accuracy,
&gsm_year, &gsm_month, &gsm_day, &gsm_hour,
&gsm_minute, &gsm_second)) {
DBG("Latitude:", String(gsm_latitude, 8),
"\tLongitude:", String(gsm_longitude, 8));
DBG("Accuracy:", gsm_accuracy);
DBG("Year:", gsm_year, "\tMonth:", gsm_month, "\tDay:", gsm_day);
DBG("Hour:", gsm_hour, "\tMinute:", gsm_minute, "\tSecond:", gsm_second);
break;
} else {
DBG("Couldn't get GSM location, retrying in 15s.");
delay(15000L);
}
}
DBG("Retrieving GSM location again as a string");
String location = modem.getGsmLocation();
DBG("GSM Based Location String:", location);
#endif
// Test the GPS functions
#if TINY_GSM_TEST_GPS && defined TINY_GSM_MODEM_HAS_GPS
DBG("Enabling GPS/GNSS/GLONASS and waiting 15s for warm-up");
#if !defined(TINY_GSM_MODEM_SARAR5) // not needed for this module
modem.enableGPS();
#endif
delay(15000L);
float gps_latitude = 0;
float gps_longitude = 0;
float gps_speed = 0;
float gps_altitude = 0;
int gps_vsat = 0;
int gps_usat = 0;
float gps_accuracy = 0;
int gps_year = 0;
int gps_month = 0;
int gps_day = 0;
int gps_hour = 0;
int gps_minute = 0;
int gps_second = 0;
for (int8_t i = 15; i; i--) {
DBG("Requesting current GPS/GNSS/GLONASS location");
if (modem.getGPS(&gps_latitude, &gps_longitude, &gps_speed, &gps_altitude,
&gps_vsat, &gps_usat, &gps_accuracy, &gps_year, &gps_month,
&gps_day, &gps_hour, &gps_minute, &gps_second)) {
DBG("Latitude:", String(gps_latitude, 8),
"\tLongitude:", String(gps_longitude, 8));
DBG("Speed:", gps_speed, "\tAltitude:", gps_altitude);
DBG("Visible Satellites:", gps_vsat, "\tUsed Satellites:", gps_usat);
DBG("Accuracy:", gps_accuracy);
DBG("Year:", gps_year, "\tMonth:", gps_month, "\tDay:", gps_day);
DBG("Hour:", gps_hour, "\tMinute:", gps_minute, "\tSecond:", gps_second);
break;
} else {
DBG("Couldn't get GPS/GNSS/GLONASS location, retrying in 15s.");
delay(15000L);
}
}
DBG("Retrieving GPS/GNSS/GLONASS location again as a string");
String gps_raw = modem.getGPSraw();
#if !defined(TINY_GSM_MODEM_SARAR5) // not available for this module
DBG("GPS/GNSS Based Location String:", gps_raw);
DBG("Disabling GPS");
modem.disableGPS();
#endif
#endif
// Test the Network time functions
#if TINY_GSM_TEST_NTP && defined TINY_GSM_MODEM_HAS_NTP
DBG("Asking modem to sync with NTP");
modem.NTPServerSync("pool.ntp.org", 20);
#endif
#if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME
int ntp_year = 0;
int ntp_month = 0;
int ntp_day = 0;
int ntp_hour = 0;
int ntp_min = 0;
int ntp_sec = 0;
float ntp_timezone = 0;
for (int8_t i = 5; i; i--) {
DBG("Requesting current network time");
if (modem.getNetworkTime(&ntp_year, &ntp_month, &ntp_day, &ntp_hour,
&ntp_min, &ntp_sec, &ntp_timezone)) {
DBG("Year:", ntp_year, "\tMonth:", ntp_month, "\tDay:", ntp_day);
DBG("Hour:", ntp_hour, "\tMinute:", ntp_min, "\tSecond:", ntp_sec);
DBG("Timezone:", ntp_timezone);
break;
} else {
DBG("Couldn't get network time, retrying in 15s.");
delay(15000L);
}
}
DBG("Retrieving time again as a string");
String time = modem.getGSMDateTime(DATE_FULL);
DBG("Current Network Time:", time);
#endif
// Test Battery functions
#if TINY_GSM_TEST_BATTERY && defined TINY_GSM_MODEM_HAS_BATTERY
int8_t chargeState = -99;
int8_t chargePercent = -99;
int16_t milliVolts = -9999;
modem.getBattStats(chargeState, chargePercent, milliVolts);
DBG("Battery charge state:", chargeState);
DBG("Battery charge 'percent':", chargePercent);
DBG("Battery voltage:", milliVolts / 1000.0F);
#endif
// Test temperature functions
#if TINY_GSM_TEST_TEMPERATURE && defined TINY_GSM_MODEM_HAS_TEMPERATURE
float temp = modem.getTemperature();
DBG("Chip temperature:", temp);
#endif
#if TINY_GSM_POWERDOWN
#if TINY_GSM_TEST_GPRS
modem.gprsDisconnect();
delay(5000L);
if (!modem.isGprsConnected()) {
DBG("GPRS disconnected");
} else {
DBG("GPRS disconnect: Failed.");
}
#endif
#if TINY_GSM_TEST_WIFI
modem.networkDisconnect();
DBG("WiFi disconnected");
#endif
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.poweroff();
DBG("Poweroff.");
#endif
DBG("End of tests.");
// Do nothing forevermore
while (true) { modem.maintain(); }
}