-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathArduino_AT_Debug.ino
100 lines (82 loc) · 3.05 KB
/
Arduino_AT_Debug.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
// 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
#include <Arduino.h>
#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
uint32_t AutoBaud()
{
static uint32_t rates[] = {115200, 9600, 57600, 38400, 19200, 74400, 74880,
230400, 460800, 2400, 4800, 14400, 28800
};
for (uint8_t i = 0; i < sizeof(rates) / sizeof(rates[0]); i++) {
uint32_t rate = rates[i];
Serial.printf("Trying baud rate %u\n", rate);
SerialAT.updateBaudRate(rate);
delay(10);
for (int j = 0; j < 10; j++) {
SerialAT.print("AT\r\n");
String input = SerialAT.readString();
if (input.indexOf("OK") >= 0) {
Serial.printf("Modem responded at rate:%u\n", rate);
return rate;
}
}
}
SerialAT.updateBaudRate(115200);
return 0;
}
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 setup()
{
// Set console baud rate
SerialMon.begin(115200);
setupModem();
if (AutoBaud()) {
Serial.println(F("***********************************************************"));
Serial.println(F(" You can now send AT commands"));
Serial.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
Serial.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
Serial.println(F(" DISCLAIMER: Entering AT commands without knowing what they do"));
Serial.println(F(" can have undesired consiquinces..."));
Serial.println(F("***********************************************************\n"));
} else {
Serial.println(F("***********************************************************"));
Serial.println(F(" Failed to connect to the modem! Check the baud and try again."));
Serial.println(F("***********************************************************\n"));
}
}
void loop()
{
while (SerialAT.available()) {
SerialMon.write(SerialAT.read());
}
while (SerialMon.available()) {
SerialAT.write(SerialMon.read());
}
}