We’ve recently been working on an example to communicate directly to individual XBee radios. The NXTBee allows for mesh-networking and a whole lot of other really awesome capabilities of the XBee radio system. One of those capabilities is to be able to send messages directly on a single radio, and only that radio.
On request from an educator who was working with her students and the NXTBee, we developed a quick example of how to communicate directly with a single radio. The example lets you read your NXTBee’s unique serial number, set the unique serial number you want to send a message to, and rewrite the serial number, allowing you to send unique messages to different destinations.
We will release a final version soon. Code is pasted below.
NXTBee – Communicate Directly.c
[sourcecode language=”cpp”] ////////////////////////////////////////////////////////////////////////////////////////////////////////// Communicate Directly
//
// This project does three things:
// – Reads the serial number of the NXTBee.
// – Sets the destination serial number for it to communicate to.
// – Resets the destination serial number for it to communicate to.
//
// In between each set and reset, it will send a list of characters and numbers to
// show that is is connected.
////////////////////////////////////////////////////////////////////////////////////////////////////////
// NXTBSer will hold the Serial Number of the NXTBee attached to the NXT
// NXTBDestSer will hold the destination we want to talk to. Note there’s a function below to reset this value.
ubyte NXTBSer[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // This will contain our 64 bit Serial Number
ubyte NXTBDestSer[] = {‘1′,’3′,’A’,’2′,’0′,’0′,’4′,’0′,’6′,’8′,’B’,’6′,’B’,’B’}; // And this is going to be the destination we’re going to send to.
// Some utility arrays.
ubyte newline = 13;
ubyte BytesRead[16];
////////////////////////////////////////////////////////////////////////
// Enter Command Mode Function
// This will put us into command mode.
////////////////////////////////////////////////////////////////////////
void enterCommandMode()
{
const ubyte cmd_enterCommandMode[] = {‘+’,’+’,’+’};
// Note: "13" is ascii representation for carriage return.
// Get into command mode.
wait1Msec(1000); // Wait 1 second.
nxtWriteRawHS(cmd_enterCommandMode[0], sizeof(cmd_enterCommandMode));
wait1Msec(1000); // Wait 1 second.
}
void exitCommandMode()
{
const ubyte cmd_exitCommandMode[] = {‘A’,’T’,’C’,’N’, 13};
wait1Msec(1000);
nxtWriteRawHS(cmd_exitCommandMode[0], sizeof(cmd_exitCommandMode));
wait1Msec(1000);
}
void clearBuffer() {
ubyte dummy[16];
nxtReadRawHS(dummy[0], 16);
}
void Receive(bool wait=false)
{
if (wait)
while (nxtGetAvailHSBytes() == 0) wait1Msec(5);
while (nxtGetAvailHSBytes() > 0) {
nxtReadRawHS(BytesRead[0], 1);
writeDebugStream("%c", BytesRead[0]);
wait1Msec(2);
}
}
//Setup High Speed Link scans for baud rate.
void setupHighSpeedLink()
{
nxtDisableHSPort(); // Make sure HS port is off.
wait1Msec(10);
nxtEnableHSPort(); // Enable the port. In case we get it right, just leave it enabled.
nxtSetHSBaudRate(9600); // Set the baud Rate to the one we’ll try this iteration.
nxtHS_Mode = hsRawMode; // Turn on the HS.
}
// This function will load up the serial number of the NXTBee attached
// to the NXT. And save it. The Serial number comes from the NXTBee in two parts, each 32 bits long.
bool GetNativeSerial(){
wait10Msec(200);
ubyte commandSH[] = {‘A’,’T’,’S’,’H’,13};
ubyte commandSL[] = {‘A’,’T’,’S’,’L’,13};
ubyte response[18];
int i = 0;
enterCommandMode();
wait1Msec(100);
while (nxtGetAvailHSBytes() == 0){
exitCommandMode();
enterCommandMode();
}
clearBuffer();
nxtWriteRawHS(commandSH[0], sizeof(commandSH));
wait1Msec(100);
memset(response, ‘0’, 18); // Reset the array.
while (nxtGetAvailHSBytes() == 0) wait1Msec(5);
while (nxtGetAvailHSBytes() > 0) {
nxtReadRawHS(NXTBSer[i],1);
writeDebugStream("%c", NXTBSer[i]);
wait1Msec(3);
i++;
}
writeDebugStreamLine(" ");
writeDebugStreamLine("%i", i);
i–;
wait1Msec(100);
clearBuffer();
nxtWriteRawHS(commandSL[0], sizeof(commandSL));
wait1Msec(100);
memset(response, ‘0’, 18); // Reset the array.
while (nxtGetAvailHSBytes() == 0) wait1Msec(5);
while (nxtGetAvailHSBytes() > 0) {
nxtReadRawHS(NXTBSer[i],1);
writeDebugStream("%c", NXTBSer[i]);
wait1Msec(3);
i++;
}
writeDebugStreamLine(" ");
writeDebugStreamLine("%i", i);
i–;
NXTBSer[i] = 0;
string strSer; // Print the NXTBee’s Serial number.
StringFromChars(strSer, NXTBSer); //
nxtDisplayCenteredTextLine(5, strSer); //
wait1Msec(60000); // And then hold it on the screen for 60 seconds so you can write it down.
return true;
}
// Sets the destination the NXTBee will communicate with. This
// function sets the NXTBee destination address.
void SetDestination(){
ubyte DH[] = {‘A’,’T’,’D’,’H’, ‘ ‘};
ubyte DL[] = {‘A’,’T’,’D’,’L’, ‘ ‘};
enterCommandMode();
wait1Msec(100);
while (nxtGetAvailHSBytes() == 0) {
exitCommandMode();
enterCommandMode();
wait1Msec(5);
}
// We will work our way backwards.
int x = sizeof(NXTBDestSer);
int i = x- 8;
writeDebugStream("DL: ");
nxtWriteRawHS(DL[0], sizeof(DL));
wait1Msec(10);
while(i < x){
nxtWriteRawHS(NXTBDestSer[i], 1);
writeDebugStream("%c", NXTBDestSer[i]);
i++;
wait1Msec(100);
}
wait1Msec(100);
nxtWriteRawHS(newline, 1);
Receive(true);
writeDebugStreamLine(" ");
wait10Msec(10);
writeDebugStream("DH: ");
i = x – 8;
x = 0;
nxtWriteRawHS(DH[0], sizeof(DH));
wait1Msec(10);
while(x < i){
ubyte out = NXTBDestSer[x];
nxtWriteRawHS(out, 1);
writeDebugStream("%c", NXTBDestSer[x]);
x++;
wait1Msec(100);
}
nxtWriteRawHS(newline, 1);
Receive(true);
exitCommandMode(); // Now we’re exiting command mode.
Receive(true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// This is an example of changing the address we want to talk to. You can copy this, change the
// address as many times as you want to.
////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChangeDestination(){
// ubyte NXTBDestSer[] = {‘1′,’3′,’A’,’2′,’0′,’0′,’4′,’0′,’6′,’8′,’B’,’6′,’B’,’B’};
// int size = sizeof(NXTBDestSer); // Size is the size of address array.
NXTBDestSer[13] = 3;
NXTBDestSer[12] = 2;
NXTBDestSer[11] = 1;
// Now the NXTBDestSer should be NXTBDestSer[] = {‘1′,’3′,’A’,’2′,’0′,’0′,’4′,’0′,’6′,’8′,’B’,’1′,’2′,’3′};
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main Task
////////////////////////////////////////////////////////////////////////////////////////////////////////
task main()
{
// First, setup the HS Link.
eraseDisplay();
setupHighSpeedLink();
PlaySound(soundFastUpwardTones);
// Get and display the serial number of the
// NXTBee connected to the NXT. This will display the
// serial number on the screen of the NXT.
GetNativeSerial();
// Set the destination you want to talk to
// This sets the destination the NXTBee will connect to.
SetDestination();
// Send a test transmission to the XBee
// It should now be going only to the Xbee Serial Number
// That we have addressed.
for(int i = 0; i < 100; i++){
nxtWriteRawHS(i, 1);
writeDebugStream("%i", i);
wait10Msec(1);
}
writeDebugStreamLine(" "); // Send a new line to the Debug Screen
wait10Msec(100);
ChangeDestination(); // Now redefine the array address of the destination.
SetDestination(); // Now set the destination as the address we want to talk to.
// Send a test transmission to the XBee; It should now be going only to the Xbee Serial Number
// That we have addressed.
for(int i = 0; i < 100; i++){
nxtWriteRawHS(i, 1);
writeDebugStream("%i", i);
wait10Msec(1);
}
writeDebugStreamLine(" "); // Send a new line to the Debug Screen
wait10Msec(100);
}
0 Comments
Leave a reply
You must be logged in to post a comment.