DigiMEsh DIOs, ADCs and PWMs example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

Fork of XBeeZB_dio_adc by Digi International Inc.

This example shows how to handle remote XBee modules DigitalInputOutputs, ADCs and PWMs. The example configures the remote XBee module pins to the desired functionality and then operates on those pins.

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 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
RSSI/DIO106PWM (Analog Output)
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.
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_AD4
    • Read DIO2_AD2 analog value
    • Set the led associated to PWM0 to different intensity levels
Committer:
hbujanda
Date:
Fri Jul 29 12:14:30 2016 +0200
Revision:
11:5de2aa212c22
Parent:
10:6d9a4f4a3740
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 10:6d9a4f4a3740 35 log_serial->printf("Sample application to demo how to handle remote XBeeDM 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 10:6d9a4f4a3740 42 XBeeDM xbee = XBeeDM(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 10:6d9a4f4a3740 47 const RemoteXBeeDM remoteDevice = RemoteXBeeDM(REMOTE_NODE_ADDR64);
hbujanda 5:dbc9d8c6f54b 48
hbujanda 10:6d9a4f4a3740 49 radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO3_AD3, DigitalInput);
hbujanda 5:dbc9d8c6f54b 50 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 51
hbujanda 10:6d9a4f4a3740 52 radioStatus = xbee.set_pin_pull_up(remoteDevice, XBeeDM::DIO3_AD3, true);
hbujanda 5:dbc9d8c6f54b 53 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 54
hbujanda 10:6d9a4f4a3740 55 radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO4, DigitalOutHigh);
hbujanda 10:6d9a4f4a3740 56 MBED_ASSERT(radioStatus == Success);
hbujanda 10:6d9a4f4a3740 57
hbujanda 10:6d9a4f4a3740 58 radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO2_AD2, Adc);
hbujanda 5:dbc9d8c6f54b 59 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 60
hbujanda 10:6d9a4f4a3740 61 radioStatus = xbee.set_pin_config(remoteDevice, XBeeDM::DIO10_PWM0, Pwm);
hbujanda 10:6d9a4f4a3740 62 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 63
hbujanda 5:dbc9d8c6f54b 64 while(true) {
hbujanda 10:6d9a4f4a3740 65 IOSampleDM ioSample = xbee.get_iosample(remoteDevice);
spastor 6:d1b362ab7d60 66
spastor 6:d1b362ab7d60 67 if (!ioSample.is_valid()) {
spastor 6:d1b362ab7d60 68 log_serial->printf("get_iosample failed, ADC and Digital Input reads will be invalid\r\n");
spastor 6:d1b362ab7d60 69 }
spastor 7:bd1400c44126 70 /* Read DIO3_AD3 digital value */
hbujanda 5:dbc9d8c6f54b 71 DioVal dio3_val;
spastor 6:d1b362ab7d60 72
hbujanda 10:6d9a4f4a3740 73 radioStatus = ioSample.get_dio(XBeeDM::DIO3_AD3, &dio3_val);
hbujanda 5:dbc9d8c6f54b 74 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 75 log_serial->printf("DIO3 value = %d\r\n", dio3_val);
hbujanda 5:dbc9d8c6f54b 76
spastor 7:bd1400c44126 77 /* Toggle LED associated to DIO4 */
hbujanda 5:dbc9d8c6f54b 78 static bool led_on = false;
hbujanda 5:dbc9d8c6f54b 79 if (!led_on)
hbujanda 10:6d9a4f4a3740 80 radioStatus = xbee.set_dio(remoteDevice, XBeeDM::DIO4, Low);
hbujanda 5:dbc9d8c6f54b 81 else
hbujanda 10:6d9a4f4a3740 82 radioStatus = xbee.set_dio(remoteDevice, XBeeDM::DIO4, High);
hbujanda 5:dbc9d8c6f54b 83 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 84 led_on = !led_on;
hbujanda 5:dbc9d8c6f54b 85
hbujanda 5:dbc9d8c6f54b 86 /* Read DIO2_AD2 analog value */
hbujanda 5:dbc9d8c6f54b 87 uint16_t adc2_val;
hbujanda 10:6d9a4f4a3740 88 radioStatus = ioSample.get_adc(XBeeDM::DIO2_AD2, &adc2_val);
hbujanda 5:dbc9d8c6f54b 89 MBED_ASSERT(radioStatus == Success);
hbujanda 5:dbc9d8c6f54b 90 log_serial->printf("ADC2 value = 0x%04x\r\n", adc2_val);
hbujanda 5:dbc9d8c6f54b 91
hbujanda 10:6d9a4f4a3740 92 /* Set the led associated to PWM0 to different intensity levels */
hbujanda 10:6d9a4f4a3740 93 static float pwm_val_list[] = { 0.0, 50.0, 70.0, 100.0 };
hbujanda 10:6d9a4f4a3740 94 static uint8_t pwm_val_idx = 0;
hbujanda 10:6d9a4f4a3740 95 log_serial->printf("Setting PWM0 to = %f\r\n", pwm_val_list[pwm_val_idx]);
hbujanda 10:6d9a4f4a3740 96 radioStatus = xbee.set_pwm(remoteDevice, XBeeDM::DIO10_PWM0, pwm_val_list[pwm_val_idx]);
hbujanda 10:6d9a4f4a3740 97 MBED_ASSERT(radioStatus == Success);
hbujanda 10:6d9a4f4a3740 98 pwm_val_idx++;
hbujanda 10:6d9a4f4a3740 99 if (pwm_val_idx == sizeof(pwm_val_list)/sizeof(pwm_val_list[0])) {
hbujanda 10:6d9a4f4a3740 100 pwm_val_idx = 0;
hbujanda 10:6d9a4f4a3740 101 }
hbujanda 5:dbc9d8c6f54b 102
hbujanda 5:dbc9d8c6f54b 103 log_serial->printf("\r\n");
spastor 7:bd1400c44126 104
hbujanda 5:dbc9d8c6f54b 105 wait(5);
hbujanda 5:dbc9d8c6f54b 106 }
hbujanda 5:dbc9d8c6f54b 107
hbujanda 5:dbc9d8c6f54b 108 delete(log_serial);
hbujanda 5:dbc9d8c6f54b 109 }