LTE Cat M1/NB-IoT Shield readSMS library update WORKING

I have been playing around with SparkFun_LTE_Shield_Arduino_Library and came up with a method to read SMS text messages. It takes some work to program the SARA-R4 modem to be able to receive messages, but I’ll outline the steps I took to make it happen.

You will need to buy a phone number through Hologram for this to work properly. It cost me $1/month to secure the number.

First, you’ll need m-center from ublox. Get it here: https://www.u-blox.com/en/product/m-center

Connect your LTE shield to an uno or similar and load up the Serial_Passthrough example to allow m-center to communicate directly with the shield. Set up the COM Port in a similar way you would set it up in the Arduino IDE by clicking “Set port”.

Click “Connect” once your port is set up. Clicking “Get info” should now populate a bunch of data on that home screen if you’re set up correctly.

Next, on the home screen of m-center click the “AT Terminal” button in the top right. This will let you try out a bunch of AT commands and program your modem to do a TON of stuff. We need to enable text message receiving. In the input line below the terminal window type at+cpms="ME","ME","ME" and hit the send button. If you have a new number and haven’t texted it yet, the following should be output to the terminal window: ```
+CPMS: 0,23,0,23,0,23


The zeroes tell you that there are no messages stored. If you send an SMS to your shield and rerun the at+cpms="ME","ME","ME" command, the zeroes should turn into ones. This means there is one message stored, and so on. 

You will have to edit the library by adding the following function to the SparkFun_LTE_Shield_Arduino_Library.cpp file, I put it right above the sendSMS function to keep it neat.

String LTE_Shield::readSMS(String index)
{
char *command;
char *indexCStr;
char *msgBegin;
char *msgEnd;
char *response;
LTE_Shield_error_t err;

//allocate memory for indexCStr
indexCStr = lte_calloc_char(index.length() +2);
if(indexCStr == NULL){
	return "Memory allocation failed: indexCStr";
}
//fill char array with index value
index.toCharArray(indexCStr, index.length() + 1);

//allocate memory for command
command = lte_calloc_char(strlen(LTE_SHIELD_READ_TEXT) + strlen(indexCStr) + 8);
if (command == NULL){
	free(indexCStr);
    return "Memory allocation failed: command";
}

//should output "+CMGR=<index>"
sprintf(command, "%s%s", LTE_SHIELD_READ_TEXT, indexCStr);
	
//allocate memory for response
response = lte_calloc_char(200);
if (response == NULL)
{
    free(command);
	free(indexCStr);
    return "Memory allocation failed: response";
}

err = sendCommandWithResponse(command, LTE_SHIELD_RESPONSE_OK,
                              response, LTE_SHIELD_STANDARD_RESPONSE_TIMEOUT);
if (err != LTE_SHIELD_ERROR_SUCCESS)
	return "Error.";
//works to here
	
//Response format: \r\n+CMGR: "REC READ","phone number",,"YY/MM/DD,HH:MM:SS-TZ"\r\n data \r\n\r\nOK\r\n

	msgBegin = strchr(response, ','); // Find beginning of phone number
	msgBegin++;
	if (msgBegin == NULL)
	{
		free(command);
		free(response);
		return "msgBegin NULL";
	}

	msgEnd = strchr(msgBegin, '\0'); // Find end of response
	msgEnd -= 8; //walk back through "\r\n\r\nOK\r\n\0" to clean up response
if (msgEnd == NULL)
{
    free(command);
    free(response);
    return "msgEnd NULL";
}
*(msgEnd) = '\0'; // Set last quote to null char -- end string

free(command);
free(indexCStr);
free(response);

return String(msgBegin);

}


Now, add the AT command char array near the top, I put it right by the LTE_SHIELD_SEND_TEXT array to keep it neat:

const char LTE_SHIELD_READ_TEXT = “+CMGR=”; // Read SMS message. Format: AT+CMGR=


Now, in the SparkFun_LTE_Shield_Arduino_Library.h file, add the function definition. Again, I put it near the sendSMS definition to keep things neat:

String readSMS(String index);


In the keywords.txt file of the library, make sure to add this near the sendSMS keyword so the IDE can highlight things correctly as you type, so your code doesn't look like a word document.

readSMS KEYWORD2


That should be it for the library.

The updated library and an example sketch are on github:

https://github.com/tuxedotshirt/readSMS.git

Updated library on github now includes a struct that gives user access to the senders phone number and message as strings. Examples have been put into the examples folder, just paste the library into your Arduino IDE libraries folder and it should work. This is the getSMS(String) function.

Hologram phone number is not neccessary unless you want a normal phone number, otherwise you’ll need the 18 digit number your caller ID displays when you send a text from the LTE shield.

I have it set as a contact in my phone with the normal number as primary and the long one as an extra so it shows up as the same contact in my messages.

The m-center steps above also don’t seem to be necessary, reading the SARA-R4 documentation makes it sound like these modules are already programmed to accept SMS messages. However, lots of people in other (not SparkFun) forums could not see their messages unless they took these steps.