SMS

Last updated on: December 31, 2019

Introduction

The SMS sample configures the button on the Icarus board, connects to the network and registers for SMS messages. When pressing the button, it tries to send an SMS message to a specified number. The destination phone number is declared at the top of the main.c file and needs to be filled in before compiling the sample. The SMSC number also required for sending and SMSC is configured through the prj.conf file.

Download sample from Icarus - SMS Sample

Project configuration

The SMS sample enables the following settings in the prj.conf file:

CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NEWLIB_LIBC=y
CONFIG_BSD_LIBRARY=y
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=1024

that enable the use of network connectivity and allow sending of AT commands to the modem.

Aditionally the sample enables the GPIO so that the button can be used:

CONFIG_GPIO=y

The following directives make sure the internal eSIM is selected (instead of the external SIM) and set the SMSC number:

CONFIG_BOARD_SELECT_SIM_EXTERNAL=n
CONFIG_SMSC_NUMBER="46707000440"

Note: the SMSC number depends on the SIM used. Make sure you set this to the proper one if using an external SIM.

Code explanation

The sample gets the GPIO handle from the Icarus device tree (gpio_dev = device_get_binding(DT_GPIO_P0_DEV_NAME);) then configures the button and adds a button press callback:

gpio_pin_configure(gpio_dev, SIM_SELECT_PIN, GPIO_DIR_OUT);
gpio_pin_configure(gpio_dev, BUTTON, GPIO_DIR_IN | GPIO_PUD_PULL_UP | GPIO_INT | GPIO_INT_DEBOUNCE | GPIO_INT_EDGE);
gpio_init_callback(&gpio_cb, button_pressed_callback, BIT(BUTTON));
gpio_add_callback(gpio_dev, &gpio_cb);
gpio_pin_enable_callback(gpio_dev, BUTTON);

with the BUTTON defined as:

#define BUTTON SW0_GPIO_PIN

After the modem is connected to the network through the Zephyr LTEM library call: lte_lc_init_and_connect(), it registers itself as an SMS client using the following code:

enum at_cmd_state state;
at_cmd_write_with_callback("AT+CNMI=3,2,0,1", at_callback, &state);

where at_callback is just a callback fromn the modem reporting the command execution status.

When the button is pressed a SMS containing the text "Button was pressed" will be sent to the number that is attributed to the SMS_DESTINATION_NUMBER define.

If upon sending the SMS the modem returns a +CMS message, the modem is either not registered with the network, the SMSC number is not properly configured in the prj.conf file or some other error ocurred. If the modem returns a message similar to: +CMGS: 2 the SMS message was successfully sent.

If a SMS message is received, its contents will be printed on the serial terminal.

The sample uses an external library for encoding and decoding SMS messages.