123

Dependencies:   mbed XBeeLib_Fixed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 using namespace XBeeLib;
00021 
00022 Serial *log_serial;
00023 
00024 /** Callback function, invoked at packet reception */
00025 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
00026 {
00027     const uint64_t remote_addr64 = remote.get_addr64();
00028 
00029     log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
00030     char str[10];
00031     int data1 = 0;
00032     
00033     for (int i = 0; i < len; i++)
00034         {
00035         str[i]=data[i];
00036         
00037         ///data1=data1|(data2 << (8*(len-i-1)));
00038         
00039         }
00040         
00041         
00042         std::sscanf(str, "d", &data1);
00043         log_serial->printf("%d ", data1);
00044 
00045     log_serial->printf("\r\n");
00046 }
00047 
00048 int main()
00049 {
00050     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00051     log_serial->baud(9600);
00052     log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
00053     log_serial->printf(XB_LIB_BANNER);
00054 
00055 #if defined(ENABLE_LOGGING)
00056     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00057 #endif
00058 
00059     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00060 
00061     /* Register callbacks */
00062     xbee.register_receive_cb(&receive_cb);
00063 
00064     RadioStatus const radioStatus = xbee.init();
00065     MBED_ASSERT(radioStatus == Success);
00066 
00067     /* Wait until the device has joined the network */
00068     log_serial->printf("Waiting for device to join the network: ");
00069     while (!xbee.is_joined()) {
00070         wait_ms(1000);
00071         log_serial->printf(".");
00072     }
00073     log_serial->printf("OK\r\n");
00074 
00075     while (true) {
00076         xbee.process_rx_frames();
00077         wait_ms(100);
00078         log_serial->printf(".");
00079     }
00080 
00081     delete(log_serial);
00082 }