0

I am trying to send AT commands to SIM800L through Arduino's Serial Monitor. However when I type any command in the serial monitor nothing happens. I'm using an Arduino MEGA 2560 and the TX/RX of the SIM800L is connected to 9 and 10 respectively. Here is the code:

#include <SoftwareSerial.h>

SoftwareSerial GPRS(9, 10);
boolean state, lastState;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  state = digitalRead(2);
  lastState = state;

  GPRS.begin(9600);
  Serial.begin(9600);

  GPRS.println("AT+CMGF=1");

  delay(1000);
}

void loop()
{
  while(GPRS.available()) {
    Serial.write(GPRS.read());
  }

  lastState = state;
  state = digitalRead(2);

  if ( state != lastState ) {
    sendSMS();
  }

  delay(500);
}

void sendSMS() {
  Serial.print("Switch was turned ");
  Serial.println(state ? "on" : "off");

  GPRS.println("AT+CMGS=\"+64XXXXXXXXX\"");

  delay(500);

  GPRS.print("Switch was turned ");
  GPRS.println(state ? "on" : "off");
  GPRS.write( 0x1a ); // ctrl+Z character

  delay(500);
}

Any help would be much appreciated!

EDIT2: I forgot to mention but I am able to send, receive, and call from the GSM Module (with a different code). However I am not able to read the the received messages, since I only get a +CMT "SM", message and I can't exactly use AT+CMGR or CMGL commands because the serial monitor does nothing with them. here's the code for that:

#include <SoftwareSerial.h>
SoftwareSerial sim(9,10);
int _timeout;
String _buffer;
String number = "+639XXXXXXXXX"; //-> change with your number
void setup() {
  delay(1000); //delay for 7 seconds to make sure the modules get the signal
  Serial.begin(9600);
  _buffer.reserve(50);
  Serial.println("Lets go!");
  sim.begin(9600);
  delay(1000);
  Serial.println("Type s to send an SMS, r to receive an SMS, and c to make a call");
}
void loop() {
  if (Serial.available() > 0)
    switch (Serial.read())
    {
      case 's':
        SendMessage();
        break;
      case 'r':
        RecieveMessage();
        break;
      case 'c':
        callNumber();
        break;
    }
  if (sim.available() > 0)
    Serial.write(sim.read());
}
void SendMessage()
{
  Serial.println ("Sending Message");
  sim.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  Serial.println ("Set SMS Number");
  sim.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
  delay(1000);
  String SMS = "Wala kang jowa!";
  sim.println(SMS);
  delay(100);
  sim.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
}
void RecieveMessage()
{
  Serial.println ("SIM800L Read an SMS");
  delay (1000);
  sim.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
  Serial.write ("Unread Message done");
}
String _readSerial() {
  _timeout = 0;
  while  (!sim.available() && _timeout < 12000  )
  {
    delay(13);
    _timeout++;
  }
  if (sim.available()) {
    return sim.readString();
  }
}
void callNumber() {
  sim.print (F("ATD"));
  sim.print (number);
  sim.print (F(";\r\n"));
  _buffer = _readSerial();
  Serial.println(_buffer);
}
4
  • when I type any command in the serial monitor nothing happens ... you don't have any code for that – jsotola Feb 22 '20 at 4:17
  • @jsotola i thought it would take any AT commands automatically in the serial monitor? anyway i'll add my code that i used where i could send/receive/call the phone but not read any messages i receive – YMSmash Feb 22 '20 at 4:34
  • the arduino will not do something on its own ... try this code ... arduino.cc/en/Tutorial/SoftwareSerialExample – jsotola Feb 22 '20 at 8:47
  • Why are you using SoftwareSerial on a Mega?!?! – Majenko Feb 22 '20 at 10:55
0

Do not use SoftwareSerial on a Mega2560 unless you absolutely know what you are doing.

SoftwareSerial does not work on many of the pins of the Mega2560, and since there are other hardware serial ports available there is seldom any reason to resort to using SoftwareSerial anyway.

Connect your SIM800L to the TX1/RX1 pins of the Mega2560 then communicate using the pre-defined Serial1 object.

7
  • I see. I looked up some stuff regarding my question on other posts and almost everyone says to not use SoftwareSerial with Mega2560. Althought tbh I still don't completely understand what to do. Do I simply remove everything related to SoftwareSerial? How do I use the hardware serial ports; do I still need to code them in like in software serial or can i just connect the SIM800L to the TX1/RX1 without worrying about anything else? Dumb questions I know, but I've been out of touch with Arduino for over a year no so please bear with me... – YMSmash Feb 22 '20 at 11:38
  • Of course you have to code them in. You read with Serial1.read() and write with Serial1.print(...) in the exact same way as any other form of serial communication. You can't just plug random things in and expect the Arduino to magically know what they are and how to work with them. – Majenko Feb 22 '20 at 12:15
  • i guess what i really wanted to ask there was whether i needed to include another library like software serial. so basically i dont have to use any libraries now, right? What I still don't understand is that everyone seems to be using SoftwareSerial <GSM name>(<TX>,<RX>) to connect their GSM modules to their Arduino, regardless of whether it's an UNO or a Mega. If I'm not supposed to use SoftwareSerial, what should I be replacing that with? Or am I missing something completely here? – YMSmash Feb 22 '20 at 12:29
  • No libraries needed. Nothing to define. Serial1 is already defined for you. Just use Serial1.print() and Serial1.read() where you use sim.print() and sim.read() and so forth. – Majenko Feb 22 '20 at 12:31
  • oh so i can just do something like sim(<TX>,<RX>) without anything before it and then it's already defined as Serial1? – YMSmash Feb 22 '20 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.