Library to easily communicate with XBee modules.
Dependencies: DigiLogger
Dependents: WaterLogger XbeeGateway XBee_Cooker ProjetReceiver ... more
Use XBee radio modules to easily improve your project with wireless connectivity. You can enable entire networks of connected devices. XBees can exchange data with other modules in the network and configure remote modules as well as trigger actions or collect sensor data on XBee nodes without needing additional external microcontrollers. Using this documentation and configuration tools with XBee modules, it is easy to develop these types of applications and more.
The XBee mbed Library is a ready-to-import mbed extension that dramatically reduces development time for XBee projects on the mbed platforms. There are many modular examples, making it an easy and smooth process to add wireless networking to a whole range of useful applications.
Info
Currently 802.15.4 (Series 1 and 2), ZigBee (Series 2) and DigiMesh (Series 1 and 2) modules are supported. The libraries can be extended to support other protocols like DigiMesh point-to-point, WiFi, etc.
User manual
The user manual can be found at this project's Wiki pages:
- Configuring the library
- Debugging the library
- Initializing modules
- Resetting the local module
- Receiving Data from other module
- Sending data to another module
- Discovering nodes in the network
- Configuring local and remote modules
- Handling modem status changes
- Handling remote modules DIOs, ADCs and PWMs
- Handling IO Data Samples from other module
- Radio Power Management
Ready to use examples
There are a lot of ready to use examples to get started quickly.
Make sure you have a valid example setup before running the examples:
Examples for ZigBee modules
Import programXBeeZB_Receive_Data
ZigBee Receive Data example for mbed XBeeLib By Digi
Import programXBeeZB_Send_Data
ZigBee Send Data example for mbed XBeeLib By Digi
Import programXBeeZB_module_config
ZigBee network configuration example for mbed XBeeLib By Digi
Import programXBeeZB_AT_Commands
ZigBee AT Commands example for mbed XBeeLib By Digi
Import programXBeeZB_dio_adc
ZigBee DIOs and ADCs example for mbed XBeeLib By Digi
Import programXBeeZB_IO_Sample_Callback
ZigBee IO Sampling Callback example for mbed XBeeLib By Digi
Import programXBeeZB_modem_status
ZigBee Modem Status example for mbed XBeeLib By Digi
Import programXBeeZB_node_discovery
ZigBee Node Discovery example for mbed XBeeLib By Digi
Import programXBeeZB_power_mngmnt_cyclic_sleep
ZigBee Power Management using Cyclic Sleep example for mbed XBeeLib By Digi
Import programXBeeZB_power_mngmnt_pin_sleep
ZigBee Power Management using Pin Sleep example for mbed XBeeLib By Digi
Examples for 802.15.4 modules
Import programXBee802_Receive_Data
802.15.4 Receive Data example for mbed XBeeLib By Digi
Import programXBee802_Send_Data
802.15.4 Send Data example for mbed XBeeLib By Digi
Import programXBee802_module_config
802.15.4 network configuration example for mbed XBeeLib By Digi
Import programXBee802_AT_Commands
802.15.4 AT Commands example for mbed XBeeLib By Digi
Import programXBee802_dio_adc_pwm
802.15.4 DIOs, ADCs and PWM example for mbed XBeeLib By Digi
Import programXBee802_IO_Sample_Callback
802.15.4 IO Sampling Callback example for mbed XBeeLib By Digi
Import programXBee802_node_discovery
802.15.4 Node Discovery example for mbed XBeeLib By Digi
Import programXBee802_power_mngmnt_cyclic_sleep
802.15.4 Power Management using Cyclic Sleep example for mbed XBeeLib By Digi
Import programXBee802_power_mngmnt_pin_sleep
802.15.4 Power Management using Pin Sleep example for mbed XBeeLib By Digi
Examples for DigiMesh modules
Import programXBeeDM_Receive_Data
DigiMesh Receive Data example for mbed XBeeLib By Digi
Import programXBeeDM_Send_Data
DigiMesh Send Data example for mbed XBeeLib By Digi
Import programXBeeDM_module_config
DigiMesh network configuration example for mbed XBeeLib By Digi
Import programXBeeDM_AT_Commands
DigiMesh AT Commands example for mbed XBeeLib By Digi
Import programXBeeDM_dio_adc_pwm
DigiMEsh DIOs, ADCs and PWMs example for mbed XBeeLib By Digi
Import programXBeeDM_IO_Sample_Callback
DigiMesh IO Sampling Callback example for mbed XBeeLib By Digi
Import programXBeeDM_modem_status
DigiMesh Modem Status example for mbed XBeeLib By Digi
Import programXBeeDM_node_discovery
DigiMesh Node Discovery example for mbed XBeeLib By Digi
Import programXBeeDM_power_mngmnt_asyncr_cyclic_sleep
DigiMesh Power Management using Asynchronous Cyclic Sleep example for mbed XBeeLib By Digi
Import programXBeeDM_power_mngmnt_pin_sleep
DigiMesh Power Management using Pin Sleep example for mbed XBeeLib By Digi
Diff: XBeeZB/XBeeZB.cpp
- Revision:
- 4:629712865107
- Parent:
- 3:8662ebe83570
- Child:
- 6:06522f3a6642
--- a/XBeeZB/XBeeZB.cpp Mon May 18 13:16:55 2015 +0200 +++ b/XBeeZB/XBeeZB.cpp Mon Jun 01 18:59:43 2015 +0200 @@ -16,33 +16,17 @@ using namespace XBeeLib; +#define BROADCAST_RADIUS_USE_NH 0x00 + /* Class constructor */ XBeeZB::XBeeZB(PinName tx, PinName rx, PinName reset, PinName rts, PinName cts, int baud) : - XBee(tx, rx, reset, rts, cts, baud), _nw_role(UnknownRole), _joined_network(false), - _vcc_exceeded_cnt(0), _broadcast_radious(0), _nd_handler(NULL), - _rx_pkt_handler(NULL), _io_data_handler(NULL) + XBee(tx, rx, reset, rts, cts, baud), _nd_handler(NULL), _rx_pkt_handler(NULL), _io_data_handler(NULL) { - _reset_timeout = RESET_TIMEOUT_MS; } RadioStatus XBeeZB::init() { RadioStatus retval = XBee::init(); - /* Determine the role of this device in the network */ - switch(_fw_version & 0xFF00) { - case 0x2100: - _nw_role = Coordinator; - break; - case 0x2300: - _nw_role = Router; - break; - case 0x2900: - _nw_role = EndDevice; - break; - default: - _nw_role = UnknownRole; - break; - } const RadioProtocol radioProtocol = get_radio_protocol(); if (radioProtocol != ZigBee) { @@ -82,8 +66,9 @@ uint32_t var32; cmdresp = get_param("SC", &var32); - if (cmdresp != AtCmdFrame::AtCmdRespOk) + if (cmdresp != AtCmdFrame::AtCmdRespOk) { return Failure; + } *chmask = var32; return Success; } @@ -205,45 +190,18 @@ AtCmdFrame::AtCmdResp cmdresp; cmdresp = set_param("JV", (uint8_t)enable); - if (cmdresp != AtCmdFrame::AtCmdRespOk) - return Failure; - return Success; + return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure; } -RadioStatus XBeeZB::enable_network_encryption(bool enable) +RadioStatus XBeeZB::set_network_security_key(const uint8_t * const key, const uint16_t length) { - AtCmdFrame::AtCmdResp cmdresp; - - cmdresp = set_param("EE", enable); - if (cmdresp != AtCmdFrame::AtCmdRespOk) - return Failure; - return Success; -} - -RadioStatus XBeeZB::set_encryption_key(const uint8_t * const key, const uint16_t length) -{ - if (key == NULL || length == 0) { + if (key == NULL || length == 0 || length > 16) { return Failure; } AtCmdFrame::AtCmdResp cmdresp; cmdresp = set_param("NK", key, length); - if (cmdresp != AtCmdFrame::AtCmdRespOk) - return Failure; - return Success; -} - -RadioStatus XBeeZB::set_tc_link_key(const uint8_t * const key, const uint16_t length) -{ - if (key == NULL || length == 0) { - return Failure; - } - AtCmdFrame::AtCmdResp cmdresp; - - cmdresp = set_param("KY", key, length); - if (cmdresp != AtCmdFrame::AtCmdRespOk) - return Failure; - return Success; + return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure; } RadioStatus XBeeZB::set_encryption_options(const uint8_t options) @@ -251,9 +209,7 @@ AtCmdFrame::AtCmdResp cmdresp; cmdresp = set_param("EO", options); - if (cmdresp != AtCmdFrame::AtCmdRespOk) - return Failure; - return Success; + return cmdresp == AtCmdFrame::AtCmdRespOk ? Success : Failure; } void XBeeZB::radio_status_update(AtCmdFrame::ModemStatus modem_status) @@ -261,18 +217,9 @@ /* Update the radio status variables */ if (modem_status == AtCmdFrame::HwReset) { _hw_reset_cnt++; - _joined_network = false; - } - else if (modem_status == AtCmdFrame::WdReset) { + } else if (modem_status == AtCmdFrame::WdReset) { _wd_reset_cnt++; - _joined_network = false; } - else if (modem_status == AtCmdFrame::JoinedNW) - _joined_network = true; - else if (modem_status == AtCmdFrame::Disassociated) - _joined_network = false; - else if (modem_status == AtCmdFrame::VccExceeded) - _vcc_exceeded_cnt++; _modem_status = modem_status; @@ -281,13 +228,14 @@ TxStatus XBeeZB::send_data(const RemoteXBee& remote, const uint8_t *const data, uint16_t len, bool syncr) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return TxStatusInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); - TxFrameZB frame = TxFrameZB(remote64, remote16, _broadcast_radious, + TxFrameZB frame = TxFrameZB(remote64, remote16, BROADCAST_RADIUS_USE_NH, _tx_options, data, len); if (syncr) { return send_data(&frame); @@ -298,18 +246,19 @@ } } -TxStatus XBeeZB::send_data(const RemoteXBee& remote, uint8_t source_ep, +TxStatus XBeeZB::send_data(const RemoteXBee& remote, uint8_t source_ep, uint8_t dest_ep, uint16_t cluster_id, uint16_t profile_id, const uint8_t *const data, uint16_t len, bool syncr) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return TxStatusInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); TxFrameZB frame = TxFrameZB(remote64, remote16, source_ep, dest_ep, - cluster_id, profile_id, _broadcast_radious, + cluster_id, profile_id, BROADCAST_RADIUS_USE_NH, _tx_options, data, len); if (syncr) { return send_data(&frame); @@ -317,16 +266,14 @@ frame.set_data(0, 0); /* Set frame id to 0 so there is no answer */ send_api_frame(&frame); return TxStatusSuccess; - } } - + TxStatus XBeeZB::send_data_to_coordinator(const uint8_t *const data, uint16_t len, bool syncr) { const uint64_t remaddr = ADDR64_COORDINATOR; - - TxFrameZB frame = TxFrameZB(remaddr, ADDR16_UNKNOWN, _broadcast_radious, - _tx_options, data, len); + + TxFrameZB frame = TxFrameZB(remaddr, ADDR16_UNKNOWN, BROADCAST_RADIUS_USE_NH, _tx_options, data, len); if (syncr) { return send_data(&frame); } else { @@ -344,14 +291,14 @@ return RemoteXBeeZB(addr64, addr16); } -NetworkRole XBeeZB::get_network_role() +XBeeZB::AssocStatus XBeeZB::get_assoc_status(void) { - return _nw_role; + return (AssocStatus)get_AI(); } bool XBeeZB::is_joined() { - return _joined_network; + return get_assoc_status() == Joined ? true : false; } void XBeeZB::register_node_discovery_cb(node_discovery_zb_cb_t function) @@ -413,8 +360,9 @@ AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint32_t * const data) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return AtCmdFrame::AtCmdRespInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); @@ -424,16 +372,18 @@ AtCmdFrame cmd_frame = AtCmdFrame(remote64, remote16, param); atCmdResponse = send_at_cmd(&cmd_frame, (uint8_t *)data, &len, RadioRemote); - if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) + if (atCmdResponse == AtCmdFrame::AtCmdRespOk && len > sizeof *data) { atCmdResponse = AtCmdFrame::AtCmdRespLenMismatch; + } return atCmdResponse; } AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, uint32_t data) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return AtCmdFrame::AtCmdRespInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); @@ -444,8 +394,9 @@ AtCmdFrame::AtCmdResp XBeeZB::set_param(const RemoteXBee& remote, const char * const param, const uint8_t * data, uint16_t len) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return AtCmdFrame::AtCmdRespInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); @@ -457,8 +408,9 @@ AtCmdFrame::AtCmdResp XBeeZB::get_param(const RemoteXBee& remote, const char * const param, uint8_t * const data, uint16_t * const len) { - if (!remote.is_valid_addr64b()) + if (!remote.is_valid_addr64b()) { return AtCmdFrame::AtCmdRespInvalidAddr; + } const uint64_t remote64 = remote.get_addr64(); const uint16_t remote16 = remote.get_addr16(); @@ -514,10 +466,7 @@ RadioStatus XBeeZB::set_dio(const RemoteXBee& remote, IoLine line, DioVal val) { - if (val == Low) - return set_pin_config(remote, line, DigitalOutLow); - else - return set_pin_config(remote, line, DigitalOutHigh); + return set_pin_config(remote, line, val == Low ? DigitalOutLow : DigitalOutHigh); } RadioStatus XBeeZB::get_dio(const RemoteXBee& remote, IoLine line, DioVal * const val) @@ -599,8 +548,9 @@ } cmdresp = set_param(remote, "PR", pr); - if (cmdresp != AtCmdFrame::AtCmdRespOk) + if (cmdresp != AtCmdFrame::AtCmdRespOk) { return Failure; + } return Success; } @@ -643,8 +593,9 @@ } cmdresp = set_param(remote, "IC", ic); - if (cmdresp != AtCmdFrame::AtCmdRespOk) + if (cmdresp != AtCmdFrame::AtCmdRespOk) { return Failure; + } return Success; }