Auto-detect baud rate
This commit is contained in:
		| @@ -1,47 +1,77 @@ | ||||
| /************************************************************** | ||||
|  * | ||||
|  * To run this tool you need StreamDebugger library: | ||||
|  *   https://github.com/vshymanskyy/StreamDebugger | ||||
|  *   or from http://librarymanager/all#StreamDebugger | ||||
|  * This script tries to auto-detect the baud rate | ||||
|  * and allows direct AT commands access | ||||
|  * | ||||
|  * TinyGSM Getting Started guide: | ||||
|  *   http://tiny.cc/tiny-gsm-readme | ||||
|  * | ||||
|  **************************************************************/ | ||||
|  | ||||
| #include <TinyGsmClient.h> | ||||
| #include <StreamDebugger.h> | ||||
|  | ||||
| char apn[]  = "YourAPN"; | ||||
| char user[] = ""; | ||||
| char pass[] = ""; | ||||
| // Set serial for debug console (to the Serial Monitor, speed 115200) | ||||
| #define SerialMonitor Serial | ||||
|  | ||||
| // Set serial for AT commands (to the module) | ||||
| // Use Hardware Serial on Mega, Leonardo, Micro | ||||
| #define GsmSerial Serial1 | ||||
| #define SerialAT Serial1 | ||||
|  | ||||
| // or Software Serial on Uno, Nano | ||||
| //#include <SoftwareSerial.h> | ||||
| //SoftwareSerial GsmSerial(2, 3); // RX, TX | ||||
| //#include <SoftwareSerialMonitor.h> | ||||
| //SoftwareSerial SerialAT(2, 3); // RX, TX | ||||
|  | ||||
| StreamDebugger DebugSerial(GsmSerial, Serial); | ||||
| TinyGsmClient gsm(DebugSerial); | ||||
| #include <TinyGsmClient.h> | ||||
| TinyGsmClient gsm(SerialAT); | ||||
|  | ||||
| void setup() { | ||||
|   // Set console baud rate | ||||
|   Serial.begin(115200); | ||||
|   delay(10); | ||||
|  | ||||
|   // Set GSM module baud rate | ||||
|   GsmSerial.begin(115200); | ||||
|   delay(3000); | ||||
|  | ||||
|   gsm.networkConnect(apn, user, pass); | ||||
|  | ||||
|   // Access AT commands from Serial | ||||
|   DebugSerial.directAccess(); | ||||
|   SerialMonitor.begin(115200); | ||||
|   delay(5000); | ||||
| } | ||||
|  | ||||
| void loop() { | ||||
|   // Detect module baud rate | ||||
|   uint32_t rate = 0; | ||||
|   uint32_t rates[] = { 115200, 9600, 57600, 19200, 74400, 74880 }; | ||||
|  | ||||
|   SerialMonitor.println("Autodetecting baud rate"); | ||||
|   for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) { | ||||
|     SerialMonitor.print(String("Trying baud rate ") + rates[i] + "... "); | ||||
|     SerialAT.begin(rates[i]); | ||||
|     delay(10); | ||||
|     if (gsm.autoBaud(3000)) { | ||||
|       rate = rates[i]; | ||||
|       SerialMonitor.println(F("OK")); | ||||
|       break; | ||||
|     } else { | ||||
|       SerialMonitor.println(F("fail")); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   if (!rate) { | ||||
|     SerialMonitor.println(F("***********************************************************")); | ||||
|     SerialMonitor.println(F(" Module does not respond!")); | ||||
|     SerialMonitor.println(F("   Check your Serial wiring")); | ||||
|     SerialMonitor.println(F("   Check the module is correctly powered and turned on")); | ||||
|     SerialMonitor.println(F("***********************************************************")); | ||||
|     delay(30000L); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // Access AT commands from Serial Monitor | ||||
|   SerialMonitor.println(F("***********************************************************")); | ||||
|   SerialMonitor.println(F(" You can now send AT commands")); | ||||
|   SerialMonitor.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\"")); | ||||
|   SerialMonitor.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor")); | ||||
|   SerialMonitor.println(F("***********************************************************")); | ||||
|  | ||||
|   while(true) { | ||||
|     if (SerialAT.available()) { | ||||
|       SerialMonitor.write(SerialAT.read()); | ||||
|     } | ||||
|     if (SerialMonitor.available()) { | ||||
|       SerialAT.write(SerialMonitor.read()); | ||||
|     } | ||||
|     delay(0); | ||||
|   } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user