diff --git a/examples/Arduino_AT_Debug/Arduino_AT_Debug.ino b/examples/Arduino_AT_Debug/Arduino_AT_Debug.ino index 86d28d6..fcf5599 100644 --- a/examples/Arduino_AT_Debug/Arduino_AT_Debug.ino +++ b/examples/Arduino_AT_Debug/Arduino_AT_Debug.ino @@ -16,6 +16,29 @@ // 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 @@ -47,11 +70,22 @@ void setup() // Set console baud rate SerialMon.begin(115200); - // Set GSM module baud rate and UART pins - SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); - 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()