/
AllFunctions.ino
341 lines (278 loc) · 8.74 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
/*
FILE: AllFunctions.ino
AUTHOR: Koby Hale
PURPOSE: Test functionality
*/
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// See all AT commands, if wanted
#define DUMP_AT_COMMANDS
/*
Tests enabled
*/
#define TINY_GSM_TEST_GPRS true
#define TINY_GSM_TEST_GPS true
#define TINY_GSM_POWERDOWN true
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "YOUR-APN"; //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;
void setup()
{
// Set console baud rate
Serial.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
// Starting the machine requires at least 1 second of low level, and with a level conversion, the levels are opposite
delay(1000);
digitalWrite(PWR_PIN, LOW);
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
Serial.println("\nWait...");
delay(1000);
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.restart()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
}
void loop()
{
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.init()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
String name = modem.getModemName();
delay(500);
Serial.println("Modem Name: " + name);
String modemInfo = modem.getModemInfo();
delay(500);
Serial.println("Modem Info: " + modemInfo);
// Set Modem GPS Power Control Pin to LOW ,turn off GPS power
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+CGPIO=0,48,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG("Set GPS Power LOW Failed");
}
#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
modem.sendAT("+CFUN=0");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=0 false ");
}
delay(200);
/*
2 Automatic
13 GSM only
38 LTE only
51 GSM and LTE only
* * * */
bool res = modem.setNetworkMode(2);
if (!res) {
DBG("setNetworkMode false ");
return ;
}
delay(200);
/*
1 CAT-M
2 NB-Iot
3 CAT-M and NB-IoT
* * */
res = modem.setPreferredMode(3);
if (!res) {
DBG("setPreferredMode false ");
return ;
}
delay(200);
/*AT+CBANDCFG=<mode>,<band>[,<band>…]
* <mode> "CAT-M" "NB-IOT"
* <band> The value of <band> must is in the band list of getting from AT+CBANDCFG=?
* For example, my SIM card carrier "NB-iot" supports B8. I will configure +CBANDCFG= "Nb-iot ",8
*/
/* modem.sendAT("+CBANDCFG=\"NB-IOT\",8 ");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CBANDCFG=\"NB-IOT\" ");
}
delay(200);*/
modem.sendAT("+CFUN=1");
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=1 false ");
}
delay(200);
#if TINY_GSM_TEST_GPRS
SerialAT.println("AT+CGDCONT?");
delay(500);
if (SerialAT.available()) {
input = SerialAT.readString();
for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i + 1) == "\n") {
pieces[counter] = input.substring(lastIndex, i);
lastIndex = i + 1;
counter++;
}
if (i == input.length() - 1) {
pieces[counter] = input.substring(lastIndex, i);
}
}
// Reset for reuse
input = "";
counter = 0;
lastIndex = 0;
for ( int y = 0; y < numberOfPieces; y++) {
for ( int x = 0; x < pieces[y].length(); x++) {
char c = pieces[y][x]; //gets one byte from buffer
if (c == ',') {
if (input.indexOf(": ") >= 0) {
String data = input.substring((input.indexOf(": ") + 1));
if ( data.toInt() > 0 && data.toInt() < 25) {
modem.sendAT("+CGDCONT=" + String(data.toInt()) + ",\"IP\",\"" + String(apn) + "\",\"0.0.0.0\",0,0,0,0");
}
input = "";
break;
}
// Reset for reuse
input = "";
} else {
input += c;
}
}
}
} else {
Serial.println("Failed to get PDP!");
}
Serial.println("\n\n\nWaiting for network...");
if (!modem.waitForNetwork()) {
delay(10000);
return;
}
if (modem.isNetworkConnected()) {
Serial.println("Network connected");
}
Serial.println("\n---Starting GPRS TEST---\n");
Serial.println("Connecting to: " + String(apn));
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
Serial.print("GPRS status: ");
if (modem.isGprsConnected()) {
Serial.println("connected");
} else {
Serial.println("not connected");
}
String ccid = modem.getSimCCID();
Serial.println("CCID: " + ccid);
String imei = modem.getIMEI();
Serial.println("IMEI: " + imei);
String cop = modem.getOperator();
Serial.println("Operator: " + cop);
IPAddress local = modem.localIP();
Serial.println("Local IP: " + String(local));
int csq = modem.getSignalQuality();
Serial.println("Signal quality: " + String(csq));
SerialAT.println("AT+CPSI?"); //Get connection type and band
delay(500);
if (SerialAT.available()) {
String r = SerialAT.readString();
Serial.println(r);
}
Serial.println("\n---End of GPRS TEST---\n");
#endif
#if TINY_GSM_TEST_GPRS
modem.gprsDisconnect();
if (!modem.isGprsConnected()) {
Serial.println("GPRS disconnected");
} else {
Serial.println("GPRS disconnect: Failed.");
}
#endif
#if TINY_GSM_TEST_GPS
Serial.println("\n---Starting GPS TEST---\n");
// Set Modem GPS Power Control Pin to HIGH ,turn on GPS power
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+CGPIO=0,48,1,1");
if (modem.waitResponse(10000L) != 1) {
DBG("Set GPS Power HIGH Failed");
}
modem.enableGPS();
float lat, lon;
while (1) {
if (modem.getGPS(&lat, &lon)) {
Serial.printf("lat:%f lon:%f\n", lat, lon);
break;
} else {
Serial.print("getGPS ");
Serial.println(millis());
}
delay(2000);
}
modem.disableGPS();
// Set Modem GPS Power Control Pin to LOW ,turn off GPS power
// Only in version 20200415 is there a function to control GPS power
modem.sendAT("+CGPIO=0,48,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG("Set GPS Power LOW Failed");
}
Serial.println("\n---End of GPRS TEST---\n");
#endif
#if TINY_GSM_POWERDOWN
// Try to power-off (modem may decide to restart automatically)
// To turn off modem completely, please use Reset/Enable pins
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
DBG("+CPOWD=1");
}
modem.poweroff();
Serial.println("Poweroff.");
#endif
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}