2013-10-10

Radio-transmission with Sun SPOT

Upplagd av victorvj

In our project we decided to use Radio-transmission between Sun SPOTs in order to create a game for kids named MagicTag.
In this project we are using a Sun SPOT + RFID reader per kid and a Tag, which they are using to transmit the information we want. This information is their own Sun SPOT address. There is a communication back when the package has been received where we send the LED color to change the color of the Sun SPOT that was touch.

Use case:
PlayerA: SunSPOT_A + TagA
PlayerB: SunSPOT_B + TagB

TagA touches SunSPOT_B
SunSPOT_B opens a connection to SunSPOT_A
SunSPOT_A receives the info. Checks the information, transmits LED Color to SunSPOT_B.
SunSPOT_B receives the color and changes its color to the new color.

Constants:

private String port = ":10";
private String commType = "radiogram";
private String EMPTY_MSG = "MESSAGE";

Receiver code:

DatagramConnection readConnection = (DatagramConnection) Connector.open(commType + "://" + port);
Datagram packet = readConnection.newDatagram(readConnection.getMaximumLength());
                    
while(true){
      readConnection.receive(packet);
      String receivedMsg = packet.readUTF();
      System.out.println("receivedMsg " + receivedMsg);

      if (receivedMsg.equals(EMPTY_MSG)) {
            System.out.println("TAG RECEIVED");
            DatagramConnection answer = (DatagramConnection) Connector.open(commType + "://" + packet.getAddress() + port);
            Datagram answerPacket = answer.newDatagram(readConnection.getMaximumLength());
            answerPacket.writeUTF(getMyColorCode());
            answer.send(answerPacket);
            System.out.println("SEND >>> Color sent: "+ getMyColorCode());
            answer.close();
      } else {
            System.out.println("COLOR RECEIVED: "+receivedMsg);
            setSpotColor(getColorFromCode(receivedMsg));
      }
}        


Sender code:

while (true) {
     try {
          String RFIDTagContent = getRFID();
          System.out.println("rfid read : " + RFIDTagContent);
                
          String enemyAddress = this.getAddressForTag(RFIDTagContent);
                                
          if (enemyAddress.equals(this.myAddress())) {
               System.out.println("Ignoring tag");
          } else {
               ledBlink();

               DatagramConnection writeConnection = (DatagramConnection)Connector.open(commType + "://" + enemyAddress + port);
               Datagram packet = writeConnection.newDatagram(writeConnection.getMaximumLength());

               packet.writeUTF(EMPTY_MSG);
               writeConnection.send(packet);
               System.out.println("SEND >>> Tag sent!");
               writeConnection.close();
          }
      } catch (IOException ex) {
          ex.printStackTrace();
      }
}


RFID code:

protected String getRFID() {
    byte[] idArray = new byte[16];
    int index = 0;

    do {
        try {
            if (board.availableUART() > 0) {
                idArray[index] = board.readUART();
                index++;
            } else {
                Utils.sleep(200);
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    } while (index < 16);

    String returnString = "";
    try {
        returnString = new String(idArray, "US-ASCII");
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }

    return returnString.trim();
}



Note: this is the basic code for communicating 2 Sun SPOTs triggering the communication with a RFID reader.

KEY points:

Some of the key points, or at least what we consider more important in the communication.
  • When creating a packet to transmit the information it is crucial to give it a size enough to transmit the information. We have seen some tutorial with wrong code:
    • Right:
Datagram packet = readConnection.newDatagram(readConnection.getMaximumLength());
    • Wrong:
Datagram packet = readConnection.newDatagram(0);

  • Always when it is possible try to close the communication. So in our case, we are closing the communication when we are transmitting back:
     writeConnection.close();
  • Don't write anything you don't need in the packet. For example, you can't extract sender address from the packet information, so don't write this kind of information in the message. There are some other things that could be useful for you, so check the API.
     packet.getAddress();

  • You can also use broadcast in this kind of communication. The only thing you need to do is changing the receiver address when you open a communication to broadcast. Then, you need to adapt your receiver code to do whatever you need.
Check more about our project here.

0 kommentarer: