Solutions for the XBee experiments for LPC812 MAX

Dependencies:   mbed

Committer:
embeddedartists
Date:
Wed Nov 20 08:26:28 2013 +0000
Revision:
0:54828b08e71d
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:54828b08e71d 1 /******************************************************************************
embeddedartists 0:54828b08e71d 2 * Includes
embeddedartists 0:54828b08e71d 3 *****************************************************************************/
embeddedartists 0:54828b08e71d 4 #include "mbed.h"
embeddedartists 0:54828b08e71d 5 #include "XBee.h"
embeddedartists 0:54828b08e71d 6
embeddedartists 0:54828b08e71d 7 /******************************************************************************
embeddedartists 0:54828b08e71d 8 * Typedefs and defines
embeddedartists 0:54828b08e71d 9 *****************************************************************************/
embeddedartists 0:54828b08e71d 10
embeddedartists 0:54828b08e71d 11 // define NODE_IS_COORDINATOR if this board should act
embeddedartists 0:54828b08e71d 12 // as XBee coordinator. Make sure it is undefined it the board
embeddedartists 0:54828b08e71d 13 // should act as End-Device
embeddedartists 0:54828b08e71d 14 #define NODE_IS_COORDINATOR (1)
embeddedartists 0:54828b08e71d 15
embeddedartists 0:54828b08e71d 16 #define CMD_BTN_MSG (0)
embeddedartists 0:54828b08e71d 17 #define CMD_ACK_MSG (1)
embeddedartists 0:54828b08e71d 18
embeddedartists 0:54828b08e71d 19 /******************************************************************************
embeddedartists 0:54828b08e71d 20 * Local variables
embeddedartists 0:54828b08e71d 21 *****************************************************************************/
embeddedartists 0:54828b08e71d 22
embeddedartists 0:54828b08e71d 23 static XBee xbee(P0_4, P0_0, P0_17, P0_7); //tx,rx,reset,sleep
embeddedartists 0:54828b08e71d 24
embeddedartists 0:54828b08e71d 25 static Serial pc(USBTX, USBRX); // tx, rx
embeddedartists 0:54828b08e71d 26
embeddedartists 0:54828b08e71d 27 // all LEDs active low
embeddedartists 0:54828b08e71d 28 static DigitalOut xbeeInitLed(D13);
embeddedartists 0:54828b08e71d 29 static DigitalOut remoteBtnLed(D12);
embeddedartists 0:54828b08e71d 30 static DigitalOut ackLed(D11);
embeddedartists 0:54828b08e71d 31 static DigitalOut led4(D10);
embeddedartists 0:54828b08e71d 32
embeddedartists 0:54828b08e71d 33 static DigitalIn button(D3);
embeddedartists 0:54828b08e71d 34 static bool xbeeIsUp = false;
embeddedartists 0:54828b08e71d 35
embeddedartists 0:54828b08e71d 36 /******************************************************************************
embeddedartists 0:54828b08e71d 37 * Local functions
embeddedartists 0:54828b08e71d 38 *****************************************************************************/
embeddedartists 0:54828b08e71d 39
embeddedartists 0:54828b08e71d 40 static void xbeeDeviceUp(void) {
embeddedartists 0:54828b08e71d 41 xbeeIsUp = true;
embeddedartists 0:54828b08e71d 42 }
embeddedartists 0:54828b08e71d 43
embeddedartists 0:54828b08e71d 44 static void xbeeDeviceDown(void) {
embeddedartists 0:54828b08e71d 45 xbeeIsUp = false;
embeddedartists 0:54828b08e71d 46 }
embeddedartists 0:54828b08e71d 47
embeddedartists 0:54828b08e71d 48 static void xbeeNodeFound(void) {
embeddedartists 0:54828b08e71d 49 uint32_t addrHi = 0;
embeddedartists 0:54828b08e71d 50 uint32_t addrLo = 0;
embeddedartists 0:54828b08e71d 51 uint8_t rssi = 0;
embeddedartists 0:54828b08e71d 52
embeddedartists 0:54828b08e71d 53 xbee.getRemoteAddress(&addrHi, &addrLo);
embeddedartists 0:54828b08e71d 54 xbee.getRssi(&rssi);
embeddedartists 0:54828b08e71d 55
embeddedartists 0:54828b08e71d 56 pc.printf("XBee: node found: hi=%lx, lo=%lx, rssi=%d\n",
embeddedartists 0:54828b08e71d 57 addrHi, addrLo, rssi);
embeddedartists 0:54828b08e71d 58 }
embeddedartists 0:54828b08e71d 59
embeddedartists 0:54828b08e71d 60 static void xbeeTxStat(void) {
embeddedartists 0:54828b08e71d 61
embeddedartists 0:54828b08e71d 62 uint8_t frameId = 0;
embeddedartists 0:54828b08e71d 63 XBee::XBeeTxStatus status = XBee::TxStatusOk;
embeddedartists 0:54828b08e71d 64
embeddedartists 0:54828b08e71d 65 xbee.getTxStatus(&frameId, &status);
embeddedartists 0:54828b08e71d 66
embeddedartists 0:54828b08e71d 67 pc.printf("XBee: Tx Stat. Id=%u, status=%d\n", frameId, (int)status);
embeddedartists 0:54828b08e71d 68 }
embeddedartists 0:54828b08e71d 69
embeddedartists 0:54828b08e71d 70
embeddedartists 0:54828b08e71d 71
embeddedartists 0:54828b08e71d 72 static void xbeeDataAvailable(void) {
embeddedartists 0:54828b08e71d 73 char* data = NULL;
embeddedartists 0:54828b08e71d 74 char ackMsg = CMD_ACK_MSG;
embeddedartists 0:54828b08e71d 75 uint8_t frameId = 0;
embeddedartists 0:54828b08e71d 76 uint8_t len = 0;
embeddedartists 0:54828b08e71d 77 uint32_t addrHi = 0;
embeddedartists 0:54828b08e71d 78 uint32_t addrLo = 0;
embeddedartists 0:54828b08e71d 79 uint8_t rssi = 0;
embeddedartists 0:54828b08e71d 80
embeddedartists 0:54828b08e71d 81 xbee.getRemoteAddress(&addrHi, &addrLo);
embeddedartists 0:54828b08e71d 82 xbee.getData(&data, &len);
embeddedartists 0:54828b08e71d 83 xbee.getRssi(&rssi);
embeddedartists 0:54828b08e71d 84
embeddedartists 0:54828b08e71d 85 pc.printf("XBee: Data available: len=%d hi=%lx, lo=%lx, rssi=%d\n",
embeddedartists 0:54828b08e71d 86 (int)len, addrHi, addrLo, rssi);
embeddedartists 0:54828b08e71d 87
embeddedartists 0:54828b08e71d 88 if (len > 0) {
embeddedartists 0:54828b08e71d 89
embeddedartists 0:54828b08e71d 90 switch(data[0]) {
embeddedartists 0:54828b08e71d 91 case CMD_BTN_MSG:
embeddedartists 0:54828b08e71d 92 if (len > 1) {
embeddedartists 0:54828b08e71d 93 // change led to indicate button state
embeddedartists 0:54828b08e71d 94 remoteBtnLed = ((data[1] == 1) ? 1 : 0);
embeddedartists 0:54828b08e71d 95
embeddedartists 0:54828b08e71d 96
embeddedartists 0:54828b08e71d 97 // Send an ACK directly to the remote node (only for
embeddedartists 0:54828b08e71d 98 // buttons state == 0)
embeddedartists 0:54828b08e71d 99 if (data[1] == 0) {
embeddedartists 0:54828b08e71d 100 xbee.send(addrHi, addrLo, &ackMsg, 1, &frameId);
embeddedartists 0:54828b08e71d 101 }
embeddedartists 0:54828b08e71d 102 }
embeddedartists 0:54828b08e71d 103 break;
embeddedartists 0:54828b08e71d 104 case CMD_ACK_MSG:
embeddedartists 0:54828b08e71d 105 for (int i = 0; i < 3; i++) {
embeddedartists 0:54828b08e71d 106 ackLed = 0;
embeddedartists 0:54828b08e71d 107 wait_ms(100);
embeddedartists 0:54828b08e71d 108 ackLed = 1;
embeddedartists 0:54828b08e71d 109 wait_ms(100);
embeddedartists 0:54828b08e71d 110 }
embeddedartists 0:54828b08e71d 111 break;
embeddedartists 0:54828b08e71d 112 }
embeddedartists 0:54828b08e71d 113
embeddedartists 0:54828b08e71d 114 }
embeddedartists 0:54828b08e71d 115
embeddedartists 0:54828b08e71d 116 }
embeddedartists 0:54828b08e71d 117
embeddedartists 0:54828b08e71d 118 static bool xbeeInit()
embeddedartists 0:54828b08e71d 119 {
embeddedartists 0:54828b08e71d 120 xbee.registerCallback(xbeeDeviceUp, XBee::CbDeviceUp);
embeddedartists 0:54828b08e71d 121 xbee.registerCallback(xbeeDeviceDown, XBee::CbDeviceDown);
embeddedartists 0:54828b08e71d 122 xbee.registerCallback(xbeeNodeFound, XBee::CbNodeFound);
embeddedartists 0:54828b08e71d 123 xbee.registerCallback(xbeeTxStat, XBee::CbTxStat);
embeddedartists 0:54828b08e71d 124 xbee.registerCallback(xbeeDataAvailable, XBee::CbDataAvailable);
embeddedartists 0:54828b08e71d 125
embeddedartists 0:54828b08e71d 126 #ifdef NODE_IS_COORDINATOR
embeddedartists 0:54828b08e71d 127 XBee::XBeeError err = xbee.init(XBee::Coordinator, "EAEA");
embeddedartists 0:54828b08e71d 128 #else
embeddedartists 0:54828b08e71d 129 XBee::XBeeError err = xbee.init(XBee::EndDevice, "EAEA");
embeddedartists 0:54828b08e71d 130 #endif
embeddedartists 0:54828b08e71d 131 if (err != XBee::Ok) {
embeddedartists 0:54828b08e71d 132 return false;
embeddedartists 0:54828b08e71d 133 }
embeddedartists 0:54828b08e71d 134
embeddedartists 0:54828b08e71d 135 return true;
embeddedartists 0:54828b08e71d 136
embeddedartists 0:54828b08e71d 137 }
embeddedartists 0:54828b08e71d 138
embeddedartists 0:54828b08e71d 139 static void reportInitFailed() {
embeddedartists 0:54828b08e71d 140 while (true) {
embeddedartists 0:54828b08e71d 141 xbeeInitLed = !xbeeInitLed;
embeddedartists 0:54828b08e71d 142 wait_ms(200);
embeddedartists 0:54828b08e71d 143 }
embeddedartists 0:54828b08e71d 144 }
embeddedartists 0:54828b08e71d 145
embeddedartists 0:54828b08e71d 146 static void ledInit() {
embeddedartists 0:54828b08e71d 147 xbeeInitLed = 0; // turn on
embeddedartists 0:54828b08e71d 148 remoteBtnLed = 1; // turn off
embeddedartists 0:54828b08e71d 149 ackLed = 1; // turn off
embeddedartists 0:54828b08e71d 150 led4 = 1; // turn off
embeddedartists 0:54828b08e71d 151 }
embeddedartists 0:54828b08e71d 152
embeddedartists 0:54828b08e71d 153
embeddedartists 0:54828b08e71d 154 /******************************************************************************
embeddedartists 0:54828b08e71d 155 * Main function
embeddedartists 0:54828b08e71d 156 *****************************************************************************/
embeddedartists 0:54828b08e71d 157
embeddedartists 0:54828b08e71d 158 int main() {
embeddedartists 0:54828b08e71d 159
embeddedartists 0:54828b08e71d 160 /*
embeddedartists 0:54828b08e71d 161 * For this example to work at least two boards with XBee nodes must
embeddedartists 0:54828b08e71d 162 * be available; one which is configured as Coordinator and another
embeddedartists 0:54828b08e71d 163 * which is configured as End-Device.
embeddedartists 0:54828b08e71d 164 *
embeddedartists 0:54828b08e71d 165 * LED1 shows the state of XBee initialization. This LED is turned
embeddedartists 0:54828b08e71d 166 * on when initialization starts and turned off when initialization
embeddedartists 0:54828b08e71d 167 * is ready and the device us up. If initialization fails LED1
embeddedartists 0:54828b08e71d 168 * will blink.
embeddedartists 0:54828b08e71d 169 *
embeddedartists 0:54828b08e71d 170 * When the Push-button on the LPC4088 QuickStart Board is pushed
embeddedartists 0:54828b08e71d 171 * a message will be broadcasted. The board(s) that receives the message
embeddedartists 0:54828b08e71d 172 * will show the state of the remote board's button by turning LED2
embeddedartists 0:54828b08e71d 173 * on or off.
embeddedartists 0:54828b08e71d 174 *
embeddedartists 0:54828b08e71d 175 * The board that receives the button state message will send back
embeddedartists 0:54828b08e71d 176 * an ACK message (only for button down). The board that receives
embeddedartists 0:54828b08e71d 177 * the ACK message will blink with LED3. This can be used to verify
embeddedartists 0:54828b08e71d 178 * that a board that is not close to you actually received a message
embeddedartists 0:54828b08e71d 179 * without having to look at that board's LED2.
embeddedartists 0:54828b08e71d 180 */
embeddedartists 0:54828b08e71d 181
embeddedartists 0:54828b08e71d 182 int buttonState = 1;
embeddedartists 0:54828b08e71d 183 char data[2];
embeddedartists 0:54828b08e71d 184 uint8_t frameId = 0;
embeddedartists 0:54828b08e71d 185
embeddedartists 0:54828b08e71d 186 // pull-up must be enabled for button
embeddedartists 0:54828b08e71d 187 button.mode(PullUp);
embeddedartists 0:54828b08e71d 188
embeddedartists 0:54828b08e71d 189 ledInit();
embeddedartists 0:54828b08e71d 190
embeddedartists 0:54828b08e71d 191 pc.printf("Initializing\n");
embeddedartists 0:54828b08e71d 192 if (!xbeeInit()) {
embeddedartists 0:54828b08e71d 193 reportInitFailed();
embeddedartists 0:54828b08e71d 194 }
embeddedartists 0:54828b08e71d 195
embeddedartists 0:54828b08e71d 196 // Wait until XBee node is reported to be up.
embeddedartists 0:54828b08e71d 197 // - For End-device this means that the node is associated with a
embeddedartists 0:54828b08e71d 198 // coordinator
embeddedartists 0:54828b08e71d 199 // - For a coordinator this means that the node is initialized and ready
embeddedartists 0:54828b08e71d 200 pc.printf("Wait until up\n");
embeddedartists 0:54828b08e71d 201 while(!xbeeIsUp) {
embeddedartists 0:54828b08e71d 202 xbee.process();
embeddedartists 0:54828b08e71d 203 }
embeddedartists 0:54828b08e71d 204
embeddedartists 0:54828b08e71d 205 // turn off led to indicate initialization is okay and node is up
embeddedartists 0:54828b08e71d 206 xbeeInitLed = 1;
embeddedartists 0:54828b08e71d 207
embeddedartists 0:54828b08e71d 208 pc.printf("Node is Up\n");
embeddedartists 0:54828b08e71d 209 while (1) {
embeddedartists 0:54828b08e71d 210
embeddedartists 0:54828b08e71d 211 if (button != buttonState) {
embeddedartists 0:54828b08e71d 212 buttonState = button;
embeddedartists 0:54828b08e71d 213
embeddedartists 0:54828b08e71d 214 data[0] = CMD_BTN_MSG;
embeddedartists 0:54828b08e71d 215 data[1] = buttonState;
embeddedartists 0:54828b08e71d 216
embeddedartists 0:54828b08e71d 217 // Send the button state. In this example we are broadcasting the
embeddedartists 0:54828b08e71d 218 // message to all nodes on the same network (PAN ID). In reality
embeddedartists 0:54828b08e71d 219 // direct messaging is preferred.
embeddedartists 0:54828b08e71d 220 xbee.send(XBEE_ADDRHI_BROADCAST, XBEE_ADDRLO_BROADCAST,
embeddedartists 0:54828b08e71d 221 data, 2, &frameId);
embeddedartists 0:54828b08e71d 222 }
embeddedartists 0:54828b08e71d 223
embeddedartists 0:54828b08e71d 224
embeddedartists 0:54828b08e71d 225 xbee.process();
embeddedartists 0:54828b08e71d 226
embeddedartists 0:54828b08e71d 227 }
embeddedartists 0:54828b08e71d 228 }