So I’ve decided to expand my horizons further into the world of Microcontrollers. As a .NET engineer with little hardware experience I found that the learning curve to enter the hardware world has drastically decreased.
For my first attempt at deploying .NET code to a .NET Micro Framework compatible device I am simply trying to send anything from 1 XBee radio to a 2nd XBee radio that is connected to a FEZ Cerbuino Bee micro controller board. The radios are not using WiFi, they are using the 802.15.4 standard which is for things like smart home automation using the ZigBee protocol. But right now I’m not using ZigBee (I’m just sending plain ASCII characters through), therefore I will not explain the details of what ZigBee is or MESH networking or anything like that. In a future blog post I’m sure I’ll get in to those.
- I am using two laptops to do this exercise but that is not necessarily required.
- I am assuming you have already set up your XBee radios for basic communication. I set them up from the tutorial in the popular book “Building Wireless Sensor Networks”.
[UPDATE] There is also a good step-by-step on setting up the XBee radios for communication at this link, although the link is for a different type of project, the section on the XBee radios is exactly what you need to do for this setup as well. http://www.instructables.com/id/Use-xbees-series-2-to-control-a-motor/step2/set-the-firmware-on-the-xbees/http://www.instructables.com/id/Use-xbees-series-2-to-control-a-motor/step2/set-the-firmware-on-the-xbees/
- I am assuming you have properly installed the Microsoft .NET Micro Framework and the applicable GHI Electronics SDK
- Here is the hardware I’m using:
2 x XBee 2mW Wire Antenna - Series 2 (The XBee radios that will be communicating)
2 x XBee to USB adapter (to make the XBee radio connect via USB to my laptop)
1 x FEZ Cerbuino Bee (The XBee receiving the data will plug into this so my .NET code can listen to it)
(FEZ Cerbuino Bee with XBee radio plugged into it)
SETTING UP THE HARDWARE
|1. First plug in the first XBee adapter to the XBee to USB adapter. || |
(Left: XBee plugged into the XBee to USB Adapter, Right: An XBee to USB Adapter by itself)
|2. Open the X-CTU software that comes with the XBee radio and read the modem configuration. || |
|3. The first radio should be set as a ZigBee Coordinator AT. The 2nd radio (which will be plugged into the Cerbuino) will be a ZigBee Router AT. || |
|4. Plug the 2nd XBee (The one set as your router) into the Cerbuino. || |
(XBee radio plugged in to the Cerbuino)
|5. In your project properties, be sure to set the deployment device to your hardware. || |
WRITING THE CODE
|6. Create a new Micro Framework Console application called XBeeReceive. |
We are going to write all our code in the Main function of Program.cs
|7. This code is a simplified mixture of misc. code I found online, with my own modifications. The primary points of the code are as follows: |
- Creating an OutputPort on the built in debug LED is always a good idea in any project.
OutputPort debugLED = new OutputPort((Cpu.Pin)FEZCerbuino.Pin.Digital.LED1,false);- Using the SerialPort object, you can create a connection from the Cerbuino board to the XBee. I connected to the XBee on COM1, but I don’t know if that’s hard coded or if that is configurable. It just happened to work.
SerialPort xbeeSerialPort = new SerialPort("COM1",9600, Parity.None, 8, StopBits.One)
- Subscribing to the DataReceived event on the serial port object is where we make our connection between the two radios.
xbeeSerialPort.DataReceived += (sender, e) =>
//... Code here
};- At the end of the main function, you will always need to do this to be sure the program doesn’t just load in the microcontroller and immediately end. As I have seen it so far in Micro Framework console apps, you generally have an infinite loop or event handler set up before calling this last.
8. Here is the complete code. This will write each message sent from the first XBee to the Debug window, one message at a time.
Since I am sending from the first XBee using a terminal program (The terminal built in to the X-CTU software) the data is getting sent one character at a time so it is outputting one character at a time. To compensate for this I put a pause (Thread.Sleep) to let more characters build up in the buffer before I spit them out to the debug window.
Be sure to watch the LED on your Cerbuino and see it light up with each character that comes through.
using Math = System.Math;
public class Program
public static void Main()
OutputPort debugLED = new OutputPort((Cpu.Pin)FEZCerbuino.Pin.Digital.LED1,false);
using(SerialPort xbeeSerialPort = new SerialPort("COM1",9600, Parity.None, 8, StopBits.One))
const int bufferSize = 256;
byte buffer = new byte[bufferSize];
xbeeSerialPort.DataReceived += (sender, e) =>
//Pause to let buffer build up (so a full word can be sent and not just a character at a time
int numberOfBytes = Math.Min(xbeeSerialPort.BytesToRead, bufferSize);
numberOfBytes = Math.Min(xbeeSerialPort.Read(buffer, 0, numberOfBytes), numberOfBytes);
if (numberOfBytes > 0)
StringBuilder sbDataReceived = new StringBuilder();
for (int i = 0; i < numberOfBytes; i++)
Shahzad Qureshi is a Software Engineer and Consultant in Salt Lake City, Utah, USA
His certifications include:
Microsoft Certified System Engineer
3CX Certified Partner
Global Information Assurance Certification – Secure Software Programmer – .NET
He is the owner of Utah VoIP Store at www.UtahVoIPStore.com and SWS Development at www.swsdev.com and publishes windows apps under the name Blue Voice.