Comments (3)
Hi @KyleIm,
To change the operating mode of your XBee you can use XCTU for example. Using the library you can only use API modes, that is AP=1
or AP=2
, since transparent mode (AP=0
) is not supported. You can use:
from digi.xbee.models.atcomm import ATStringCommand
from digi.xbee.models.mode import OperatingMode
[...]
# Set operating mode to 1 (API without escapes)
xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.API_MODE.code]))
# Set operating mode to 2 (API with escapes)
xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.ESCAPED_API_MODE.code]))
[...]
The method open(force_settings=True)
of an XBeeDevice
object forces the XBee configuration to the one provided when you instances it. In your case to 9600 bauds, 8 data bits, no parity, 1 stop bit, and no flow control. It also forces the operating mode of the XBee to AP=1
if it is not configured to use an API mode (that is, if AP!=1
and AP!=2
)
See Open the XBee connection.
The reset()
method does not restore the configuration of your XBee. It performs a software reset of the module, it executes the command FR
. See Reset the device.
To restore default values for your module you must execute RE
command (see RE (Restore Defaults)). After restoring:
- To permanently store changes on the XBee you must write them (
write_changes()
) - Reset the module so it use the established default configuration (
reset()
) - Then close the connection (
close()
)
If after this, you need to continue communicating with the XBee, you must open again the connection. This means, after using open(force_settings=True)
, some parameters will be modified to use the communication parameters you set on the object instance and the AP
setting value to be 1, since the default value, 0 (transparent mode) is not supported by the library (see Open the XBee connection)
For example:
import sys
from digi.xbee.models.atcomm import ATStringCommand
from digi.xbee.devices import XBeeDevice
from digi.xbee.exception import XBeeException
DEFAULT_NODE_ID = " "
NODE_ID = "xbee0"
def restore_defaults_and_close(xbee):
try:
xbee.execute_command(ATStringCommand.RE, apply=False)
xbee.write_changes()
print("XBee default configuration restored!")
except XBeeException as exc:
print("Error restoring XBee default configuration: %s" % str(exc))
sys.exit(1)
try:
xbee.reset()
except XBeeException as exc:
# This is expected if the previous XBee connection parameters
# are different to the default ones
print(" [Expected] Error resetting the XBee: %s" % str(exc))
finally:
xbee.close()
def open_xbee(xbee):
try:
xbee.open(force_settings=True)
except XBeeException as exc:
print("Unable to establish communication with XBee: %s" % str(exc))
sys.exit(1)
def run():
xbee0 = XBeeDevice("/dev/ttyUSB5", 115200)
open_xbee(xbee0)
# Set node ID to a non default value, i.e., xbee0
try:
xbee0.set_node_id(NODE_ID)
xbee0.write_changes()
xbee0.apply_changes()
# Read the node ID to test the value
node_id = xbee0.get_node_id()
print("\nNode id BEFORE restoring settings: '%s'\n" % node_id)
if node_id != NODE_ID:
print("ERROR: Expected value '%s', retrieved '%s'" % (NODE_ID, node_id))
sys.exit(1)
except XBeeException as exc:
print("Unable to set XBee node ID: %s" % str(exc))
sys.exit(1)
# Restore defaults and close the connection
restore_defaults_and_close(xbee0)
# Here you can reopen the device, but some configuration parameters will
# be changed. For example:
# * the baudrate to be 115200 (since 'xbee0' was instantiated to use
# 115200 instead the defaults 9600)
# * or the operating mode (AP), since the default mode, transparent
# (AP=0) is not supported by the library. The XBee will be reconfigured
# to use API without escapes (AP=1).
print("Re-opening XBee")
open_xbee(xbee0)
# Read the node ID, it should be a white space (the default value)
node_id = xbee0.get_node_id()
print("\nNode id AFTER restoring settings: '%s'\n" % node_id)
if node_id != DEFAULT_NODE_ID:
print("ERROR: Expected value '%s', retrieved '%s'" % (DEFAULT_NODE_ID, node_id))
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
run()
The output is:
Node id BEFORE restoring settings: 'xbee0'
XBee default configuration restored!
[Expected] Error resetting the XBee: Timeout waiting for the modem status packet.
Re-opening XBee
Node id AFTER restoring settings: ' '
Hope this helps.
from xbee-python.
Thanks I solved it. However, xbee0.set_parameter(ATStringCommand.AP, bytearray([OperatingMode.API_MODE.code])) should be xbee0.set_parameter(('AP'), bytearray([OperatingMode.API_MODE.code]))
When I copied it, it showed TypeError: object of type 'ATStringCommand' has no len().
from xbee-python.
Great @KyleIm!
Using ATStringCommand.AP
in get_parameter()
, set_parameter()
, and execute_parameter()
methods should work since version 1.4.0 of the library.
Best Regards.
from xbee-python.
Related Issues (20)
- Sending HTTP request after getting packet from Xbee HOT 1
- GettingStarted Code Returns Cannot Import Name XbeeDevice HOT 1
- add_io_sample_received_callback() always using the same remote device HOT 3
- How to receive io samples from one specific node on a network?
- Accessing LQI value HOT 2
- [REQUEST] Open ZigBee Coordinator Backup Format support for Zigbee network backups and restoring from and to Digi XBee 3 Zigbee 3 RF modules HOT 1
- `Serial exception while reading: device reports readiness to read but no returned data HOT 1
- Route hops order in `create_source_route` vs `CreateSourceRoutePacket` vs XBee User Guide HOT 4
- Need help with Xmodem HOT 9
- Problems getting the LQI value HOT 4
- Node timeout doesn't work in my script HOT 4
- except TimeoutException, ex: HOT 1
- OTA Failure
- Can't set channel via library. HOT 1
- Large delay in continuous read_data() operations HOT 6
- Broadcast other node information in a mesh network HOT 7
- Error while trying to get 64-bit address of XBee HOT 2
- Reading sensor data using SMT development board HOT 2
- Extracting Transmit Statis for coordinator device HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xbee-python.