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:
Wed Apr 29 17:57:44 2015 +0200
Revision:
0:bfbc01d0df22
Child:
1:7a75ab5e7485
Automatic upload

Who changed what in which revision?

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