DigiMesh Receive Data example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_Receive_Data by Digi International Inc.

Committer:
hbujanda
Date:
Wed Apr 29 17:59:11 2015 +0200
Revision:
0:0d348bfc487e
Child:
2:994e979d9df8
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:0d348bfc487e 1 /**
hbujanda 0:0d348bfc487e 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:0d348bfc487e 3 * All rights not expressly granted are reserved.
hbujanda 0:0d348bfc487e 4 *
hbujanda 0:0d348bfc487e 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:0d348bfc487e 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:0d348bfc487e 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:0d348bfc487e 8 *
hbujanda 0:0d348bfc487e 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:0d348bfc487e 10 * =======================================================================
hbujanda 0:0d348bfc487e 11 */
hbujanda 0:0d348bfc487e 12
hbujanda 0:0d348bfc487e 13 #include "mbed.h"
hbujanda 0:0d348bfc487e 14 #include "XBeeLib.h"
hbujanda 0:0d348bfc487e 15 #if defined(ENABLE_LOGGING)
hbujanda 0:0d348bfc487e 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:0d348bfc487e 17 using namespace DigiLog;
hbujanda 0:0d348bfc487e 18 #endif
hbujanda 0:0d348bfc487e 19
hbujanda 0:0d348bfc487e 20 using namespace XBeeLib;
hbujanda 0:0d348bfc487e 21
hbujanda 0:0d348bfc487e 22 Serial *log_serial;
hbujanda 0:0d348bfc487e 23
hbujanda 0:0d348bfc487e 24 /** Callback function, invoked at packet reception */
hbujanda 0:0d348bfc487e 25 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
hbujanda 0:0d348bfc487e 26 {
hbujanda 0:0d348bfc487e 27 Addr64 remote64;
hbujanda 0:0d348bfc487e 28 uint16_t remote16;
hbujanda 0:0d348bfc487e 29
hbujanda 0:0d348bfc487e 30 remote.get_addr(&remote64);
hbujanda 0:0d348bfc487e 31 remote.get_addr(&remote16);
hbujanda 0:0d348bfc487e 32
hbujanda 0:0d348bfc487e 33 log_serial->printf("\r\nGot a %s RX packet [%08x:%08x:|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST",
hbujanda 0:0d348bfc487e 34 remote64.get_high32(), remote64.get_low32(), remote16, len);
hbujanda 0:0d348bfc487e 35
hbujanda 0:0d348bfc487e 36 for (int i = 0; i < len; i++)
hbujanda 0:0d348bfc487e 37 log_serial->printf("%02x ", data[i]);
hbujanda 0:0d348bfc487e 38
hbujanda 0:0d348bfc487e 39 log_serial->printf("\r\n");
hbujanda 0:0d348bfc487e 40 }
hbujanda 0:0d348bfc487e 41
hbujanda 0:0d348bfc487e 42 int main()
hbujanda 0:0d348bfc487e 43 {
hbujanda 0:0d348bfc487e 44 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:0d348bfc487e 45 log_serial->baud(9600);
hbujanda 0:0d348bfc487e 46 log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
hbujanda 0:0d348bfc487e 47 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:0d348bfc487e 48
hbujanda 0:0d348bfc487e 49 #if defined(ENABLE_LOGGING)
hbujanda 0:0d348bfc487e 50 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 0:0d348bfc487e 51 #endif
hbujanda 0:0d348bfc487e 52
hbujanda 0:0d348bfc487e 53 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 0:0d348bfc487e 54
hbujanda 0:0d348bfc487e 55 /* Register callbacks */
hbujanda 0:0d348bfc487e 56 xbee.register_receive_cb(&receive_cb);
hbujanda 0:0d348bfc487e 57
hbujanda 0:0d348bfc487e 58 RadioStatus const radioStatus = xbee.init();
hbujanda 0:0d348bfc487e 59 MBED_ASSERT(radioStatus == Success);
hbujanda 0:0d348bfc487e 60
hbujanda 0:0d348bfc487e 61 /* Wait until the device has joined the network */
hbujanda 0:0d348bfc487e 62 log_serial->printf("Waiting for device to join the network: ");
hbujanda 0:0d348bfc487e 63 while (!xbee.is_joined()) {
hbujanda 0:0d348bfc487e 64 wait_ms(1000);
hbujanda 0:0d348bfc487e 65 log_serial->printf(".");
hbujanda 0:0d348bfc487e 66 }
hbujanda 0:0d348bfc487e 67 log_serial->printf("OK\r\n");
hbujanda 0:0d348bfc487e 68
hbujanda 0:0d348bfc487e 69 while (true) {
hbujanda 0:0d348bfc487e 70 xbee.process_rx_frames();
hbujanda 0:0d348bfc487e 71 wait_ms(100);
hbujanda 0:0d348bfc487e 72 log_serial->printf(".");
hbujanda 0:0d348bfc487e 73 }
hbujanda 0:0d348bfc487e 74
hbujanda 0:0d348bfc487e 75 delete(log_serial);
hbujanda 0:0d348bfc487e 76 }