I found a mistake in my sketch. I had accidentally turned "CMGW" into "CMGS" and I also left out the "CMSS" which does the actual send. One problem is that the CMSS command needs to know what SMS memory location was set by CMGW. I added code to "WaitForReply" that grabs the number reported by CMGW and then I can use that when sending "AT+CMSS: n".
Try this new version:
#include <SoftwareSerial.h>
SoftwareSerial ATCommandStream(2, 3);
// Connect the ATCommandStream TX to Arduino pin 2 RX.
// Connect the ATCommandStream RX to Arduino pin 3 TX.
const char * number = "+639358861057";
int MemoryWriteLocation = 1;
void setup()
{
// start th serial communication with the host computer
Serial.begin(19200);
while (!Serial);
Serial.println("Arduino with ATCommandStream is ready");
// start communication with the ATCommandStream in 19200
ATCommandStream.begin(19200);
Serial.println("ATCommandStream started at 19200");
delay(1000);
Serial.println("Setup Complete! ATCommandStream is Ready!");
// Turn on verbose messages
SendShortCommand("AT+CMEE");
// Set character set to GSM:
SendShortCommand("AT+CSCS=\"GSM\"");
// Set the SMS output to text mode
SendShortCommand("AT+CMGF=1");
// Check the phone number type
SendShortCommand("AT+CSTA?");
Serial.println("Note: 129=Unknown, 161=National, 145=International, 177=Network Specific");
// Test for CMGS command support:
SendShortCommand("AT+CMGS=?");
}
void loop()
{
// Keep reading from SIM900 and send to Arduino Serial Monitor
if (ATCommandStream.available())
{
char c = ATCommandStream.read();
Serial.write(c);
}
// Keep reading from Arduino Serial Monitor and send to ATCommandStream
if (Serial.available())
{
char c = Serial.read();
if (c == 's')
{
SendSMSMessage(number, "sim900a sms");
}
}
}
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("+CMGW:"))
{
reply.remove(0, 6); // Strip off "+CMGW:"
MemoryWriteLocation = reply.toInt();
}
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 followed by Ctrl-Z/ESC");
Serial.print("Sending: ");
Serial.print("AT+CMGW=\"");
Serial.print(number);
Serial.println("\"(CR)");
Serial.print(message); // The SMS text you want to send
Serial.println("(ESC)");
ATCommandStream.print("AT+CMGW=\""); // Write message to memory
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 code of CTRL+Z
WaitForResponse();
String command = "AT+CMSS=" + String(MemoryWriteLocation);
SendShortCommand(command); // Send message in memory location 1
}