Pages: [1]   Go Down
Author Topic: Problem with two way xbee communication  (Read 466 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using one Arduino UNO board with Xbee shield and Xbee Pro module at station 1.
And a Xbee Explorer with Xbee Pro module at station 2.

The problem is I want to transmit the GPS co-ordinates of station 1 to station2.
But when station 2 sends a character to station 1 it should turn on its camera.
My code does not work when I test it with Xbee modules. But it works when I test it on station 1 using serial com port.
Please HELP.


Code:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#define camPin 13
#define RXPIN 10
#define TXPIN 11
#define GPSBAUD 9600
TinyGPS gps;
SoftwareSerial uart_gps(RXPIN, TXPIN);
void getgps(TinyGPS &gps);

long previousMillis = 0;  
long interval = 1000;
boolean camToggle=false;
void setup() {

  Serial.begin(9600);
  pinMode(camPin,OUTPUT);
  uart_gps.begin(GPSBAUD);
  Serial.println("");
  Serial.println("GPS");
  Serial.println("       ...waiting for lock...           ");
  Serial.println("");
}

void loop() {
  unsigned long currentMillis = millis();

  //Transmit at every 1 second interval
  if(currentMillis - previousMillis > interval) {     

    while(uart_gps.available())  {
      int c = uart_gps.read();  
      if(gps.encode(c)) {
        getgps(gps);      
      }
    }
    previousMillis = currentMillis;  
  }
  else if(Serial.available()>0) {
    char received = Serial.read();
    Serial.println(received);
    if (received=='c' || received=='C') {
      camToggle=~camToggle;
    }
  }

  if (camToggle) {
    digitalWrite(camPin,HIGH);  
  }
  else if(!camToggle) {
    digitalWrite(camPin,LOW);  
  }    
}


//Get GPS Co-ordinates
void getgps(TinyGPS &gps) {
  float latitude, longitude;
  gps.f_get_position(&latitude, &longitude);
  Serial.print(latitude,5);
  Serial.print(",");
  Serial.println(longitude,5);
}
« Last Edit: May 01, 2013, 01:36:58 am by ashubhatt » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My code does not work when I test it with Xbee modules.
So, you don't suppose it's important to tell us WHICH XBees you have, how they are connected to the Arduinos, or how you have configured them?

Code:
  if(currentMillis - previousMillis > interval) {     

    while(uart_gps.available())  {
      int c = uart_gps.read();   
      if(gps.encode(c)) {
        getgps(gps);     
      }
    }
    previousMillis = currentMillis;   
  }
  else if(Serial.available()>0) {
Get rid the if test and the else part of the else if statement. You do NOT collect gps data "for a while". You collect it when it is available, as often as that is possible.

Reading from the serial port only when not listening to the GPS does not make sense.

It was a piss-poor example you started from.
Logged

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

I'm using Xbee Pro S2B Modules. I've connected Xbee to Arduino using Xbee Shield.
I've already configured them in XCTU using USB Explorer Modules and they work like a charm.

And yes I'm not so good at programming, that's why I posted a Question in forum. (A Common Sense!!)
Basically I want to send GPS co-ordinates serially at every one second interval & in the remaining time Arduino should listen for any serially incoming characters that will trigger the Camera Pin.
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have the little switch on shield set to UART (pins 0 and 1) or to the other position (pins 2 and 3)?  You shouldn't have to do anything to get data to go thru.  They should communicate right out of the box with their default settings.  The RSSI LED should come on for a few seconds when a module receives data.
Logged

Experience, it's what you get when you were expecting something else.

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

No there is no such switch.

This is how my Xbee Shield looks:
http://site.gravitech.us/Arduino/XBeeShield/ARD-XBEE_1.jpg
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok sorry, I have the red one from Sparkfun.  What pins are your xbee serial comms on?
Logged

Experience, it's what you get when you were expecting something else.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Basically I want to send GPS co-ordinates serially at every one second interval & in the remaining time Arduino should listen for any serially incoming characters that will trigger the Camera Pin.
How often does your GPS send data? Often, it is only once a second. So, the method you have for collecting serial data for a period of time is wrong.
Logged

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

okay.. Got your point..!!
GPS sends data at a rate of 1 Hz i.e. every 1 second it transmits data.
Then how to program my arduino, so that whenever the data from GPS is available, it should transmit it via XBee and for the remaining time it should Listen to the Serial Port.
Can you help me with the programming part?
An example program will be helpful.
Thanks for the replies.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then how to program my arduino, so that whenever the data from GPS is available, it should transmit it via XBee and for the remaining time it should Listen to the Serial Port.
It isn't an either/or situation. That's what I'm trying to tell you.

On every pass through loop(), you collect any GPS data. Most of the time there will be none, but occasionally, there will be. You collect that data, each time checking to see if that is the last of the data. If it is, you send that data and clear the array.

On every pass through loop(), you collect any Serial data. Most of there time, there will be none, but occasionally, there will be. If there is, deal with it.

Your loop() function will look like:
Code:
void loop()
{
   while(uart_gps.available() > 0)
   {
      // Collect the data

      // If the end of a sentence has arrived, send it
   }

   if(Serial.available() > 0)
   {
      // Read that data

      // Do something with it
   }
}

Notice that there is no mention of time. If time WERE a factor, it would be in the "If the end of the sentence" part, where you would decide that the GPS is sending data to you more often (not the case with your GPS) than you want to forward it.

The code that you already have will go after the appropriate comments above.
Logged

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

Thanks buddy..
It worked like a charm..  smiley-grin
Logged

Pages: [1]   Go Up
Jump to: