Search code examples
arduinoat-commandsim900

Sending message to a number stored as String in Arduino


I am using SIM900 with arduino mega and have to send a message to a specific number I store in a variable, using AT commands. I am storing the number as a String but it gives an error. Below are the relevant lines of code:

String number1 = "923360234233";
Serial1.write("AT+CMGS=\"" + number1 + "\"");

It gives the following error.

no matching function for call to 'HardwareSerial::write(StringSumHelper&)'

What am I doing wrong here?


Solution

  • Method write can be used only for C-strings char *, uint8_t * and similar buffers.

    However if you've used string addition: const char * + String + const char * you'll get StringSumHelper which is not supported by write.

    So you can use:

    Serial1.print("AT+CMGS=\"" + number1 + "\"");
    

    or

    Serial.write(("AT+CMGS=\"" + number1 + "\"").c_str());
    

    or

    Serial1.write("AT+CMGS=\"");
    Serial.print(number1);
    Serial.write("\"");