Yun, I2C, Xbee sending and receiving not working?

Hello all,

I’m trying to finish up my project using the following equipment, and running into a problem:

The problem is that when I connect everything, I’m not seeing communication happening between the MCs. I’m disconnecting the Rx/Tx pins before uploading the sketches to the boards to avoid errors there, but still see nothing. I believe my code is correct, but am not 100% sure. I’m posting it below.

I’m anxious to finish this project, and would REALLY appreciate the help of the community if you’re willing.

The goal is:

Coordinator:

  • Broadcasts a specific ASCII key to activate router code, causing router to broadcast desired data.
  • Waits for broadcast from router, appends received data to txt file with timestamp from I2C.
  • Broadcasts next ASCII key & repeats process

Router:

  • Waits to receive ASCII key, triggering a broadcast of the live reading.
  • Goes back to waiting

Coordinator Code:

#include <FileIO.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
int incomingByte;
char station = 65;   //This, when sent, will be the ASCII character
                     //'A' and can be incremented
                      
void setup() {
  // Coordinator setup:
  Bridge.begin();
  Serial.begin(9600);
  FileSystem.begin();
  establishContact(); //Program will attempt to establish contact before                                     
}                     //anything else



void loop() {
  // When a response is received, read it into the string
  incomingByte = Serial.read();
    String dataString;
    dataString += " = ";
  
  //This statement should print into string: station ID, timestamp, reading
  if (Serial.available() > 0) {
    //Get incoming transmission and add Station ID
    dataString += station;
    dataString += ", ";
    //Add timestamp
    dataString += getTimeStamp();
    //Write incoming data to string
    dataString += incomingByte;
    delay(5000);
    dataString += ", ";
    incomingByte = Serial.read();
    dataString += incomingByte;
    station++;
        
  }
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  // The FileSystem card is mounted at the following "/mnt/FileSystem"
  File dataFile = FileSystem.open("/mnt/sda1/voltagelog.txt", FILE_APPEND);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too, to be removed after proof of concept phase:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 

  delay(5000);
  establishContact();

}
  
// This function return a string with the time stamp
String getTimeStamp() {
  String result;
  Process time;
  // date is a command line utility to get the date and the time 
  // in different formats depending on the additional parameter 
  time.begin("date");
  time.addParameter("+%D-%T");  // parameters: D for the complete date mm/dd/yy
                                //             T for the time hh:mm:ss    
  time.run();  // run the command

  // read the output of the command
  while(time.available()>0) {
    char c = time.read();
    if(c != '\n')
      result += c;
  }

  return result;
}
  
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print(station);  // Send ASCII character across serial
    delay (15000);
  }
}

Receiver Code:

int sensor = 0;
const int numReadings = 30;
float readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
float total = 0;                  // the running total
float average = 0;                // the average

float currentValue = 0;

void setup() {
  // Initialize the Serial port
    Serial.begin(9600);
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;       
}

void loop() {
  // 
  if (Serial.read() == 'A'){
    sensorRead();
    Serial.write(sensor);
    delay(5000);
    sensor = analogRead(A0);
    Serial.write(sensor);
  }

delay(15000);

}

void sensorRead(){
      total= total - readings[index];          
    readings[index] = analogRead(0); //Raw data reading
    readings[index] = (readings[index]-510)*5/1024/0.04-0.04;//Data processing:510-raw data from analogRead when the input is 0; 5-5v; the first 0.04-0.04V/A(sensitivity); the second 0.04-offset val;
    total= total + readings[index];       
    index = index + 1;                    
    if (index >= numReadings)              
      index = 0;                           
    average = total/numReadings;   //Smoothing algorithm (http://www.arduino.cc/en/Tutorial/Smoothing)    
    currentValue= average;
    sensor = currentValue;
    delay(30);
}

After doing some research, it seems I may need to use the softwareserial.h library in order to communicate with the XBees on Yun boards because of a lack of UART chip onboard. I'm away from my equipment at the moment, so does anyone know if I'm on the right track?

Thanks!

Hi, Did you begin your sketches for this project in stages.

Had you established communication between the YUN/Xbee pair before adding the extra features?

Did you write a sketch just to do this, prove you have comms?

Tom.... :)