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:
hbujanda
Date:
Thu Jul 28 10:31:19 2016 +0000
Revision:
10:5e8f8db1f994
Parent:
7:f4af8c290fd4
Child:
11:d0952218884b
Initial commit

Who changed what in which revision?

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