German English
Radio

Transmit data wirelessly via a IEEE 802.15.4 radio transceiver

The radio module Preon32 contains a 2.4 GHz radio transceiver to send and receive data in the frame format of IEEE 802.15.4. This frame format is the basis for the implementation of higher protocol layers such as ZigBee or 6LoWPAN. This exampels shows the initialization of the radio transceiver and how to transmit data wirelessly.
The IEEE 802.15.4 standard uses frames to exchange data. This frames contains the payload data and other information like which is needed for addressing. After all information is set frames can be transmitted. A transmission can result in an error. The cause can be a busy channel or the frame was not acknowleged.
The data of the frames are plain text messages which contains the text "Hello World" and a run counter.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */

package com.virtenio.webdemo;

import com.virtenio.drivers.ieee_802_15_4.AT86RF231;
import com.virtenio.drivers.ieee_802_15_4.NativeAT86RF231;
import com.virtenio.platform.Platform;
import com.virtenio.radio.ieee_802_15_4.Frame;
import com.virtenio.radio.ieee_802_15_4.FrameControl;
import com.virtenio.testsuite.common.proglettools.ProgletMisc;

import java.io.IOException;

public class SendRF {

    public static void main() throws IOException {

        int channel = 24;
        int panID = 0xCAFE;
        int addrSend = 0xAFFE;
        int addrResc = 0xBABE;

        NativeAT86RF231 radio = NativeAT86RF231.getInstance(0);
        initRadio(radio, channel, panID, addrSend);
        radio.open();

        for (int i = 0; i < 10; i++) {
            String mesg = "Hello World(" + i + ")";
            boolean isOK = false;
            while (!isOK) {
                try {
                    FrameControl frameControl = initFrameControl();
                    //
                    Frame frame = new Frame(frameControl);
                    frame.setSrcAddr(addrSend);
                    frame.setSrcPanId(panID);
                    frame.setDestAddr(addrResc);
                    frame.setDestPanId(panID);
                    frame.setPayload(mesg.getBytes());
                    //
                    radio.setState(AT86RF231.STATE_TX_ARET_ON);
                    radio.transmitFrame(frame);
                    //
                    ProgletMisc.LedBlinker(Platform.LED_GREEN, 100, false);
                    isOK = true;
                } catch (IOException e) {
                    ProgletMisc.LedBlinker(Platform.LED_RED, 100, false);
                }
            }
        }
        radio.close();
    }

    private static FrameControl initFrameControl() {
        return new FrameControl(
                FrameControl.TYPE_DATA |
                FrameControl.ACK_REQUEST |
                FrameControl.DST_ADDR_16 |
                FrameControl.INTRA_PAN |
                FrameControl.SRC_ADDR_16
        );
    }

    private static void initRadio(NativeAT86RF231 radio,
                                  int channel, int panID, int addr) throws IOException {
        try {
            radio.reset();
            radio.setState(AT86RF231.STATE_TRX_OFF);
            radio.setFrameProtection(AT86RF231.FRAME_PROTECTION_INCOMING |
                                     AT86RF231.FRAME_PROTECTION_OUTGOING);
            radio.setMaxFrameRetries(5);
            radio.setCsmaRetries(1);
            radio.setCsmaSeed(511);
            radio.setBackoffExponent(3, 5);
            radio.setChannel(channel);
            radio.setTransmitPower(0); // 0 = +3dB, 15 = -17dB
            radio.setCCAMode(3, 7);
            if (panID >= 0) {
                radio.setAddressFilter(panID, addr, 0, false);
            }
            radio.setPendingData(false);
            radio.setState(AT86RF231.STATE_RX_AACK_ON);
        } catch (Exception e) {
            System.out.println("transceiver error");
        }
    }

}