arduinogsmat-commandserial-communicationsim800l

Program is executing but AT commands not showing in serial monitor


My purpose was to send SMS using GSM SIM800L coreboard and Arduino UNO. Here is the code


    #include <SoftwareSerial.h>

//Create a software serial object to communicate with SIM800L
SoftwareSerial mySerial(3, 2); //SIM800L Tx & Rx is connected to Arduino #3 & #2

void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(115200);

  //Begin serial communication with Arduino and SIM800L
  mySerial.begin(115200);

  Serial.println("Initializing..."); 
  delay(1000);

  mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();

  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  mySerial.println("AT+CMGS=\"+ZZxxxxxxxxx\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  updateSerial();
  mySerial.print("TEST"); //text content
  updateSerial();
  mySerial.write(26);
}

void loop()
{
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

And here is the serial monitor output

 22:31:19.430 -> Initializing...

However, when I run the code, I get the text message to my mobile phone, but I can't see any AT commands in the serial monitor. It only outputs "Initializing..." . All the connections and baud rates are okay, checked a thousand times. Has connected 2A, 4.4v power supply to the GSM coreboard and shorten the wires, and sho No bad soldering joints. GSM module red led flash per 3 seconds. And again, I'm getting the text message to my phone. So that means the problem is with the Arduino serial monitor or code, not in the hardware. I need to see AT commands because I need to put more commands through the serial monitor, I tried typing and click send, But it's not showing anything. Any assistance you can provide would be greatly appreciated.


Solution

  • Your logic is reversed in the updateSerial() function.

    Actually, you are sending the AT command over mySerial at the setup function, then you need to wait for the answer to come in that object mySerial.

    So, you should do the while (!mySerial.available()) ; to be able to read something from it. Once this loop ends, you can read from mySerial.

    However, you want to forward it to the serial monitor so, you also need to check if the Serial is available to be written to, that is why you also waits for it, resulting in the while (!mySerial.available() || !Serial.available()) ;.

    Once you are sure both serials are available, you can read from one and write what you just read into the other one: Serial.Write(mySerial.read()).

    Also, I do not see any need for the mySerial.write(Serial.read()) call, because the Serial is being used just to forward what you are receiving from the SIM800L, thus, you could simply remove that part.

    Thus, the correction of your function would result in this:

    void updateSerial()
    {
        delay(500);
        while (!mySerial.available() || !Serial.available())
            ;
    
        Serial.write(mySerial.read());
    }
    

    So, with this, everything you receive from the SIM800L is forwarded to the serial monitor.