802.15.4 IO Sampling Callback example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

This example shows how to monitor remote XBee modules DigitalInputs and ADCs. The remote XBee module sends packets periodically to the local XBee module containing the value of its DigitalInputs and ADCs. This library process those packets and calls a user registered function callback so the user can handle them.

See Handling IO Data Samples from other modules chapter for more information.

Common Setup

Make sure you have a valid Example Common Setup

Example Setup

Application

You have to configure the remote device 64-bit address by customizing the REMOTE_NODE_ADDR64_MSB and REMOTE_NODE_ADDR64_LSB defines with the remote XBee module 64-bit address.

Hardware

On the Remote XBee module, wire following peripherals to the pins specified on the table and schematics, or change the application pins according to your hardware setup:

LinePinValue
DIO2/ADC218ADC (Analog Input)
DIO3/ADC317Digital Input

/media/uploads/hbujanda/dio_adc_schem.png

Running the example

Build and deploy the example to the mbed module.
Reset the mbed module so the example starts. You should see the example debug information through the debug interface configured in the 'Local Setup' chapter.
The application will do following operations:

  • Register the function callback that will be called by the library when corresponding IO sample frame is received.
  • Configure the remote XBee module pins according to the table above.
  • Configure the sample rate.
  • Configure remote XBee module to send IO Samples to local XBee module
  • Periodically ask the XBee library to process received frames

When a IO Sample frame is received the user callback does:

  • Read DIO3_AD3 digital value
  • Read DIO2_AD2 analog value
