xbee.discover() examples
Since the call to xbee.discover() returns an iterator which will block each time it is queried, the way that elements in the returned list are accessed can affect the timing of the application. The following examples shows two ways you can use xbee.discover() (the examples assume an N? time of 10 seconds).
Handle responses as they are received
Using xbee.discover() as the iterator in a for loop will handle each response as it is received.
- Access the MicroPython environment.
- Copy the sample code shown below.
import xbee
for i in xbee.discover():
print(i)
- At the MicroPython >>> prompt type Ctrl+E to enter paste mode. The terminal displays paste mode; Ctrl-C to cancel, Ctrl-D to finish.
- At the MicroPython >>> prompt, right-click and select the Paste option.
- Press Ctrl+D to run the code.
Running the above code prints out each response as it is received over the course of 10 seconds. Keep the processing for each response (in other words the code in the for loop) to a minimum to avoid missing responses.
Gather all responses into a list
Calling list(xbee.discover()) will block until the discovery completes and return a list of all responses found.
Access the MicroPython environment.
- Copy the sample code shown below.
>import xbee
for i in list(xbee.discover()):
print(i)
- At the MicroPython >>> prompt type Ctrl+E to enter paste mode. The terminal displays paste mode; Ctrl-C to cancel, Ctrl-D to finish.
- At the MicroPython >>> prompt, right-click and select the Paste option.
- Press Ctrl+D to run the code.
Running the above code will wait for 10 seconds then print out a list of all the responses that were received during that time. This method has less chance of missing a response due to processing, but uses more memory at run time as it has to keep track of all the responses at once.