Hi all,
I'm having a pretty weird issue with the code that you'll find attached.
This program is supposed to forward any received SMS.
I developped this code using the documentation of the GSM modem, and instead of using libraries like AltSoftSerial (as SoftwareSerial can't be used with GSM.h library), I'm sending AT commands directly through the theGSM3ShieldV1ModemCore command, which actually achieves the same results without the extra step of loading another library.
DEVICES USED
- Arduino Uno R3
- Official GSM Shield
WHAT I DID
I was having issues with my code.
Instead of receiving a SMS on my phone with the whole content of the forwarded SMS, I was only getting the first letter of each text sent to the arduino.
Here is an extract of the ReadAndFormat fonction from my code (attached):
void ReadAndFormat(){
// Lire le SMS reçu
char characters_Received;
char sms_Received[] = "";
int position = 0;
// Read the SMS and store it into the sms_Received array
while (characters_Received = sms.read()){
sms_Received[position] = characters_Received;
position++;
}
// Add a null character (\0) at the en of the array
// sms_Received[position++] = '\0';
sprintf(text_toBeSent, "Text received from %s : \n\"%s\"", sender_Number, sms_Received);
}
Well, as I was only getting the first letter of a SMS forwared, I tried to debug it and added Serial.print commands. So here is the same function with a few more line of code:
void ReadAndFormat(){
// Lire le SMS reçu
char characters_Received;
char sms_Received[] = "";
int position = 0;
// Read the SMS and store it into the sms_Received array
while (characters_Received = sms.read()){
sms_Received[position] = characters_Received;
/*DEBUG*/ Serial.print(F("\n** characters_Received = "));
/*DEBUG*/ Serial.print(characters_Received);
/*DEBUG*/ Serial.print(F(" & sms_Received["));
/*DEBUG*/ Serial.print(position);
/*DEBUG*/ Serial.print(F("] = "));
/*DEBUG*/ Serial.println(sms_Received[position]);
position++;
}
// Add a null character (\0) at the en of the array
// sms_Received[position++] = '\0';
sprintf(text_toBeSent, "Text received from %s : \n\"%s\"", sender_Number, sms_Received);
}
I really wanted to see how the char from the text were added to the array at each step.
Now this is were things go crazy...
You'll need to get a look at the entire file to get an idea of the issue I'm facing, but when I add these Serial.print instructions, I'm indeed receiving a text message with the whole forwared sms message content...
but...
I'm also getting pretty weird crashes :
- an instruction from the DeleteSMS will be concatenated with the SMS. I mean, really >:( .
- if the original SMS is too long, the arduino will simply reset itself, the message never being deleted, and the arduino will try to read it again and again, unable to delete it or to forward it to my phone
In both cases, the content of the SMS received is successfully read.
But, in the first case, for a SMS that contained "This is a test", the message that is being forwarded to my phone becomes:
SMS received from (phone number): "This is a test@AT+QMGDA="DEL ALL"
So, I have the original SMS content concatenated with an instruction (an AT command to delete all SMS, AT+QMGDA=DEL ALL) that is actually from another function. The whole thing is being sent to my phone... And, as the AT+QMGDA=DEL ALL command is sent to me, it does not do what it was meant to: delete all the messages... So the Arduino just gets stuck, rebooting, sending me this weird SMS, and rebooting, etc.
In the second case, when the Arduino receives "Another test bro", this is what I'm getting in my serial monitor :
** SMS received from (phone number)
** characters_Received = A& sms_Received[0] = A
** characters_Received = n& sms_Received[1] = n
** characters_Received = o& sms_Received[2] = o
** characters_Received = t& sms_Received[3] = t
** characters_Received = h& sms_Received[4] = h
** characters_Received = e& sms_Received[5] = e
** characters_Received = r& sms_Received[6] = r
** characters_Received = & sms_Received[7] =
** characters_Received = t& sms_Received[8] = t
** characters_Received = e& sms_Received[9] = e
** characters_Received = s& sms_Received[10] = s
** characters_Received = t& sms_Received[11] = t
** characters_Received = & sms_Received[12] =
** characters_Received = b& sms_Received[13] = b
** characters_Received = r& sms_Received[14] = rC!%%%%%%%%%%%%%%�HHII �х�с ***
***************
** Scanning network:
and then the arduino reboots itself. :o :o :o
Man, has anyone any idea of what's happening here?
How come Serial.println commands could mess the program up so much?!
Should I avoid sprintf() as it uses so much RAM (from what I observed)?
I know that I'm a noob, but I just can't figure this out by myself...
Thanks in advance for any input...
GSM_Forward.ino (3.73 KB)