802.15.4 DIOs, ADCs and PWM example for mbed XBeeLib By Digi

Dependencies:   XBeeLib mbed

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:11:00 2016 +0200
Revision:
10:5f5bb759a57e
Parent:
9:01e90da5b15d
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 5:d336705f4a78 1 /**
hbujanda 5:d336705f4a78 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 5:d336705f4a78 3 * All rights not expressly granted are reserved.
hbujanda 5:d336705f4a78 4 *
hbujanda 5:d336705f4a78 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 5:d336705f4a78 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 5:d336705f4a78 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 5:d336705f4a78 8 *
hbujanda 5:d336705f4a78 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 5:d336705f4a78 10 * =======================================================================
hbujanda 5:d336705f4a78 11 */
hbujanda 5:d336705f4a78 12
hbujanda 5:d336705f4a78 13 #include "mbed.h"
hbujanda 5:d336705f4a78 14 #include "XBeeLib.h"
hbujanda 5:d336705f4a78 15 #if defined(ENABLE_LOGGING)
hbujanda 5:d336705f4a78 16 #include "DigiLoggerMbedSerial.h"
hbujanda 5:d336705f4a78 17 using namespace DigiLog;
hbujanda 5:d336705f4a78 18 #endif
hbujanda 5:d336705f4a78 19
hbujanda 5:d336705f4a78 20 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200)
spastor 7:5ea7bdc55069 21
hbujanda 9:01e90da5b15d 22 #error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
hbujanda 9:01e90da5b15d 23 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x01234567)
hbujanda 5:d336705f4a78 24
hbujanda 5:d336705f4a78 25 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
hbujanda 5:d336705f4a78 26
hbujanda 5:d336705f4a78 27 using namespace XBeeLib;
hbujanda 5:d336705f4a78 28
hbujanda 5:d336705f4a78 29 Serial *log_serial;
hbujanda 5:d336705f4a78 30
hbujanda 5:d336705f4a78 31 int main()
hbujanda 5:d336705f4a78 32 {
hbujanda 5:d336705f4a78 33 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 5:d336705f4a78 34 log_serial->baud(9600);
hbujanda 5:d336705f4a78 35 log_serial->printf("Sample application to demo how to handle remote XBee802 devices DIOs, ADCs and PWMs\r\n\r\n");
hbujanda 5:d336705f4a78 36 log_serial->printf(XB_LIB_BANNER);
hbujanda 5:d336705f4a78 37
hbujanda 5:d336705f4a78 38 #if defined(ENABLE_LOGGING)
hbujanda 5:d336705f4a78 39 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 5:d336705f4a78 40 #endif
hbujanda 5:d336705f4a78 41
hbujanda 5:d336705f4a78 42 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 5:d336705f4a78 43
hbujanda 5:d336705f4a78 44 RadioStatus radioStatus = xbee.init();
hbujanda 5:d336705f4a78 45 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 46
hbujanda 5:d336705f4a78 47 const RemoteXBee802 remoteDevice = RemoteXBee802(REMOTE_NODE_ADDR64);
hbujanda 5:d336705f4a78 48
hbujanda 5:d336705f4a78 49 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::DIO3_AD3, DigitalInput);
hbujanda 5:d336705f4a78 50 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 51
hbujanda 5:d336705f4a78 52 radioStatus = xbee.set_pin_pull_up(remoteDevice, XBee802::DIO3_AD3, true);
hbujanda 5:d336705f4a78 53 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 54
hbujanda 5:d336705f4a78 55 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::DIO4_AD4, DigitalOutHigh);
hbujanda 5:d336705f4a78 56 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 57
hbujanda 5:d336705f4a78 58 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::DIO2_AD2, Adc);
hbujanda 5:d336705f4a78 59 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 60
hbujanda 5:d336705f4a78 61 radioStatus = xbee.set_pin_config(remoteDevice, XBee802::PWM0, Pwm);
hbujanda 5:d336705f4a78 62 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 63
hbujanda 5:d336705f4a78 64 while(true) {
spastor 6:d0224db329e1 65 IOSample802 ioSample = xbee.get_iosample(remoteDevice);
spastor 6:d0224db329e1 66
spastor 6:d0224db329e1 67 if (!ioSample.is_valid()) {
spastor 6:d0224db329e1 68 log_serial->printf("get_iosample failed, ADC and Digital Input reads will be invalid\r\n");
spastor 6:d0224db329e1 69 }
spastor 6:d0224db329e1 70
hbujanda 5:d336705f4a78 71 /* Read DIO3_AD3 digital value */
hbujanda 5:d336705f4a78 72 DioVal dio3_val;
spastor 6:d0224db329e1 73 radioStatus = ioSample.get_dio(XBee802::DIO3_AD3, &dio3_val);
hbujanda 5:d336705f4a78 74 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 75 log_serial->printf("DIO3 value = %d\r\n", dio3_val);
hbujanda 5:d336705f4a78 76
spastor 7:5ea7bdc55069 77 /* Toggle LED associated to DIO4_AD4 */
hbujanda 5:d336705f4a78 78 static bool led_on = false;
hbujanda 5:d336705f4a78 79 log_serial->printf("Setting DIO4 to = %d\r\n", led_on);
hbujanda 5:d336705f4a78 80 if (!led_on)
hbujanda 5:d336705f4a78 81 radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO4_AD4, Low);
hbujanda 5:d336705f4a78 82 else
spastor 7:5ea7bdc55069 83 radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO4_AD4, High);
hbujanda 5:d336705f4a78 84 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 85 led_on = !led_on;
hbujanda 5:d336705f4a78 86
hbujanda 5:d336705f4a78 87 /* Read DIO2_AD2 analog value */
hbujanda 5:d336705f4a78 88 uint16_t adc2_val;
spastor 6:d0224db329e1 89 radioStatus = ioSample.get_adc(XBee802::DIO2_AD2, &adc2_val);
hbujanda 5:d336705f4a78 90 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 91 log_serial->printf("ADC2 value = 0x%04x\r\n", adc2_val);
hbujanda 5:d336705f4a78 92
spastor 7:5ea7bdc55069 93 /* Set the led associated to PWM0 to different intensity levels */
hbujanda 5:d336705f4a78 94 static float pwm_val_list[] = { 0.0, 50.0, 70.0, 100.0 };
hbujanda 5:d336705f4a78 95 static uint8_t pwm_val_idx = 0;
hbujanda 5:d336705f4a78 96 log_serial->printf("Setting PWM0 to = %f\r\n", pwm_val_list[pwm_val_idx]);
hbujanda 5:d336705f4a78 97 radioStatus = xbee.set_pwm(remoteDevice, XBee802::PWM0, pwm_val_list[pwm_val_idx]);
hbujanda 5:d336705f4a78 98 MBED_ASSERT(radioStatus == Success);
hbujanda 5:d336705f4a78 99 pwm_val_idx++;
spastor 6:d0224db329e1 100 if (pwm_val_idx == sizeof(pwm_val_list)/sizeof(pwm_val_list[0])) {
hbujanda 5:d336705f4a78 101 pwm_val_idx = 0;
spastor 6:d0224db329e1 102 }
hbujanda 5:d336705f4a78 103
hbujanda 5:d336705f4a78 104 log_serial->printf("\r\n");
spastor 7:5ea7bdc55069 105
hbujanda 5:d336705f4a78 106 wait(5);
hbujanda 5:d336705f4a78 107 }
hbujanda 5:d336705f4a78 108
hbujanda 5:d336705f4a78 109 delete(log_serial);
hbujanda 5:d336705f4a78 110 }