ZB Coord API for XBee: This XBee library drives Digi XBee ZB Modules in API Operation mode. Most commands include remote communication's are supported by the functions of this library. You can control XBee devices through the API software modules in this, without using any AT commands. Please refer to the following page and some of sample codes:

Dependents:   sample02_sw xbee_aging sample01_led sample04_sens

XBee library 'ZB Coord API'

/media/uploads/bokunimowakaru/nucleoboard.jpg

Nucleo Board + Arduino Wireless Shield + XBee ZB Module + LCD

This XBee liblary is developed for Digi XBee ZB Modules on Nucleo F401RE Micro Computer Board through Arduino Wireless Shield.

Quote:

When you will use this liblary for anything other than Nucleo microcomputer board, please change the 4th line of xbee/xbee.cpp. The above code is only for STM32 Nucleo board.

  • RawSerial _xbee_serial(SERIAL_TX, SERIAL_RX);

/media/uploads/bokunimowakaru/nucleo_xbee.jpg

Hardware Jumper Settings

The hardware soldering jumper setting for the serial ports is necessary for Nucleo board.

  • Set OPEN for SB14(TX)
  • Set SHORT jumper for SB62
  • Set SHORT jumper for SB63

/media/uploads/bokunimowakaru/nucleo_jp.jpg

Examples

Sample Code 01 LED

An XBee ZB Coordinator on a Nucleo Board controls LEDs on the other XBee ZB device.

#include "xbee.h"
DigitalOut myled(LED1);
 
int main(){
    // Please set MAC Address to access your remote XBee device.
    byte dev_gpio[]   = {0x00,0x13,0xA2,0x00,0x40,0x30,0xC1,0x6F};
    
    myled = 1;                          // set Higher Level(3.3V) for LED port on NUCLEO
    xbee_init( 0 );                     // Initialize XBee COM Port Settings
    xbee_atnj( 0xFF );                  // Enable Network Join
    myled = 0;                          // set Lower Level(0.0V) for LED port on NUCLEO
    
    while(1){ 
        myled = 1;                      // set Higher Level(3.3V) for LED port on NUCLEO
        xbee_gpo(dev_gpio,11,1);        // set H Level for GPO port 11 on remote xbee
        xbee_gpo(dev_gpio,12,1);        // set H Level for GPO port 12 on remote xbee
        wait(1);
        myled = 0;                      // set Lower Level(0.0V) for LED port on NUCLEO
        xbee_gpo(dev_gpio,11,0);        // set L Level for GPO port 11 on remote xbee
        xbee_gpo(dev_gpio,12,0);        // set L Level for GPO port 12 on remote xbee
        wait(1);
    }
}

http://mbed.org/users/bokunimowakaru/code/sample01_led/

Sample Code 02 Switch

To push buttons (or switches) on an XBee ZB device report the conditions of input ports on it to a Nucleo board, and display them on a LCD.

#include "xbee.h"
#include "TextLCD.h"
TextLCD lcd(PA_9, PC_7, PB_5, PB_4, PB_10, PA_8); // rs, e, d4-d7
  
int main(){
    byte data;
    int i;
    XBEE_RESULT xbee_result;
    byte dev_gpio[]   = {0x00,0x13,0xA2,0x00,0x40,0x30,0xC1,0x6F};
    
    lcd.cls(); lcd.printf("Sample 2 SW");
    xbee_init( 0x00 ); 
    lcd.cls(); lcd.printf("ATNJ");
    xbee_atnj( 0xFF ); 
    lcd.cls(); lcd.printf("gpio init");
    xbee_gpio_init(dev_gpio);           // Send GPIO mode settings to remote device.
    lcd.cls(); lcd.printf("DONE");
    
    while(1){
        data = xbee_rx_call( &xbee_result );
        if( xbee_result.MODE == MODE_GPIN){     // When a GPIO packet is recieved,
            lcd.cls();
            for( i=7; i>=0 ; i--) lcd.printf( "%c",(char)( (int)'0' + ((data>>i) & 0x01) ) );
                                        // Display the recieved data.
        }
    }
}