Committer:
hbujanda
Date:
Fri Jul 29 12:11:11 2016 +0200
Revision:
10:1584fb379965
Parent:
9:7aeb107dd28e
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:1b067903c9a7 1 /**
hbujanda 5:1b067903c9a7 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:1b067903c9a7 3 * All rights not expressly granted are reserved.
hbujanda 5:1b067903c9a7 4 *
hbujanda 5:1b067903c9a7 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:1b067903c9a7 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:1b067903c9a7 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:1b067903c9a7 8 *
hbujanda 5:1b067903c9a7 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:1b067903c9a7 10 * =======================================================================
hbujanda 5:1b067903c9a7 11 */
hbujanda 5:1b067903c9a7 12
hbujanda 5:1b067903c9a7 13 #include "mbed.h"
hbujanda 5:1b067903c9a7 14 #include "XBeeLib.h"
hbujanda 5:1b067903c9a7 15 #if defined(ENABLE_LOGGING)
hbujanda 5:1b067903c9a7 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:1b067903c9a7 17 using namespace DigiLog;
hbujanda 5:1b067903c9a7 18 #endif
hbujanda 5:1b067903c9a7 19
hbujanda 5:1b067903c9a7 20 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
spastor 7:e2dca4050bea 21
hbujanda 9:7aeb107dd28e 22 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
hbujanda 9:7aeb107dd28e 23 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x01234567)
hbujanda 5:1b067903c9a7 24
hbujanda 5:1b067903c9a7 25 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 5:1b067903c9a7 26
hbujanda 5:1b067903c9a7 27 using namespace XBeeLib;
hbujanda 5:1b067903c9a7 28
hbujanda 5:1b067903c9a7 29 Serial *log_serial;
hbujanda 5:1b067903c9a7 30
hbujanda 5:1b067903c9a7 31 static void io_data_cb(const RemoteXBee802& remote, const IOSample802& sample_data)
hbujanda 5:1b067903c9a7 32 {
hbujanda 5:1b067903c9a7 33 if (remote.is_valid_addr16b()) {
hbujanda 5:1b067903c9a7 34 log_serial->printf("\r\nGot a 16-bit IO Sample Data [%04x]\r\n", remote.get_addr16());
hbujanda 5:1b067903c9a7 35 } else {
hbujanda 5:1b067903c9a7 36 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 5:1b067903c9a7 37
hbujanda 5:1b067903c9a7 38 log_serial->printf("\r\nGot a 64-bit IO Sample Data [%08x:%08x]\r\n", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64));
hbujanda 5:1b067903c9a7 39 }
hbujanda 5:1b067903c9a7 40
hbujanda 5:1b067903c9a7 41 RadioStatus radioStatus;
hbujanda 5:1b067903c9a7 42
hbujanda 5:1b067903c9a7 43 DioVal dio3;
hbujanda 5:1b067903c9a7 44 radioStatus = sample_data.get_dio(XBee802::DIO3_AD3, &dio3);
hbujanda 5:1b067903c9a7 45 if (radioStatus != Success) {
hbujanda 5:1b067903c9a7 46 log_serial->printf("sample_data.get_dio(XBee802::DIO3_AD3, &dio3) FAILED\r\n");
hbujanda 5:1b067903c9a7 47 } else {
hbujanda 5:1b067903c9a7 48 log_serial->printf("DIO3 Digital value %d\r\n", dio3);
hbujanda 5:1b067903c9a7 49 }
hbujanda 5:1b067903c9a7 50
hbujanda 5:1b067903c9a7 51 uint16_t ad2;
hbujanda 5:1b067903c9a7 52 radioStatus = sample_data.get_adc(XBee802::DIO2_AD2, &ad2);
hbujanda 5:1b067903c9a7 53 if (radioStatus != Success) {
hbujanda 5:1b067903c9a7 54 log_serial->printf("sample_data.get_adc(XBee802::DIO2_AD2, &ad2) FAILED\r\n");
hbujanda 5:1b067903c9a7 55 } else {
hbujanda 5:1b067903c9a7 56 log_serial->printf("AD2 Analog value %04x\r\n", ad2);
hbujanda 5:1b067903c9a7 57 }
hbujanda 5:1b067903c9a7 58 }
hbujanda 5:1b067903c9a7 59
spastor 7:e2dca4050bea 60 int main()
hbujanda 5:1b067903c9a7 61 {
hbujanda 5:1b067903c9a7 62 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:1b067903c9a7 63 log_serial->baud(9600);
hbujanda 5:1b067903c9a7 64 log_serial->printf("Sample application to demo how to receive IO Sample Data from a RemoteXBee802 Node\r\n\r\n");
hbujanda 5:1b067903c9a7 65 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:1b067903c9a7 66
hbujanda 5:1b067903c9a7 67 #if defined(ENABLE_LOGGING)
hbujanda 5:1b067903c9a7 68 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 5:1b067903c9a7 69 #endif
hbujanda 5:1b067903c9a7 70
hbujanda 5:1b067903c9a7 71 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 5:1b067903c9a7 72
hbujanda 5:1b067903c9a7 73 /* Register callback */
hbujanda 5:1b067903c9a7 74 xbee.register_io_sample_cb(&io_data_cb);
hbujanda 5:1b067903c9a7 75
hbujanda 5:1b067903c9a7 76 RadioStatus radioStatus;
hbujanda 5:1b067903c9a7 77 radioStatus = xbee.init();
hbujanda 5:1b067903c9a7 78 MBED_ASSERT(radioStatus == Success);
hbujanda 5:1b067903c9a7 79
hbujanda 5:1b067903c9a7 80 const RemoteXBee802 remoteDevice = RemoteXBee802(REMOTE_NODE_ADDR64);
hbujanda 5:1b067903c9a7 81 log_serial->printf("Configuring remote Device (%08X:%08X) DIO3_AD3 as Input and DIO2_AD2 as ADC\r\n", REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB);
hbujanda 5:1b067903c9a7 82 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::DIO3_AD3, DigitalInput);
hbujanda 5:1b067903c9a7 83 MBED_ASSERT(radioStatus == Success);
hbujanda 5:1b067903c9a7 84 log_serial->printf("Enabling remote Device (%08X:%08X) internal Pull-up resistor on DIO3_AD3\r\n", REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB);
hbujanda 5:1b067903c9a7 85 radioStatus = xbee.set_pin_pull_up(remoteDevice, XBee802::DIO3_AD3, true);
hbujanda 5:1b067903c9a7 86 MBED_ASSERT(radioStatus == Success);
hbujanda 5:1b067903c9a7 87 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::DIO2_AD2, Adc);
hbujanda 5:1b067903c9a7 88 MBED_ASSERT(radioStatus == Success);
hbujanda 5:1b067903c9a7 89
hbujanda 5:1b067903c9a7 90 log_serial->printf("Configuring remote Device (%08X:%08X) to send samples every 5 seconds\r\n", REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB);
hbujanda 5:1b067903c9a7 91 xbee.set_io_sample_rate(remoteDevice, 5.0);
hbujanda 5:1b067903c9a7 92
hbujanda 5:1b067903c9a7 93 log_serial->printf("Configuring remote Device (%08X:%08X) to send samples to this device\r\n", REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB);
hbujanda 5:1b067903c9a7 94 const uint64_t localAddr = xbee.get_addr64();
hbujanda 5:1b067903c9a7 95 xbee.config_io_sample_destination(remoteDevice, localAddr);
hbujanda 5:1b067903c9a7 96
hbujanda 5:1b067903c9a7 97 while (true) {
hbujanda 5:1b067903c9a7 98 xbee.process_rx_frames();
hbujanda 5:1b067903c9a7 99 wait_ms(100);
hbujanda 5:1b067903c9a7 100 log_serial->printf(".");
hbujanda 5:1b067903c9a7 101 }
hbujanda 5:1b067903c9a7 102
hbujanda 5:1b067903c9a7 103 delete(log_serial);
hbujanda 5:1b067903c9a7 104 }