Pages: [1]   Go Down
Author Topic: Xbee series 2. Two way communication of sensor network.{Code Critique?}  (Read 1458 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay. I have 3 series 2 xbees. The coordinator is attached to a Arduino Mega rv 3 and an lcd and a couple switches. The other two router/end devices are also connected to arduinos with temperature and humidity sensors and some relays. I realize how to communicate towards the coordinator so i can put the sensor data on the lcd but I also want the switches on the the coordinator node to affect the other nodes. i dont know if i should leave them in AT mode or use put them in api mode. I dont really want to run processing or other languages bc i want to keep it on just the arduinos till i get the hang of it. I've read Building Wireless Sensor Networks but every example they have that has three or more nodes either uses a different language or the coordinator doesn't talk back to the nodes, it just recieves data.
Anyone have any pointers, sample code or just somewhere you can point me to. Any help would be greatly appreciated.
« Last Edit: March 01, 2013, 04:32:14 pm by dracen4 » Logged

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

We really need to know how you have configured the three XBees, and what code is running on the three Arduinos.

Typically, you would use the XBee library, and API mode, so that end 1 can talk to the coordinator, and the coordinator can talk to end 1 without end 2 knowing anything about it. Similarly, end 2 could talk to the coordinator, and the coordinator could talk to end 2, without end 1 knowing anything about it.

To properly use API mode, though, you need to know the addresses of the three devices and use them in the sketches.
Logged

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

We really need to know how you have configured the three XBees, and what code is running on the three Arduinos.

I don't really have them configured yet, I was looking for a way to do this first and if there was any special way they need to be configured to do this.

Typically, you would use the XBee library, and API mode, so that end 1 can talk to the coordinator, and the coordinator can talk to end 1 without end 2 knowing anything about it. Similarly, end 2 could talk to the coordinator, and the coordinator could talk to end 2, without end 1 knowing anything about it.

 I understand they can talk to each other, but what I haven't found is a way for them to talk to each other at the same time. Typically what i want is for the end devices to send data to the coordinator to put on the lcd. What I don't know how to do is how to make the coordinator to talk back with making the end devices just listen for it.

So, I guess what I've gotten from this so far is that i definitely need them in api mode not at mode, and the answer is just simply right in front of me. Is it as simple as writing a bit of code for the end device to listen for a transfer from the coordinator, because if it is then -facepalm-.

Again, I just want to say thank you for this and any future help I may be given. I know I don't have any specific code, so maybe I just need to experiment. If anyone has anymore pointers, let me know. Otherwise you will hear back if I have a eureka moment. Right now I have to get some sleep.

Logged

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

Ok so I've been experimenting the past couple of days and came up with this code. It is very simple and crude. Started with the library examples and took out what wasn't needed and combined the tx and rx into one program... I just run this on both arduinos and it works as of right now.

Info: I switch the address for the different arduinos* meaning for one i have the address of the other and vice versa.*

Both arduinos are mega rv3s and both the xbees are series 2 in api mode. both are on adafruit xbee adapter kit v 1.1.

if anyone could give me any code critiques i would be happy to accept them.
 
Ty

Code:

/* Bare Bones Recieving and Transfering via Xbee
This example is for Series 2 XBee
 */
#include <XBee.h>
#include <NewSoftSerial.h>


long previousMillis = 0;
long interval = 1000; //How often You want to recieve
long previousMillis2 = 0;
long interval2 = 1100;//How often you want to transmit

XBee xbee = XBee(); //intializing the Xbee
XBeeResponse response = XBeeResponse();

//Recieving Xbee data
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

//transmitting Xbee Data
uint8_t payload[] = { 0,0,0,0,0,0,0,0,0,0,0,0};// Make sure this is big enough

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4089DFF8); // need one of these for each xbee to transmit to
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

