Pages: [1]   Go Down
Author Topic: XBee variable send and receive  (Read 1124 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
I want to send multiple variables with a XBEE and receive!

How do I do it

Michael
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to send multiple variables with a XBEE and receive!
Exactly the same way you would using a wire.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I have an Arduino UNO an Arduino MEGA 2 XBEE (Series 1).
And would like to connect multiple Poti on Arduino MEGA and the XBEE module to send Arduino UNO and several servo control.
So with potentiometer 1 servo 1
       with potentiometer 2 servo 2
       with potentiometer 3 servo 3
        etc. .. Taxes!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And would like to connect multiple Poti on Arduino MEGA and the XBEE module to send Arduino UNO and several servo control.
So with potentiometer 1 servo 1
       with potentiometer 2 servo 2
       with potentiometer 3 servo 3
        etc. .. Taxes!

Suppose that you connected TX of the UNO to RX of the Mega, and RX of the UNO to TX of the Mega, and connected the grounds. You'd use Serial.print(), Serial.println(), or Serial.write() to send the data and you'd use Serial.available() and Serial.read() to receive the data.

Well, guess what. You'd use EXACTLY the same code with the XBees in place, instead of the wires.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is my cod of the transmitter
Code:
int potPin_1 = 0;      // 1. Poti
int potPin_2 = 1;      // 2. Poti
int potPin_3 = 2;      // 3. Poti

void setup()
{
  Serial.begin(9600);       
}

void loop()
{
  int potVal = analogRead(potPin_1);         
  byte servoVal_1 = map(potVal,0,1023,0,179);
 
int potVal = analogRead(potPin_2);         
  byte servoVal_2 = map(potVal,0,1023,0,179);

  int potVal = analogRead(potPin_3);         
  byte servoVal_3 = map(potVal,0,1023,0,179);
 
  Serial.write(servoVal_1);     
  Serial.write(servoVal_2);     
  Serial.write(servoVal_3);   
  delay(100);                 
how can my cod for the receiver look!

I mean as I kan variable (servoVal_1, servoVal_2, servoVal_3) and receive the correct Servo to assign?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial data is not guaranteed to be delivered. As your sender is constructed, the receiver would read a byte, and assume it was for servo 1. Then, it would read a byte, and assume it is for servo 2. Then, it would read another byte, and assume that it is for servo 3.

Now, suppose a byte got lost. Say the one for servo 2. Servo 3's data would be read, and sent to servo 2. Then, servo 1's data would be read and sent to servo 3. Then, servo 2's data would be read and sent to servo 1.

Doesn't sound good, does it?

Sending something like "<1:120>" would be better, wouldn't it? No doubt what the value is, or which servo it is for. You could read the data using code like this:

Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}
Where it says "Process the packet", you'd use strtok() to extract the servo number (as a character) and the servo value, as a string, and atoi() to convert the string to a number.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS

I am like any on line!

I getesten so much and I'm in no branch!
Work only 1 month with Arduino and still do not understand anything!

I want to make something finite like that!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.write(servoVal_1);
should become
Code:
  Serial.write("<1,");
  Serial.write(servoVal_1);     
  Serial.write(">");

The receiver code that I posted should be easy to modify, to parse a string like "1,145" (which is what would be in inData, if servoVal_1 was 145.

Give it your best attempt, and I'll correct any issues.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
Today I tested with XBee.h What and now I receive Pot'i of first dates!
But it captures the power of nothing or I do something wrong?

Cod transmitter
Code:
#include <XBee.h>

int potPin = 0;
//XBEE------------------------------------------------------------------
XBee xbee = XBee();
uint8_t payload[] = {0,0};
//uint8_t payload[] = {0};
Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID

void setup()
{
  Serial.begin(9600);
  Serial.print("Bereit!");
  xbee.begin(9600);
}
void loop()
{
   //was auch immer machen
 int potVal = analogRead(potPin);
  byte servoVal = map(potVal,0,1023,0,179);
   payload[0] = (servoVal); // werte setzen
   //...
   
   Serial.println(servoVal);
   xbee.send(tx);
   delay(100);
}
Cod receiver
Code:
#include <Servo.h>

#include <XBee.h>
Servo servo_1;


//XBEE------------------------------------------------------------------
XBee xbee = XBee();
Rx16Response rx16 = Rx16Response();
void setup()
{
  xbee.begin(9600);
  servo_1.attach(9);
}
void loop()
{
  if (xbee.readPacket(5000))
  {   
    if (xbee.getResponse().isAvailable())
    {
       xbee.getResponse().getZBRxResponse(rx16);
       Serial.println(rx16.getData()[0]);
    {
       servo_1.write(rx16.getData()[0]);
    } 
}
   
}
}


Michael
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I do something wrong?
Yes.

Code:
Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID
This does not bind payload and tx. It simply copies the data in payload to the rx instance.

Code:
   payload[0] = (servoVal); // werte setzen
The parentheses are useless. You are assigning a new value to payload, but:
Code:
   xbee.send(tx);
sending the old value.

Code:
    {
       servo_1.write(rx16.getData()[0]);
    } 
Useless curly braces.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS

I did not understand your answer!
How Solte dan see my Cod right out?

Can you tell me please please help me continue to ben on despair?

Michael
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I did not understand your answer!
How Solte dan see my Cod right out?
It's only fair that you not understand my answer. I don't understand this question.

After you have a payload to be sent, you need to create an instance of the Tx16Request class to send the data.
Code:
  int potVal = analogRead(potPin);
   byte servoVal = map(potVal,0,1023,0,179);
   payload[0] = servoVal; // werte setzen
  
   Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID

   Serial.println(servoVal);
   xbee.send(tx);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS
again from the beginning!
I would like to. with one each pot'i a servo control (RC remote control)
  I build models of rc in scale 1/32 of Siku and liked me for these models build a remote control!

Michael
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to. with one each pot'i a servo control (RC remote control)
The snippet I fixed does that for one pot for one servo.

If you want to do it for more than one pot for more than one serve, you need to make the payload array larger and store more values in it.

When all the values have been stored in it, create the tx instance and send it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS

we show ourselves the right to request that!



 And that's what I liked a remote control with Arduino and xbee.
Logged

Pages: [1]   Go Up
Jump to: