Simple SMS application

Hi

I have a FXT009 and want to try the SMS functionality.

The code I use is taken from “AirPrime - Open AT Tutorial”

/* sample code implementing the SMS send functionality */
#include "adl_global.h"
#include "adl_sms.h"

const u16 wm_apmCustomStackSize = 4096;

// Local variables
u8 Event;
u16 Nb;
s8 smshandle;
ascii telno[]="0046703565444";
ascii *smstimelnth;
ascii *smstext="The text for sms goes here";
s8 sendhandle;
s8 unshandle;

//Local Functions
bool SmsHandler(ascii *telno, ascii *smstimelnth,ascii *smstext) {
	adl_atSendResponse(ADL_AT_UNS,"inside sms handler");
	return (0);
}

void Timerhdl(u8 id) {

	TRACE (( 1, "Unsubscribing the SMS" ));
	adl_smsUnsubscribe(smshandle);

}

/* sms control handler captures the events received on SMS sending*/
void SmsCtrlHandler(u8 Event,u16 Nb){

	/*TRACE((1,"INSIDE SMSCTRL HANDLER, telno="));
	TRACE((1,telno));
	TRACE((1,smstext));*/

	s8 sRet;
	switch(Event) {
		case ADL_SMS_EVENT_SENDING_OK:
			adl_atSendResponse(ADL_AT_RSP,"SMS Sent Successfully");
			TRACE((1,"Inside ADL_SMS_EVENT_SENDING_OK EVENT"));
			/* if the SMS send is successful, subscribe to timer of short duration and in
			the timer handler unsubscribe from the SMS service.*/
			adl_tmrSubscribe(FALSE, 10, ADL_TMR_TYPE_100MS, (adl_tmrHandler_t) Timerhdl);
			TRACE (( 1, "SMS sent successfully, now unsubscribe" ));
			break;

		case ADL_SMS_EVENT_SENDING_ERROR:
			adl_atSendResponse(ADL_AT_RSP,"error sending sms");
			sRet = adl_smsSend(smshandle,telno,smstext,ADL_SMS_MODE_TEXT);
			TRACE (( 1, "error sending sms, try again" ));
			break;

		case ADL_SMS_EVENT_SENDING_MR:
			TRACE (( 1, "sms send successful" ));
			TRACE((1,"Inside ADL_SMS_EVENT_SENDING_MR EVENT"));
			break;

		default:
			adl_atSendResponse(ADL_AT_UNS,"Inside default");
			break;
	}
}

bool wind_4_handler(adl_atUnsolicited_t * paras) {

	s8 sRet=0;
	TRACE((1,"Inside wind 4 handler"));
	sRet = adl_smsSend(smshandle,telno,smstext,ADL_SMS_MODE_TEXT);
	return (0);
}
// Main function
void adl_main ( adl_InitType_e InitType ) {

	TRACE (( 1, "Embedded Application: Main " ));
	smshandle=adl_smsSubscribe((adl_smsHdlr_f)SmsHandler,(adl_smsCtrlHdlr_f)SmsCtrlHandler,ADL_SMS_MODE_TEXT);
	adl_atUnSoSubscribe("+WIND: 4",wind_4_handler);
}

And the TRACES I get are these:

