I've been testing a sketch found here by an active contributor, used to test and wait for a response from a GSM modem, for commands sent. Using this send SMS example (with a SIM7670G), the code sends not just the const char * SMSMessage = "New Message";
but it sends a message like this:
I have tested many different sketches using the actual number and message text, but I want to use variables for the phone number and message like this example.
I can't see in the code why this would be happening, any advice on this appreciated.
//---- courtesy of John Wasser ---
//---- Sends "AT+CMGF=1 AT+CMGS+"+17xxxxxxxx3" New Message" all as a text message ??? ----
#include <SoftwareSerial.h>
SoftwareSerial ATCommandStream(3, 4);
unsigned long ATComandBaudRate = 9600;
const char * DestinationNumber = "+17xxxxxxxx3";
const char * SMSMessage = "New Message";
void setup()
{
// start th serial communication with the host computer
Serial.begin(9600);
while (!Serial);
delay(200);
Serial.println("Sketch started.");
ATCommandStream.begin(ATComandBaudRate);
Serial.print("ATCommandStream started at baud rate ");
Serial.println(ATComandBaudRate);
// Set the SMS output to text mode
SendShortCommand("AT+CMGF=1");
SendSMSMessage(DestinationNumber, SMSMessage);
SendShortCommand("AT"); // Just checking that it responds with OK
}
void loop() {}
bool WaitForResponse()
{
unsigned long startTime = millis();
while (millis() - startTime < 5000)
{
String reply = ATCommandStream.readStringUntil('\n');
if (reply.length() > 0)
{
Serial.print("Received: \"");
Serial.print(reply);
Serial.println("\"");
if (reply.startsWith("OK"))
return true;
if (reply.startsWith("ERROR"))
return false;
}
}
Serial.println("Did not receive OK.");
return false;
}
bool SendShortCommand(String command)
{
Serial.print("Sending command: \"");
Serial.print(command);
Serial.println("\"");
ATCommandStream.print(command);
ATCommandStream.print("\r\n");
return WaitForResponse();
}
void SendSMSMessage(const char *number, const char *message)
{
Serial.println("Sending SMS text");
Serial.print("Sending: ");
Serial.print("AT+CMGS=\"");
Serial.print(number);
Serial.println("\"(CR)");
Serial.print(message); // The SMS text you want to send
Serial.println("(EM)");
ATCommandStream.print("AT+CMGS=\""); // Send SMS
ATCommandStream.print(number);
ATCommandStream.print("\"\r"); // NOTE: Command ends with CR, not CRLF
ATCommandStream.print(message); // The SMS text you want to send
ATCommandStream.write(26); // ASCII EndMessage (EM) CTRL+Z character
WaitForResponse();
}
void setup()
{
// start th serial communication with the host computer
Serial.begin(9600);
while (!Serial);
delay(200);
Serial.println("Sketch started.");
ATCommandStream.begin(ATComandBaudRate);
Serial.print("ATCommandStream started at baud rate ");
Serial.println(ATComandBaudRate);
delay(5000);
// Set the SMS output to text mode
SendShortCommand("AT+CMGF=1");
SendSMSMessage(DestinationNumber, SMSMessage);
SendShortCommand("AT"); // Just checking that it responds with OK
}
Sketch started.
ATCommandStream started at baud rate 9600
Sending command: "AT+CMGF=1"
Received: "AT+CMGF=1
"
Received: "OK
"
Sending SMS text
Sending: AT+CMGS="+17xxxxxxxx3"(CR)
New Message(EM)
Received: "AT+CMGS="+17xxxxxxx63"
New Message
"
Received: "> "
Did not receive OK.
Sending command: "AT"
Received: "AT"
Did not receive OK.
Restarting module to retest again...
It worked after restarting Nano and SIM7670G module.
Here is Serial monitor output:
f���`f���~�������`�~`fSketch started.
ATCommandStream started at baud rate 9600
Sending command: "AT+CMGF=1"
Received: "AT+CMGF=1"
Did not receive OK.
Sending SMS text
Sending: AT+CMGS="17xxxxxxxx3"(CR)
New Message(EM)
Received: "AT+CMGS="17xxxxxxxx3"
New Message
"
Received: "+CMGS: 8
"
Received: "
"
Received: "OK
"
Sending command: "AT"
Received: "
"
Received: "+CPIN: READY
"
Received: "
"
Received: "SMS DONE
"
Received: "AT
"
Received: "OK
"
Can you remove most of the Serial prints to limit the interruptions and also add a string to wait for instead of the hardwired « OK » so that you really await what’s needed like the > for when sending the sms