Go Down

Topic: Problem with two way xbee communication (Read 501 times) previous topic - next topic

ashubhatt

May 01, 2013, 08:34 am Last Edit: May 01, 2013, 08:36 am by ashubhatt Reason: 1
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: [Select]
#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);
}

PaulS

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: [Select]
  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.

ashubhatt

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.

afremont

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.
Experience, it's what you get when you were expecting something else.


afremont

Ok sorry, I have the red one from Sparkfun.  What pins are your xbee serial comms on?
Experience, it's what you get when you were expecting something else.

PaulS

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.

ashubhatt

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.

PaulS

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: [Select]
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.

ashubhatt

Thanks buddy..
It worked like a charm..  :D

Go Up