-
Notifications
You must be signed in to change notification settings - Fork 241
/
Arduino_TinyGSM.ino
332 lines (254 loc) · 8.28 KB
/
Arduino_TinyGSM.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
// Please select the corresponding model
// #define SIM800L_IP5306_VERSION_20190610
// #define SIM800L_AXP192_VERSION_20200327
// #define SIM800C_AXP192_VERSION_20200609
#define SIM800L_IP5306_VERSION_20200811
// #define TEST_RING_RI_PIN //Note will cancel the phone call test
// #define ENABLE_SPI_SDCARD //Uncomment will test external SD card
// See all AT commands, if wanted
#define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
#include "utilities.h"
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
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) */
// Server details
const char server[] = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
// Your GPRS credentials (leave empty, if missing)
const char apn[] = ""; // Your APN
const char gprsUser[] = ""; // User
const char gprsPass[] = ""; // Password
const char simPIN[] = ""; // SIM card PIN code, if any
TinyGsmClient client(modem);
const int port = 80;
/*
This is just to demonstrate how to use SPI device externally.
Here we use SD card as a demonstration. In order to maintain versatility,
I chose three boards with free pins as SPI pins
*/
#ifdef ENABLE_SPI_SDCARD
#include "FS.h"
#include "SD.h"
#include <SPI.h>
SPIClass SPI1(HSPI);
#define MY_CS 33
#define MY_SCLK 25
#define MY_MISO 27
#define MY_MOSI 26
void setupSDCard()
{
SPI1.begin(MY_SCLK, MY_MISO, MY_MOSI, MY_CS);
//Assuming use of SPI SD card
if (!SD.begin(MY_CS, SPI1)) {
Serial.println("Card Mount Failed");
} else {
Serial.println("SDCard Mount PASS");
String size = String((uint32_t)(SD.cardSize() / 1024 / 1024)) + "MB";
Serial.println(size);
}
}
#else
#define setupSDCard()
#endif
void setupModem()
{
#ifdef MODEM_RST
// Keep reset high
pinMode(MODEM_RST, OUTPUT);
digitalWrite(MODEM_RST, HIGH);
#endif
pinMode(MODEM_PWRKEY, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
// Turn on the Modem power first
digitalWrite(MODEM_POWER_ON, HIGH);
// Pull down PWRKEY for more than 1 second according to manual requirements
digitalWrite(MODEM_PWRKEY, HIGH);
delay(100);
digitalWrite(MODEM_PWRKEY, LOW);
delay(1000);
digitalWrite(MODEM_PWRKEY, HIGH);
// Initialize the indicator as an output
pinMode(LED_GPIO, OUTPUT);
digitalWrite(LED_GPIO, LED_OFF);
}
void turnOffNetlight()
{
SerialMon.println("Turning off SIM800 Red LED...");
modem.sendAT("+CNETLIGHT=0");
}
void turnOnNetlight()
{
SerialMon.println("Turning on SIM800 Red LED...");
modem.sendAT("+CNETLIGHT=1");
}
void setup()
{
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Start power management
if (setupPMU() == false) {
Serial.println("Setting power error");
}
setupSDCard();
// Some start operations
setupModem();
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
}
void loop()
{
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Turn off network status lights to reduce current consumption
turnOffNetlight();
// The status light cannot be turned off, only physically removed
//turnOffStatuslight();
// Or, use modem.init() if you don't need the complete restart
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork(240000L)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
// When the network connection is successful, turn on the indicator
digitalWrite(LED_GPIO, LED_ON);
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
SerialMon.print(F("Connecting to APN: "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print("Connecting to ");
SerialMon.print(server);
if (!client.connect(server, port)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
// Make a HTTP GET request:
SerialMon.println("Performing 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");
client.println();
unsigned long timeout = millis();
while (client.connected() && millis() - timeout < 10000L) {
// Print available data
while (client.available()) {
char c = client.read();
SerialMon.print(c);
timeout = millis();
}
}
SerialMon.println();
// Shutdown
client.stop();
SerialMon.println(F("Server disconnected"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
// DTR is used to wake up the sleeping Modem
// DTR is used to wake up the sleeping Modem
// DTR is used to wake up the sleeping Modem
#ifdef MODEM_DTR
bool res;
modem.sleepEnable();
delay(100);
// test modem response , res == 0 , modem is sleep
res = modem.testAT();
Serial.print("SIM800 Test AT result -> ");
Serial.println(res);
delay(1000);
Serial.println("Use DTR Pin Wakeup");
pinMode(MODEM_DTR, OUTPUT);
//Set DTR Pin low , wakeup modem .
digitalWrite(MODEM_DTR, LOW);
// test modem response , res == 1 , modem is wakeup
res = modem.testAT();
Serial.print("SIM800 Test AT result -> ");
Serial.println(res);
#endif
#ifdef TEST_RING_RI_PIN
#ifdef MODEM_RI
// Swap the audio channels
SerialAT.print("AT+CHFA=1\r\n");
delay(2);
//Set ringer sound level
SerialAT.print("AT+CRSL=100\r\n");
delay(2);
//Set loud speaker volume level
SerialAT.print("AT+CLVL=100\r\n");
delay(2);
// Calling line identification presentation
SerialAT.print("AT+CLIP=1\r\n");
delay(2);
//Set RI Pin input
pinMode(MODEM_RI, INPUT);
Serial.println("Wait for call in");
//When is no calling ,RI pin is high level
while (digitalRead(MODEM_RI)) {
Serial.print('.');
delay(500);
}
Serial.println("call in ");
//Wait 10 seconds for the bell to ring
delay(10000);
//Accept call
SerialAT.println("ATA");
delay(10000);
// Wait ten seconds, then hang up the call
SerialAT.println("ATH");
#endif //MODEM_RI
#endif //TEST_RING_RI_PIN
// Make the LED blink three times before going to sleep
int i = 3;
while (i--) {
digitalWrite(LED_GPIO, LED_ON);
modem.sendAT("+SPWM=0,1000,80");
delay(500);
digitalWrite(LED_GPIO, LED_OFF);
modem.sendAT("+SPWM=0,1000,0");
delay(500);
}
//After all off
modem.poweroff();
SerialMon.println(F("Poweroff"));
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
/*
The sleep current using AXP192 power management is about 500uA,
and the IP5306 consumes about 1mA
*/
}