Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

Telit Cinterion IoT Developer Community

How to send text messages

Tutorial, January 27, 2014 - 7:45pm, 120753 views

Experience Level
Basic

Motivation/Description
Learn how to send text messages using  Cinterion® Java Modules.

Covered topics

  • SMS related commands
  • Text mode
  • PDU (Protocol Data Unit) mode
  • Sending AT commands
  • CTRL+Z in Java

Keywords/Tags
sms, short message, message, registration, at command, creg, cmgf, cmgs, ctrl+z, sub, substitute

Scenario

  • Create an ATCommand object
  • Make sure the module is registered to the network
  • Switch to the text mode
  • Use AT+CMGS command to provide the receiver number
  • Type the message text
  • Terminate with the Ctrl+Z

Prerequisites

  • IDE (this tutorial uses Eclipse as an IDE)
  • EHS5/EHS6/TC65i/EGS5/Concept Board
  • SIM card

Actual Tutorial

The SMS messaging is quite an old network service. Never the less it is still very popular, even in the age of video calls. The biggest advantage for the M2M world is that SMS messaging is possible at any place where a cellular network is available. Even if the signal quality is pure and the voice call is impossible - it is possible that the SMS will work.

People are still exchanging text messages because it is very simple to use this service on mobile phones. There is no difference in the Gemalto Modules.

Create an ATCommand object

Since the text messages are sent via the AT commands, we will need to create the ATCommand object. It will give us an access to the module’s command interpreter.

We will use one of the ATCommand constructors. We don’t want to use CSD on this object, so we will set the constructor csd parameter to false.

 ATCommand atc = new ATCommand(false); 

Make sure the module is registered to the network

To send the textmessages the module ***** to be registered to a network. You can simply check this using the AT+CREG? command. The response should contain “+CREG: 0,1” or, in case of roaming “+CREG: 0,5”. The other values indicate that the module is not registered.

To check the registration status of the module we need to send the +CREG command and parse the response. More detailed description of the AT+CREG command you can find in the AT command specification (Chapter 8.5).

String registration_response = atc.send("AT+CREG?\r");
	
int localy_registered = registration_response.indexOf(",1");
int roaming_registered = registration_response.indexOf(",5");
		
if((localy_registered >-1 ) && (roaming_registered >-1)){
	System.out.println("Module registered to the network");
}else{
	System.out.println("Module not registered to the network");
}

If you are not sure how to register to the network, or want to get more information regarding this procedure, please check our How to register to a network tutorial (coming soon). All information about the registration you will also find in the AT command specification of the module.

Switch to the text mode

Now we will finally start to use the text message function. First of all, you need to know, that there are two modes of sending the Short Messages.

text 

  • for simple text messages; provides an easy procedure of creating the text message. It does not allow setting any advance SMS parameters like SMS class PID etc.

PDU   

  • for advanced usage; It allows to create the messages with the advanced parameters changed. It requires a complex creator, which compiles all the parameters into a hexadecimal data package.

Since the PDU mode, in comparison to the text mode, is really complex– we will present the text mode.

The module is by default in the PDU mode. To change this we need to send the AT+CMGF=1 command.

 String at_cmd_response = atc.send("AT+CMGF=1\r"); 

The value ‘1’ stands for text mode. You can verify if the command was executed properly, by checking if the response contains the „OK“.

If the command was invoked properly the current AT command interpreter will be in text mode. It means that now we can send SMS by just providing the receiver number and the body of the message. The module will combine the PDU data for us and it will send it to the network afterwords.

To send the message we use the AT+CMGS command. When you send it, don’t expect the OK response. The proper response for this command is just ‘>’. This is a character indicating that the body of the module is ready to get the SMS message. The only way for you to go further is to provide the text which you would like to send.

at_cmd_response = atc.send("AT+CMGS=\”+49123456789\”\r");
System.out.println(at_cmd_response);
		
at_cmd_response = atc.send("Message \r text" + (char) 26);

Notice that we did not confirm the end of the message so far. Even the ‘\r’ character did not terminate the command. This is because the new line may be also the part of the text message. The termination character in this case is CTRL+Z instead of the carriage return like in other commands.

Maybe you perceive typing the CTRL+Z  in Java code, like the easiest thing on the world – and you are right. Since the CTRL+Z command is a so-called substitute character it is on the ASCII table. The decimal value for it is ‘26’. You just need to send the char primitive of this value and the module will react like you would press CTRL+Z on the keyboard.

If there is some issue with sending the message (e.g. no network coverage) you will get the ERROR response. If the message is sent successfully you will get OK.

Now you should receive the text message on the target device.

Outlook: Suggested next steps (for further training)

When you have learnt how to send the SMS from the Java application, you can try to send more complex messages e.g. including some special characters like - $”@. If you have any issues with that please check the ATStringConverter class.

You may also want to check the following Gemalto Java Tutorials:

  • How to receive SMS
  • How to use SocktetConnection
  • How to use GPIOs
  • How to use the BubbleBoard buttons
  • And many others...

Resources

Software:

  • Gemalto M2M Ehs5 Install CD

Documentation:

  • wm02_java_users_guide_v04.pdf
  • module AT command specification

Code:

  • SmsSender.java

Other:

  • SmsSender.zip – Archive with the full eclipse project ready to be imported
  • SmsSender.jad
  • SmsSender.jar
km's picture
km
Pretty sure that the module can be either registered local and roaming not both. Line 6: if((localy_registered >-1 ) && (roaming_registered >-1)){ Should be: if((localy_registered >-1 ) || (roaming_registered >-1)){

Nice Hint!!!!

Somewhere over the rainbow!!! Looking for the Oz Land!!!

help?
Where is link?

Hi,

This is all very well, but I am having issues at the moment accessing the CMTK to access suitable package for ATCommand Java Objects in my Java IDE.

I'm working on an EHS6T terminal and have no such installation CD with any CMTK or WTK sdk and files.

I attempted to join your Cinterion Concept Board community by registering and providing IMEI of the abovementioned terminal. It seems to reject the IMEI saying Invalid IMEI entry.

I've been searching all over your forum for help and others have asked a similar question with no suitable guidance at all.

It's 2018...do you plan to fix this error? Otherwise how am I supposed to develop Java ME applications on the EHS6T terminal??

Apologies for the expression of frustration.

Distributor provided this some time shortly after .

Many thanks.

There appears to be no hyperlinks to the other tutorials or to the code/documentation? How can I download them? Thanks.

Author

Mani Gemalto Moderator's picture
Mani Gemalto Moderator