mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			93 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/**************************************************************
 | 
						|
 *
 | 
						|
 * 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
 | 
						|
 *
 | 
						|
 **************************************************************/
 | 
						|
 | 
						|
// Select your modem:
 | 
						|
#define TINY_GSM_MODEM_SIM800
 | 
						|
// #define TINY_GSM_MODEM_SIM900
 | 
						|
// #define TINY_GSM_MODEM_A6
 | 
						|
// #define TINY_GSM_MODEM_A7
 | 
						|
// #define TINY_GSM_MODEM_M590
 | 
						|
// #define TINY_GSM_MODEM_ESP8266
 | 
						|
 | 
						|
#include <TinyGsmClient.h>
 | 
						|
 | 
						|
// Set serial for debug console (to the Serial Monitor, speed 115200)
 | 
						|
#define SerialMon Serial
 | 
						|
 | 
						|
// Set serial for AT commands (to the module)
 | 
						|
// Use Hardware Serial on Mega, Leonardo, Micro
 | 
						|
#define SerialAT Serial1
 | 
						|
 | 
						|
// or Software Serial on Uno, Nano
 | 
						|
//#include <SoftwareSerial.h>
 | 
						|
//SoftwareSerial SerialAT(2, 3); // RX, TX
 | 
						|
 | 
						|
TinyGsm modem(SerialAT);
 | 
						|
 | 
						|
// Module baud rate
 | 
						|
uint32_t rate = 0; // Set to 0 for Auto-Detect
 | 
						|
 | 
						|
void setup() {
 | 
						|
  // Set console baud rate
 | 
						|
  SerialMon.begin(115200);
 | 
						|
  delay(5000);
 | 
						|
}
 | 
						|
 | 
						|
void loop() {
 | 
						|
 | 
						|
  if (!rate) {
 | 
						|
      static uint32_t rates[] = { 115200, 9600, 57600, 19200, 38400, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
 | 
						|
 | 
						|
      SerialMon.println("Autodetecting baud rate");
 | 
						|
      for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
 | 
						|
        SerialMon.print(String("Trying baud rate ") + rates[i] + "... ");
 | 
						|
        SerialAT.begin(rates[i]);
 | 
						|
        delay(10);
 | 
						|
        if (modem.autoBaud(1000)) {
 | 
						|
          rate = rates[i];
 | 
						|
          SerialMon.println(F("OK"));
 | 
						|
          break;
 | 
						|
        } else {
 | 
						|
          SerialMon.println(F("fail"));
 | 
						|
        }
 | 
						|
      }
 | 
						|
  }
 | 
						|
 | 
						|
  if (!rate) {
 | 
						|
    SerialMon.println(F("***********************************************************"));
 | 
						|
    SerialMon.println(F(" Module does not respond!"));
 | 
						|
    SerialMon.println(F("   Check your Serial wiring"));
 | 
						|
    SerialMon.println(F("   Check the module is correctly powered and turned on"));
 | 
						|
    SerialMon.println(F("***********************************************************"));
 | 
						|
    delay(30000L);
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  SerialAT.begin(rate);
 | 
						|
 | 
						|
  // Access AT commands from Serial Monitor
 | 
						|
  SerialMon.println(F("***********************************************************"));
 | 
						|
  SerialMon.println(F(" You can now send AT commands"));
 | 
						|
  SerialMon.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
 | 
						|
  SerialMon.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
 | 
						|
  SerialMon.println(F("***********************************************************"));
 | 
						|
 | 
						|
  while(true) {
 | 
						|
    if (SerialAT.available()) {
 | 
						|
      SerialMon.write(SerialAT.read());
 | 
						|
    }
 | 
						|
    if (SerialMon.available()) {
 | 
						|
      SerialAT.write(SerialMon.read());
 | 
						|
    }
 | 
						|
    delay(0);
 | 
						|
  }
 | 
						|
}
 | 
						|
 |