Well i have 2xbees and im trying to send 3sensor values in a packet to the over xbee
Transmitter end
void loop()
Serial.print("<A");
Serial.print(Sensor1);
Serial.print(">");
Serial.print("<B");
Serial.print(Sensor2);
Serial.print(">");
Serial.print("<C");
Serial.print(Sensor3);
Serial.print(">");
Reciver end
char inData[24];
byte index;
boolean started = false;
boolean ended = false;
void setup()
{
Serial.begin(9600);
Serial.println("Sensors");
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
inData[index] = ' ';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = ' ';
}
}
if(started && ended)
{
// Use the value
if(inData[0] == A')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor1:");
Serial.print(inData);
}
else if(inData[0] == 'B')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor2:");
Serial.print(inData);
}
else if(inData[0] == 'C')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor3:");
Serial.print(inData);
}
started = false;
ended = false;
index = 0;
inData[index] = ' ';
}
}
Is there a beeter way off doing this a tryed this but it dosnt work it would grab the last value. Help
system
October 13, 2012, 10:01pm
#2
Well i have 2xbees and im trying to send 3sensor values in a packet to the over xbee
Not with that code, you aren’t. You are sending three packets. each containing one value.
Serial.print("<A");
Serial.print(Sensor1);
Serial.print(", B");
Serial.print(Sensor2);
Serial.print(", C");
Serial.print(Sensor3);
Serial.print(">");
Three values in one packet, separated by commas.
Then, you need to change what happens in the
if(started && ended)
{
}
block. The strtok() function comes to mind.
inData[index] = ' ';
Wrong. Strings are NULL terminated, not space terminated.
Thanks i made them changes to the TX end know the RX end whats the strtok() function about?
do i need to change much for the
if(started && ended)
{
// Use the value
if(inData[0] == A')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor1:");
Serial.print(inData);
}
else if(inData[0] == 'B')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor2:");
Serial.print(inData);
}
else if(inData[0] == 'C')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor3:");
Serial.print(inData);
}
started = false;
ended = false;
index = 0;
inData[index] = ' ';
}
}
system
October 14, 2012, 6:57pm
#4
do i need to change much for the
Yes. You need to change quite a bit. That code assumes that there will be one token in inData. There is no longer just one token.
The strtok() function is for extracting tokens from a larger string. Suppose that inData contains:
"A14,B49;C-14". Calling strtok(inData, ","), the function will return "A14". Calling strtok(NULL, ","), the function will return "B49". Calling it again, the function will return "C-14". Calling it again, the function will return a NULL, so that you know that you have obtained all the tokens.
Each token can be dealt with by that code. Putting that code in a function, and passing each token to that function will be the easiest thing to do.
HI i have been reading this page Arduino Forum
is this what im looking for? how dose this separate 4 values so it comes up on the serial monitor like this
and in the right sensor line
Sensor1: 20.00
Sensor2: 15.00
Sensor3: 05.66
Sensor4: 90.00
if(started && ended)
{
int count=0;
Serial.print(inData);
Serial.print("\n");
char *p = inData;
char *str;
while (str=strtok_r(p,",",&p))
{
Data[count]=*str;
Serial.print(str);Serial.print("\n");
p = NULL;
count++;
}
//Reset
started = false;
ended= false;
index = 0;
inData[index]='\0';
}
system
October 15, 2012, 10:49am
#6
The strtok() function comes to mind.
So, you elected to use the more complicated, thread safe version on a single threaded system. Well, OK, whatever works for you.
I have no idea what you are sending, since you are not showing any serial output.
Data[count]=*str;
How is Data defined. This hardly looks like the correct way to assign a string to it. Just adding *s and &s to make the compiler happy is useless.
That code contains exactly the same mistakes pointed out in the other thread. Why didn’t you read to the end and fix them?
well i havent tryd that code just trying to get some idea on how to do the reviver end
well what my output from the TX unit would look like sending 4Values and i would like the RX end to display that in the serial monitor like this
<A10.11,B20.99,C49.00,C39.00>
And i would like the RX end to display that in the serial monitor like this
Sensor1: 20.00
Sensor2: 15.00
Sensor3: 05.66
Sensor4: 90.00
its the reviver side of this im not sure on
system
October 15, 2012, 7:20pm
#8
So, you want to send 10.11 for the first sensor, and see 20.00 in the Serial Monitor. Well, I'm confused.
Snippets-r-us is down the road a ways. If you want help here, post all of your code (after at least compiling it, and, preferably trying it).
oh no that was just an example so this is what TX sends out
<Axx.xx,Bxx.xx,Cxx.xx,Dxx.xx>
Thats what i want to happin on the RX end serial monitor
Sensor1: xx.xx
Sensor2: xx.xx
Sensor3: xx.xx
Sensor4: xx.xx
this is the tx code
Serial.print("<A");
Serial.print(Sensor1);
Serial.print(", B");
Serial.print(Sensor2);
Serial.print(", C");
Serial.print(Sensor3);
Serial.print(", E");
Serial.print(Sensor4);
Serial.print(">");
and the RX code
This is the part i need to change but not sure on
char inData[24];
byte index;
boolean started = false;
boolean ended = false;
void setup()
{
Serial.begin(9600);
Serial.println("Sensors");
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
inData[index] = ' ';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0 ';
}
}
if(started && ended)
{
// Use the value
if(inData[0] == A')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor1:");
Serial.print(inData);
}
else if(inData[0] == 'B')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor2:");
Serial.print(inData);
}
else if(inData[0] == 'C')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.print("Sensor3:");
Serial.print(inData);
}
started = false;
ended = false;
index = 0;
inData[index] = ' ';
}
}
This is somthing i put together for the RX end so you get an idea if what im trying to do. so help would be great
char inData[24];
byte index;
boolean started = false;
boolean ended = false;
void setup()
{
Serial.begin(9600);
Serial.println("Sensors");
}
void loop()
{
Serial.println("<A>");
Serial.println("Waiting");
waitForReply(2000);
delay(500)
Serial.println("<B>");
Serial.println("Waiting");
waitForReply(2000);
delay(500)
Serial.println("<C>");
Serial.println("Waiting");
waitForReply(2000);
delay(500)
Serial.println("<D>");
Serial.println("Waiting");
waitForReply(2000);
delay(500)
}
void waitForReply(unsigned long howLongToWait)
{
index = 0;
unsigned long startTime = millis();
while(millis() - startTime < 3000)
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
inData[index] = ' ';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0 ';
}
}
if(started && ended)
{
// Use the value
if(inData[0] == AT')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.println("MONITOR 1");
Serial.print("Sensor1:");
Serial.println(inData);
if(inData[0] == AH')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.print("Sensor2:");
Serial.print(inData);
if(inData[0] == AM')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.print("Sensor3:");
Serial.print(inData);
if(inData[0] == AV')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.print("Sensor3:");
Serial.print(inData);
}
else if(inData[0] == 'BT')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.println("MONITOR 2");
Serial.print("Sensor1:");
Serial.print(inData);
}
else if(inData[0] == 'CT')
{
inData[0] = ' ';
int Val = atoi(inData);
Serial.println(" ");
Serial.println("MONITOR 3");
Serial.print("Sensor1:");
Serial.print(inData);
}
started = false;
ended = false;
index = 0;
inData[index] = ' ';
}
}
system
October 16, 2012, 12:05pm
#12
All this time, and you still haven't looked at, or tried to implement strtok() or atoi(). This is going nowhere until you do.
I had a go at this last nite but nothing. must be missing something.
what about the id part? <ATxx.xx>
char inData[24];
byte index;
boolean started = false;
boolean ended = false;
void setup()
{
Serial.begin(9600);
Serial.println("Sensors");
}
void loop()
{
Serial.println("<A>");
Serial.println("Waiting");
waitForReply(2000);
delay(500);
Serial.println("<B>");
Serial.println("Waiting");
waitForReply(2000);
delay(500);
Serial.println("<C>");
Serial.println("Waiting");
waitForReply(2000);
delay(500);
Serial.println("<D>");
Serial.println("Waiting");
waitForReply(2000);
delay(500);
}
void waitForReply(unsigned long howLongToWait)
{
index = 0;
unsigned long startTime = millis();
while(millis() - startTime < 3000)
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
inData[index] = '\0 ';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0 ';
}
}
char *Data=NULL;
if(started && ended)
{
Serial.print(inData);
Serial.print("\n");
Data =strtok(inData,",");
while(Data != NULL)
{
Serial.print(Data);Serial.print("\n");
Data=strtok(NULL,",");
}
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
system
October 16, 2012, 7:40pm
#14
Serial.print(Data);Serial.print("\n");
As opposed to
Serial.println(Data);
?
must be missing something.
Yes. Something like sample output.
what about the id part? <ATxx.xx>
What about it?
One step at a time. Are you getting input? Is it what you expect? Is it being parsed correctly?
If i put this into the serial monitor <AT21.80, AH47.20, AM422, AV8.64> this is what i get
AT21.80, AH47.20, AM422,'<45>< AV8.64 AT21.80
AT21.80
AH47.20
AM422
'<45>< AV8.64 AT21.80
affter i change this code
AT21.80
AH47.20
AM422
'<AT21 AV8.64 7.20
AM422
AV8.64>
char *Data=NULL;
if(started && ended)
{
Data =strtok(inData,",");
while(Data != NULL)
{
Serial.print(Data);Serial.print("\n");
Data=strtok(NULL,",");
}
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
system
October 17, 2012, 11:58am
#16
If i put this into the serial monitor <AT21.80, AH47.20, AM422, AV8.64> this is what i get
The next step is to identify exactly what that output means.
Serial.print(inData);
Serial.print("\n");
needs to be
Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");
Similarly, every you output data, you need to have something before it that identifies what the data is, and something after it that indicates where the data ends.
Only then can we tackle the problem of why the data is not right, if that is the problem.
Ok so i Tryed that out sending this <AT13.90, AH68.50, AM550, AV10.04> and this is the output im getting
inData: [AT13.90, AH68.50, AM550,(AH68. AV10.04 ]
AT13.90
AH68.50
AM550
(AH68. AV10.04
char *Data=NULL;
if(started && ended)
{
Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");
Data =strtok(inData,",");
while(Data != NULL)
{
Serial.print(Data);Serial.print("\n");
Data=strtok(NULL,",");
}
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
system
October 17, 2012, 6:29pm
#18
Ok so i Tryed that out sending this <AT13.90, AH68.50, AM550, AV10.04> and this is the output im getting
The data in inData does not match what you say you are sending. So, something is corrupting the data in transit. So, we need to investigate why. How far apart are the XBees? Is there electrical activity between them? How are they configured? What kind are they?
well that test was done with no xbees just using the serial monitor to send <AT21.80, AH47.20, AM422, AV8.64> and thats the output i get back. it dose the same thing with the xbees so the xbees cant be the proplem and thay are like 500mm apart
so the XBEEs are S1
802.15.4
ID 3005
DL 10
LY 11
ID 3005
DL 11
LY 10
ok so i just tryed the same code in a arduino mega 1280 in the serial monitor i send
<AT21.80, AH47.20, AM422, AV8.64> and i get this back
AT21.80, AH47.20, AM422,'<45.4 AV8.64
AT21.80
AH47.20
AM422
'<45.4 AV8.64
so the only thing it must be is the code i could try it on more boads but i dont think its that