AT Commands Code

Hi there, so I am using SoftwareSerial to communicate to a GSM module and I have it working with Adafruit’s test code (found here) but I would like to not use Adafruit’s library for it because it’s so all-encompassing and convoluted. My main goal at this moment is to figure out an alternative to their “sendCheckReply()” function, which basically sends an AT command to the module and waits for a response with a specified timeout.

Here is the code I have so far, but for some reason I don’t get a response back. I also realize I am using the String class which probably isn’t recommended.

#include <SoftwareSerial.h>

#define SIM_RX 3 // SoftwareSerial
#define SIM_TX 4
#define SIM_RST 2 // SIM5320A reset pin
#define PWR_ON 5 // If you pulse this low for a certain amount of time, the unit turns on

SoftwareSerial mySerial = SoftwareSerial(SIM_TX, SIM_RX);

void setup() {
  Serial.begin(115200);

  pinMode(PWR_ON, OUTPUT);
  pinMode(SIM_RST, OUTPUT);

  SIM5320_powerOn(); // Power on the GSM module

//  SIM5320_setup(7000); // This function would involve the "sendATcommand" function. Don't worry about this for now
  if (!sendATcommand("AT", "OK", 500)) Serial.println("AT command failed!"); // Sample usage of the function
}

void loop() {
  // Let's just worry about the sendATcommand function for now
}

bool sendATcommand(String ATcommand, String expectedReply, unsigned int timeout) {
  String reply;

  while(mySerial.available()) mySerial.read(); // Flush serial
  
  mySerial.println(ATcommand); // Send AT command
  Serial.print("\t---> "); Serial.println(ATcommand);

  unsigned long startTime = millis();

  while (1) {
    if (mySerial.available()) {
      reply = mySerial.readString(); // I'm not getting any response
    }
    
    if (millis() - startTime > timeout) break; // Timeout waiting for response
  }

  Serial.print("\t<--- "); Serial.println(reply); // Print the response
  if (reply != expectedReply) return false;

  return true;
}

void SIM5320_powerOn() {
  Serial.println("Turning on...");
  digitalWrite(PWR_ON, LOW);
  delay(150); // Pulse low for a little bit, then release
  digitalWrite(PWR_ON, HIGH);

  delay(4000); // Allow time to boot up
}

Aren’t you missing a mySerial.begin(xxx);?

(mySerial is not a great variable name, gsmSerial would be better)

Oh wow, great catch! I totally forgot that :)

However, when I put "mySerial.begin(4800);" in setup() and run the code, it still doesn't respond to the AT commands.

Is 4800 the right baud rate? Depends on the hardware, but if you are using a bluetooth module like the HC-05 or 06 the usual rate is 9600.

Yea 4800 should work because that's what the Adafruit code uses and their code works.

Does the rest of my code look fine for reading the response?

I figured it out. It was my SoftwareSerial syntax. I had edited the one in the Adafruit test code. It needs to be this:

SoftwareSerial gsmSerial(GSM_TX, GSM_RX);

Doesn't it work on 9600? My SIM900 is working on 9600