Here's the next one then Cattledog:
Everything works in standalone code.
As soon as i try to implement the GSM code into my larger project; it freaks out.
It looks like; instead of sending all characters correctly, some get 'lost in translation'. Unfortunately being the one i need - the last ".
I can probably just count along the bytes & start grabbing at the message start, but thought you might have an insight or be curious to solve it.
(FYI, I am only wanting to grab the 1st 4 letters of every message)
GSM code is:
#include <SoftwareSerial.h>
SoftwareSerial GSMSerial(7, 8);
char letter1;
char letter2;
char letter3;
char letter4;
void setup()
{
Serial.begin(9600);
GSMSerial.begin(9600);
Serial.println("Initializing...");
delay(1000);
GSMSerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
GSMSerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
GSMSerial.println("AT+CNMI=1,2,0,0,0");
updateSerial();
}
void loop()
{
newcode();
}
void updateSerial()
{
delay(500);
while (Serial.available()>0)
{
GSMSerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(GSMSerial.available()>0)
{
Serial.write(GSMSerial.read());//Forward what Software Serial received to Serial Port
}
}
void getGSM() {
while (GSMSerial.available() > 0) //If there is stuff in the buffer
{
char textMessage[60] = {}; //size this buffer for the expected message
byte numChars = GSMSerial.readBytes(textMessage, 5);//readBytes returns number read
textMessage[numChars] = '\0';//null Terminator
Serial.println(textMessage); //
}
}
void newcode() {
char receivedMsg[100];//size for max message
char textMessage[5];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;
while (GSMSerial.available() > 0)
{
//readBytes returns number read not zero referenced
byte numChars = GSMSerial.readBytes(receivedMsg, 100);
receivedMsg[numChars] = '\0';//null Terminate
newMessage = true;
}
//if (strncmp(receivedMsg, "+CMT", 4) == 0 && newMessage == true)
if (strstr(receivedMsg, "+CMT") != 0 && newMessage == true)
{
//Serial.println("prefixMatch +CMT");
prefixMatch = true;
newMessage = false;
}
else if (newMessage == true)
{
Serial.println("no prefixMatch");
//Serial.println(receivedMsg);
newMessage = false;
}
if (prefixMatch == true)
{
prefixMatch = false;
char* index;
index = strrchr(receivedMsg, '"'); //find last " with strrchr()
strcpy(textMessage,index+1);//copies from after " pointer to ending null
letter1 = textMessage[2];
letter2 = textMessage[3];
letter3= textMessage[4];
letter4 = textMessage[5];
Serial.print(letter1);
Serial.print(letter2);
Serial.print(letter3);
Serial.print(letter4);
}
}
Which prints: (correctly)
Initializing...
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
This
The joined code looks like this: (I had to delete some of the functions from here due to word count, but the issue is before it even gets that far.)
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial GSMSerial(7, 8);
Servo pan;
Servo tilt;
Servo tap;
char incomingByte;
char letter1;
char letter2;
char letter3;
char letter4;
int posPan;
int posTilt;
int posTap;
int Letter;
int movements [28][4] = {{97,63,105,17},{98,100,55,20},{99,77,65,21},
{100,75,81,20},{101,79,101,20},{102,85,79,20},
{103,93,75,20},{104,103,70,20},{105,118,75,20},
{106,115,68,20},{107,120,68,20},{108,130,70,20},
{109,123,51,20},{110,113,51,20},{111,125,77,20},
{112,135,80,20},{113,65,115,16},{114,85,92,17},
{115,68,92,20},{116,93,90,16},{117,112,75,20},
{118,87,65,20},{119,70,110,16},{120,71,72,20},
{121,100,85,16},{122,63,85,20},{32,100,45,13},
{46,138,60,20}};
void setup()
{
Serial.begin(9600);
GSMSerial.begin(9600);
Serial.println("Initializing...");
delay(1000);
GSMSerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
GSMSerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
GSMSerial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
pan.attach(9);
tilt.attach(10);
tap.attach(11);
pan.write(90); //intialise servos to center
tilt.write(90);
tap.write(90);
}
void loop()
{
letter1=""; //initialise letter1
letter2=""; //initialise letter2
letter3=""; //initialise letter3
letter4="";
getGSM();
letter1Select(); //find letter in letter1 and move to it
letter2Select(); //find letter in letter2 and move to it
letter3Select(); //find letter in letter3 and move to it
letter4Select();
resetArms();
}
void updateSerial()
{
delay(500);
while (Serial.available()>0)
{
GSMSerial.write(Serial.read());
}
while(GSMSerial.available()>0)
{
Serial.write(GSMSerial.read());
}
}
void getGSM() {
boolean newMessage = false;
boolean prefixMatch = false;
char receivedMsg[100];//size for max message
char textMessage[5];//size appropriately for parsed characters
while (GSMSerial.available() > 0)
{
byte numChars = GSMSerial.readBytes(receivedMsg, 100);
receivedMsg[numChars] = '\0';//null Terminate
newMessage = true;
}
//if (strncmp(receivedMsg, "+CMT", 4) == 0 && newMessage == true)
if (strstr(receivedMsg, "+CMT") != 0 && newMessage == true)
{
//Serial.println("prefixMatch +CMT");
prefixMatch = true;
newMessage = false;
}
else if (newMessage == true)
{
Serial.println("no prefixMatch");
Serial.println(receivedMsg);
newMessage = false;
}
if (prefixMatch == true)
{
prefixMatch = false;
char* index;
index = strrchr(receivedMsg, '"'); //find last " with strrchr()
strcpy(textMessage,index+1);//copies from after " pointer to ending null
letter1 = textMessage[2];
letter2 = textMessage[3];
letter3= textMessage[4];
letter4 = textMessage[5];
Serial.print(letter1);
Serial.print(letter2);
Serial.print(letter3);
Serial.print(letter4);
}
}
void letter1Select(){
//byte key1 = letter1;
byte b = 29;
while (b>0) {b--;
if (letter1==movements[b][0]){
tapLetter(movements[b][1],movements[b][2],movements[b][3]); break;
};
};
};
and prints:
Initializing...
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
/06/
So I printed receivedMsg to see the issue
+CMT: "+447980xxxxxx","","20/06/08,12:56:09+
/06/
So the last " is causing issues. Just wondering what the solution would be to print correctly whether it freaks or not? Maybe I use the last + character and index+2?