I am sending an SMS message to the Arduino+SIM900 shield. The SMS message is being corrupted when I am going thru the debugger. This corruption is happening only when I send it to Arduino, if I send this SMS message to another phone, everything is fine.
The code is below. I listed the whole code for completness, but the problem is happening at the very beginning when I am trying to read the message.
Any help greatly appreciated. (I suspect that I am in some different character set or something like that).
//
// SmsCommand - Active Pin 13 On/OFF depending on the received SMS message (on/off).
// This soft also send the PIN code to SIM card when appropriate.
// Based on Serial Relay. Arduino will still patch a serial link between the
// computer and the GPRS Shield at 19200 bps 8-N-1.
//
// *** Configure Serial Monitor to Carriage Return, 19200 bauds ***
//
// *** VERY IMPORTANT!!! ***
// *** FOR READING SMS, DO NOT FORGET TO CHANGE ***
// *** THE BUFFER SIZE OF NewSoftSerial to 128 bytes ***
// *** see line _NewSS_MAX_RX_BUFF in NewSoftSerial.h ***
//
//
// http://mchobby.be/wiki/index.php?title=GeekOnFire_GSM/GPRS_Shield
//
//
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
// EN: String buffer for the GPRS shield message
// FR: Mémoire tampon de type string pour les messages du shield GPRS
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
// FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS
int SmsContentFlag = 0;
// EN: Pin of the LED to turn ON and OFF depending on the received message
// FR: Pin de la LED a allumer/éteindre en fonction du message reçu
int ledPin = 5;
// EN: Code PIN of the SIM card (if applied)
// FR: Code PIN de la carte SIM (si applicable)
String SIM_PIN_CODE = String( "3333" );
int8_t answer;
int x;
char SMS[200];
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
Serial.println("Started!");
// Initialize la PIN
pinMode( ledPin, OUTPUT );
digitalWrite( ledPin, HIGH );
}
void loop()
{
char SerialInByte;
if(Serial.available())
{
mySerial.print((unsigned char)Serial.read());
}
else if(mySerial.available())
{
char SerialInByte;
SerialInByte = (unsigned char)mySerial.read();
// Relay to Arduino IDE Monitor - Here I get the corrupted characters!!!!!!!!!!!!
Serial.print( SerialInByte );
// -------------------------------------------------------------------
// Program also listen to the GPRS shield message.
// -------------------------------------------------------------------
// If the message ends with <CR> then process the message
// -------------------------------------------------------------------
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
// ---------------------------------------------------
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
// -------------------------------------------------
}
else {
// EN: store the current character in the message string buffer
// --------------------------------------------------------------
msg += String(SerialInByte);
}
answer = 1;
if (answer == 1)
{
answer = 0;
while(Serial.available() == 0);
// this loop reads the data of the SMS
do{
if(Serial.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer
SMS[x] = Serial.read();
x++;
if (strstr(SMS, "OK") != NULL) // check if the desired answer (OK) is in the response of the module
{
answer = 1;
Serial.println("Got it!");
}
}
}while(answer == 0); // Waits for the asnwer with time out
SMS[x] = '\0';
Serial.print(SMS);
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
}
// EN: Make action based on the content of the SMS.
// Notice than SMS content is the result of the processing of several GPRS shield messages.
// --------------------------------------------------------------------------------------------
//
void ProcessSms( String sms ){
sms.toLowerCase();
Serial.print( "ProcessSms for [" );
Serial.print( sms );
Serial.println( "]" );
if( sms.indexOf("on") >= 0 ){
digitalWrite( ledPin, HIGH );
Serial.println( "LED IS ON" );
return;
}
if( sms.indexOf("off") >= 0 ){
digitalWrite( ledPin, LOW );
Serial.println( "LED IS OFF" );
return;
}
}
// Send the SIM PIN Code to the GPRS shield
// ---------------------------------------------------
void GprsSendPinCode(){
if( SIM_PIN_CODE.indexOf("XXXX")>=0 ){
Serial.println( "*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***" );
Serial.println( "*** Please, define the SIM_PIN_CODE variable . ***" );
return;
}
mySerial.print("AT+CPIN=");
mySerial.println( SIM_PIN_CODE );
}
// Request Text Mode for SMS messaging
// -------------------------------------------------------
void GprsTextModeSMS(){
mySerial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
// Serial.print( "GprsReadSmsStore for storePos " );
// Serial.println( SmsStorePos );
mySerial.print( "AT+CMGR=" );
mySerial.println( SmsStorePos );
}
// Clear the GPRS shield message buffer
// --------------------------------------------------------------
void ClearGprsMsg(){
msg = "";
}
// interpret the GPRS shield message and act appropiately
// ------------------------------------------------------------------
void ProcessGprsMsg() {
Serial.println("");
Serial.print( "GPRS Message: [" );
Serial.print( msg );
Serial.println( "]" );
if( msg.indexOf( "+CPIN: SIM PIN" ) >= 0 ){
Serial.println( "*** NEED FOR SIM PIN CODE ***" );
Serial.println( "PIN CODE *** WILL BE SEND NOW" );
//GprsSendPinCode();
}
if( msg.indexOf( "Call Ready" ) >= 0 ){
Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
// unsolicited message received when getting a SMS message
// ----------------------------------------------------------------
if( msg.indexOf( "+CMTI" ) >= 0 ){
Serial.println( "*** SMS Received ***" );
// Look for the coma in the full message (+CMTI: "SM",6)
// In the sample, the SMS is stored at position 6
// --------------------------------------------------------------------
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos+1 );
Serial.print( "SMS stored at " );
Serial.println( SmsStorePos );
// Ask to read the SMS store
// ---------------------------------------------------
GprsReadSmsStore( SmsStorePos );
}
// SMS store readed through UART (result of GprsReadSmsStore request)
// ---------------------------------------------------------------------------
if( msg.indexOf( "+CMGR:" ) >= 0 ){
// Next message will contains the BODY of SMS
// ---------------------------------------------------------------
SmsContentFlag = 1;
// Following lines are essentiel to not clear the flag!
// -----------------------------------------------------------------
ClearGprsMsg();
return;
}
// +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
// -------------------------------------------------------------------------------
if( SmsContentFlag == 1 ){
Serial.println( "*** SMS MESSAGE CONTENT ***" );
Serial.println( msg );
Serial.println( "*** END OF SMS MESSAGE ***" );
ProcessSms( msg );
}
ClearGprsMsg();
// Always clear the flag
// -------------------------------------------------------
SmsContentFlag = 0;
}