//Example Data shells
//Tx
byte test_byte; // For sending simple bytes
char test_char; // chars are automatically converted to bytes
int test_int;// split up into two bytes
String name; // split up each char into a byte
union {  // put in a float... get four bytes
      float f;
      byte b[4];
   } temperaturetx;


union {
      float f;
      byte b[4];
   } temperaturerx;
byte unite1;
byte unite2;
int num1;
char precChar;
void setup() {
  // start serial
  xbee.begin(9600);
 
}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
   
         XbeeTx();
         XbeeRx();
           
}
void XbeeTx(){
unsigned long currentMillis2 = millis();
   if (currentMillis2-previousMillis2 > interval2) {
     previousMillis2 = currentMillis2;

   setData();
   xbee.send(zbTx);
   }
}
void XbeeRx(){
  xbee.readPacket();
   
    if (xbee.getResponse().isAvailable()) {
      // got something
     
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
       
        //now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
       
          getData();
         }
    }

}

void getData(){
  Serial.println("Getting Message");
        num1 = rx.getData(0);
        Serial.println(num1);
        precChar = char(rx.getData(1));
        Serial.println(precChar);
        unite1 = rx.getData(2);
        unite2 = rx.getData(3);
        Serial.println(word(unite1,unite2));
        temperaturerx.b[0] = rx.getData(4);
        temperaturerx.b[1] = rx.getData(5);
        temperaturerx.b[2] = rx.getData(6);
        temperaturerx.b[3] = rx.getData(7);
        Serial.println(temperaturerx.f);
        String name;
        name += char(rx.getData(8));
        name += char(rx.getData(9));
        name += char(rx.getData(10));
        name += char(rx.getData(11));
        Serial.println(name);
}
void setData(){
  test_byte = 4;
  payload[0] = test_byte; //sending simple byte
 
  test_char = 'b';
  payload[1] = test_char; //converts char into byte
  test_int = 444;
  payload[2] = test_int >> 8 & 0xff; //Splits int into two bytes
  payload[3] = test_int & 0xff;
 
  temperaturetx.f = 96.52;
  payload[4] = temperaturetx.b[0];// Converting using the data type defined in "union"
  payload[5] = temperaturetx.b[1];
  payload[6] = temperaturetx.b[2];
  payload[7] = temperaturetx.b[3];
 
  name = "myst";
  payload[8] = name[0];
  payload[9] = name[1];
  payload[10] = name[2];
  payload[11] = name[3];
}

Im about to try and add a third into the network a try to get two to communicate with the coordinator and the coordinator to communicate with both. Wish me luck.
Logged

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

Code:
uint8_t payload[] = { 0,0,0,0,0,0,0,0,0,0,0,0};// Make sure this is big enough
You make sure the array is big enough by explicitly setting its size. Each element will automatically be initialized to 0.

Code:
long previousMillis = 0;
long interval = 1000; //How often You want to recieve
long previousMillis2 = 0;
long interval2 = 1100;//How often you want to transmit
Nothing, two, three,... Is that how you normally count?

Code:
union {  // put in a float... get four bytes
      float f;
      byte b[4];
   } temperaturetx;


union {
      float f;
      byte b[4];
   } temperaturerx;
Once wasn't enough? Are you sure twice will be? You can create multiple instances of the union in one step.