2013/05/17;13:32:31:241;002;ADL;1;Binary header at 002A0000
2013/05/17;13:32:31:242;001;ADL;16;[ADL PORT] subs (002A3759) : 0
2013/05/17;13:32:31:243;001;ADL;16;[ADL PORT] subs (002A537F) : 1
2013/05/17;13:32:31:244;001;ADL;22;[ADL] flash subs 2 : 0
2013/05/17;13:32:31:244;002;ADL;22;Flh Obj 0000 Len : 0
2013/05/17;13:32:31:245;001;ADL;16;[ADL PORT] event : 0 (port 80 ; state 0)
2013/05/17;13:32:31:246;001;ADL;16;[ADL PORT] event : 0 (port 01 ; state 0)
2013/05/17;13:32:31:247;001;ADL;22;Flh Obj 0000 Len : 0
2013/05/17;13:32:31:247;002;ADL;22;Write Flh Obj 0000 (4) : 0 ; 393192
2013/05/17;13:32:31:248;002;ADL;16;[ADL PORT] unsubs (1) : 0
2013/05/17;13:32:31:357;001;ADL;1;Embedded Application: Main 
2013/05/17;13:32:31:358;001;ADL;26;SMS subs : 0
2013/05/17;13:32:31:473;001;ADL;16;[ADL PORT] event : 0 (port 03 ; state 0)
2013/05/17;13:32:45:006;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:007;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:008;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:009;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:010;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:016;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:018;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:021;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:022;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:023;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:024;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:029;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:031;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:033;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:034;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:037;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:042;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:044;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:044;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:046;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:047;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:049;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:049;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:049;004;ADL;26;SMS send : 0
2013/05/17;13:32:45:049;005;ADL;1;error sending sms, try again
2013/05/17;13:32:45:049;007;ADL;26;SMS send : 0
2013/05/17;13:32:45:065;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:099;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:100;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:101;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:102;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:103;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:104;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:107;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:108;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:110;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:111;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:112;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:113;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:114;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:115;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:116;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:117;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:118;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:119;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:120;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:120;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:121;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:122;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:182;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:183;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:184;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:185;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:186;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:190;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:191;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:194;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:196;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:197;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:198;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:198;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:200;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:201;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:202;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:202;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:203;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:204;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:205;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:206;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:207;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:208;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:208;003;ADL;26;SMS send : 0
2013/05/17;13:32:45:209;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:210;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:211;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:212;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:212;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:213;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:214;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:217;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:218;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:219;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:219;002;ADL;1;error sending sms, try again
2013/05/17;13:32:45:220;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:221;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:222;002;ADL;26;SMS send : 0
2013/05/17;13:32:45:223;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:224;001;ADL;26;SMS send : 0
2013/05/17;13:32:45:225;001;ADL;1;error sending sms, try again
2013/05/17;13:32:45:230;001;ADL;1;Binary header at 002A0000
2013/05/17;13:32:45:231;001;ADL;16;[ADL PORT] subs (002A3759) : 0
2013/05/17;13:32:45:231;002;ADL;16;[ADL PORT] subs (002A537F) : 1
2013/05/17;13:32:45:232;001;ADL;22;[ADL] flash subs 2 : -4
2013/05/17;13:32:45:233;001;ADL;22;Flh Obj 0000 Len : 4
2013/05/17;13:32:45:234;001;ADL;22;Read Flh Obj 0000 (4) : 0
2013/05/17;13:32:45:236;001;ADL;16;[ADL PORT] event : 0 (port 80 ; state 0)
2013/05/17;13:32:45:237;001;ADL;16;[ADL PORT] event : 0 (port 01 ; state 0)
2013/05/17;13:32:45:238;001;ADL;22;Flh Obj 0000 Len : 4
2013/05/17;13:32:45:239;001;ADL;16;[ADL PORT] unsubs (1) : 0
2013/05/17;13:32:45:245;001;ADL;1;Embedded Application: Main 
2013/05/17;13:32:45:246;001;ADL;26;SMS subs : 0
2013/05/17;13:32:45:248;001;SYS;1;OAT Task index : 1
2013/05/17;13:32:45:250;002;SYS;1;Watch dog reset. Tsk 31
2013/05/17;13:32:45:285;001;ADL;16;[ADL PORT] event : 0 (port 03 ; state 0)

I assume that the sample code from the tutorial is bullet proof, so maybe I am doing something wrong.

BR

You have put the telephone number as
0046703565444

Try using
+46703565444
or
0703565444
instead.

I have tried altering the telephone number but the result is still the same…

Maybe there is something not working with the SIM card, should test that.

BR

From trace, SMS retry happening…

Suggest to first check the registration status, signal strength and SIM status, e.g.

AT+CPIN?
AT+CREg?
AT+CSQ

Also make sure the SMS service is activated on that SIM subscription.
You may also first test sending SMS via AT command.

Thanks for the reply!

This is what I get back when requesting the commands you suggested:

AT+CPIN?

+CPIN: READY

AT+CREg?

+CREG: 0,2

AT+CSQ

+CSQ: 22,3

I’m not sure how to respond to these commands, is there a AT command manual I have not found?

Regarding SMS service being activated on the SIM subscribtion, I am certain that it is.

I will try to find out how to send a sms through AT commands!

Best resgards

To send SMS via AT Commands:

AT+CMGS=072XXXXXXX

Press Enter key. You will get:

>

Type your message then press Ctrl+Z to send the message.

You really need to have a look at the AT command user guide though.

Your traces don’t correspond to the code you have provided. Ignoring all traces over level 14 (under level 14 is user traces and over is ADL traces), you should see the following sequence:

  1. Binary header at 002A0000
  2. Embedded Application: Main
  3. Inside wind 4 handler
  4. Inside ADL_SMS_EVENT_SENDING_OK EVENT or error sending sms or sms send successful

The traces you provided are missing trace 3 above, indicating that the WIND indication is not being received before trying to send the message. Maybe try the following code:

