-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathATdebug.ino
85 lines (76 loc) · 2.8 KB
/
ATdebug.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
/**
* @file ATdebug.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-10-26
*
*/
#include "utilities.h"
#include "Arduino.h"
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_POWERON_PIN
pinMode(BOARD_POWERON_PIN, OUTPUT);
digitalWrite(BOARD_POWERON_PIN, HIGH);
#endif
// Set modem reset pin ,reset modem
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);
pinMode(BOARD_PWRKEY_PIN, OUTPUT);
digitalWrite(BOARD_PWRKEY_PIN, LOW);
delay(100);
digitalWrite(BOARD_PWRKEY_PIN, HIGH);
delay(100);
digitalWrite(BOARD_PWRKEY_PIN, LOW);
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()
{
if (SerialAT.available()) {
Serial.write(SerialAT.read());
}
if (Serial.available()) {
SerialAT.write(Serial.read());
}
delay(1);
}