Get started with MicroPython
Exit MicroPython mode
Digi XBee® 3 802.15.4 RF Module User Guide
47
format_packet(p)
else:
time.sleep(0.25)
3. Press
Ctrl+E
to enter paste mode.
4. At the
MicroPython >>>
prompt, right-click and select the
Paste
option. Once you paste the
code, it executes immediately.
Example code on the remote module
The following example code joins the 802.15.4 network from the previous example, and continuously
prints out any incoming data. This device also sends its temperature data every 5 seconds to the
coordinator address.
1. Access the MicroPython environment.
2. Copy the following sample code:
print("Joining network as an end device...")
xbee.atcmd("NI", "End Device")
network_settings = {"CE": 0, "A1": 4, "CH": 0x13, "ID": 0x3332, "EE": 0}
for command, value in network_settings.items():
xbee.atcmd(command, value)
xbee.atcmd("AC")
# Apply changes
time.sleep(1)
while network_status() != 0:
time.sleep(0.1)
print("Connected to Network\n")
last_sent = time.ticks_ms()
interval = 5000
# How often to send a message
# Start the transmit/receive loop
print("Sending temp data every {} seconds".format(interval/1000))
while True:
p = xbee.receive()
if p:
format_packet(p)
else:
# Transmit temperature if ready
if time.ticks_diff(time.ticks_ms(), last_sent) > interval:
temp = "Temperature: {}C".format(xbee.atcmd("TP"))
print("\tsending " + temp)
try:
xbee.transmit(xbee.ADDR_COORDINATOR, temp)
except Exception as err:
print(err)
last_sent = time.ticks_ms()
time.sleep(0.25)
3. Press
Ctrl+E
to enter paste mode.
4. At the
MicroPython >>>
prompt, right-click and select the
Paste
option. Once you paste the
code, it executes immediately.
Exit MicroPython mode
To exit MicroPython mode: