forked from Xinyuan-LilyGO/LilyGo-Modem-Series
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATdebug.ino
More file actions
137 lines (119 loc) · 4.21 KB
/
ATdebug.ino
File metadata and controls
137 lines (119 loc) · 4.21 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
/**
* @file ATdebug.ino
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-10-26
*
*/
#include "utilities.h"
#include "Arduino.h"
bool checkRespond()
{
for (int j = 0; j < 10; j++) {
SerialAT.print("AT\r\n");
String input = SerialAT.readString();
if (input.indexOf("OK") >= 0) {
return true;
}
delay(200);
}
return false;
}
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 setup()
{
Serial.begin(115200); // Set console baud rate
Serial.println("Start Sketch");
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX_PIN, MODEM_TX_PIN);
#ifdef BOARD_LED_PIN
pinMode(BOARD_LED_PIN, OUTPUT);
digitalWrite(BOARD_LED_PIN, !LED_ON);
#endif
#ifdef BOARD_POWERON_PIN
/* Set Power control pin output
* * @note Known issues, ESP32 (V1.2) version of T-A7670, T-A7608,
* when using battery power supply mode, BOARD_POWERON_PIN (IO12) must be set to high level after esp32 starts, otherwise a reset will occur.
* */
pinMode(BOARD_POWERON_PIN, OUTPUT);
digitalWrite(BOARD_POWERON_PIN, HIGH);
#endif
// Set modem reset pin ,reset modem
#ifdef MODEM_RESET_PIN
pinMode(MODEM_RESET_PIN, OUTPUT);
digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL); delay(100);
digitalWrite(MODEM_RESET_PIN, MODEM_RESET_LEVEL); delay(2600);
digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL);
#endif
#ifdef MODEM_FLIGHT_PIN
// If there is an airplane mode control, you need to exit airplane mode
pinMode(MODEM_FLIGHT_PIN, OUTPUT);
digitalWrite(MODEM_FLIGHT_PIN, HIGH);
#endif
// Pull down DTR to ensure the modem is not in sleep state
pinMode(MODEM_DTR_PIN, OUTPUT);
digitalWrite(MODEM_DTR_PIN, LOW);
// Turn on the modem
pinMode(BOARD_PWRKEY_PIN, OUTPUT);
digitalWrite(BOARD_PWRKEY_PIN, LOW);
delay(100);
digitalWrite(BOARD_PWRKEY_PIN, HIGH);
delay(MODEM_POWERON_PULSE_WIDTH_MS);
digitalWrite(BOARD_PWRKEY_PIN, LOW);
// Check whether it has been started
bool started = checkRespond();
if (!started) {
Serial.println("Wait modem started...");
// Wait for the modem to finish booting
delay(MODEM_START_WAIT_MS);
}
// Determine the communication baud rate
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 consequences..."));
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"));
}
#ifdef BOARD_LED_PIN
pinMode(BOARD_LED_PIN, OUTPUT);
digitalWrite(BOARD_LED_PIN, LED_ON);
#endif
}
void loop()
{
if (SerialAT.available()) {
Serial.write(SerialAT.read());
}
if (Serial.available()) {
SerialAT.write(Serial.read());
}
delay(1);
}