Xbee & SoftwareSerial Question

Hi everyone!

I am trying to send data over an xbee using the softwareserial library, but it doesn't seem to be working properly.

have an arduino uno connected to some temperature/pressure sensors, and this same arduino is also connected to an Xbee (series 1). This collects data that it then transmits every 5 seconds.

On the receiving end, I have a PC connected to another arduino uno, which is also connected to an Xbee (series 1).

Firstly, I know that both xbees are configured correctly and that they are talking to each other correctly (all the tests went well). I think my issue is with the sketch I am loading on the receiving end, and hence, my posting and humble request for help :slight_smile:

When I connect the xbee to the regular rx(0) and tx(1) ports in the arduino and I use this sketch (lets call it sketch #1), I get the correct data in the serial output:

4/12/2013 1:11:14 unix_time: 1365729074 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 31.02 Atm_Press2_mbars: 1012.48
4/12/2013 1:11:19 unix_time: 1365729079 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 31.01 Atm_Press2_mbars: 1012.50
4/12/2013 1:11:25 unix_time: 1365729085 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 30.96 Atm_Press2_mbars: 1012.41
etc etc.

/*
 Simple arduino code to receive data via xbee
 */

int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    Serial.write(incomingByte);
    Serial.print("");


    }
}

However... when I connect the xbee to virtual ports using SoftwareSerial (digital pins 2 and 3 in the arduino) and I use this sketch (lets call it sketch #2), the serial output I get is not correct towards the end of each line:

4/12/2013 1:14:8 unix_time: 1365729248 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 31.24AmPes_br:11.1
4/12/2013 1:14:14 unix_time: 1365729254 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 31.26AmPes_br:11.6
4/12/2013 1:14:20 unix_time: 1365729260 Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA Humidity2_%: 56.30 Temp3_C: 29.00 Temp4_C: 31.26AmPes_br:11.5
etc etc...

/*
 arduino code using softwareserial to receive data via xbee
 */

#include <SoftwareSerial.h>

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

int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  portOne.begin(9600);
}

void loop() {
  // see if there's incoming serial data:
  if (portOne.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = portOne.read();
        
    Serial.write(incomingByte);
    Serial.print("");

    }
  }

Does anyone know what the issue could be? How come it works well when I use the standard 0/1 pins, but not well when I use the 2/3 pins?

Any thoughts would be super appreciated...

Thanks!!

How are the XBees connected to the Arduinos? What does the sending sketch look like?

How are the XBees connected to the Arduinos? What does the sending sketch look like?

Hi Paul

thanks for your response. Both Xbees are connected to the arduino using the rx/tx pins (in both cases, the Dout from xbee goes into rx for arduino, and the Din of xbee goes into the tx pin of arduino.

The sending sketch looks like this:

// Lets load the libraries needed:

#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"
#include <Adafruit_BMP085.h>

#define DHTPIN 2     // what pin the DHT is connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

RTC_DS1307 RTC;
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;


//load the settings and call the libraries to be used:
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    
    dht.begin();
 
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }

  // following line sets the RTC to the date & time this sketch was compiled
  RTC.adjust(DateTime(__DATE__, __TIME__));    
}
 
 
void loop () {
     //Time stuff
    DateTime now = RTC.now();

    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(" ");
    Serial.print("unix_time: ");
    Serial.print(now.unixtime());
    Serial.print(" ");
    
    
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // print the 1st part of the line
  Serial.print("Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA "); 
   
       
  // Here we will start filling in the list for the sensors
  // for the arduino (XBee)
  Serial.print("Humidity2_%: "); 
  Serial.print(h);
  Serial.print(" ");
  Serial.print("Temp3_C: "); 
  Serial.print(t);
  Serial.print(" ");
  Serial.print("Temp4_C: ");
  Serial.print(bmp.readTemperature());
  Serial.print(" ");    

  //Convert the atmospheric pressure from Pa to millibars        
  double mbars = (double)bmp.readPressure()/100;
  Serial.print("Atm_Press2_mbars: ");
  Serial.print(mbars);    
  Serial.println();

      delay(5000);    // delay of 5 minutes (5 * 60* 1000)
}

hey Paul

I think I figured out how to fix it. It looked like there were too many characters in one line for the xbee to handle, so I just reduced the amount of text per line. Things look happy now :slight_smile:

Thanks!!

It looked like there were too many characters in one line for the xbee to handle, so I just reduced the amount of text per line. Things look happy now

Excellent. I was going to suggest that, which was why I wanted to see the sender.

There is, for instance, no reason to send this every time:

  // print the 1st part of the line
  Serial.print("Humidity1_%: NA Temp1_C: NA Temp2_C: NA Atm_Press1_mbars: NA Light_level: NA ");

"Humidity2_%: " doesn't really convey any more information than "H:", does it? The data at this position is Humidity data, whether the world is spelled out, or not. The sender knows that tthe value is a percentage. The receiver can assume that.