Pages: [1]   Go Down
Author Topic: Use coordinator to tell xbee(series1) end devices when to send data  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I'm trying to get 3 end devices and 1 coordinator to talk together.

My setup is series 1 and AP2 mode.
When the end devices just send with a delay (1000ms) the coordinator might not receive data from all three end devices. So one end device might get trough with its data more than the others.

What I have tried is to hook up pin 20 on end devices to a digital pin on Arduino to read if its high or low.

Coordinator sends an AT request to set pin 20 high on end device #1.
When end device #1 is high it sends data back to coordinator.

This works sort of, the end device sends but it like the coordinators not in sync and doesn't always receive the data.
With the counter I have in end device code it can jump as much as 100 at a time. I would like it to count 1 at a time.

Have also tried to get the end devices to set there own pin20 to low at end of code, but when I do this it won't send any data.
Any suggestions. Used google, read other forums post, pdf documents, but none the wiser.

End device code below
Code:
#include <SoftwareSerial.h>
#include <XBee.h>

SoftwareSerial softserial(2, 3); // RX, TX


XBee xbee = XBee();
uint8_t package[] = { 0, 0, 0  };
Tx16Request tx1 = Tx16Request(0x1234, package, sizeof(package));


// Set DIO0 (pin 20)
uint8_t d0Cmd[]={'D','0'};
uint8_t valueHigh[]={5};
uint8_t valueLow[]={4};

int xbeePin20 = 6;                 // LED connected to digital pin 13
int times;

void setup()
{
  Serial.begin(115200);
  pinMode(xbeePin20, INPUT);  
  
    softserial.begin(9600);
  xbee.setSerial(softserial);
  xbee.begin(softserial);
  
  delay(5000);
}

void loop()
{

  
  while  (digitalRead(xbeePin20) == 0){
    
    
   Serial.println("pin 0 is LOW");  
 
  }  

     times = times + 1; //adds 1 to see if all the data is received.
     package[1] = times >> 8 & 0xff;
     package[2] = times & 0xff;
    
       xbee.send(tx1);      
    
    Serial.print("pin 0 is HIGH");
    Serial.println(digitalRead(xbeePin20));
  
    
    }

Coordinator code. I have removed the code for the other Xbees as I first need coordinator to receive from just 1 end device all the time.
Code:
#include <XBee.h>
#include <SoftwareSerial.h>

SoftwareSerial softserial(2, 3); // RX, TX
XBee xbee = XBee();


XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();

int value1;
int value2;
int value3;

int count1;
int count2;
int count3;



// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload1[] = { 0, 0, 0 };
uint8_t payload2[] = { 0, 0, 0 };
uint8_t payload3[] = { 0, 0, 0 };

// Set DIO0 (pin 20)
uint8_t d0Cmd[]={'D','0'};
uint8_t valueHigh[]={5};
uint8_t valueLow[]={4};

// SH + SL of your remote radio
XBeeAddress64 remoteAddress1 = XBeeAddress64(0x0013a200, 0x407C917C);
XBeeAddress64 remoteAddress2 = XBeeAddress64(0x0013a200, 0x4002392E);
XBeeAddress64 remoteAddress3 = XBeeAddress64(0x0013a200, 0x400239EE);

// Create a remote AT request with the IR command
RemoteAtCommandRequest remoteHigh1 = RemoteAtCommandRequest(remoteAddress1, d0Cmd, valueHigh, sizeof(valueHigh));
RemoteAtCommandRequest remoteHigh2 = RemoteAtCommandRequest(remoteAddress2, d0Cmd, valueHigh, sizeof(valueHigh));
RemoteAtCommandRequest remoteHigh3 = RemoteAtCommandRequest(remoteAddress3, d0Cmd, valueHigh, sizeof(valueHigh));

//set low
RemoteAtCommandRequest remoteLow1 = RemoteAtCommandRequest(remoteAddress1, d0Cmd, valueLow, sizeof(valueLow));
RemoteAtCommandRequest remoteLow2 = RemoteAtCommandRequest(remoteAddress2, d0Cmd, valueLow, sizeof(valueLow));
RemoteAtCommandRequest remoteLow3 = RemoteAtCommandRequest(remoteAddress3, d0Cmd, valueLow, sizeof(valueLow));


void setup() {
  
  Serial.begin(115200);
  
  softserial.begin(9600);
  xbee.setSerial(softserial);
  xbee.begin(softserial);
  
  delay(5000);
  
}

void loop() {
    



xbee.send(remoteHigh1);  


 xbee.readPacket();  
 

  
    if (xbee.getResponse().isAvailable()) {
      // got something
      
      if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
        // got a rx packet
        
        if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
                xbee.getResponse().getRx16Response(rx16);
         for (int i = 0; i < rx16.getDataLength(); i++)
          payload1[i] = rx16.getData(i);
          
       }
          
    }
  
 
   uint8_t analogHigh1 = payload1[1];
   uint8_t analogLow1 = payload1[2];
   value1 = analogLow1 + (analogHigh1 * 256);
  
 
  count1 = count1 + 1;
  
   Serial.print("value1  ");
   Serial.print(value1);
     Serial.print("    ");
   Serial.println(count1);
   xbee.send(remoteLow1);  
  
   delay (500);
   }
    
  
    
      
}



  


« Last Edit: November 10, 2012, 08:33:17 am by m_maursund » Logged

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

Code:
SoftwareSerial softserial(2, 3); // RX, TX
You have a softserial attached to these pins? If so, what is a softserial? If not, couldn't you have used a better name?

Code:
void loop() {
   



xbee.send(remoteHigh1);   


 xbee.readPacket();   
 

 
    if (xbee.getResponse().isAvailable()) {
Damn my scroll finger is getting tired. Is all this wasted space necessary?

Your coordinator code is assuming, incorrectly, that a reply is received immediately after a message is send to a device. That doesn't happen. Sending and receiving are asynchronous events. You send when you want a reply, like sending an e-mail. You forget about that request. It might take 10 seconds to get a response. It might take two weeks. It might never happen. You need to deal with the responses that you do get just like e-mail. When you get mail, you look to see who it is from. You do NOT assume that the responses arrive in the order sent.
Logged

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

The softserial is where the xbee module is connected.
Code:
  softserial.begin(9600);
  xbee.setSerial(softserial);
  xbee.begin(softserial);


I've been fiddling around with different delay times after xbee.send(remoteHigh1); but have not found a solution there.
PaulS, you write your post like you know how to fix the issue, please let me know instead of writing it like a teacher....


Logged

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

Quote
please let me know instead of writing it like a teacher....
You have to treat sending and receiving messages just like e-mail. On any given pass through loop() you may, or may not, send a message.

On any given pass through loop(), you may, or may not, receive a reply. The reply that you receive may be from the last message sent, or it may be from a message sent 8 messages back. You can't assume that the latest reply is because of the latest message sent.

There is no simple solution to your problem, because your whole program is built on a fundamentally incorrect assumption that serial data transmission and reply reception is instantaneous. It is not. You need to make a copy of your program, and modify the copy. Delete everything before setup() except the #include statements. Delete everything is setup(). Delete everything in loop().

Then, start adding stuff back in, understanding that reply 1 is not necessarily in response to message 1, and is not by any means going to happen in the same pass through loop().
Logged

Pages: [1]   Go Up
Jump to: