Hey,
I have the sparkfun cellular shield hooked up to Deumilanove, I am trying to get data from sms messages added to variables.
By connecting to the cellular module using the sparkfun passthrough sketch and entering command manually through the serial input I can see the response from the get text messages command is this:
+CMGL: 1,0,"REC UNREAD","phonenumber","11/01/08,20:36:09+00"
x,module1,54
+CMGL: 2,0,"REC UNREAD","phonenumber","11/01/08,20:36:27+00"
x,module2,43
So the bit of the first sms I am interested in is the "module1" and "54", ideally what I would like is a variable named module1 set with a value of 54.
The code I have so far, which is mostly from forum member woprbyte:
#include <NewSoftSerial.h>
#include <string.h>
#define BUFFSIZE 512
char ATBuff[BUFFSIZE];
char SMSBuff[BUFFSIZE];
int CELL_REG = 0;
int CELL_AT = 0;
int CELL_SMSMODE = 0;
int nBufPos;
NewSoftSerial cell(2,3);
void setup()
{
Serial.begin(9600);
cell.begin(9600);
delay(1500);
Serial.println("starting serial");
}
void loop() {
while(CELL_REG == 0 || CELL_AT == 0) { // Keep running loop untill cell network is properly established
ATGetResp();
ParseATResp();
}
if (digitalRead(8) == HIGH) { // On button press, collect sms messages.
ATGetSMS();
}
}
void ATGetSMS(void) {
if (CELL_REG == 1 && CELL_AT == 1) { // check for cell connection
if (!CELL_SMSMODE) { // if module is not set to text mode change it
cell.println("AT+CMGF=1");
CELL_SMSMODE = 1;
Serial.println("CMFG SET to TEXT");
}
cell.println("AT+CMGL=\"ALL\""); // return all texts from module
delay(3000);
ParseSMS();
Serial.println("ATBuff: ");
Serial.println(ATBuff);
Serial.println("SMSBuff:");
Serial.println(SMSBuff);
}
}
//************************************************************
// Get AT command respnse from the Cell module. Starts and
// ends with CR LF.
//************************************************************
void ATGetResp() {
char c;
memset(ATBuff, '\0', BUFFSIZE); // clear array
nBufPos = 0; // Reset array counter
int nBytes = 0;
int trigger = 0;
nBytes = cell.available(); // Store number of bytes in serial queue
if (nBytes > 0) {
for (int i = 1; i <= nBytes; i++) { // Keep running loop till no more bytes in queue
if (nBufPos == BUFFSIZE - 3) { // Stop if array is full
nBufPos = 0;
}
c = cell.read(); // Set c to current serial character
if (c == '\r') { // If c is carriage return
ATBuff[nBufPos] = c; // add it to the array
c = cell.read(); // Doesnt work without this line, can anyone explain why?
if (c == '\n') { // If c is newline
ATBuff[nBufPos] = c; // add to array
ATBuff[nBufPos+1] = '\0'; // and terminate
return; // then return to program
}
}
ATBuff[nBufPos] = c;
nBufPos++;
}
}
ATBuff[nBufPos] = '\0';
return;
}
void ParseSMS() { // This function looks for x the first letter of the message then adds the rest of the message to array
char c;
memset(SMSBuff, '\0', BUFFSIZE);
nBufPos = 0;
int nBytes = 0;
int trigger = 0;
nBytes = cell.available();
if (nBytes > 0) {
for (int i = 1; i <= nBytes; i++) {
if (nBufPos == BUFFSIZE - 3) {
nBufPos = 0;
}
c = cell.read();
if (c == 'x' || trigger == 1) {
trigger = 1;
SMSBuff[nBufPos] = c;
c = cell.read();
}
// if (c == '\r') {
// SMSBuff[nBufPos] = '\0';
// return;
// }
nBufPos++;
}
}
SMSBuff[nBufPos] = '\0';
return;
}
//************************************************************
// Parse AT Response string
//************************************************************
void ParseATResp(void) {
if (strstr(ATBuff, "+SIND: 11") != 0) {
CELL_REG = 1;
Serial.println("Almost there...");
return;
}
if (strstr(ATBuff, "+SIND: 4") != 0) {
CELL_AT = 1;
Serial.println("Ready to go!");
return;
}
if (strstr(ATBuff, "+SIND: 7") != 0 ||strstr(ATBuff, "+SIND: 8") != 0) {
CELL_AT = 1;
Serial.println("Something's wrong.");
return;
}
}
which returns:
starting serial
Almost there...
Ready to go!
CMFG SET to TEXT
ATBuff:
+SIND: 4
SMSBuff:
What I am attempting to do, which clearly isn't working, is when I call for the text messages, look through the incoming serial characters till I see an x, which is what my messages all start with then store what comes after the x in the array, obviously I will need to do something else with it after that, but until I get this bit working I can't do much.
Anyone able to shed any light on my problem?
(my programming skills are pretty limited, so baby steps would be greatly appreciated...)
Phil.