Using both analog and digital UART pins with RF transmitter and Xbee on arduino

Robin2:
I'm confused.

Is the link in Reply #18 the source of the code in Reply #16 or is it something new that you have stumbled on?

...R

Sorry if I wasn't clear. Yes, that link is the source of the code.

Robin2:
Try using SoftwareSerial for the GPS and see what happens.

I will give it a shot. Thanks

Robin2:
Try using SoftwareSerial for the GPS and see what happens.

...R

SoftwareSerial gives me tons of elbow room and pin options. Thanks for that.

I've found some code that seems simple, but I'm having trouble to sent it to the xbee or serial monitor to see what it's doing. I'm quite positive I'm doing something wrong, so again forgive my lack of understanding. I've been at it for several days browsing multiple tutorials and forums which many are outdated...

I'd appreciate any help. Thanks

#include <TinyGPS.h>
#include <SoftwareSerial.h>
 
long lat,lon; // create variable for latitude and longitude object
 
SoftwareSerial gpsSerial(2, 3); // create gps sensor connection
TinyGPS gps; // create gps object
 
void setup(){
  Serial.begin(9600); // connect serial
  gpsSerial.begin(4800); // connect gps sensor
}
 
void loop(){
  while(gpsSerial.available()){ // check for gps data
   if(gps.encode(gpsSerial.read())){ // encode gps data
    gps.get_position(&lat,&lon); // get latitude and longitude
    // display position
    Serial.print("Position: ");
    Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
    Serial.print("lon: ");Serial.println(lon); // print longitude
   }
  }
Serial.print("");
}

There are Serial.print() commands in your code. What is being displayed?

I know it is a very crude suggestion but try putting delay(500); as the last thing in loop() and see what happens.

Reading serial input using WHILE is not reliable because the Arduino can read the data much faster than it arrives and the WHILE may terminate before all the data has arrived.

Have a look at the second example in Serial Input Basics. If you use that then I think you would do gps.encode(receivedChars); and go on from there.

...R

Robin2:
There are Serial.print() commands in your code. What is being displayed?

I know it is a very crude suggestion but try putting delay(500); as the last thing in loop() and see what happens.

The delay(500); didn't work. With or without the delay, this is what shows up after random intervals: þþþþþþþþ.

Robin2:
Have a look at the second example in Serial Input Basics. If you use that then I think you would do gps.encode(receivedChars); and go on from there.

I am assuming you are referring to the second code example in post #1. I've read the posts and it helped somewhat. Once again, forgive my lack of understanding. The code seems like a somewhat generic code for serial input data, and prints a byte of data for each new line?

When I want to use SoftwareSerial, would I replace every line containing serial with SoftwareSerial?

This code is the closest thing I'm looking for and by far the best one I've tried. Cheers!

slicknessessity:
When I want to use SoftwareSerial, would I replace every line containing serial with SoftwareSerial?

If you are using my code to receive data over SoftwareSerial then wherever my code uses Serial to RECEIVE data you should use whatever name you have given to your SoftwareSerial instance. However where my code uses Serial to show the results you will probably want to continue using Serial so that the output can be seen on the Serial Monitor.

If you are having trouble it is easier to help if you post your latest code.

...R

Robin2:
If you are using my code to receive data over SoftwareSerial then wherever my code uses Serial to RECEIVE data you should use whatever name you have given to your SoftwareSerial instance. However where my code uses Serial to show the results you will probably want to continue using Serial so that the output can be seen on the Serial Monitor.

If you are having trouble it is easier to help if you post your latest code.

...R

I'm using 2 arduino uno's with of course 2 xbees. Both talk to each other fine. However, when I tried to send gps data, it's receiving, but in decimal form. I may be missing something, but this is what I have:

Your code sending out to xbee:

const byte numChars = 32;
char receivedChars[numChars];  // an array to store the received data

boolean newData = false;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  
  // if (Serial.available() > 0) {
           while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}

Data from serial monitor sending out:

This just in ... $GPRMC,170906.00,A,4105.77264,N
This just in ... $GPVTG,,T,,M,0.013,N,0.025,K,A*
This just in ... $GPGGA,170906.00,4105.77264,N,0
This just in ... $GPGSA,A,3,26,16,31,03,22,23,29
This just in ... $GPGSV,4,1,15,03,35,267,38,04,7

Receiving xbee code:

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

void loop()
{
  if (Serial.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.print(Serial.read());
  }
}

Data receiving on serial monitor:

6065114100117105110111321051153211410197100121621310606511410011710511011132105115321141019710012162131084104105115321061171151163210511032464646321061310606511410011710511011132105115321141019710012162131084104105115321061171151163210511032464646

Not sure what I'm doing wrong

slicknessessity:
Not sure what I'm doing wrong

I have no time now so I will look at this in the morning.

...R

Robin2:
I have no time now so I will look at this in the morning.

...R

Not a problem. You've helped out a lot already! You've certainly earned yourself a beer!