http://mbed.org/users/bokunimowakaru/code/sample02_sw/

Sample Code 04 Sensor

Measured results of an XBee wireless sensor device are displayed a LCD on Nucleo board with an XBee coordinator which is in API mode.

#include <xbee.h>
#include "TextLCD.h"
#define FORCE_INTERVAL  100
TextLCD lcd(PA_9, PC_7, PB_5, PB_4, PB_10, PA_8); // rs, e, d4-d7
byte dev_sens[8];   // IEEE Address
 
int main(){
    int i;
    int dev_en = 0;                 // Device Detection Flag
    XBEE_RESULT xbee_result;            // Stracture for recieved data
    int trig=0;
    float value;

    lcd.cls(); lcd.printf("Sample 4 SENS");
    xbee_init( 0x00 ); 
    lcd.cls(); lcd.printf("Searching:SENSOR");
    if( xbee_atnj(10) ){                // Enable Network Join during 10 seconds.
        lcd.cls(); lcd.printf("Found a device");
        xbee_from( dev_sens );          // Get address of remote device. 
        dev_en = 1;
        xbee_gpio_config( dev_sens, 1 , AIN ); // Analog Input mode to port 1 on remote device
        xbee_gpio_config( dev_sens, 2 , AIN ); // Analog Input mode to port 2 on remote device
    }else{
        lcd.cls(); lcd.printf("Failed:no dev.");
    }
    wait(1);
    lcd.cls();
    
    while(1){
        if(dev_en){ 
            if(trig<=0){
                lcd.locate(12,0); lcd.printf("Trig"); wait(0.1);
                xbee_force(dev_sens);           // Send a request command to remote device.
                trig = FORCE_INTERVAL;
                lcd.locate(12,0); lcd.printf("    ");
            }
        }else{
            lcd.cls(); lcd.printf("Waiting for XBee");
            wait(0.5);
            lcd.cls();
        }
        trig--;
 
        xbee_rx_call( &xbee_result );       // Check a recieved packet.
        switch( xbee_result.MODE ){         // Check the packet type.
            case MODE_RESP:                 // the responce for the request in xbee_force().
            case MODE_GPIN:                 // Report data from remote xbee device.
                if( bytecmp( dev_sens , &(xbee_result.FROM[0]) ,8 ) == 0 && xbee_result.STATUS == STATUS_OK ){
                    value = xbee_sensor_result( &xbee_result, LIGHT);
                    lcd.locate(0,0); lcd.printf( "%.1f Lux   ",value);
                    value = xbee_sensor_result( &xbee_result, TEMP);
                    lcd.locate(0,1); lcd.printf( "%.1f C   ",value);
                }
                break;
            case MODE_IDNT:             // Commissioning Button Report is recieved.
                lcd.cls(); lcd.printf("found a new dev");
                for( i=0;i<8;i++ ) dev_sens[i]=xbee_result.FROM[i];
                                        // Save MAC address of reported device.
                dev_en = 1;             // Flag on (A sensor is detected.)
                trig = 0;
                xbee_gpio_config( dev_sens, 1 , AIN ); // Set Analog Mode to port 1 on remote XBee
                xbee_gpio_config( dev_sens, 2 , AIN ); // Set Analog Mode to port 2 on remote XBee
                lcd.cls();
                break;
            default:
                break;
        }
    }
}

http://mbed.org/users/bokunimowakaru/code/sample04_sens/

Revision:
8:ddded5d4f96c
Parent:
5:6ce45b34d3f0
--- a/xbee.cpp	Sat Oct 04 01:53:54 2014 +0000
+++ b/xbee.cpp	Fri Oct 31 10:08:36 2014 +0000
@@ -1,9 +1,9 @@
-// for ARM mbed Compiler
+//	for ARM mbed Compiler
 #define ARM_MBED
 #include <mbed.h>
 RawSerial _xbee_serial(SERIAL_TX, SERIAL_RX);	// for STM32 Nucleo
