Arduino and NXT

Hello,

I am new to Arduino and just started a new Project trying to connect an Arduino with an NXT brick over a wireless communication using Xbee. I am actually trying to replicate this project: Connect LEGO Mindstorms and the Arduino

The setup I have going on:

  • An Arduino Uno board + Arduino Wireless Proto Shield + Xbee Series 2 (Coordinator)
  • An NXT brick + NXTBee + Xbee Series 2 (End Point)

Using X-CTU, I have updated the firmwares of both Xbee as follows:

  • The Coordinator -> ZNET 2.5 COORDINATOR AT (v. 1047) with (PAN ID = 1AAA) ; (SC = 13) ; (DH = High Address of End point) ; (DL = Low Address of End point)
  • The End Point -> ZNET 2.5 ROUTER/END POINT AT (v. 1247) with (PAN ID = 1AAA) , (SC = 13) ; (DIO7 = RS-485 ENABLED HIGH for communication with the NXT Brick)

I have the following RobotC code running on the NXT. It's an example program by dexter industries that just reads every byte received by the end point Xbee and displays it on the screen.

/**********************************************\
|*  This program will continuously read in    *|
|*  bytes over the XBee connection, flushing  *|
|*  and displaying them to the LCD one at a   *|
|*  time as long as there are bytes to read.  *|
\**********************************************/

task main()
{
  nxtEnableHSPort();                                //Enable High Speed Port #4
  nxtSetHSBaudRate(9600);                           //Xbee Default Speed
  nxtHS_Mode = hsRawMode;                           //Set to Raw Mode (vs. Master/Slave Mode)

  //short bytesRead;
  ubyte incomingData;

	eraseDisplay();
	bNxtLCDStatusDisplay = true; // Enable top status line display
	wait1Msec(25);

  while(true)
  {
	  if(nxtGetAvailHSBytes())                        //Check to see if we have any data coming in
	  {
		   if(nxtReadRawHS(&incomingData, 1))            //Ask for the data, save to "incomingDate".
		   {
		     //If we actually got data, display it to the LCD
		     nxtDisplayTextLine(1, "Receive: %c, %i", (char)incomingData, (int)incomingData);
		   }
		}
		else  //No Data, Wait a little before trying again
		{
      wait1Msec(25);                                //Wait 25ms before checking again.
		}
  }
  nxtDisableHSPort();                               //Disable HS Port #4
}

I have the following code running on the Arduino board. It is the exact same code taken from the example project given above via the link.

#include <SoftwareSerial.h>

int inByte = 0; // Data coming in from computer to ARduino

byte power = 0; // Initializing power, steering, direction.
byte steer = 0;
byte direc = 0;

#define rxPin 2
#define txPin 3

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup() {
  Serial.begin(9600);
  // set the data rate for the NewSoftSerial port
  mySerial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) { // Take steering information in
    // get incoming byte:
    // w – forward
    // a – left
    // d – right
    // z – reverse
    inByte = Serial.read();
    Serial.println((char)inByte);
    if (inByte == 'w') {
      power = 10;
      direc = 1;
    }
    if (inByte == 'a') steer = -70; // Turn left
    if (inByte == 'd') steer = 70; // Turn right
    if (inByte == 'z') { // Reverse
      power = 10;
      direc = 0;
    }
    if (inByte == 's') power = 0; // Stop
    if (inByte == 'q') { // Go straigh
      steer = 0;
    }
  }
  
  //char data[] = {NULL, 100, 50, 30};
  //mySerial.print(data[0]);
  
  mySerial.print('/');
  // RobotC now knows the next two transmissions will be power and steering respectively.
  
  mySerial.print(power); // Send Power
  mySerial.print(steer); // Send steering info.
  mySerial.print(direc);

  delay(25);

}

When I run both programs, the XBee seem to discover each other which is awesome. However, the only data the end point is receiving is what I enter in the Serial Monitor. So for example, when I enter 'w' in the Serial Monitor and press send I would expect the COORDINATOR to send the char '/' followed by the values of power, steer and direction. But the COORDINATOR actually just send the char 'w' which I entered in the monitor and thus on the NXT brick display I get "Received: w, 119".

Thus, it seems that the program on the Arduino board is ignored and the Serial Monitor immediately sends to the Xbee module which transmits whatever was received from the monitor. The wireless shield is put in usb mode otherwise I cannot talk with it using the Serial Monitor.

Any suggestions as to how I can fix this?

Cheers

Anyone an idea or a suggestion? I am stuck...

I have tried to change firmwares to API, but then the Xbee's do not discover each other. Also, when they are in API mode they do not respond to AT commands. I have tried the Xbee-Arduino library (GitHub - andrewrapp/xbee-arduino: Arduino library for communicating with XBee radios in API mode), and used the ATCommand Example:

#include <XBee.h>

XBee xbee = XBee();

// serial high
uint8_t shCmd[2] = {'S','H'};
// serial low
uint8_t slCmd[2] = {'S','L'};
// association status
uint8_t assocCmd[2] = {'A','I'};

AtCommandRequest atRequest = AtCommandRequest(shCmd);

AtCommandResponse atResponse = AtCommandResponse();

void setup() { 
  xbee.begin(9600);
  // start soft serial
  Serial.begin(9600);
  
  // Startup delay to wait for XBee radio to initialize.
  // you may need to increase this value if you are not getting a response
  delay(5000);
}

void loop() {

  // get SH
  sendAtCommand();
  
  // set command to SL
  atRequest.setCommand(slCmd);  
  sendAtCommand();

  // set command to AI
  atRequest.setCommand(assocCmd);  
  sendAtCommand();
  
  // we're done.  Hit the Arduino reset button to start the sketch over
  while (1) {};
}

void sendAtCommand() {
  Serial.println("Sending command to the XBee");

  // send the command
  xbee.send(atRequest);

  // wait up to 5 seconds for the status response
  if (xbee.readPacket(5000)) {
    // got a response!

    // should be an AT command response
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);

      if (atResponse.isOk()) {
        Serial.print("Command [");
        Serial.print(atResponse.getCommand()[0]);
        Serial.print(atResponse.getCommand()[1]);
        Serial.println("] was successful!");

        if (atResponse.getValueLength() > 0) {
          Serial.print("Command value length is ");
          Serial.println(atResponse.getValueLength(), DEC);

          Serial.print("Command value: ");
          
          for (int i = 0; i < atResponse.getValueLength(); i++) {
            Serial.print(atResponse.getValue()[i], HEX);
            Serial.print(" ");
          }

          Serial.println("");
        }
        
      } else {
        Serial.print("Command return error code: ");
        Serial.println(atResponse.getStatus(), HEX);
      }
    } else {
      Serial.print("Expected AT response but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
    }   
  } else {
    // at command failed
    if (xbee.getResponse().isError()) {
      Serial.print("Error reading packet.  Error code: ");  
      Serial.println(xbee.getResponse().getErrorCode());
    } else {
      Serial.println("No response from radio");  
    }
  }
}

The result I get is this:

Sending command to the Xbee
~     SH[No response from radio
Sending command to the Xbee
~     SLWNo response from radio
Sending command to the Xbee
~     AI1No response from radio

Which is very weird... The strange characters I get are the result of calling "xbee.send(atRequest);".

I am starting to think, that the Arduino Wireless Proto Shield does not work with the Xbee 2 modules. Should I buy the Series 1 Xbee modules instead?