/* sample code implementing the SMS send functionality */
#include "adl_global.h"
#include "adl_sms.h"

const u16 wm_apmCustomStackSize = 4096;

// Local variables
u8 Event;
u16 Nb;
s8 smshandle;
ascii telno[]="+27721226194";
ascii *smstimelnth;
ascii *smstext="The text for sms goes here";
s8 sendhandle;
s8 unshandle;

enum
{
    NOT_REGISTERED_NOT_SEARCHING,
    REGISTERED_HOME,
    NOT_REGISTERED_SEARCHING,
    REGISTRATION_DENIED,
    UNKNOWN,
    REGISTERED_ROAMING
};

//Local Functions
bool SmsHandler(ascii *telno, ascii *smstimelnth,ascii *smstext)
{
   adl_atSendResponse(ADL_AT_UNS,"inside sms handler");
   return (0);
}

void Timerhdl(u8 id)
{

   TRACE (( 1, "Unsubscribing the SMS" ));
   adl_smsUnsubscribe(smshandle);

}

/* sms control handler captures the events received on SMS sending*/
void SmsCtrlHandler(u8 Event,u16 Nb)
{
   /*TRACE((1,"INSIDE SMSCTRL HANDLER, telno="));
   TRACE((1,telno));
   TRACE((1,smstext));*/

   s8 sRet;
   switch(Event) {
      case ADL_SMS_EVENT_SENDING_OK:
         adl_atSendResponse(ADL_AT_RSP,"SMS Sent Successfully");
         TRACE((1,"Inside ADL_SMS_EVENT_SENDING_OK EVENT"));
         /* if the SMS send is successful, subscribe to timer of short duration and in
         the timer handler unsubscribe from the SMS service.*/
         adl_tmrSubscribe(FALSE, 10, ADL_TMR_TYPE_100MS, (adl_tmrHandler_t) Timerhdl);
         TRACE (( 1, "SMS sent successfully, now unsubscribe" ));
         break;

      case ADL_SMS_EVENT_SENDING_ERROR:
         adl_atSendResponse(ADL_AT_RSP,"error sending sms");
         sRet = adl_smsSend(smshandle,telno,smstext,ADL_SMS_MODE_TEXT);
         TRACE (( 1, "error sending sms, try again" ));
         break;

      case ADL_SMS_EVENT_SENDING_MR:
         TRACE (( 1, "sms send successful" ));
         TRACE((1,"Inside ADL_SMS_EVENT_SENDING_MR EVENT"));
         break;

      default:
         adl_atSendResponse(ADL_AT_UNS,"Inside default");
         break;
   }
}

static bool gsmCREGHandler (adl_atUnsolicited_t * AT_Response)
{
    adl_strID_e str_id = adl_strGetID(AT_Response->StrData);

    if (str_id != ADL_STR_OK)
    {
        /* Get the CREG Stat (0 - 5)
         * 0 not registered, ME is not currently searching for a new operator
         * 1 registered, home network
         * 2 not registered, ME currently searching for a new operator
         * 3 registration denied
         * 4 unknown
         * 5 registered, roaming
         */
        static ascii str_CREGParameter[2];
        wm_strGetParameterString(str_CREGParameter, AT_Response->StrData, 1);
        s8 creg_stat = wm_atoi(str_CREGParameter);

        TRACE((1, AT_Response->StrData));

        switch (creg_stat)
        {
            case NOT_REGISTERED_NOT_SEARCHING:

                TRACE((1, "[CREGHandler] Not Registered, not searching for operator."));
            break;

            case REGISTERED_HOME:
                TRACE((1, "[CREGHandler] Registered on Home network"));
            break;

            case NOT_REGISTERED_SEARCHING:
                TRACE((1, "[CREGHandler] Not Registered. Searching for operator..."));
            break;

            case REGISTRATION_DENIED:
                TRACE((1, "[CREGHandler] Registration denied"));
            break;

            case UNKNOWN:
                TRACE((1, "[CREGHandler] Registration status Unknown"));
            break;

            case REGISTERED_ROAMING:
                TRACE((1, "[CREGHandler] Registered on Roaming network"));
            break;

            default:
                TRACE((1, "[CREGHandler] Unknown Status."));
            break;
        }

        if ((creg_stat == REGISTERED_ROAMING) || (creg_stat == REGISTERED_HOME))
        {
            s8 sRet = adl_smsSend(smshandle,telno,smstext,ADL_SMS_MODE_TEXT);

            TRACE((1, "adl_smsSend result: %d", sRet));
        }
    }

#ifdef __DEBUG_APP__
    return TRUE;
#else
    return FALSE;
#endif
}

