Programming body sensor networks can be a daunting task due to the limited computation, memory and energy resources available.  BSNOS is an operating system specifically designed for Body Sensor Network nodes focused on enabling an easy to use development platform without sacrificing efficiency.  The operating system is bundled with an Integrated Development Environment (IDE), BSNOS IDE.
Programming simple body sensor network applications can be achieved easily by following the step by step tutorials.  Get started here

Getting Started with BSNOS
Download the BSNOS IDE package from here.
Unzip the contents and copy it to your preferred location.  The executable is located in the top level of the BSNOSIDE directory and is named BSNOSIDE.
To install the BSN USB board driver, plug the BSN USB board into your USB port and wait for the driver to automatically install.  If the driver does not automatically install, then you can manually install it from the BSNOSIDE/drivers directory. 
Let's get started with our first BSN application, Hello World a blinking application: click here. 

Your first BSN Application, Hello World!
Let's start by creating the simplest BSN application possible, a blink application.
In the BSNOS IDE, create a new "BSNOS Java Project" by:
  • Right click in the project explorer (or select the "File" menu).
  • Select the "New" menu item followed by the "Project" menu item.


Expand the "BSNOS" project group and select "BSNOS Java Project Wizard", and press "Next".

Enter the project name "Blink", and press "Finish".

The project "Blink" will be created and it will be displayed in the "Project Explorer".  If the project explorer is not visible on screen you can show it by selecting the "Window" menu, followed by "Show View" and then "Project Explorer".
Expand the "Blink" project to reveal its contents.  Then expand the "src" tree element, and further expand the "(default package)" element to reveal the source code as depicted below:
The file "BSNOSAppMain.java" contains the main execution starting point.  Open up this file by double-clicking on it.  The starting execution point is defined by the annotation "@BSNOSStart()".  Let's now add the source code which will toggle an LED and then sleep a while.  We want the application to perform this action ad infinitum, so we'll wrap the LED toggling and sleeping in a "while loop".  So, create a while true loop in the "main()" function as follows:
while (true) {


The BSN hardware functions are encapsulated inside the "BSN" Java class.  Type "BSN" on the first line inside the while block, then press "CTRL" and hit the space bar at the same time.  This will display the auto-complete feature as shown below:
The first suggestion in the auto-complete popup should be the BSN Java class (bsnos.platforms.bsn).  Press "." since we want to access the class' functions to interact with the LEDs  The code to toggle LED number 0 followed by sleeping for 500 milliseconds is displayed below:
while (true) {
BSN.toggleLed( (byte) 0 );
BSN.waitMS( (short) 500 ); 
The "toggleLed(byte ledNr)" function is used to toggle the LED (i.e. LED 0).  The "waitMS(short ms)" function is used to wait a number of milliseconds.  Since numeric literals are represented by integers by the Java compiler, the LED number must be typecasted to "byte" and similarly the milliseconds specified must be typecasted to "short".

If any errors exist in the code they will be underlined in red as shown below:
If any errors exist, the "Problems" window can be displayed to further investigate the error.  Display this window by selecting the "Window" menu followed by "Show View" and then "Problems".  In the case above, the "waitMS" function was misspelt as "wajtMS".

Once, no errors are reported in the source we can compile the code to BSNOS bytecode by selecting the project we want to compile in the "Project Explorer" and then clicking "BSNOSCompile":
The "BSNOSCompileConsole" window will display compilation messages.  A successful compilation will end with the text "Compilation Complete." as below:
Now, that the code is successfully compiling to BSNOS bytecode, we can program a BSN node.  Ensure that the BSN USB board is connected and that a BSN main board is on the USB board.  If you are plugging in the USB board for the first time you may be required to install the drivers for the board (auto-installation of the drivers may work as well).
To download the application to the BSN node first click the project that you would like to download, in this case the "Blink" project.  Then select the COM port which the USB board is connected to, by clicking the "COM Port" dropdown arrow shown below:
Then press the "BSNOSDownloadApp" button which is next to the dropdown arrow.
The "BSNOSDownloadConsole" will display download messages.  First BSNOS will be downloaded to the BSN node, followed by the application you have written.  A successful download will look similar to below:
If the download was successful you should now see one of the LEDs on the main BSN board toggling! 
Next, we will show you how to implement a sample and send application.  Click here for the next part.

Sample and Send
Sample and send applications are often sufficient for many Body Sensor Network deployments.  Let's create a sample and send application that samples the accelerometer and broadcasts the sensed values via the wireless module.
Start by creating a new "BSNOS Java Project" and name it "SampleAndSend".
To sample a sensor a single call is required to be made.  The following table lists the different sensor sample functions:
Magnetometer / Compass 
After sampling a sensor, the respective sensor's sensed values can be retrieved using:
Sensor Value 
Accelerometer X 
short BSN.getAccelX() 
Accelerometer Y 
short BSN.getAccelY() 
Accelerometer Z 
short BSN.getAccelZ() 
Gyroscope X 
short BSN.getGyroX() 
Gyroscope Y 
short BSN.getGyroY() 
Gyroscope Z 
short BSN.getGyroZ() 
Magnetometer X 
short BSN.getMagX() 
Magnetometer Y 
short BSN.getMagY() 
Magnetometer Z 
short BSN.getMagZ() 

The BSNOS API exposes a number of functions to easily build up radio messages.  Individual values of type byte, short, int and float can be appended to a radio message to be sent.  The following list is used to append values to a radio message:
Value to append 
BSN.appendByteToRadio(byte b) 
BSN.appendShortToRadio(short s) 
BSN.appendIntToRadio(int i) 
BSN.appendFloatToRadio(float f) 
Once a message has been built and is ready to be sent, a single call to the following function is used to send the radio message:
BSN.sendRadioMsg( short destination_addr )
To send broadcast messages the following constant is used:

To create the sample and send application, the first step is to sample the sensor.  Let's sample the accelerometer by making a call to BSN.performAccelSample() as follows:
public static void main() {
The next step is to get the sensor readings sensed and append them to a radio message.  So, we will use BSN.getAccelX to retrieve the accelerometer's X axis reading, and then append it to a radio message by passing the returned value into BSN.appendShortToRadio.
public static void main() {
BSN.appendShortToRadio( BSN.getAccelX() ); 
The same is required for the Y and Z axes, so the code needs updating as follows:
public static void main() {
BSN.appendShortToRadio( BSN.getAccelX() );
BSN.appendShortToRadio( BSN.getAccelY() );
BSN.appendShortToRadio( BSN.getAccelZ() );
 Now, let's send the radio message as a broadcast message:
public static void main() {
BSN.appendShortToRadio( BSN.getAccelX() );
BSN.appendShortToRadio( BSN.getAccelY() );
BSN.appendShortToRadio( BSN.getAccelZ() );
The above code samples the sensor and sends the 3 axes' values over the wireless module.  This needs to be repeated in a while loop so that it will continuously sample and send.  In order to not constantly sample and send since this will consume the radio bandwidth, a wait function call should be implemented (in the code below we add a 100 millisecond wait).  Also, code to toggle the LED can be added to indicate that the sensor is operating.  The final code is as follows:
public static void main() {
  while ( true ) {
   BSN.appendShortToRadio( BSN.getAccelX() );
   BSN.appendShortToRadio( BSN.getAccelY() );
    BSN.appendShortToRadio( BSN.getAccelZ() );
   BSN.sendRadioMsg( BSN.BROADCAST_ADDR );
   BSN.waitMS( (short) 100 );
   BSN.toggleLed( (byte) 0 ); 
 The above code is all that is needed to create a sample and send application.  Having loaded this code onto a BSN node you should see the LED blinking.  The next step is to receive the data on a base station.  Click here to move on to the next part.

Base Station
Base stations are mainly used for collecting data from sensor networks and forwarding the received data to the PC.  Let's create a simple base station which forwards messages received to the PC.
Create a new "BSNOS Base Station Java Project".
The default code is enough to have an operating base station.  The default code performs the following:
  • Listens for incoming messages
  • When a message is received it forwards it over the serial connection, and then toggles a LED
The serial protocol used to communicate from base stations to the PC is defined by:
Now, compile the base station project and download it to the base station node.  Once downloaded, ensure that your base station is powered, and then power on a node that has been programmed with the "SampleAndSend" application and you should see the base station node's LED blinking.  This means that it is receiving data (and attempting to forward it over the serial connection).  The next step is to actually see what data is being sent to the PC.  Click here for the next part.

PC Side Application
Data retrieved in a sensor network is usually meant to be forwarded to a PC for analysis.
At this stage, you should have a sample and send node, along with a base station node receiving the sampled data.
To create a PC side application, create a new "PC Side Base Station Project".
Expand the project in the "Project Explorer", and then further expand "src" and "(default package)".  Then open the Java file.
The "main" function creates an instance of the PC Side thread, which listens to a serial port for incoming data.
Upon receiving a valid serial message, the "serialPacketReceived" function will be called.
The "msg" parameter is the data packet received.
Once the data is received, we can do anything with it, for example save it to a file, display its contents in a console, or a graph.
The following is code to dump the received message in HEX to the console:
public void serialPacketReceived(byte[] msg) {
  String s = "";
  for(byte b : msg) {
    int bi = b & 0xFF;
    if (bi < 16) {
      s += "0"; 
    s += Integer.toHexString(bi);
    s += " "; 
If your base station is connected to your computer, then you can run the Java application by clicking the Debug button for the project.  You should be prompted to select the COM port your BSN board is connected to.  Thereafter, if your BSN sample and send node is sending data, and your base station node is receiving data, you should see your data being received in the console.
Next we will look into understanding the data received.  Click here to move on to the next part. 

Understanding the Messages
The "Sample and Send" application samples and the accelerometer and sends the X, Y and Z coordinate over the wireless medium.
Radio messages also contain other information:
  • Message Length
  • Source Address
  • Destination Address
  • RSSI (received signal strength indicator)
  • CRC (cyclic redundancy check)
We will now work on parsing the radio message received.
Create a new "PC Side Base Station Project", and open the Java file in the "src/(default package)" directory.
The "serialPacketReceived" function is called when a message is received, and the "msg" parameter contains the raw data received.
To parse the data all we need to do is define the structure of the message.
Create a new Java class, named "AccelerometerMessage".
The radio message structure for the Sample and Send application is as follows:
<Dest_Addr> <Source_Addr> <Length> <X> <Y> <Z> <RSSI> <CRC>
A protocol helper class is included in the BSNOS IDE package which will automatically translate the raw data received into a given structure.  In order to use it a class must be defined with the structure of the message.  For message field an int field must be created.  Also, each field must be attributed with one of the following:
Used for 
Signed 8 bit values 
Signed 32 bit values 
Signed 16 bit values 
Unsigned 8 bit values 
Unsigned 16 bit values 
Therefore, the following code should be used to represent the message inside the AccelerometerMessage.java file:
public int destination;
public int source;
public int length;
public int x;
public int y;
public int z;
public int rssi;
public int crc;
Note that the order of the fields are important, so they should not be altered. 
To change the raw bytes in the "serialPacketReceived" function to the message structure, and output the coordinates to the console use the following code:
AccelerometerMessage m = 
(AccelerometerMessage) ProtocolHelper.getObjectFromMessage(msg, AccelerometerMessage.class);
System.out.println("X:\t" + m.x + "\tY:\t" + m.y + "\tZ:\t" + m.z); 
 In the next part we will look into visualising the data in a graph.  Click here to move on to the next part.

Graphing the Data
Looking at the readings can help you to design algorithms.
A graphical UI frame is included in the toolset namely the "MultiSeriesJChart2DFrame" class.
Create an instance of the frame and instruct its thread to execute as follows (you must also declare the variable type):
graphFrame = new MultiSeriesJChart2DFrame( 200 );
To add data to the graph the "addReading" function is used:
graphFrame.addReading(seriesNumber, value); 
In order to compile and run the code you must import the jchart2d library by the menu: Build Path > Configure Build Path > Libraries > Add External Jars.  Then select the 'jchart2d-3.2.0.jar' file.
That's it, using these tools you should now be able to see your data.  Majority of other tasks you will require to do will involve Java PC side coding.  Good luck!