Let's say that I've tried sending an SMS directly from serial monitor or any kind of terminal. For whatever reason (syntax error, etc), my attempt fails. Now, I try sending it again, but this time by code (as seen on this site):
#include <SoftwareSerial.h>
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
void setup() {
//Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
while(!Serial);
//Being serial communication witj Arduino and SIM800
serialSIM800.begin(9600);
delay(1000);
Serial.println("Setup Complete!");
Serial.println("Sending SMS...");
//Set SMS format to ASCII
serialSIM800.write("AT+CMGF=1\r\n");
delay(1000);
//Send new SMS command and message number
serialSIM800.write("AT+CMGS=\"07194XXXXX\"\r\n");
delay(1000);
//Send SMS content
serialSIM800.write("TEST");
delay(1000);
//Send Ctrl+Z / ESC to denote SMS message is complete
serialSIM800.write((char)26);
delay(1000);
Serial.println("SMS Sent!");
}
void loop() {
}
The code sends not only the SMS within the code ("TEST"), but also the unwanted junk / remains of the previously unsuccessful sending attempt.
Is there a way to clear the module's buffer before sending a new message?
I guess I could restart the module before sending messages, but I was wondering whether there's a cleaner solution.
P.S. I apologize for the slightly misleading and irrelevant tags, but I can not use the nonexistent sim800L, which would be better suited for my question.
-
Do you get "TEST[...AT COMMANDS...]TEST" in your SMS? I.e., does it appear that the previous SMS doesn't get terminated and everything sent for the second SMS gets appended to it and then sent?Majenko– Majenko2016年10月17日 10:45:32 +00:00Commented Oct 17, 2016 at 10:45
-
Yes, that's exactly what I receive. Since the attempt fails, the AT commands and previous text stay, and I don't even get to send CTRL-Z, in order to denote message end. And everything that stays, gets picked up on the next try.FiddlingAway– FiddlingAway2016年10月17日 11:48:54 +00:00Commented Oct 17, 2016 at 11:48
1 Answer 1
According to the SIM800 AT Command Set you can terminate the sending of an SMS part way through by sending ESC
(character 27).
So you can begin your sketch by first sending a number of ESC
to ensure that any pre-existing text input for an earlier SMS is abandoned.
serialSIM800.write(27);
serialSIM800.write(27);
serialSIM800.write(27);
Only one should be needed, but it is best to repeat it a couple of times in case the first one isn't taken. You should then do a CR/LF
to make sure it's ready for the next command.
serialSIM800.print("\r\n");
However, your method of controlling the modem is crude at best and not particularly reliable. The modem sends back responses from all the commands you send it. You should be reading in those responses and checking that the commands you sent actually worked. That way you can know if a command hasn't been accepted because it is in SMS reading mode (no response within 1 second, say), and you can force an escape.
-
Works fine, thanks. I can't believe I didn't try that. In regards to the code, it is indeed crude. I've yet to define or implement any kind of error catching, and devise a proper response to it.FiddlingAway– FiddlingAway2016年10月18日 07:11:02 +00:00Commented Oct 18, 2016 at 7:11