// Main function
void adl_main ( adl_InitType_e InitType )
{

   TRACE (( 1, "Embedded Application: Main " ));

   smshandle=adl_smsSubscribe((adl_smsHdlr_f)SmsHandler,(adl_smsCtrlHdlr_f)SmsCtrlHandler,ADL_SMS_MODE_TEXT);
   TRACE (( 1, "SMS subscribe handle: %d", smshandle ));

   // Enable unsolicited GSM registration message
   adl_atCmdSend("AT+CREG=2", NULL, NULL );

   // Subscribe to GSM registration messages
   adl_atUnSoSubscribe("+CREG", gsmCREGHandler);
}

I get these traces (only enable traces on ADL level 1):

2013/05/20;17:44:55:971;001;ADL;1;Binary header at 00260000
2013/05/20;17:44:55:973;001;ADL;1;Embedded Application: Main 
2013/05/20;17:44:55:976;001;ADL;1;SMS subscribe handle: 0
2013/05/20;17:44:58:486;001;ADL;1;<CR><LF>+CREG: 2<CR><LF>
2013/05/20;17:44:58:490;001;ADL;1;[CREGHandler] Not Registered. Searching for operator...
2013/05/20;17:45:20:792;001;ADL;1;<CR><LF>+CREG: 1,"2AAC","2B67"<CR><LF>
2013/05/20;17:45:20:795;001;ADL;1;[CREGHandler] Registered on Home network
2013/05/20;17:45:20:798;001;ADL;1;adl_smsSend result: 0
2013/05/20;17:45:24:809;001;ADL;1;sms send successful
2013/05/20;17:45:24:812;001;ADL;1;Inside ADL_SMS_EVENT_SENDING_MR EVENT
2013/05/20;17:45:24:815;001;ADL;1;Inside ADL_SMS_EVENT_SENDING_OK EVENT
2013/05/20;17:45:24:818;001;ADL;1;SMS sent successfully, now unsubscribe
2013/05/20;17:45:25:804;001;ADL;1;Unsubscribing the SMS

This is true, but undocumented: Undocumented: ADL_TRC_ Trace Levels used internally by ADL - #10 by awneil

User code can use levels >14.

Not all levels >14 are actually used by ADL - some are never used (any more); some are only used by specific features.

Thanks tomridl for your answer, I found the AT command user guide after some digging.

It works to send SMS with the AT commands.

Your example code worked when I changed the method head of the main from:

void adl_main ( adl_InitType_e InitType )

to

void main ( void)

And then I realized that that might have something to do with why my example is not working, but when I changed my method head the same way I get this error output:

../src/adl_defaut_tasks.c:(.text+0x6): undefined reference to `adl_main'
c:/program files (x86)/sierra wireless/developer studio/tools/armeabigcc/4.4.1/bin/../lib/gcc/arm-none-eabi/4.4.1/../../../../arm-none-eabi/bin/ld.exe: TEST_SMS.axf: hidden symbol `adl_main' isn't defined

According to the information provided, SIM is detected and signal strength is quite good, however from (+CREG: ,)

Stat “2” actually means not registered, ME currently searching for a new operator.

It is true that it takes some time for module to search and register with network carrier, the signal coverage and band selection affect the time as well.
But this explains why we found several SMS resend in traces, as network is not yet available thus retry.

tomridl’s code will solve this problem as function gsmCREGHandler() is taking care the registration state before sending SMS.
Probably you can fine tune the sample code to wait before sending sms or take care the +CREG status same as tomridl.

Hope it helps.

No - that’s not the case, I’m afraid.

In fact, it is very seldom (if ever) the case with any “sample code” from any source.

The thing is, with any real-life application, only about 20% of the code deals with the “normal” or “successful” case - the other 80% is to do with all the possible exceptions and errors.
This is sometimes known as the “80:20 rule” (or even “90:10”); eg,

8052.com/forum/read/96560

8052.com/forum/read/100862
8052.com/forum/read/100866

Clearly, if “sample code” were written to be fully “bullet proof”, the central point being illustrated (how to send an SMS, in this case) would just get lost in all the noise!

Hence “sample code” should never be taken as “production-ready” :exclamation:

Remember that there are two ways to write an ADL application: The ‘old’ way (single task using adl_main, which has been depreciated), and the ‘new’ way, using multiple tasks, the task initialisation table and no adl_main function. Have a look at the beginning of the ADL user guide where this is explained. Most of the sample applications have not been updated to use the ‘new’ method, however this is the method that should be used. If you have a generated.c file in your project, you are most likely using the new way.