-// Please set serial ports in conformity to your microcomputer board.
-// 上記の括弧内をお手持ちのマイコンボードのシリアルピン名に書き換えてください。
+//	Please set serial ports in conformity to your microcomputer board.
+//	上記の括弧内をお手持ちのマイコンボードのシリアルピン名に書き換えてください。
 
 /*********************************************************************
 本ソースリストおよびソフトウェアは、ライセンスフリーです。(詳細は別記)
@@ -19,6 +19,7 @@
 							   Copyright (c) 2010-2014 Wataru KUNINO
 							   http://www.geocities.jp/bokunimowakaru/
 *********************************************************************/
+
 /*
 既知のバグ
 	[PC用/H8用/Arduino共通]
@@ -225,8 +226,12 @@
 						 ・効果はArduino上でSD使用時のメモリ確保など
 						- xbee_bytesで規定サイズを超えた場合のリーク対策
 						- 下位マスク0x00のGPIN受信時にエラー出力バグ修正
-	2014/10/25	1.91	- ARM mbedでのシリアル受信時のロックアップ対策
+	2014/10/25	1.91	- CQ出版 ZigBee Arduinoプログラム全集 第2版用
+						- ARM mbedでのシリアル受信時のロックアップ対策
 						- ARM mbedを用いた時のシリアル送信タイミング調整
+						- xbee_end_deviceでRouterへ送信時にIR値を設定
+						 (従来はエラー応答だったが、設定後に正常応答)
+	2014/10/31	1.92	- ARM mbed対応
 
 *********************************************************************/
 /*
@@ -5079,7 +5084,7 @@
 入力:byte ir = 0:自動送信切、1~65:自動送信間隔(秒)
 入力:byte pin = 0:通常のスリープ、 1:SLEEP_RQ端子を有効に設定
 出力:戻り値 = 0x00 指示成功、 その他=エラー理由
-		1: 	 対応デバイス(ZigBee End Deviceなど)以外に設定しようとした
+		1: 	 対応デバイス以外に設定しようとした
 		2: 	 スリープ解除しようとしたのに失敗した
 		4: 	 スリープ移行待ち時間ST設定の失敗
 		8: 	 スリープ間隔SP設定の失敗
@@ -5093,7 +5098,21 @@
 	unsigned int time;
 	
 	xbee_address( address );							// 送信先を指定
-	
+	if( ir > 65 ) ir = 65;
+	time = (unsigned int)ir * 1000; 			// ms単位の数字に変換
+		
+	#ifndef XBEE_WIFI // ZigBee 
+		/* XBee子機から自動送信する周期を設定します */
+		data[0] = (byte)(time / 256);
+		data[1] = (byte)(time % 256);						// スリープ時間 sleep_time
+		if( !xbee_tx_rx("RATIR",data,2) ) ret|=32;
+		
+		/* エンドデバイス以外の時に終了する */
+		if( xbee_tx_rx("RATVR",data,0) ) if(data[18] != 0x28){
+			return( ret );
+		}
+	#endif
+
 	if( sp == 0x00 ){
 		data[0] = 0x00;
 		if( !xbee_tx_rx("RATSM",data,1) ) ret|=2;		// スリープ解除
@@ -5114,13 +5133,6 @@
 			if( !xbee_tx_rx("ATSP",data,2) ) ret|=16;				// 保持時間 sleep_time
 		#endif
 		
-		/* XBee子機から自動送信する周期を設定します */
-		if( ir > 65 ) ir = 65;
-		time = (unsigned int)ir * 1000; 			// ms単位の数字に変換
-		data[0] = (byte)(time / 256);
-		data[1] = (byte)(time % 256);						// スリープ時間 sleep_time
-		if( !xbee_tx_rx("RATIR",data,2) ) ret|=32;
-		
 		/* XBee子機のスリープ移行待ち時間を設定します */
 		data[0]=0x01; data[1]=0xF4; 						// 保持時間 500 ms
 		if( !xbee_tx_rx("RATST",data,2) ) ret|=4;