-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAllFunctions.ino
More file actions
427 lines (360 loc) · 12.1 KB
/
AllFunctions.ino
File metadata and controls
427 lines (360 loc) · 12.1 KB
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
/**************************************************************
*
* SimpleNB README:
* https://github.com/techstudio-design/SimpleNB/blob/master/README.md
*
* NOTE:
* Some of the functions may be unavailable for your modem.
* Just comment them out.
*
**************************************************************/
// Select your modem:
// #define SIMPLE_NB_MODEM_SIM7000
// #define SIMPLE_NB_MODEM_SIM7000SSL
// #define SIMPLE_NB_MODEM_SIM7020
// #define SIMPLE_NB_MODEM_SIM7070
#define SIMPLE_NB_MODEM_SIM7080
// #define SIMPLE_NB_MODEM_SIM7090
// #define SIMPLE_NB_MODEM_BG96
// #define SIMPLE_NB_MODEM_SARAR4
// #define SIMPLE_NB_MODEM_UBLOX
// #define SIMPLE_NB_MODEM_SEQUANS_MONARCH
// #define SIMPLE_NB_MODEM_XBEE
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#ifndef __AVR_ATmega328P__
#define SerialAT Serial2
// 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 SIMPLE_NB_DEBUG Serial
// Add a reception delay, if needed.
// This may be needed for a fast processor at a slow baud rate.
// #define SIMPLE_NB_YIELD() { delay(2); }
/*
* Tests enabled
*/
#define SIMPLE_NB_TEST_GPRS false
#define SIMPLE_NB_TEST_DATA true // not implemented on UBlox, Sara4, SequansMonarch and XBee
#define SIMPLE_NB_TEST_TCP true
#define SIMPLE_NB_TEST_SSL false
#define SIMPLE_NB_TEST_BATTERY true
#define SIMPLE_NB_TEST_GSM_LOCATION true
#define SIMPLE_NB_TEST_NTP true
#define SIMPLE_NB_TEST_TIME true
#define SIMPLE_NB_TEST_GPS false
#define SIMPLE_NB_POWERDOWN true
#define SIMPLE_NB_TEST_TEMPERATURE false
#define SIMPLE_NB_TEST_CALL false
#define SIMPLE_NB_TEST_SMS false
#define SIMPLE_NB_TEST_USSD false
// set GSM PIN, if any
#define GSM_PIN ""
#define PWRKEY 23 // GPIO pin used for PWRKEY
#define BAUD_RATE 115200 // Baud rate to be used for communicating with the modem
// 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 gprsUser[] = "";
const char gprsPass[] = "";
// Server details to test TCP/SSL
const char server[] = "postman-echo.com";
const char resource[] = "/ip";
const int port = 80;
const int securePort = 443;
bool res = false;
#include <SimpleNBClient.h>
// uncomment the following line for dumping all AT commands to erial Monitor
// #define DUMP_AT_COMMANDS
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
SimpleNB modem(debugger);
#else
SimpleNB modem(SerialAT);
#endif
// different modem may have different time requirements, check datasheet of your modem
void powerUp() {
digitalWrite(PWRKEY, LOW);
delay(1000);
digitalWrite(PWRKEY, HIGH);
delay(2500);
}
void setup() {
// Set console baud rate
Serial.begin(115200);
delay(10);
Serial.println();
DBG("Powering Up...");
digitalWrite(PWRKEY, HIGH);
pinMode(PWRKEY, OUTPUT);
powerUp();
// Set GSM module baud rate
SimpleNBBegin(SerialAT, BAUD_RATE);
// If unsure the baud rate work, alternatively use
// SimpleNBAutoBaud(SerialAT, 9600, 115200);
}
void loop() {
// Restart takes quite some time
// To skip it, call init() instead of restart()
// if (!modem.restart()) {
if (!modem.init()) {
DBG("Failed to initialize modem, delaying 10s and retrying");
// restart autobaud in case GSM just rebooted
// SimpleNBBegin(SerialAT, 115200);
return;
}
String name = modem.getModemName();
DBG("Modem Name:", name);
String modemInfo = modem.getModemInfo();
DBG("Modem Info:", modemInfo);
#if SIMPLE_NB_TEST_GPRS
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
#endif
#if SIMPLE_NB_TEST_GPRS && defined SIMPLE_NB_MODEM_XBEE
// The XBee must run the gprsConnect function BEFORE waiting for network!
modem.gprsConnect(apn, gprsUser, gprsPass);
#endif
DBG("Waiting for network registration...");
if (!modem.waitForRegistration(60000L, true)) {
delay(1000);
return;
}
if (modem.isNetworkRegistered()) { DBG("Network registered"); }
#if SIMPLE_NB_TEST_DATA
DBG("Activate Data Network... ");
if (!modem.activateDataNetwork()) {
delay(1000);
return;
}
#endif
#if SIMPLE_NB_TEST_GPRS
DBG("Connecting to", apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
res = modem.isGprsConnected();
DBG("GPRS status:", res ? "connected" : "not connected");
#endif
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);
int csq = modem.getSignalQuality();
DBG("Signal quality:", csq);
// Use the following to get IP as a String
// String local = modem.getLocalIP();
IPAddress local = modem.localIP();
DBG("Local IP:", local);
#if SIMPLE_NB_TEST_USSD && defined SIMPLE_NB_SUPPORT_SMS
String mobile_imei = modem.sendUSSD("*#06#"); //this is universal USSD for getting IMEI
DBG("IMEI (USSD):", mobile_imei);
#endif
#if SIMPLE_NB_TEST_TCP && defined SIMPLE_NB_SUPPORT_TCP
SimpleNBClient client(modem, 0);
DBG("Connecting to", server, "port", port);
if (!client.connect(server, port)) {
DBG("... failed");
} else {
// Make a HTTP GET request:
client.print(String("GET ") + resource + " HTTP/1.1\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 response[640] = {'\0'};
int read_chars = 0;
while (client.connected() && millis() - start < 10000L) {
while (client.available()) {
response[read_chars] = client.read();
response[read_chars + 1] = '\0';
read_chars++;
start = millis();
}
}
Serial.println(response);
DBG("##### RECEIVED:", strlen(response), "CHARACTERS");
client.stop();
}
#endif
#if SIMPLE_NB_TEST_SSL && defined SIMPLE_NB_SUPPORT_SSL
SimpleNBClientSecure secureClient(modem, 1);
DBG("Connecting securely to", server, "port", securePort);
if (!secureClient.connect(server, securePort)) {
DBG("... failed");
} else {
// Make a HTTP GET request:
secureClient.print(String("GET ") + resource + " HTTP/1.1\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 response[640] = {'\0'};
int read_res = 0;
while (secureClient.connected() && millis() - startS < 10000L) {
while (secureClient.available()) {
response[read_res] = secureClient.read();
response[read_res + 1] = '\0';
read_res++;
startS = millis();
}
}
Serial.println(response);
DBG("##### RECEIVED:", strlen(response), "CHARACTERS");
secureClient.stop();
}
#endif
#if SIMPLE_NB_TEST_CALL && defined SIMPLE_NB_SUPPORT_CALLING && defined CALL_TARGET
DBG("Calling:", CALL_TARGET);
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
#if SIMPLE_NB_TEST_SMS && defined SIMPLE_NB_SUPPORT_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
#if SIMPLE_NB_TEST_GSM_LOCATION && defined SIMPLE_NB_SUPPORT_GSM_LOCATION
CellLBS_t lbs;
for (int8_t i = 0; i < 3; i++) {
DBG("Requesting current GSM location (with GMT time)");
if (modem.getGsmLocation(lbs, 0)) {
DBG("Latitude:", String(lbs.lat, 8), "\tLongitude:", String(lbs.lon, 8));
DBG("Accuracy:", lbs.accuracy);
DBG("Year:", lbs.year, "\tMonth:", lbs.month, "\tDay:", lbs.day);
DBG("Hour:", lbs.hour, "\tMinute:", lbs.minute, "\tSecond:", lbs.second);
break;
} else {
DBG("Couldn't get GSM location, retrying in 15s.");
delay(15000L);
}
}
DBG("Retrieving GSM location again as a string (without time)");
String location = modem.getGsmLocation(0);
DBG("GSM Based Location String:", location);
#endif
#if SIMPLE_NB_TEST_GPS && defined SIMPLE_NB_SUPPORT_GPS
DBG("Enabling GPS/GNSS/GLONASS and waiting 15s for warm-up");
GPS_t gps;
modem.enableGPS();
for (int8_t i = 0; i < 3; i++) {
DBG("Requesting current GPS/GNSS/GLONASS location");
if (modem.getGPS(gps)) {
DBG("Latitude:", String(gps.lat, 8), "\tLongitude:", String(gps.lon, 8));
DBG("Speed:", gps.speed, "\tAltitude:", gps.alt);
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.getGPS();
DBG("GPS/GNSS Based Location String:", gps_raw);
DBG("Disabling GPS");
modem.disableGPS();
#endif
#if SIMPLE_NB_TEST_NTP && defined SIMPLE_NB_SUPPORT_NTP
DBG("Asking modem to sync with NTP");
modem.NTPServerSync("pool.ntp.org", 32);
#endif
#if SIMPLE_NB_TEST_TIME && defined SIMPLE_NB_SUPPORT_TIME
DateTime_t dt;
for (int8_t i = 0; i < 3; i++) {
DBG("Requesting current network time");
if (modem.getNetworkTime(dt)) {
DBG("Year:", dt.year, "\tMonth:", dt.month, "\tDay:", dt.day);
DBG("Hour:", dt.hour, "\tMinute:", dt.minute, "\tSecond:", dt.second);
DBG("Timezone:", dt.timezone/4.0F);
break;
} else {
DBG("Couldn't get network time, retrying in 15s.");
delay(15000L);
}
}
DBG("Retrieving time again as a string");
String time = modem.getNetworkTime();
DBG("Current Network Time:", time);
#endif
#if SIMPLE_NB_TEST_BATTERY && defined SIMPLE_NB_SUPPORT_BATTERY
Battery_t batt;
modem.getBatteryStatus(batt);
DBG("Battery charge state:", batt.chargeState);
DBG("Battery charge 'percent':", batt.percent);
DBG("Battery voltage:", batt.milliVolts / 1000.0F);
#endif
#if SIMPLE_NB_TEST_TEMPERATURE && defined SIMPLE_NB_SUPPORT_TEMPERATURE
float temp = modem.getTemperature();
DBG("Chip temperature:", temp);
#endif
#if SIMPLE_NB_TEST_GPRS
if (modem.gprsDisconnect()) {
DBG("GPRS disconnected");
} else {
DBG("GPRS disconnect: Failed.");
}
#endif
#if SIMPLE_NB_TEST_DATA
if (modem.deactivateDataNetwork()) {
DBG("Data Network Deactivated");
} else {
DBG("Network Deactivation Failed");
}
#endif
#if SIMPLE_NB_POWERDOWN
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.powerOff();
DBG("Power Off.");
#endif
DBG("End of tests.");
// Do nothing forevermore
while (true) { modem.maintain(); }
}