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);
}
-
when I type any command in the serial monitor nothing happens ... you don't have any code for thatjsotola– jsotola2020年02月22日 04:17:23 +00:00Commented Feb 22, 2020 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 receiveYMSmash– YMSmash2020年02月22日 04:34:11 +00:00Commented Feb 22, 2020 at 4:34
-
the arduino will not do something on its own ... try this code ... arduino.cc/en/Tutorial/SoftwareSerialExamplejsotola– jsotola2020年02月22日 08:47:15 +00:00Commented Feb 22, 2020 at 8:47
-
1Why are you using SoftwareSerial on a Mega?!?!Majenko– Majenko2020年02月22日 10:55:38 +00:00Commented Feb 22, 2020 at 10:55
2 Answers 2
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.
-
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– YMSmash2020年02月22日 11:38:11 +00:00Commented Feb 22, 2020 at 11:38
-
Of course you have to code them in. You read with
Serial1.read()
and write withSerial1.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– Majenko2020年02月22日 12:15:14 +00:00Commented Feb 22, 2020 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– YMSmash2020年02月22日 12:29:15 +00:00Commented Feb 22, 2020 at 12:29 -
No libraries needed. Nothing to define.
Serial1
is already defined for you. Just useSerial1.print()
andSerial1.read()
where you usesim.print()
andsim.read()
and so forth.Majenko– Majenko2020年02月22日 12:31:12 +00:00Commented Feb 22, 2020 at 12:31 -
oh so i can just do something like
sim(<TX>,<RX>)
without anything before it and then it's already defined asSerial1
?YMSmash– YMSmash2020年02月22日 12:43:51 +00:00Commented Feb 22, 2020 at 12:43
- Make sure the RX of your arduino board is connected to TX of GSM and the TX should be connected in RX of GSM module.
- Next try to do this
GPRS.begin(9600);
Serial.begin(115200);
- Also double check your wirings, if you have a tester you might wanna check if both end are connected with each other. And in the loop try to print this
Serial.println(GPRS.available())
if it is not greater than 0 then you have a problem with your connection.
#include <SoftwareSerial.h>
SoftwareSerial sim(2,3);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
sim.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available() > 0){
sim.write(Serial.read());
}
while(sim.available() > 0){
Serial.write(sim.read());
}
}
diagram reference. enter image description here
Hope this help.
-
1why would you wire it to RX/TX of the Mega and what is the
GPRS
object in your answer?2023年06月23日 16:41:23 +00:00Commented Jun 23, 2023 at 16:41 -
Where did I get the GPRS? from his code. well actually just follow my supper simple diagram from the link bellow for you to understand imageup.me/images/a49982fe-c592-44d7-b696-ed80e725fec5.png and follow my supper simple code for you again to understand. :-) #include <SoftwareSerial.h> SoftwareSerial sim(2,3); void setup() { Serial.begin(115200); sim.begin(9600); } void loop() { while(Serial.available() > 0){ sim.write(Serial.read()); } while(sim.available() > 0){ Serial.write(sim.read()); } }homerio– homerio2023年06月25日 09:05:52 +00:00Commented Jun 25, 2023 at 9:05
-
@homerio Your link gives "Oops! An unhandled exception occurred while processing the request.".2023年06月25日 09:20:31 +00:00Commented Jun 25, 2023 at 9:20
-
@homerio Comments are not for posting images and code. Please amend your answer to make it clearer.2023年06月25日 09:28:19 +00:00Commented Jun 25, 2023 at 9:28
-
Follow this link also for the diagram reference - you can embed images into answers. That is better because external links can become link-dead.2023年06月25日 09:29:14 +00:00Commented Jun 25, 2023 at 9:29