Greetings,
I've been having some trouble with developing the beginnings of a simple wireless communication protocol. I want one module to have a piece of data to transmit, while the other will receive it once it is in range after some handshaking. The stationary module will transmit "Hello!" over and over. When the other module has received this twice, it will send back an acknowledgement. Then the stationary module will send a piece of data until it receives confirmation that it was received. After all of this it will stop transmitting.
That's a bit of background information about my goals. In part of my code I need to count how many "Hello!"s have been received, and am trying to do so with a comparison. The comparison NEVER triggers, even though I print it out right before I do the comparison and it appears equivalent to what I'm comparing it against. I've tried using strings with strcmp and also Strings with .equals(), but the same thing happens in both cases.
All of the code is a bit messy as I've been tinkering with it a bunch and it's really a very rough draft to begin with, so please pardon my mess. ^^; Here is a portion of the code from one module:
int inByte = 0;
int inputSize = 0;
char incomingByte;
String command = "";
int helloCount = 0;
int coord = 0;
String hello = "Hello!"; //Previously used for a string .equals comparison attempt as double quotes are character arrays.
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0){
incomingByte = Serial.read();
if(incomingByte== 10){
processCommand(command);
//Serial.println(command);
command = "";
}
else{
command += incomingByte;
}
}
}
void processCommand(String theCommand){
char myCmd[128] = ""; //create a character array to hold the converted String
Serial.println("Processing command");
theCommand.toCharArray(myCmd,128); //Convert the String to CharArray.
//Serial.println(strcmp(myCmd, "Hello!"));
Serial.println(myCmd); //This prints "Hello!"
if(strcmp(myCmd, "Hello!") == 0){
Serial.println("Got the Hello"); //This code is never reached.
helloCount++;
if(helloCount >= 2){
Serial.print('Y'); //Say hello back.
delay(1000);
}
}
else{
if(theCommand[0] > 47 && theCommand[0] < 58 && helloCount >= 2){ //decimal (coordinate)
char * tempPointer;
theCommand.toCharArray(tempPointer, 20);
coord = atoi(tempPointer);
Serial.println(coord);
Serial.write('T'); //say thanks
delay(1000);
helloCount = 0;
}
}
}
And here is the code from the stationary module:
int state = 0;
//0 = hello!, 1 = coordinate, 2 = finished.
int incomingByte;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(state == 0){
if(Serial.available() > 0){
incomingByte = Serial.read();
if(incomingByte == 'Y'){ //They said hello back.
state = 1;
}
}
else{
delay(1000);
Serial.println("Hello!");
}
}
if(state == 1){
if(Serial.available()>0){
incomingByte = Serial.read();
if(incomingByte == 'T'){ //They said thanks.
state = 2;
}
}
else{
Serial.println("5");
delay(1000);
}
}
if(state == 2){
delay(1000);
Serial.println("I've done my time. End.");
}
}
Some additional information: I'm using xBee series 1 wireless modules on arduino uno boards in their standard communication mode (ie, not command mode). I know that they are able to communicate with each other, and I even know that one of my boards is receiving "Hello!", but the comparison doesn't trigger.
This one has had me stumped for a while. Any insights would be greatly appreciated.
-Pher