German English
Radio

Receive data from IEEE 802.15.4 radio transceiver

The radio module Preon32 contains a 2.4 GHz IEEE 802.15.4 transceiver to send and receive data wirelessly. The IEEE 802.15.4 frame format is the basis for other protocol layers such as ZigBee or 6LoWPAN. This examples shows how to initialize the radio transceiver to receive data and how to receive frames.
It is assumed that the received data is plain text which can be printed to the standard output stream.
/*
 * 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.testsuite.common.proglettools.ProgletMisc;

import java.io.IOException;

public class ReceiveRF {

    public static void main() throws IOException {

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

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

        while (true) {
            Frame f = null;
            try {
                f = new Frame();
                radio.setState(AT86RF231.STATE_RX_AACK_ON);
                radio.waitForFrame(f);
                ProgletMisc.LedBlinker(Platform.LED_GREEN, 100, false);
            } catch (IOException e) {
                ProgletMisc.LedBlinker(Platform.LED_RED, 100, false);
            }

            byte[] dg = f.getPayload();
            String str = new String(dg, 0, dg.length);
            String hex_addr = Integer.toHexString((int) f.getSrcAddr());
            System.out.println("FROM(" + hex_addr + "): " + str);
        }
    }

    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");
        }
    }

}