Connect Two Make Controllers Wirelessly
This How-to is intended for:
Microcontroller Developers
Purpose
You want to enable 2 Make Controllers to talk with one another without physically connecting them.
Prerequisities
- This how-to requires building firmware for the Make Controller, so you should be set up to do that. Check the Programming the Make Controller tutorial for more info.
- 2 XBee modules, mounted on XBee Boards. Check the XBee tutorial for info on how to mount them.
- And, of course, 2 Make Controllers.
Step by step
The XBee modules are designed by default to work as a "transparent serial line". This means that you send data to one module and it passes that data straight through to another module wirelessly. It doesn't touch your data, or respond to it in any way other than sending it along to another module. Simple, right? Right. This is the mode we'll be using the XBee modules in for this tutorial.
In this how-to, since it's just an example, we'll be sending pretty arbitrary data - a 1 or a 0 once a second, which we'll use to turn the LEDs on or off. You will undoubtedly have something more exciting to do, but this should cover how to get everything set up and working properly :)
So, we're going to be modifying our make.c file. Our strategy is going to look like:
- Create a new task, in which we'll
- Periodically send data via an XBee module
- Read data arriving at the connected XBee module
Take a look at all the code here for an overview. We'll break down the relevant sections below.
Create a Task
We're going to set up our task and do some initialization.
void XBeeTask( void *p )
{
(void)p;
uchar readBuf[BUF_SIZE]; // the buffer we'll be reading into
int writeCounter = 0; // we'll only want to write every once in a while
bool dataValue = false; // keep track of whether we're sending a 1 or a 0
int available;
Serial_SetActive( 1 ); // initialize the Serial subsystem
while( 1 ) // forever
{
// do our work in here
}
}
Sending Data
Because the XBee is connected to the Make Controller's serial port, we'll use the Serial subsystem to send data through it. In this example we'll be sending a message once a second, so we need to keep track of how much time has passed since we last sent a message. Each time we go through our while loop, we're sleeping for a millisecond, so we can increment our writeCounter by one, and then send our message when it's greater than 1000.
Again, in this example all we're doing is sending a 1 or a 0.
if( writeCounter++ > WRITE_PERIOD )
{
uchar x = dataValue ? '1' : '0';
dataValue = !dataValue; // swap this for next time
Serial_Write( x, 1, 0 ); // send it
writeCounter = 0; // reset the counter
}
Reading Data
Each time we come through the while loop we're going to check to see if there's any new data to read from the XBee module with a call to Serial_GetReadable( ). If there's data available we read it and act upon it accordingly. Again, in this example we're just expecting to get a 1 or a 0 and we'll use that to turn on the LEDs, but you may have some more interesting processing of the data to do.
available = Serial_GetReadable( );
if( available )
{
// make sure we only read as much as we have room for
available = (available > BUF_SIZE) ? BUF_SIZE : available;
Serial_Read( readBuf, available, 0 );
// this is where you'd do your own processing of the data
if( readBuf[0] == '1' )
AppLed_SetAll( 1 );
else
AppLed_SetAll( 0 );
}
Further information
You can download both the source code and a compiled .bin file to try this out. Grab xbee-wireless.bin below and upload it to a couple of your Make Controllers, connect your XBee modules and observe the LEDs blinking. Grab the make.c below and start hacking around to get going with your own projects.
Shopping Cart