Sending and receiving SMS with a GSM shield is quite straightforward but without starting to write your code you should understand how they work. We will actually see and program what your cell phone does when you try to send an SMS.
SMS Configuration for receiving and sending SMS via GSM Shields
First of all, you need to read the datasheet of your GSM module carefully. All information that you are going to need is there but still there are few things might be tricky. In my example I am going to use Sixfab’s GSM shield which use Quectel M66 Module and Raspberry Pi 3. You can buy only the module as well but you need to make circuit for power and serial communication.
Datasheet and commands manual can be found on the manufacturer website. Alike to other modules, this module is also well documented. It should not be forgotten that this module can be programmed via serial communication. So before going ahead module should be powered up (in my case it is 5V) and RX (Module) – TX (Raspberry), TX (Module) – RX (Raspberry) connection should be wired.
After connection has been done, first command would be AT. Datasheet says the response of the AT command should be OK when module properly powered up.
serialPrintf(fd,"AT+CPIN="); //Send PIN serialPrintf(fd,simPin.c_str()); serialPrintf(fd,"\n"); serialPrintf(fd,"AT+IFC=1,1\n"); // Set TE-TA Control Character Framing delay(300); serialPrintf(fd,"AT+CMGF=1\n"); //Select sms format delay(300); serialPrintf(fd, "AT+CSMP=\"SM\",\"SM\",\"SM\"\n"); //Set SMS Text Mode Parameters delay(300); serialPrintf(fd,"AT+CLIP=1\n"); //Calling Line Identification Presantation delay(300); serialPrintf(fd,"AT+COLP=1\n"); //Connected Line Identification Presentation delay(300); serialPrintf(fd,"AT+CSCS=\"IRA\"\n"); //Select TE Character Set delay(300); serialPrintf(fd,"AT+QCCID\n"); //Show ICCID delay(300); serialPrintf(fd,"AT+CIMI\n"); // Request IMSI
These lines would be enough to configure shield. Delay times are set to maximum response times according to information in command manual.
Sending SMS command requires certain sms message format and character set to the certain character set. We did it in the configuration above. Also to send sms, module should support Send Sms or Send Sms From Storage.
Sytanx of sending SMS is AT+CMGS=”+905361234567″<CR>Example message body<Ctrl+z>
(<CTRL+Z> is ASCII 26 and <CR> is ASCII 13).
The response should be OK and it can be read from any serial monitor. If it is error, with the correspond error code, reason can be found in the command manual again.
The logic of receiving SMS is checking the inbox of the module in every loop. When there is nothing in the inbox, response would be just OK but otherwise response would contain some tags. Also inbox must be checked with these tags. These tags are like this:
REC UNREAD: Show unread messages
REC READ: Show read messages
STO UNSENT: Show stored unsent messages
STO SENT: Show stored sent messages
ALL: Show all messages
For example a message comes to the inbox with
REC UNREAD tag firstly, after listing it, its tag turns into
REC READ. So when you list messages with unread tag it will apeear only once.
+CMGL:"REC UNREAD", "+905361234567","","2018/09/01 15:01:01+32","Incoming message body"
Response can be read via serial monitor and parsed to use later. If there is an error during receiving sms. Further information and more commands can be found in the command manual of the product. Sending and Receiving sms obviously are not only capability of these modules. They can connect to the internet, make calls and some of them even can be used as GPS.