[SOLVED] AT Command to SIM900 Returns 'ERROR'

Hello community. Once again I'm desperate need of your assistance... :confused:

I'm controlling Arduino Uno with VB.NET application which is working just fine. I also have SIM900 shield connected to Arduino as I need to send text messages to various numbers. The phone numbers are extracted from MySQL DB by the VB application and send to Arduino. This is my first time using SIM900 and AT commands so I checked Google and there are hundreds of simple guides out there for general uses. So at first I tested with the simple 4-line send SMS program which worked well and then I switched to 'call' command which is just 1 line and cost no money to test with it while developing.

So far everything that worked perfectly was this - VB app sends this:

spArduino.WriteLine("<ATD+ " & phone & ";>")

where 'phone' was the extracted number from the DB. And in Arduino it looked like this:

if (receivedChars[0] == 'A') {
    digitalWrite(indication, HIGH);
    SIM900.println(receivedChars);
    //updateSerial();
    delay(1000);
    digitalWrite(indication, LOW); }

The above worked outstandingly. So, I reached the very end of my project and all I had left to do is switch the voice call command to SMS command.

The VB command became:

spArduino.WriteLine("<AT + CMGS = \""" & phone & "\"">")

...and the Arduino code became:

if (receivedChars[0] == 'A') {
    digitalWrite(indication, HIGH);
    SIM900.println("AT+CMGF=1");
    updateSerial();
    SIM900.println(receivedChars);
    updateSerial();
    SIM900.println("sms body"); 
    updateSerial();
    SIM900.println((char)26);
    updateSerial();
    SIM900.println();
    delay(1000);
    updateSerial();
    digitalWrite(indication, LOW); }

And it turned out for some reason this is not working. I turned on the debugging 'updateSerial();' to monitor what's happening and it appears the trouble line is 'SIM900.println(receivedChars);'.

If I change it to 'SIM900.println("AT+CMGS="+35xxxxxxxxx5"");' it works but when it comes from VB it doesn't. I checked the whole line symbol by symbol and it's absolutely the same, it makes no sense not to work. I also tried splitting the command in 2 with end line:

spArduino.WriteLine("<\""" & phone & "\"">")
if (receivedChars[2] == '+') {
    digitalWrite(indication, HIGH);
    SIM900.println();
    SIM900.println("AT+CMGF=1");
    updateSerial();
    SIM900.println();
    SIM900.print("AT+CMGS="); 
    SIM900.print(receivedChars);
    SIM900.println();
    updateSerial();
    SIM900.print("sms body");
    delay(3000);
    SIM900.println();
    SIM900.write(26); 
    updateSerial();
    digitalWrite(indication, LOW);

Same error on the same line. I thought maybe the '' symbols are doing something wrong inside the array as this is the only difference between the voice call command and the SMS command so I tried converting to String and use it like that but still the same error on the same place.

I honestly have no idea what else I can try and I'd really appreciate any advise here.

Thank you.

Looks to me like your escaping of the quotes round the phone number is wrong.

You only need " when you are writing the string in the Arduino IDE to be compiled. That’s so the compiler doesn’t think the " is the closing quote that terminates the string. The compiler removes the backslash and creates a compiled string with a single unescaped " in its place.

From your debug output it looks like the received telephone number at the Arduino end contains actual backslash escapes. That’s wrong.
a) You don’t need to escape quotes when sending over serial
and
b) Escaping of special characters doesn’t use backslash in VB. A quote is escaped by having two of them consecutively.

So your vb code should be something like...

 spArduino.WriteLine("<""" & phone & """>")

Which will send this over the serial...

<"123456789">

And should result in this being sent to the module...

AT+CMGS="123456789"

But as you’ve only shown snippets, not a complete sketch, it’s hard to tell exactly what format your receiver expects for an incoming phone number.

Thank you! Seing your example made me realize I can actually use the split command variant and take the special symbols outside the array. So, finally, it worked with this:

VB:

spArduino.WriteLine("<" & phone & ">")

Arduino:

    SIM900.print("AT+CMGS=\""); 
    SIM900.print(receivedChars);
    SIM900.print("\"");
    SIM900.println();

And it works just fine now. Why didn't I see this opportunity earlier... Anyway, thank you again!