DigiMesh IO Sampling Callback example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_IO_Sample_Callback by Digi International Inc.

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:
spastor
Date:
Tue May 05 18:29:53 2015 +0200
Revision:
1:302f8488174e
Parent:
0:ca7f55ed75b9
Child:
2:6e93a0039da9
Automatic upload

Who changed what in which revision?

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