If the MicroPython Read-Evaluate-Print Loop (REPL) mode is active in the XBee device, you can access it through any serial terminal application as long as it is configured with the corresponding serial port parameters of the XBee device.
Once the connection with the device is established, you can interact with the MicroPython REPL of the XBee device as you usually do on your computer with any Python interpreter.
MicroPython is only available through the UART interface and does not work with SPI. |
MicroPython Terminal of XBee Studio
XBee Studio includes an embedded tool called MicroPython Terminal to interact with the MicroPython REPL of an XBee device. This tool includes helper features and is able to interpret escape sequences and control codes correctly, making it ideal to streamline REPL interactions.
Follow these steps to access the MicroPython Terminal view of your XBee 3 BLU device:
-
Connect your device to a PC where XBee Studio is installed.
This section assumes that you know how to connect the XBee 3 BLU device to a PC. For more information on how to connect an XBee device to a PC, see the Step 2 - Set up the hardware page from the getting started section. -
Open XBee Studio.
-
Wait for your device to be discovered.
A module that’s never been configured should be discovered automatically, but if you have problems discovering your local device, see the Device discovery tools section of XBee Studio User Guide to learn how to do it manually. -
Click on the device. The Dashboard page appears. It contains the device information, connection interface and a list of quick actions.
-
Click the MicroPython Terminal item in the sidebar of the tab, located within the Development section. MicroPython Terminal view is displayed.
-
Open the connection by clicking on the plug adapter button on the top toolbar.
To use the REPL, your module must be set to MicroPython REPL operating mode. If it isn’t, the tool will prompt you with a confirmation pop-up to automatically switch the mode. -
Press Ctrl+B to get the MicroPython version banner and prompt.
MicroPython v1.20.0-1829-gcf5c2e9fb on 2023-07-20; XBee BLE with EFR32MG Type "help()" for more information. >>>
Other terminal applications
If you do not use the MicroPython Terminal of XBee Studio, you can use other terminal programs to communicate with the MicroPython REPL of the XBee 3 BLU.
-
Tera Term for Windows. You can download and install it from the Tera Term releases page.
-
Picocom for Linux. You can see installation instructions in the Picocom documentation page.
Before using a terminal application different than XBee Studio’s MicroPython Terminal, you must make sure your XBee 3 BLU device is working in MicroPython REPL mode. If not, refer to the Set the MicroPython REPL operating mode topic to set it. |
Tera Term for Windows
Follow these steps to establish a connection with the MicroPython REPL of your XBee 3 BLU device using Tera Term:
-
Open Tera Term. The Tera Term: New connection window appears.
-
Click the Serial radio button to select a serial connection.
-
From the Port: drop-down menu, select the COM port that the XBee 3 BLU is connected to.
-
Click OK. The COMxx - Tera Term VT terminal window appears and Tera Term attempts to connect to the device at a baud rate of 9600 bps.
If your XBee device is configured with a different baud rate, the terminal will not allow communication with the device.
You can find the serial port configuration of your device in the device’s dashboard page of XBee Studio. You must change this rate to the correct one:
-
Select Setup > Serial Port from the main menu. The Tera Term: Serial port setup window appears.
-
In the Tera Term: Serial port setup window, set the correct Baud rate.
-
Click OK to apply the changes to the serial port settings. The settings should go into effect right away.
-
-
Verify that local echo is not enabled and that extra line-feeds are not enabled:
-
Select Setup > Terminal from the main menu. The Tera Term: Terminal setup window appears.
-
In the New-line area of the Tera Term: Terminal setup window, click the Receive drop-down menu and select AUTO if it does not already show that value.
-
Make sure the Local echo box is not checked.
-
Click OK. The Tera Term: Terminal setup window closes.
-
-
Press Ctrl+B to get the MicroPython version banner and prompt.
MicroPython v1.20.0-1829-gcf5c2e9fb on 2023-07-20; XBee BLE with EFR32MG Type "help()" for more information. >>>
Picocom for Linux
Follow these steps to establish a connection with the MicroPython REPL of your XBee 3 BLU device using Picocom:
-
Open a terminal in Linux and type
picocom -b 115200 /dev/ttyUSB0
.If your device is configured with a different baud rate than 115200, replace it with the correct one. You can find the serial port configuration of your device in the device’s dashboard page of XBee Studio. This assumes you have no other USB-to-serial devices attached to the system. If you do have other USB-to-serial devices attached:
-
Before attaching the XBee 3 BLU device, check the directory
/dev/
for any devices namedttyUSBx
, wherex
is a number. An easy way to list these is to type:ls /dev/ttyUSB*
. This produces a list of any device with a name that starts withttyUSB
. -
Take note of the devices present with that name, and then connect the XBee 3 BLU device.
-
Check the directory again and you should see one additional device, which is the XBee 3 BLU. In this case, replace
/dev/ttyUSB0
in the command with/dev/ttyUSB<number>
, where<number>
is the new number that appeared.
-
-
It connects and shows
Terminal ready
. -
Press Ctrl+B to get the MicroPython version banner and prompt.
MicroPython v1.20.0-1829-gcf5c2e9fb on 2023-07-20; XBee BLE with EFR32MG Type "help()" for more information. >>>
Test Micropython
Once you are connected to the MicroPython REPL of the XBee device, you can interact with it the same way you do in your computer with a Python interpreter.
For more information about MicroPython support in XBee devices and available APIs, refer to the Digi MicroPython Programming Guide. |
Example: Hello World
You can try to execute a simple Hello World by:
-
Press ENTER to display the MicroPython
>>>
prompt. -
Type the Python command:
print("Hello, World!")
. -
Press ENTER to execute the command. The terminal echos back
Hello, World!
.
Example: Execute AT Commands
There are also APIs to read and write the value of settings using AT commands.
Follow these steps to change the value of the NI
setting and then read it:
-
Import the
xbee
module by typing:import xbee
-
Press ENTER to apply the import.
-
Run the method of the
xbee
module corresponding to the AT command execution (atcmd
) to change the value of theNI
setting. Provide as first parameter the ID of the setting to configure (NI
) and as second parameter the new value of the setting (MY DEVICE
):xbee.atcmd("NI", "MY DEVICE")
-
Press ENTER to execute the command. The terminal does not provide any feedback of the operation, which means that the setting was changed successfully.
-
Run the method of the
xbee
module corresponding to the AT command execution to read the value of theNI
setting. In this case the read operation requires just one parameter which corresponds to the ID of the setting to read (NI
):xbee.atcmd("NI")
-
Press ENTER to execute the command. The terminal prints the value of the
NI
setting:'MY DEVICE'
For more information about AT commands refer to AT commands. |
Keyboard shortcuts
The MicroPython REPL of the XBee devices and terminal tools described in this topic support the following control characters that you can use to manage the MicroPython code being executed:
Keyboard shortcut | Description |
---|---|
Ctrl+A |
Enters raw REPL mode. This is like a permanent paste mode, except that characters are not echoed back. |
Ctrl+B |
Prints the MicroPython banner. Leaves raw mode and returns to the regular REPL. Reprints the MicroPython banner followed by a REPL prompt. |
Ctrl+C |
Regains control of the terminal. Interrupts the currently running program. |
Ctrl+D |
Reboots the MicroPython REPL. Soft-resets MicroPython, clears the heap. |
Ctrl+E |
Enters paste mode.
Does not auto-indent and compiles pasted code all at once before execution.
Uses a REPL prompt of |
Ctrl+F |
Uploads code to flash.
Uses a REPL prompt of |
Ctrl+R |
Runs code in flash. |