Sending SMS from Particle Electron using AT Commands

I think one of the powerful API in Electron is the Cellular APIs. With Cellular APIs you can turn on/off the cellular module, read the RSSI, set custom APN in case you are using your own SIM card, etc… One of the interesting and most powerful API is Cellular.command. This API allows you to send AT commands directly to the cellular module. This is a powerful API, especially it gives you the ability to send AT commands supported by the U-Blox cellular module and parse the response.

Here is a sample application to text messages (SMS) using the Cellular.command API. You can refer to this U-Blox Commands Manual and this Example Application Note for the supported AT Commands. From the document you can see that +CMGS command can be used to send direct message. Format to send this AT Command is:

AT+CMGS="<number>"<129/145><CR><message body><ctrl+z>

An Example is:

AT+CMGS="+00123456789",145<CR>Message Body<Ctrl+Z>

The first part AT+MGS="123456789",145<CR> specifies the telephone number and its address type. Address type can be 129 or 145. The type 129 specifies the number is formatted with ISDN/ITU but does not specify whether it is international, national or other type. The type 145 specifies this is ISDN/ITU formatted and is an international number starts with “+” symbol. The last character in the first part is Carriage Return (CR), ASCII code 13 (0x0D).

The second part is the message body terminates with Ctrl+Z ASCII Code 26 (0x1A).

Sample Application

The sample application uses a TSL256 Luminosity sensor to measure light value, If it is below a configurable minimum value, it will send text message to a predefined telephone number. Please not that here I am using third party (my own) SIM, not Particle SIM. I didn’t test it with Particle SIM.

A sample text message is

It's too dark here (9.25 lux)

Here is the a code snippet from the application that sends text message:

int sendMessage(char* pMessage){
    char szCmd[64];
    
    sprintf(szCmd, "AT+CMGS=\"+%s\",145\r\n", szPhoneNumber);
    
    Serial.print("Sending command ");
    Serial.print(szCmd);
    Serial.println();
    
    char szReturn[32] = "";
    
    Cellular.command(callback, szReturn, TIMEOUT, "AT+CMGF=1\r\n");
    Cellular.command(callback, szReturn, TIMEOUT, szCmd);
    Cellular.command(callback, szReturn, TIMEOUT, pMessage);
    
    sprintf(szCmd, "%c", CTRL_Z);
    
    int retVal = Cellular.command(callback, szReturn, TIMEOUT, szCmd);
    
    if(RESP_OK == retVal){
        Serial.println("+OK, Message Send");
    }
    else{
        Serial.println("+ERROR, error sending message");
    }
    
    return retVal;
}

Schematics

Screenshot

Demo Video

12 Likes

Thanks for posting this tutorial up. Up until this point I didn’t think you could send SMS messages with the Electron.

How about sending text messages to the Electron and reading the text to trigger functions? Have you tried that yet?

@RWB, I too thought about that but I didn’t find any trigger functions, not sure whether this is possible or not.

I’m just wondering if the Electron can receive Text messages send from your normal cell phone.

Like can I send a Text message to the Electron that says “ARM Alarm”, then have the Electron parse that incoming message and then trigger a function I wrote that would trigger my home alarm system to turn on and be active.

Or are you saying that there is no Alert pin on the Ublox module to trigger the SMT32 micro to take action when a new SMS is received?

@krvarma Would you be so kind as to post the callback function you use?

Very cool!

@RWB, I don’t know about the alert pin, also not sure about this. But I am trying now, I will update my progress here.

2 Likes

@bpr, Sorry I forgot to paste the link to the Github Repo. It is now at the top of the page.

1 Like

@krvarma , I get:

Sending command AT+CMGS="+15555555555",145 {removed my real #; 
       also tried 129, with and without area code and got same result}

Return: 
OK

Return: 
>
Return:  
Return: 
+CMS ERROR: Call Barred

+ERROR, error sending message

Any ideas on the cause?

What SIM are you using? As far as I’m aware, the Particle SIM won’t support anything other than internet.

@bpr, I am reasonably sure that this is a restriction of the SIM card and not the U-Blox modules. They can certainly send and receive SMS, but if the provider blocks it then it won’t work. @krvarma, are you using the Particle SIM?

Aha! that would explain it as I’m using the Particle SIM

@Stevie, I am using my own SIM, not Particle SIM. I just update the post to reflect that.

Okay, that clarifies it. Very cool anyway!

It should be used pin RI (RING Indicator).
Pin from Ublox is connected to the microcontroller. It can be seen from the scheme.
But most likely it is able to receive and messages without using RI. The official Arduino GSM Shield does not use RI. It is based on Quectel M10. Also some Shield based on SIM900 do not use RI. Can most likely to perform the software without using an external interruption. But however the pin RI is already connected :slight_smile:


3 Likes

@krvarma I’m also using 3rd-party sim since the official sims are not working for me right now. It seems like my own sim gets connected to the cloud (blinking cyan) but it won’t take any command (Tinker App and terminal commands). Do you have any suggestion? I’m based in Thailand.

Thanks,
Jakk

@izeroman, are you sure Electron is online and is showing online in build.particle.io?

1 Like

As @krvarma implies, blinking cyan is only indicating the attempt to connect to the cloud. Active cloud connection would be indicated by breathing cyan.

@krvarma, @ScruffR

All my devices show up in both Build and Dashboard and the dashboard shows those Electron and ‘breathing cyan’.

Thanks for replying. :smile:

I

@izeroman, can you manually call digitalwrite function (Tinker Application) using CURL and see what error is returning?