Skip to content

Instantly share code, notes, and snippets.

@vishnumaiea
Last active July 22, 2021 16:48
Show Gist options
  • Save vishnumaiea/1de36ee92c825dffe0107e3c684f1c29 to your computer and use it in GitHub Desktop.
Save vishnumaiea/1de36ee92c825dffe0107e3c684f1c29 to your computer and use it in GitHub Desktop.
AT command tester for Goouuu Tech IOT-GA6 GSM/GPRS module with Arduino Due
//================================================================//
// //
// ## GA6 GSM Module AT Command Tester ## //
// //
// Send AT commands to GA6 GSm module. This program was written //
// for Arduino Due with Serial1 port for GSM module and Serial0 //
// port for debugging. AT commands use CR as delimiter. You may //
// also send a NL. If you're not accepting CR from terminal, an //
// explicit CR should be sent after the command. NL is optional. //
// AT command responses are terminated by CRLF by default. //
// //
// Author : Vishnu M Aiea //
// Website : https://www.vishnumaiea.in //
// GitHub : https://github.com/vishnumaiea //
// License : MIT //
// Version : 1.7 //
// Last modified : IST 09:34 PM 18-04-2019, Thursday //
// //
//================================================================//
String inputString = "";
String lastCommand = "";
bool upperCaseStatus = false; //keep tracks of whether incoming string needs to be converted to upper case
//================================================================//
void setup() {
Serial.begin(115200); //debugging port
Serial1.begin(115200); //GSM module's port
Serial.println();
Serial.println("## GA6 GSM Module - AT Tester ##");
Serial.println("--------------------------------");
Serial.println();
Serial.println("-- Usage --");
Serial.println();
Serial.println("Start AT commands with \"at\" or \"AT\" (<CR> is automatically appended)");
Serial.println("Use \">\" shorthand for \"AT+\" (eg. \">CREG?\")");
Serial.println("Start string with \"$\" (eg. \"$message\")");
Serial.println("Start decimal number with \"#d\" or \"#D\" (eg. \"#D42\")");
Serial.println("Start hex number with \"#h\" or \"#H\" (eg. \"#H42\")");
Serial.println("Send carriage return with \"<cr>\" or \"<CR>\"");
Serial.println("Send newline with \"<lf>\" or \"<LF>\"");
Serial.println("Start custom commands with \"/\" (eg. \"/command\")");
Serial.println();
delay(1000);
}
//================================================================//
void loop() {
if(Serial.available()) { //monitor the serial 0 interface
inputString = Serial.readString(); //read the contents of serial buffer as string
Serial.println();
Serial.print("-- Input (");
Serial.print(inputString.length());
Serial.println(") --");
lastCommand = inputString; //just a backup of the original string
//----------------------------------------------------------------//
//custom commands
if(inputString.startsWith("/")) { //allows you to send non-AT commands
Serial.println(inputString);
processCustomCmd();
}
//----------------------------------------------------------------//
//send a number as hex
else if(inputString.startsWith("#H") || inputString.startsWith("#h")) { //allows you to send hex value
Serial.println(inputString);
String subString = inputString.substring(2); //remove "#h" part
const char* hexString = subString.c_str(); //convert String object to C-style string
uint8_t intNumber = strtol(hexString, NULL, 16); //convert hex formatted C-style string to int value
Serial1.write(intNumber); //can only write values between 0-255
}
//----------------------------------------------------------------//
//send a number as dec
else if(inputString.startsWith("#D") || inputString.startsWith("#d")) { //allows you to send dec value
Serial.println(inputString);
String subString = inputString.substring(2); //remove "#h" part
uint8_t intNumber = subString.toInt();
Serial1.write(intNumber); //can only write values between 0-255
}
//----------------------------------------------------------------//
//send the CR character
else if(inputString.startsWith("<cr>") || inputString.startsWith("<CR>")) { //allows you to send dec value
Serial1.write(0xD); //can only write values between 0-255
}
//----------------------------------------------------------------//
//send the line feed or newline charcater
else if(inputString.startsWith("<lf>") || inputString.startsWith("<LF>")) { //allows you to send dec value
Serial1.write(0xA); //can only write values between 0-255
}
//----------------------------------------------------------------//
//sends out a string without '$' or CR, LF characters
else if(inputString.startsWith("$")) { //allows you to send text
Serial.println(inputString);
String subString = inputString.substring(1); //remove the $ char
Serial1.print(subString);
upperCaseStatus = false;
}
//----------------------------------------------------------------//
//at command
else if(inputString.startsWith("at") || inputString.startsWith("AT")){
inputString.toUpperCase();
Serial.println(inputString);
Serial1.print(inputString);
Serial1.write(0xD); //carriage return : important
// Serial1.write(0xA); //newline
upperCaseStatus = true;
}
//----------------------------------------------------------------//
//at command
else if(inputString.startsWith(">")){
inputString = "AT+" + inputString.substring(1);
inputString.toUpperCase();
Serial.println(inputString);
Serial1.print(inputString);
Serial1.write(0xD); //carriage return : important
// Serial1.write(0xA); //newline
upperCaseStatus = true;
}
//----------------------------------------------------------------//
}
listenToGsm(upperCaseStatus);
}
//================================================================//
//custom commands start with "/"
void processCustomCmd() {
//add your custom command handles below
if(inputString == "/send sms") {
Serial.println();
Serial.println("-- Input --");
Serial.println("AT+CMGF=1");
Serial1.println("AT+CMGF=1"); //access text mode
delay(500);
listenToGsm(true); //we must wait after each command
Serial.println();
Serial.println("-- Input --");
Serial.println("AT+CMGS=\"+918089397933\"");
Serial1.println("AT+CMGS=\"+918089397933\""); //set phone number
delay(500);
listenToGsm(true);
Serial.println();
Serial.println("-- Input --");
Serial.println("Test Message");
Serial.println("\n");
Serial1.print("Test Message"); //sms content
Serial1.write(26); //substitute char to end message
delay(500);
listenToGsm(); //wait for the confirmation
Serial.println("");
}
}
//================================================================//
//listen to GSM module port
void listenToGsm(bool toUpperCase) {
if(Serial1.available()) { //monitor the serial 1 interface
inputString = Serial1.readString(); //read the contents of serial buffer as string
Serial.println();
Serial.print("-- Response (");
Serial.print(inputString.length());
Serial.println(") --");
if(toUpperCase)
inputString.toUpperCase();
Serial.print(inputString);
}
}
void listenToGsm() {
listenToGsm(false);
}
//================================================================//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment