John Bailey
/
XBeeApiRemoteIOExample
Example of using the XBeeApi library to read inputs on a remote XBee
main.cpp@0:3261f9bef845, 2014-07-28 (annotated)
- Committer:
- johnb
- Date:
- Mon Jul 28 13:32:59 2014 +0000
- Revision:
- 0:3261f9bef845
Example of using the XBeeApi library to read inputs on a remote XBee
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:3261f9bef845 | 1 | /** |
johnb | 0:3261f9bef845 | 2 | @file |
johnb | 0:3261f9bef845 | 3 | @brief Example of using the XBeeApi library to read inputs on |
johnb | 0:3261f9bef845 | 4 | a remote XBee |
johnb | 0:3261f9bef845 | 5 | |
johnb | 0:3261f9bef845 | 6 | @author John Bailey |
johnb | 0:3261f9bef845 | 7 | |
johnb | 0:3261f9bef845 | 8 | @copyright Copyright 2014 John Bailey |
johnb | 0:3261f9bef845 | 9 | |
johnb | 0:3261f9bef845 | 10 | @section LICENSE |
johnb | 0:3261f9bef845 | 11 | |
johnb | 0:3261f9bef845 | 12 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:3261f9bef845 | 13 | you may not use this file except in compliance with the License. |
johnb | 0:3261f9bef845 | 14 | You may obtain a copy of the License at |
johnb | 0:3261f9bef845 | 15 | |
johnb | 0:3261f9bef845 | 16 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:3261f9bef845 | 17 | |
johnb | 0:3261f9bef845 | 18 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:3261f9bef845 | 19 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:3261f9bef845 | 20 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:3261f9bef845 | 21 | See the License for the specific language governing permissions and |
johnb | 0:3261f9bef845 | 22 | limitations under the License. |
johnb | 0:3261f9bef845 | 23 | |
johnb | 0:3261f9bef845 | 24 | */ |
johnb | 0:3261f9bef845 | 25 | |
johnb | 0:3261f9bef845 | 26 | #include "mbed.h" |
johnb | 0:3261f9bef845 | 27 | #include "xbeeapi.hpp" |
johnb | 0:3261f9bef845 | 28 | |
johnb | 0:3261f9bef845 | 29 | Serial pc(USBTX, USBRX); // tx, rx |
johnb | 0:3261f9bef845 | 30 | |
johnb | 0:3261f9bef845 | 31 | /* TODO: You may need to change these based on the device/connections that you're using */ |
johnb | 0:3261f9bef845 | 32 | #define XBEE_TX_PIN PTA2 |
johnb | 0:3261f9bef845 | 33 | #define XBEE_RX_PIN PTA1 |
johnb | 0:3261f9bef845 | 34 | |
johnb | 0:3261f9bef845 | 35 | /* Network address for our XBee */ |
johnb | 0:3261f9bef845 | 36 | const uint16_t myNetworkAddress = 0x1234; |
johnb | 0:3261f9bef845 | 37 | |
johnb | 0:3261f9bef845 | 38 | /* Network address for the remote XBee */ |
johnb | 0:3261f9bef845 | 39 | const uint16_t remoteNetworkAddress = 0x4321; |
johnb | 0:3261f9bef845 | 40 | |
johnb | 0:3261f9bef845 | 41 | /* ID for the Personal Area Network we're going to join */ |
johnb | 0:3261f9bef845 | 42 | const XBeeApiCmdAt::panId_t myPANId = 1000; |
johnb | 0:3261f9bef845 | 43 | |
johnb | 0:3261f9bef845 | 44 | /* Network channel to use */ |
johnb | 0:3261f9bef845 | 45 | const XBeeApiCmdAt::channel_t myChannelId = 14; |
johnb | 0:3261f9bef845 | 46 | |
johnb | 0:3261f9bef845 | 47 | XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC ); |
johnb | 0:3261f9bef845 | 48 | |
johnb | 0:3261f9bef845 | 49 | /* Once the remote XBee is set up this function will monitor for changes to |
johnb | 0:3261f9bef845 | 50 | the I/O readings and report them */ |
johnb | 0:3261f9bef845 | 51 | void monitor( XBeeDeviceRemoteAt& p_remote ) |
johnb | 0:3261f9bef845 | 52 | { |
johnb | 0:3261f9bef845 | 53 | time_t analogueTime[ XBEE_API_ADC_CHANNEL_COUNT ] = { 0, }; |
johnb | 0:3261f9bef845 | 54 | time_t digitalTime[ XBEE_API_DIO_CHANNEL_COUNT ] = { 0, }; |
johnb | 0:3261f9bef845 | 55 | |
johnb | 0:3261f9bef845 | 56 | while(1) |
johnb | 0:3261f9bef845 | 57 | { |
johnb | 0:3261f9bef845 | 58 | uint8_t i; |
johnb | 0:3261f9bef845 | 59 | |
johnb | 0:3261f9bef845 | 60 | /* Loop all the analogue channels, see if the timestamp |
johnb | 0:3261f9bef845 | 61 | associated with the value has changed and if so, |
johnb | 0:3261f9bef845 | 62 | report it */ |
johnb | 0:3261f9bef845 | 63 | for( i = 0; |
johnb | 0:3261f9bef845 | 64 | i < XBEE_API_ADC_CHANNEL_COUNT; |
johnb | 0:3261f9bef845 | 65 | i++ ) |
johnb | 0:3261f9bef845 | 66 | { |
johnb | 0:3261f9bef845 | 67 | uint16_t val; |
johnb | 0:3261f9bef845 | 68 | time_t updateTime = p_remote.getAnalogueValue( i, val ); |
johnb | 0:3261f9bef845 | 69 | |
johnb | 0:3261f9bef845 | 70 | if( updateTime != analogueTime[ i ] ) |
johnb | 0:3261f9bef845 | 71 | { |
johnb | 0:3261f9bef845 | 72 | analogueTime[i] = updateTime; |
johnb | 0:3261f9bef845 | 73 | pc.printf("Analogue Channel %d updated : %d\r\n",i,val); |
johnb | 0:3261f9bef845 | 74 | } |
johnb | 0:3261f9bef845 | 75 | } |
johnb | 0:3261f9bef845 | 76 | |
johnb | 0:3261f9bef845 | 77 | /* Loop all the digital channels, report changes */ |
johnb | 0:3261f9bef845 | 78 | for( i = 0; |
johnb | 0:3261f9bef845 | 79 | i < XBEE_API_DIO_CHANNEL_COUNT; |
johnb | 0:3261f9bef845 | 80 | i++ ) |
johnb | 0:3261f9bef845 | 81 | { |
johnb | 0:3261f9bef845 | 82 | bool val; |
johnb | 0:3261f9bef845 | 83 | time_t updateTime = p_remote.getDigitalState( i, val ); |
johnb | 0:3261f9bef845 | 84 | |
johnb | 0:3261f9bef845 | 85 | if( updateTime != digitalTime[ i ] ) |
johnb | 0:3261f9bef845 | 86 | { |
johnb | 0:3261f9bef845 | 87 | digitalTime[i] = updateTime; |
johnb | 0:3261f9bef845 | 88 | pc.printf("Digital Channel %d updated : %d\r\n",i,val); |
johnb | 0:3261f9bef845 | 89 | } |
johnb | 0:3261f9bef845 | 90 | } |
johnb | 0:3261f9bef845 | 91 | |
johnb | 0:3261f9bef845 | 92 | wait_ms(1000); |
johnb | 0:3261f9bef845 | 93 | } |
johnb | 0:3261f9bef845 | 94 | } |
johnb | 0:3261f9bef845 | 95 | |
johnb | 0:3261f9bef845 | 96 | int main() { |
johnb | 0:3261f9bef845 | 97 | /* This example will use the blocking API for simplicity */ |
johnb | 0:3261f9bef845 | 98 | XBeeApiCmdAtBlocking atIf( &xbeeDevice ); |
johnb | 0:3261f9bef845 | 99 | |
johnb | 0:3261f9bef845 | 100 | XBeeDevice::XBeeDeviceReturn_t status; |
johnb | 0:3261f9bef845 | 101 | |
johnb | 0:3261f9bef845 | 102 | /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions. |
johnb | 0:3261f9bef845 | 103 | This step may not be needed in the case that the XBee has already been configured |
johnb | 0:3261f9bef845 | 104 | to use Mode 2 and the setting has been stored in NV */ |
johnb | 0:3261f9bef845 | 105 | status = xbeeDevice.setUpApi(); |
johnb | 0:3261f9bef845 | 106 | |
johnb | 0:3261f9bef845 | 107 | if( status == XBeeDevice::XBEEDEVICE_OK ) |
johnb | 0:3261f9bef845 | 108 | { |
johnb | 0:3261f9bef845 | 109 | uint16_t readNetworkAddress; |
johnb | 0:3261f9bef845 | 110 | |
johnb | 0:3261f9bef845 | 111 | /* Set the 16-bit source address of this XBee */ |
johnb | 0:3261f9bef845 | 112 | atIf.setSourceAddress( myNetworkAddress ); |
johnb | 0:3261f9bef845 | 113 | |
johnb | 0:3261f9bef845 | 114 | if( atIf.getSourceAddress( &readNetworkAddress ) ) |
johnb | 0:3261f9bef845 | 115 | { |
johnb | 0:3261f9bef845 | 116 | /* Set up a peer-to-peer network using the specified PAN and channel */ |
johnb | 0:3261f9bef845 | 117 | if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) ) |
johnb | 0:3261f9bef845 | 118 | { |
johnb | 0:3261f9bef845 | 119 | uint16_t sampleRate; |
johnb | 0:3261f9bef845 | 120 | XBeeApiCmdAt::XBeeApiDioConfig_e dioConfig; |
johnb | 0:3261f9bef845 | 121 | uint32_t remoteDaHigh,remoteDaLow; |
johnb | 0:3261f9bef845 | 122 | /* Set up a remote XBee device using the specified 16-bit address (64-bit address not used here) */ |
johnb | 0:3261f9bef845 | 123 | XBeeDeviceRemoteAt remoteXBee( &xbeeDevice, remoteNetworkAddress ); |
johnb | 0:3261f9bef845 | 124 | |
johnb | 0:3261f9bef845 | 125 | /* Tell the destination XBee that it should transmit packets to this XBee's address */ |
johnb | 0:3261f9bef845 | 126 | remoteXBee.setDestinationAddress( myNetworkAddress ); |
johnb | 0:3261f9bef845 | 127 | |
johnb | 0:3261f9bef845 | 128 | /* Set I/O port 0 on remote XBee to be a digital input */ |
johnb | 0:3261f9bef845 | 129 | remoteXBee.setDioConfig( 0, XBeeApiCmdAt::XBEE_API_DIO_INPUT ); |
johnb | 0:3261f9bef845 | 130 | |
johnb | 0:3261f9bef845 | 131 | /* Set I/O port 1 on remote XBee to be an analogue input */ |
johnb | 0:3261f9bef845 | 132 | remoteXBee.setDioConfig( 1, XBeeApiCmdAt::XBEE_API_DIO_ADC ); |
johnb | 0:3261f9bef845 | 133 | |
johnb | 0:3261f9bef845 | 134 | /* Set sample rate on remote XBee to 100ms */ |
johnb | 0:3261f9bef845 | 135 | remoteXBee.setSampleRate( 100 ); |
johnb | 0:3261f9bef845 | 136 | |
johnb | 0:3261f9bef845 | 137 | /* Apply the above changes to the remote XBee. Calling remoteXBee.setApplyChanges(true) before |
johnb | 0:3261f9bef845 | 138 | the setDestinationAddress() would apply the changes as they were made and mean that |
johnb | 0:3261f9bef845 | 139 | this call would not be necessary */ |
johnb | 0:3261f9bef845 | 140 | remoteXBee.requestApplyChanges(); |
johnb | 0:3261f9bef845 | 141 | |
johnb | 0:3261f9bef845 | 142 | wait_ms( 500 ); |
johnb | 0:3261f9bef845 | 143 | |
johnb | 0:3261f9bef845 | 144 | if( !remoteXBee.getSampleRate( &sampleRate ) ) |
johnb | 0:3261f9bef845 | 145 | { |
johnb | 0:3261f9bef845 | 146 | pc.printf("Didn't get sample rate\r\n"); |
johnb | 0:3261f9bef845 | 147 | } |
johnb | 0:3261f9bef845 | 148 | if( !remoteXBee.getDioConfig( 0, &dioConfig ) ) |
johnb | 0:3261f9bef845 | 149 | { |
johnb | 0:3261f9bef845 | 150 | pc.printf("Didn't get dio config\r\n"); |
johnb | 0:3261f9bef845 | 151 | } |
johnb | 0:3261f9bef845 | 152 | if( !remoteXBee.getDestinationAddressHigh( &remoteDaHigh ) ) |
johnb | 0:3261f9bef845 | 153 | { |
johnb | 0:3261f9bef845 | 154 | pc.printf("Didn't get destination address (high)\r\n"); |
johnb | 0:3261f9bef845 | 155 | } |
johnb | 0:3261f9bef845 | 156 | if( !remoteXBee.getDestinationAddressLow( &remoteDaLow ) ) |
johnb | 0:3261f9bef845 | 157 | { |
johnb | 0:3261f9bef845 | 158 | pc.printf("Didn't get destination address (low)\r\n"); |
johnb | 0:3261f9bef845 | 159 | } |
johnb | 0:3261f9bef845 | 160 | |
johnb | 0:3261f9bef845 | 161 | monitor( remoteXBee ); |
johnb | 0:3261f9bef845 | 162 | } |
johnb | 0:3261f9bef845 | 163 | else |
johnb | 0:3261f9bef845 | 164 | { |
johnb | 0:3261f9bef845 | 165 | pc.printf("xbeeSetNetworkTypeP2P failed\r\n"); |
johnb | 0:3261f9bef845 | 166 | } |
johnb | 0:3261f9bef845 | 167 | } |
johnb | 0:3261f9bef845 | 168 | else |
johnb | 0:3261f9bef845 | 169 | { |
johnb | 0:3261f9bef845 | 170 | pc.printf("setSourceAddress() failed\r\n"); |
johnb | 0:3261f9bef845 | 171 | } |
johnb | 0:3261f9bef845 | 172 | } |
johnb | 0:3261f9bef845 | 173 | else |
johnb | 0:3261f9bef845 | 174 | { |
johnb | 0:3261f9bef845 | 175 | pc.printf("setUpApi failed with status %d\r\n",status); |
johnb | 0:3261f9bef845 | 176 | } |
johnb | 0:3261f9bef845 | 177 | } |