@@ -2,8 +2,9 @@ Communicate with XBee devices
22=============================
33
44The XBee Python Library provides the ability to communicate with remote nodes in
5- the network. The communication between XBee devices in a network involves the
6- transmission and reception of data.
5+ the network, IoT devices and other interfaces of the local device. The
6+ communication between XBee devices in a network involves the transmission and
7+ reception of data.
78
89.. warning ::
910 Communication features described in this topic and sub-topics are only
@@ -745,6 +746,66 @@ The ``send_sms_async`` method may fail for the following reasons:
745746 generic ``XBeeException ``.
746747
747748
749+ .. _communicateSendRelayData :
750+
751+ Send data to other interfaces
752+ -----------------------------
753+
754+ XBee3 modules have the ability to send and receive data to/from other XBee
755+ interfaces through User Data Relay frames. The supported interfaces are: Serial
756+ port (SPI, or UART when in API mode), Bluetooth Low Energy and MicroPython.
757+ This can be useful if your application wants to transmit data to a MicroPython
758+ program running on the module or a cellphone connected to it over BLE.
759+
760+ The ``XBeeDevice `` class and its subclasses provide the following method to
761+ send data to any XBee interface:
762+
763+ +-------------------------------------------------------------+-----------------------------------------------------------------+
764+ | Method | Description |
765+ +=============================================================+=================================================================+
766+ | **send_user_data_relay(UserDataRelayInterface, Bytearray) ** | Specifies the destination relay interface and the data to send. |
767+ +-------------------------------------------------------------+-----------------------------------------------------------------+
768+
769+ This method is asynchronous, which means that your application does not block during the transmit process.
770+
771+ **Send user data relay message **
772+
773+ .. code :: python
774+
775+ [... ]
776+
777+ # Instantiate an XBee device object.
778+ device = XBeeDevice(" COM1" , 9600 )
779+ device.open()
780+
781+ destInterface = UserDataRelayInterface.MICROPYTHON
782+ message = " MicroPython, are you there?"
783+
784+ # Send the message to the MicroPython interface.
785+ device.send_user_data_relay(destInterface, message.encode(" utf8" ))
786+
787+ [... ]
788+
789+ The ``send_user_data_relay `` method may fail for the following reasons:
790+
791+ * If the relay interface or data is ``None ``, the method throws a
792+ ``ValueError ``.
793+ * Errors register as ``XBeeException ``:
794+ * If the operating mode of the device is not ``API `` or
795+ ``ESCAPED_API_MODE ``, the method throws an
796+ ``InvalidOperatingModeException ``.
797+ * If there is an error writing to the XBee interface, the method throws a
798+ generic ``XBeeException ``.
799+
800+ +------------------------------------------------------------------------------------------------------------------------------------------------------------------+
801+ | Example: Send User Data Relay |
802+ +==================================================================================================================================================================+
803+ | The XBee Python Library includes a sample application that demonstrates how to send a User Data Relay message. You can locate the example in the following path: |
804+ | |
805+ | **examples/communication/relay/SendUserDataRelaySample ** |
806+ +------------------------------------------------------------------------------------------------------------------------------------------------------------------+
807+
808+
748809Receive data
749810------------
750811
@@ -1504,6 +1565,73 @@ unsubscribe the already-registered listener.
15041565+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
15051566
15061567
1568+ .. _communicateReceiveRelayData :
1569+
1570+ Receive data from other interfaces
1571+ ----------------------------------
1572+
1573+ You can be notified when a new User Data Relay message has been received if you
1574+ are subscribed or registered to the User Data Relay reception service by using
1575+ the ``add_user_data_relay_received_callback `` method.
1576+
1577+ **User Data Relay reception registration **
1578+
1579+ .. code :: python
1580+
1581+ [... ]
1582+
1583+ # Instantiate an XBee device object.
1584+ device = XBeeDevice(" COM1" , 9600 )
1585+ device.open()
1586+
1587+ # Define the callback.
1588+ def my_data_relay_callback (relay_message ):
1589+ print (" Relay data received from %s >> '%s '" %
1590+ (relay_message.relay_interface.description,
1591+ relay_message.data.decode(" utf-8" )))
1592+
1593+ # Add the callback.
1594+ device.add_user_data_relay_received_callback(my_data_relay_callback)
1595+
1596+ [... ]
1597+
1598+ When a new User Data Relay message is received, your callback is executed
1599+ providing a ``UserDataRelayMessage `` object as parameter. This object contains
1600+ the source interface that sent the data and the data itself.
1601+
1602+ To stop listening to new User Data Relay messages, use the
1603+ ``del_user_data_relay_received_callback `` method to unsubscribe the
1604+ already-registered listener.
1605+
1606+ **Deregister User Data Relay reception **
1607+
1608+ .. code :: python
1609+
1610+ [... ]
1611+
1612+ device = [... ]
1613+
1614+ def my_data_relay_callback (relay_message ):
1615+ [... ]
1616+
1617+ device.add_user_data_relay_received_callback(my_data_relay_callback)
1618+
1619+ [... ]
1620+
1621+ # Delete the User Data Relay callback.
1622+ device.del_user_data_relay_received_callback(my_data_relay_callback)
1623+
1624+ [... ]
1625+
1626+ +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1627+ | Example: Receive User Data Relay messages |
1628+ +==============================================================================================================================================================================================================================================+
1629+ | The XBee Python Library includes a sample application that demonstrates how to subscribe to the User Data Relay reception service in order to receive messages from other XBee interfaces. You can locate the example in the following path: |
1630+ | |
1631+ | **examples/communication/relay/ReceiveUserDataRelaySample ** |
1632+ +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1633+
1634+
15071635.. _communicateReceiveModemStatus :
15081636
15091637Receive modem status events
0 commit comments