Code:
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
  
         XbeeTx();
         XbeeRx();
            
}
void XbeeTx(){
Lots of blank space in the functions that are jammed up tight against each other. Why?

That's about as far as I got. The indenting and style are awful. Try Tools + Auto Format, sometime.
« Last Edit: March 01, 2013, 07:58:00 pm by PaulS » Logged

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

How's this?

Code:
/* Bare Bones Recieving and Transfering via Xbee
 This example is for Series 2 XBee in api mode
 */
#include <XBee.h>
#include <NewSoftSerial.h>

long previousMillis = 0;
long interval = 1000; //How often You want to recieve
long previousMillis1 = 0;
long interval1 = 1100;//How often you want to transmit

XBee xbee = XBee(); //intializing the Xbee
XBeeResponse response = XBeeResponse();

//Recieving Xbee data
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

//transmitting Xbee Data
uint8_t payload[12];// Make sure this is big enough

// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4089DFF8); // need one of theses for each xbee to transmit to
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

//Example Data shells
//Tx
byte test_byte; // For sending simple bytes
char test_char; // chars are automatically converted to bytes
int test_int;// split up into two bytes
String name; // split up each char into a byte
union {  // put in a float... get four bytes
  float f;
  byte b[4];
}
temperaturetx, temperaturerx;

byte unite1;
byte unite2;
int num1;
char precChar;
void setup() {
  // start serial
  xbee.begin(9600);

}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {

  XbeeTx();

  XbeeRx();
}
void XbeeTx(){
  unsigned long currentMillis1 = millis();
  if (currentMillis1-previousMillis1 > interval1) {
    previousMillis1 = currentMillis1;

    setData();
    xbee.send(zbTx);
  }
}
void XbeeRx(){
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    // got something

    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
      // got a zb rx packet

      //now fill our zb rx class
      xbee.getResponse().getZBRxResponse(rx);   

      getData();
    }
  }
}

void getData(){
  Serial.println("Getting Message");
  num1 = rx.getData(0);
  Serial.println(num1);
  precChar = char(rx.getData(1));
  Serial.println(precChar);
  unite1 = rx.getData(2);
  unite2 = rx.getData(3);
  Serial.println(word(unite1,unite2));
  temperaturerx.b[0] = rx.getData(4);
  temperaturerx.b[1] = rx.getData(5);
  temperaturerx.b[2] = rx.getData(6);
  temperaturerx.b[3] = rx.getData(7);
  Serial.println(temperaturerx.f);
  String name;
  name += char(rx.getData(8));
  name += char(rx.getData(9));
  name += char(rx.getData(10));
  name += char(rx.getData(11));
  Serial.println(name);
}
void setData(){
  test_byte = 4;
  payload[0] = test_byte; //sending simple byte

    test_char = 'b';
  payload[1] = test_char; //converts char into byte
  test_int = 444;
  payload[2] = test_int >> 8 & 0xff; //Splits int into two bytes
  payload[3] = test_int & 0xff;

  temperaturetx.f = 96.52;
  payload[4] = temperaturetx.b[0];// Converting using the data type defined in "union"
  payload[5] = temperaturetx.b[1];
  payload[6] = temperaturetx.b[2];
  payload[7] = temperaturetx.b[3];

  name = "myst";
  payload[8] = name[0];
  payload[9] = name[1];
  payload[10] = name[2];
  payload[11] = name[3];
}

I know the formatting is/was horrible because I had to piece it all together before I had to go to work. But, do you see anything wrong with the logic of the code?
Logged

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

Code:
union {  // put in a float... get four bytes
  float f;
  byte b[4];
}
temperaturetx, temperaturerx;
I don't like the variable names hanging around like that. They look misplaced. I prefer them on the line following the }. It's the ONLY time I think anything should be on the same line as the }.

Code:
void loop() {

  XbeeTx();

  XbeeRx();
}
void XbeeTx(){
would look, to me, a lot better as:
Code:
void loop()
{
  XbeeTx();
  XbeeRx();
}

void XbeeTx()
{
Functions should be separated, visually. Unless there is a change in functionality/behavior/etc. in the code in a function, blank lines contribute nothing.

Code:
  String name;
  name += char(rx.getData(8));
  name += char(rx.getData(9));
  name += char(rx.getData(10));
  name += char(rx.getData(11));
  Serial.println(name);
Using a String to avoid using 4 print statements, or a for loop, doesn't win many friends on this forum.

My only other comment is that you have far too many global variables that are used in only one function.
Logged

Pages: [1]   Go Up
Jump to: