You are on page 1of 14

XBee and SunSPOT

Author: Jonnathan Jimenez Date: 2 April 2012 Version: 1.0

Introduction
This document is about the connection between XBee and SunSPOT. The document tells how to set-up a network with these devices. The XBee is a small, cheap and power efficient radio hardware module to be used to send data from a rational distance. The specifications of the XBees that we used are displayed in the table below. XBee series 1 Indoor/ urban range Up to 30 m Outdoor/RF Up to 100 m Transmit power output 1 mW RF data rate 250 Kbps Network topologies Point to point, star RF protocol 802.15.4 XBee series 2 Up to 40m Up to 120 m 2 mW 250 Kbps Point to point, star, mesh 802.15.4, Zigbee

Contents
Introduction .................................................................................................................................................. 2 1. 2. 3. Sun SPOT and XBee ............................................................................................................................... 4 Resetting / Unable to communicate with modem ............................................................................... 7 Code ...................................................................................................................................................... 8

1.

Sun SPOT and XBee

The XBee can be connected to a SunSPOT and from there can you send data to another XBee that is connected to a computer or e.g. a Sun SPOT. Before the XBee can be used you need to configure it first. The most usual configuration (a star network) is explained below. The XBee can be configured with X-CTU. This program can be downloading from here.
Figure 1: Star network

For a star network you need a coordinator and end devices. The coordinator is the one that receives all the data from the end devices.
Figure 2: Coordinator

1 PAN ID: this is the ID of the network this module uses to communicate with the end devices. 2 DH: destination address to which this module transmits, this is set to FFFF to enable broadcast mode. 3 NI: this is the identifier used to identify this particular module (e.g. coordinator) 4 FUNCTION SET: this is used to specify if this module should be a COORDINATOR.

Figure 2: end device

PAN ID: this is the ID of the network this module and the other modules use to communicate with the coordinator. DH: destination address to which this module transmits, this is set to 0 to enable broadcast mode (you can also set the coordinator address to send transmit only with them). NI: this is the identifier used to identify this particular module (e.g. end_device_1) FUNCTION SET: this is used to specify if this module should be an END DEVICE.

** The XBee sometimes need to be reset when you read or write. The XBee can be reset by contact between RST and GND pins with a wire.

Now we need physically connect the XBee and the SunSPOT, below the pin layout for the connection.
Figure 3: physically connection between XBee and SunSPOT

The red board is optional but easier, because you have a good review of the pins.

XBee Red: GND Blue: 3.3v Green: DOUT Violet: DIN

SunSPOT Red: GND Blue: 3.3v Green: DO Violet: D1

2.

Resetting / Unable to communicate with modem

The following fix has been quoted from: http://www.digi.com/support/kbase/kbaseresultdetl?id=3203 Problems may occur when attempting to communicate with an XBee. The baud rate may be unknown, or the firmware within has disabled the serial port. If you work with the XBee AIO or DIO adapters, you will probably need to use this procedure. You can help XCTU reflash the XBee by manually activating the XBee Bootloader. Open an X-CTU Terminal Window Change the baud rate to 115200 Assert/check DTR, De-assert/uncheck RTS, Assert/check Break Hit the /RESET button on the XBee development board (example: XBIB) De-assert/uncheck Break Click on the Terminal Window tab, then Type a Band hit Enter or Carriage Return (the B must be upper case) The XBee should return the Bootloader Menu that looks something like this:

Without resetting or power cycling the XBee go to the Modem Configuration tab Check Always Update Firmware& select firmware you wish to load Click Write

3.

Code

The XBee uses a serial connection. The old SunSPOT version has only one serial connection on the Edemoboard with the pins D0 and D1.
Figure 4: Old Edemoboard

The new SunSPOT version has three serial connections, one is in the Edemoboard with the pins D0 and D1, the second are the smaller header in the Edemoboard with the RX and TX pins and the last one is in the top of the processor board. Figure 5: New Edemoboard:

With the following code you can use the serial connections of the SunSPOT. In the code is described how to choose the serial connection that you want to use. The code can be used in both Sun Spot versions (old and yellow).

/* * SunSpotApplication, you can use this and deploy in the SunSPOT for the * sending of data through a serial communication. * Also you can use this and deploy in the SunSPOT for the * reciving of data through a serial communication. */ package org.sunspotworld; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * The main of the class * Here you can give which you wan to use, here the possibilities: * edemoserial : is available on the pins D0 and D1 on the sensor board * usart0 or usart : is available on the pins TX and RX on the sensor board * usart1 : is available through the top connector on the processor board whit a suitable adaptor */ public class SunSpotApplication extends MIDlet { Serial serial; protected void startApp() throws MIDletStateChangeException { serial = new Serial("usart"); serial.outputOpenConnection(); serial.inputOpenConnection(); while(true){ serial.sendData("Ok "); serial.readData(); } } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { //Is not use, but don't remove it. } protected void pauseApp() { //Is not use, but don't remove it. } }

/* * Serial are the class that control the communication from * the serial communication. */ package org.sunspotworld; import com.sun.spot.sensorboard.EDemoBoard; import com.sun.spot.util.Utils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; /** * The main of the class * @author Jonnathan Jimenez */ public class Serial { // This is compare whit the UART, if wel then it go use edemo. private static String edemoserial = "edemoserial"; // These is use for the UART on the sensor board. UART edemo; InputStream input; OutputStream output; String uart; boolean streamOut, streamIn; // Param for the UART. int braudrate, databits, parity_u, stopbit_u, sleepTime; //sleeptime in milliseconds.; String parity; float stopbit; /** * Use the follow possibilities to make connection to a serial port * edemoserial: is available on pins D0 & D1 on the sensor board * usart: go to the pins RT & TX on the sensor board * usart1: is available through the top connector on the processor board whit a suitable adaptor * @param uart */ public Serial (String uart){ this.uart = uart; edemo = new UART(); streamIn = false; streamOut = false; // Standaard settings for the UART connection braudrate = 9600; databits = 8; parity = "none"; stopbit = 1; parity_u = 0; stopbit_u = 1; sleepTime = 500; } /** * Open the way for sending data through the serial. */ public void outputOpenConnection(){ if(!checkEdemoserial() && !streamOut){ try { output = Connector.openOutputStream("serial://"+uart+"?baudrate="+braudrate+"&databits="+databits+"&parity="+parity+"&stopbits="+stopbit); streamOut = true; System.out.println("Output connetor started"); } catch (IOException ex) { ex.printStackTrace(); } } else{ edemo.UARTinit(); } } //<<The code would be continue on the next page >>

/** * Open the way for reading receive data through the serial. */ public void inputOpenConnection(){ if(!checkEdemoserial() && !streamIn){ try { input = Connector.openInputStream("serial://"+uart+"?baudrate="+braudrate+"&databits="+databits+"&parity="+parity+"&stopbits="+stopbit); streamIn = true; System.out.println("Input connetor started"); } catch (IOException ex) { ex.printStackTrace(); } } else{ edemo.UARTinit(); } } /** * Send data. * @param message */ public void sendData(String message){ if(!checkEdemoserial() && streamOut){ try { output.write(message.getBytes()); } catch (IOException ex) { ex.printStackTrace(); } Utils.sleep(sleepTime); } else{ edemo.sendData(message); } } /** * Read the bits that are available on the serial and print it out in the console. */ public void readData(){ String message = null; if(!checkEdemoserial()&& streamIn){ while (true){ try { byte mssg = (byte) input.read(); char mssg_chart = (char)(mssg); message = new String(new char[]{mssg_chart}); System.out.println(message); Utils.sleep(sleepTime); } catch (IOException ex) { ex.printStackTrace(); } } } else{ edemo.readData(); } } //<< code would be continue on the next page >>

/** * Here you can set the parameters for the serial connection * @param braudrate * @param databits * @param parity * @param stopbit */ public void setUARTParam(int braudrate, int databits, String parity, float stopbit){ if(!checkEdemoserial()){ this.braudrate = braudrate; this.databits = databits; this.parity = parity; this.stopbit = stopbit; System.out.println("UART param:"+braudrate+","+databits+", "+parity+", "+stopbit); } else{ edemo.setUARTparam(braudrate, databits, parity, stopbit); } } /** * Check if the serial that they want to use are on the sensor board or not. * @return true/false. */ private boolean checkEdemoserial(){ if(uart.equals(edemoserial))return true; else return false; } /** * Close the output way from the serial connection. */ public void outputCloseConnection(){ if(!checkEdemoserial() && streamOut){ try { output.close(); streamOut = false; System.out.println("Output connection is close."); } catch (IOException ex) { ex.printStackTrace(); } } else{ edemo.UARTclose(); } } /** * Close the input way from the serial connection. */ public void inputCloseConnection(){ if(!checkEdemoserial() && streamIn){ try { input.close(); streamIn = false; System.out.println("Input connection is close"); } catch (IOException ex) { ex.printStackTrace(); } } else{ edemo.UARTclose(); } } // << The code would be continue on the next page >>

/** * This class is use when de UART on the sensor board is called. * whit it you can change to: start,close and setting on the UART. */ private class UART { EDemoBoard board; boolean uartI; /** * Constructor for UART. */ public UART (){ board = EDemoBoard.getInstance(); uartI = false; } /** * The UART is initialized whit the standard variables. */ public void UARTinit (){ if (!uartI){ board.initUART(braudrate, databits, parity_u, stopbit_u); uartI = true; System.out.println("UART started..."); } else{System.out.println("UART is already started...");} } /** * The UART are terminated. */ public void UARTclose (){ if(uartI){ board.closeUART(); System.out.println("UART is closed..."); } else{System.out.println("Not UART started get...");} } /** * Data is sending. * @param mssg */ public void sendData(String mssg){ if(uartI){ board.writeUART(mssg); Utils.sleep(sleepTime); } else{} }

//<< The code would be continue on the next page >>

/** * Data is reading and printed in the console. */ public void readData (){ if(uartI){ String message = null; byte mssg; char mssg_chart; try { if(board.availableUART()>0){ mssg = board.readUART(sleepTime); mssg_chart = (char)(mssg); message = new String(new char[]{mssg_chart}); System.out.println(message); } else{} } catch (IOException ex) { ex.printStackTrace(); } } else{} } /** * Here you can set the parameters for the UART connection. * @param br * @param db * @param pt * @param sb */ public void setUARTparam(int br, int db, String pt, float sb){ braudrate = br; databits = db; parity_u = Integer.valueOf(pt).intValue(); stopbit_u = (int)sb; System.out.println("Change UART param to:"+br+", "+db+", "+pt+", "+sb); } } }