Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Sending and receiving SMS messages with Sony Ericsson phones

Using the AT command set

17 June 2010
Contact me at: flippersms-arduino {at } yahoo { dot } co { dot } uk


I recently decided to get stuck in with sending and receiving text messages from an old SonyEricsson phone I got off eBay (I got a T210).

I found brilliant articles and forum threads on getting it up and running really easily, particularly these ones:

http://forum.arduino.cc/index.php/topic,22411.html

So I bought the phone and decided to get stuck in.

Now, the wiring up was easy - as described in the above article - I just got a SonyEricsson USB cable and chopped off the end and wired that into my TX/RX/ground pins (well not actually my ones, the ones on my Arduino).

However, sending and receiving SMS messages proved to be a little harder. This is partly because each phone seems to work in a slightly different way, and also because some phones will only work using the horrible, horrible, painful PDU method.

Luckily the T210 I bought can just use plain text to send and receive SMS, these phones are so cheap I can't really see any point in faffing about with PDU when you can just buy one for £10 that you can talk to in plain text.

So anyway, these are the commands I use with the phone to make it work beautifully:

To initialise at startup:

AT&F (Reset to factory settings)
AT+CMGF=1 (Turn off the horrors of PDU mode)
AT+CPMS="ME" (Save SMS messages to phone memory, not SIM)
AT+CNMI=,1 (When SMS message received - send notification to serial)

So there we go - easy as that.

To send a text message:

AT+CMGS="[phone number]"

So for example:
AT+CMGS="+332732894022"

After entering that command, you then type your message. When you've finished typing your message, you send a Ctrl-Z and the phone should send it.

To receive a text message

When the phone receives a message, it should send the following text:

+CMGL: [number]
Where number is the text message number in the phone, e.g. +CMGL: 1

NOTE: I need to check if this is the exact string - if not it's very similar to this

To view the contents of this message, just enter the command:

AT+CMGR=[message number]

When you've extracted the information, then delete the message (as there isn't much phone memory to hold these):

AT+CMGD=[message number]

:: The code ::

Yes, yes I know all of this is very, very bad code and doesn't check for errors particularly well, but it does work for me reliably and consistently.

So, first up, to initialise the phone:


String send_command(String at_cmd, int dly){

  String msg;
  int incomingByte;

  Uart.println(at_cmd);
  delay(dly);
  while (Uart.available() > 0) 
  {
    incomingByte = (Uart.read());
    msg = msg + char(incomingByte);
  }  
  delay(1000);

  return msg;
}

void setup() {

  Uart.begin(9600);
  Serial.begin(9600);

  String at_response;

  if (send_command("AT&F",500) == "AT&F\r\r\nOK\r\n") {
    Serial.println("Modem reset...");
  }

  if (send_command("AT+CMGF=1",500) == ("OK\r\n")) {
    Serial.println("Non-PDU mode set...");
  }

  at_response = send_command("AT+CPMS=\"ME\"",500);
  if (at_response.substring(at_response.length()-4) == ("OK\r\n")) {
    Serial.println("Messages saved to phone memory set...");
  }

  at_response = send_command("AT+CNMI=,1",500);       
  if (at_response.substring(at_response.length()-4) == ("OK\r\n")) {
    Serial.println("Messages routed to terminal...");
    Serial.println("Ready to go (I hope)...");
  }

}


Next up, a little function to send an SMS (note - this uses the send_command function from the code snippet above):


int send_SMS(String phoneno, String msgtext) {
  int result = 0; 
  Serial.println("Sending SMS to "+phoneno+": "+msgtext);
  String at_response = send_command("AT+CMGS=\""+phoneno+"\"",500);
  Serial.write(".");
  at_response = send_command(msgtext,500);
  Serial.write(".");

  at_response = send_command(char(26),5000);
  Serial.println(".");

  if (at_response.substring(at_response.length()-4) == ("OK\r\n")) {
    Serial.println("Message successfully sent!");
    result = 1;
  }
  if (at_response.substring(at_response.length()-4) == ("OR\r\n")) {
    Serial.println("Error sending message!");
  }

  return result;
}



Will post up the code to receive and parse SMS messages when I get a chance..