ZigBee DIOs and ADCs example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

This example shows how to handle remote XBee module's DigitalInputOutputs and ADCs. The example configures the remote XBee module pins to the desired functionality and then operates on the state of the pins themselves.

See Handling remote modules DIOs ADCs and PWMs 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 on 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
DIO411Digital Output

/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.
Once joined to the coordinator, the application will do following operations:

  • Configure the remote XBee module pins according to the table above.
  • Do following loop every 5 seconds:
    • Read DIO3_AD3 digital value
    • Toggle LED associated to DIO4
    • Read DIO2_AD2 analog value
Committer:
hbujanda
Date:
Fri Jul 29 12:12:39 2016 +0200
Revision:
10:7611c2070ffe
Parent:
7:bd1400c44126
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:dbc9d8c6f54b 1 /**
hbujanda 5:dbc9d8c6f54b 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:dbc9d8c6f54b 3 * All rights not expressly granted are reserved.
hbujanda 5:dbc9d8c6f54b 4 *
hbujanda 5:dbc9d8c6f54b 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:dbc9d8c6f54b 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:dbc9d8c6f54b 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:dbc9d8c6f54b 8 *
hbujanda 5:dbc9d8c6f54b 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:dbc9d8c6f54b 10 * =======================================================================
hbujanda 5:dbc9d8c6f54b 11 */
hbujanda 5:dbc9d8c6f54b 12
hbujanda 5:dbc9d8c6f54b 13 #include "mbed.h"
hbujanda 5:dbc9d8c6f54b 14 #include "XBeeLib.h"
hbujanda 5:dbc9d8c6f54b 15 #if defined(ENABLE_LOGGING)
hbujanda 5:dbc9d8c6f54b 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:dbc9d8c6f54b 17 using namespace DigiLog;
hbujanda 5:dbc9d8c6f54b 18 #endif
hbujanda 5:dbc9d8c6f54b 19
hbujanda 5:dbc9d8c6f54b 20 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
spastor 7:bd1400c44126 21
spastor 7:bd1400c44126 22 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
spastor 7:bd1400c44126 23 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x01234567)
hbujanda 5:dbc9d8c6f54b 24
hbujanda 5:dbc9d8c6f54b 25 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 5:dbc9d8c6f54b 26
hbujanda 5:dbc9d8c6f54b 27 using namespace XBeeLib;
hbujanda 5:dbc9d8c6f54b 28
hbujanda 5:dbc9d8c6f54b 29 Serial *log_serial;
hbujanda 5:dbc9d8c6f54b 30
hbujanda 5:dbc9d8c6f54b 31 int main()
hbujanda 5:dbc9d8c6f54b 32 {
hbujanda 5:dbc9d8c6f54b 33 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:dbc9d8c6f54b 34 log_serial->baud(9600);
hbujanda 5:dbc9d8c6f54b 35 log_serial->printf("Sample application to demo how to handle remote XBeeZB devices DIOs and ADCs\r\n\r\n");
hbujanda 5:dbc9d8c6f54b 36 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:dbc9d8c6f54b 37
hbujanda 5:dbc9d8c6f54b 38 #if defined(ENABLE_LOGGING)
hbujanda 5:dbc9d8c6f54b 39 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 5:dbc9d8c6f54b 40 #endif
hbujanda 5:dbc9d8c6f54b 41
hbujanda 5:dbc9d8c6f54b 42 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 5:dbc9d8c6f54b 43
hbujanda 5:dbc9d8c6f54b 44 RadioStatus radioStatus = xbee.init();
hbujanda 5:dbc9d8c6f54b 45 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 46
hbujanda 5:dbc9d8c6f54b 47 /* Wait until the device has joined the network */
hbujanda 5:dbc9d8c6f54b 48 log_serial->printf("Waiting for device to join the network: ");
hbujanda 5:dbc9d8c6f54b 49 while (!xbee.is_joined()) {
hbujanda 5:dbc9d8c6f54b 50 wait_ms(1000);
hbujanda 5:dbc9d8c6f54b 51 log_serial->printf(".");
hbujanda 5:dbc9d8c6f54b 52 }
hbujanda 5:dbc9d8c6f54b 53 log_serial->printf("OK\r\n");
hbujanda 5:dbc9d8c6f54b 54
hbujanda 5:dbc9d8c6f54b 55 const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
hbujanda 5:dbc9d8c6f54b 56
hbujanda 5:dbc9d8c6f54b 57 radioStatus = xbee.set_pin_config(remoteDevice, XBeeZB::DIO3_AD3, DigitalInput);
hbujanda 5:dbc9d8c6f54b 58 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 59
hbujanda 5:dbc9d8c6f54b 60 radioStatus = xbee.set_pin_pull_up(remoteDevice, XBeeZB::DIO3_AD3, true);
hbujanda 5:dbc9d8c6f54b 61 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 62
hbujanda 5:dbc9d8c6f54b 63 radioStatus = xbee.set_pin_config(remoteDevice, XBeeZB::DIO4, DigitalOutHigh);
hbujanda 5:dbc9d8c6f54b 64 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 65
hbujanda 5:dbc9d8c6f54b 66 radioStatus = xbee.set_pin_config(remoteDevice, XBeeZB::DIO2_AD2, Adc);
hbujanda 5:dbc9d8c6f54b 67 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 68
hbujanda 5:dbc9d8c6f54b 69 //#define TEST_SUPPLY_VOLTAGE
hbujanda 5:dbc9d8c6f54b 70
hbujanda 5:dbc9d8c6f54b 71 #ifdef TEST_SUPPLY_VOLTAGE
hbujanda 5:dbc9d8c6f54b 72 /* Set a high V+ value so radio sends Supply Voltage information */
hbujanda 5:dbc9d8c6f54b 73 uint32_t v_value = 0x0C00;
hbujanda 5:dbc9d8c6f54b 74 log_serial->printf("\r\nSetting remote device V+ parameter to 0x%04x:\r\n", v_value);
hbujanda 5:dbc9d8c6f54b 75 AtCmdFrame::AtCmdResp cmdresp = xbee.set_param(remoteDevice, "V+", v_value);
hbujanda 5:dbc9d8c6f54b 76
hbujanda 5:dbc9d8c6f54b 77 if (cmdresp == AtCmdFrame::AtCmdRespOk)
hbujanda 5:dbc9d8c6f54b 78 log_serial->printf("OK\r\n");
hbujanda 5:dbc9d8c6f54b 79 else
hbujanda 5:dbc9d8c6f54b 80 log_serial->printf("FAILED with %d\r\n", (int) cmdresp);
hbujanda 5:dbc9d8c6f54b 81 #endif
hbujanda 5:dbc9d8c6f54b 82
hbujanda 5:dbc9d8c6f54b 83 while(true) {
spastor 6:d1b362ab7d60 84 IOSampleZB ioSample = xbee.get_iosample(remoteDevice);
spastor 6:d1b362ab7d60 85
spastor 6:d1b362ab7d60 86 if (!ioSample.is_valid()) {
spastor 6:d1b362ab7d60 87 log_serial->printf("get_iosample failed, ADC and Digital Input reads will be invalid\r\n");
spastor 6:d1b362ab7d60 88 }
spastor 7:bd1400c44126 89 /* Read DIO3_AD3 digital value */
hbujanda 5:dbc9d8c6f54b 90 DioVal dio3_val;
spastor 6:d1b362ab7d60 91
spastor 6:d1b362ab7d60 92 radioStatus = ioSample.get_dio(XBeeZB::DIO3_AD3, &dio3_val);
hbujanda 5:dbc9d8c6f54b 93 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 94 log_serial->printf("DIO3 value = %d\r\n", dio3_val);
hbujanda 5:dbc9d8c6f54b 95
spastor 7:bd1400c44126 96 /* Toggle LED associated to DIO4 */
hbujanda 5:dbc9d8c6f54b 97 static bool led_on = false;
hbujanda 5:dbc9d8c6f54b 98 if (!led_on)
hbujanda 5:dbc9d8c6f54b 99 radioStatus = xbee.set_dio(remoteDevice, XBeeZB::DIO4, Low);
hbujanda 5:dbc9d8c6f54b 100 else
spastor 7:bd1400c44126 101 radioStatus = xbee.set_dio(remoteDevice, XBeeZB::DIO4, High);
hbujanda 5:dbc9d8c6f54b 102 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 103 led_on = !led_on;
hbujanda 5:dbc9d8c6f54b 104
hbujanda 5:dbc9d8c6f54b 105 /* Read DIO2_AD2 analog value */
hbujanda 5:dbc9d8c6f54b 106 uint16_t adc2_val;
spastor 6:d1b362ab7d60 107 radioStatus = ioSample.get_adc(XBeeZB::DIO2_AD2, &adc2_val);
hbujanda 5:dbc9d8c6f54b 108 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 109 log_serial->printf("ADC2 value = 0x%04x\r\n", adc2_val);
hbujanda 5:dbc9d8c6f54b 110
hbujanda 5:dbc9d8c6f54b 111 #ifdef TEST_SUPPLY_VOLTAGE
hbujanda 5:dbc9d8c6f54b 112 uint16_t voltaje_val;
spastor 6:d1b362ab7d60 113 radioStatus = ioSample.get_adc(XBeeZB::SUPPLY_VOLTAGE, &voltaje_val);
hbujanda 5:dbc9d8c6f54b 114 if (radioStatus == Success)
hbujanda 5:dbc9d8c6f54b 115 log_serial->printf("Remote module Supply Voltage = 0x%04x\r\n", voltaje_val);
hbujanda 5:dbc9d8c6f54b 116 #endif
hbujanda 5:dbc9d8c6f54b 117
hbujanda 5:dbc9d8c6f54b 118 log_serial->printf("\r\n");
spastor 7:bd1400c44126 119
hbujanda 5:dbc9d8c6f54b 120 wait(5);
hbujanda 5:dbc9d8c6f54b 121 }
hbujanda 5:dbc9d8c6f54b 122
hbujanda 5:dbc9d8c6f54b 123 delete(log_serial);
hbujanda 5:dbc9d8c6f54b 124 }