Thanks

I went with connecting the gps tx directly into the xbee DIN, and had the xbee receiver DOUT to the arduino to translate the incoming data and print to serial monitor, all of which is on 9600 baud, and it works very well!

Unfortunately, I can't seem to get the xbee to send an additional signal without interfering with the gps signal. I tried to do a test hooking up an arduino to it and a simple send/receive code. I setup digital pin 4 on both xbees, one as DIN, and the other as DOUT low. The problem is that it seems to echo back whatever message I send on the router when I look at the serial monitor, and the base doesn't receive a message. I was curious to see if I could send a message through the base, but it does the same thing as the router. Both are set to AT mode.

I'll keep trying the settings, but any helpful input would be appreciated.

Thanks

From the two programs in Reply #25 I really have no idea what you are trying to do.

Are they alternatives? - either one taking data from the GPS depending on which you upload to the Arduino?
Or is one of them taking data from the other? - and in which direction?

In both programs you only have one Serial connection yet both "appear" to be communicating with two devices - the GPS and the Serial Monitor.

If you want a program to take data from a GPS and send it to the serial monitor (or to the XBee if it is connected to pins 0 and 1) try this version which uses SoftwareSerial

You will need to connect the GPS to pins 4 and 5 and GND

// receive GPS data using SoftwareSerial and send it to the Serial monitor 

#include <SoftwareSerial.h>

const byte rxPin = 4; // you can use different pins if necessary (but NOT pins 0 and 1)
const byte txPin = 5;

SoftwareSerial gpsSerial (rxPin, txPin);


const byte numChars = 32;
char receivedChars[numChars];  // an array to store the received data

boolean newData = false;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  gpsSerial.begin(9600);
}

void loop() {
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (gpsSerial.available() > 0 && newData == false) {
        rc = gpsSerial.read();

        if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
        }
   }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}

The output from showNewData() could also be received by a copy of receiveWithEndMarker( running on a different Arduino.

This is Reply #29. It would be useful if you could remind me what you are trying to connect to what.

If you think more carefully about how you describe what you want to do, or what you are trying to do I reckon you will benefit from that as much as we will. When I am trying to figure something out I generally write an essay for my own use to focus my thoughts and ensure I don't overlook things. It also acts as a record of things I tried and did not work.

...R

I made a diagram of both a simple working setup of what is working, and an ideal setup.

In my working setup, The sending xbee only carries 1 signal, the gps, across the xbee. The receiving arduino has the code almost identical to what you posted in reply #29.

The ideal setup is having gps and a reoccurring message across the xbees. The sending xbee will carry gps in one input, and a void loop of some message on another input. The Receiving xbee will grab both and display it. If I can't display both on one serial monitor, maybe I can aim for an led screen for one display, and serial monitor for the other?

My current components:
2 arduino uno
2 xbees: 802.14.5 serial 1
1 GPS unit: GP-20U7
1 LED display unit: ADM1602U

Images from Reply # 30 so people don't have to download it. See Image Guide

...R

I won't pretend I understand the diagrams. If I number the pictures in Reply #31 like this

1 2
3 4

Picture 1 seems to make no use of the Arduino.

Picture 2 seems to be receiving data from the XBee on Pin 0

Picture 3 seems to be expecting the XBee to take 2 inputs.

Picture 4 seems to be expecting the XBee to make 2 outputs to different Arduino pins.

I don't know anything about XBees so I have no idea what they might do with two inputs, or how they might make two outputs.

The way I would deal with this is to have the GPS feed the Arduino (NOT the XBEE) and have the Arduino send the GPS message as well as any other message to the XBee. I would use SoftwareSerial for the GPS.

On the receiving side the XBee would receive whatever it gets and pass it to the Arduino using SoftwareSerial. The Arduino would then show the stuff on the Serial Monitor.

To illustrate I imagine the GPS sending
$GPRMC,170906.00,A,4105.77264,N
to the first Arduino. The Arduino would then send
$GPRMC,170906.00,A,4105.77264,N
My Other Message
to the first XBee

which would be received by the second XBEE and passed on to the second Arduino.

This concept seems very much simpler to me.

...R

Robin2:
The way I would deal with this is to have the GPS feed the Arduino (NOT the XBEE) and have the Arduino send the GPS message as well as any other message to the XBee. I would use SoftwareSerial for the GPS.

On the receiving side the XBee would receive whatever it gets and pass it to the Arduino using SoftwareSerial. The Arduino would then show the stuff on the Serial Monitor.

To illustrate I imagine the GPS sending
$GPRMC,170906.00,A,4105.77264,N
to the first Arduino. The Arduino would then send
$GPRMC,170906.00,A,4105.77264,N
My Other Message
to the first XBee

which would be received by the second XBEE and passed on to the second Arduino.

This concept seems very much simpler to me.

...R

Your method works like a charm! It made all the difference to do it that way. It was really easy too! I appreciate you taking the time to help out new guys like me.

Thanks again!