XBee MicroPython Examples

Introduction

Simple programs can make a big difference! An XBee running small amounts of code can perform some pretty important tasks. Cryptic readings can be transformed into useful data, excess transmissions can be intelligently filtered out, modern sensors and actuators can be employed directly, operational logic can glue inputs and outputs together in an intelligent way.

Here are some useful MicroPython examples that should run within 12KB of RAM, useful even in a small sandboxed implementation. Required parts and a method for simulating limited RAM are noted below.

Examples


Send “Hello World”

This example shows how to send some text data via an XBee in transparent mode.

  1. SETUP: Connect the XBee (configured to factory defaults) as shown in the diagram below:XBee Pyboard Basic_bbXBee Pyboard Basic_schem
  2.  PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the text data. Each time you reset the pyboard, it sends “hello world!” one time to your computer. The results will look like this:
    XBee MicroPython Send Text screenshot


Read, Transform and Send Value

This example shows how to read a sensor, transform that data into correct units, then send via an XBee in transparent mode.

  1. SETUP: Connect the XBee (configured to factory defaults) and a TMP36 temperature sensor as shown in the diagram below. You can use the XBee pyboard skin to easily connect the radio module, and a small breadboard connected with jumper wires for the TMP36.
    XBee Pyboard Read Transform Send_bbXBee Pyboard Read Transform Send_schem
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will look like this:
    XBee MicroPython Read Tranfsorm Send screenshot

 


Read, Transform and Send Only High Sensor Values

This example shows how to read a sensor, filter out low values, then send via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show ONLY readings above the limit of 30 Celsius, no other data will be displayed:
    XBee MicroPython Read Tranform Send Greater Than screenshot

Read, Transform and Send Only High or Low Values

This example shows how to read a sensor, filter out mid-range values, then send any high or low values via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show ONLY readings below 28 Celsius or above 30 Celsius, no other data will be displayed:
    XBee MicroPython Read Tranform Send High Low screenshot

Send “Heartbeat” Sensor Data

This example shows how to send periodic “heartbeat” data values via an XBee in transparent mode. Used with filters or alarms to confirm normal operation.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show data readings once per minute, though in typical use it might be set to daily or even weekly.
    XBee MicroPython Send Heartbeat Data screenshot

Send “Heartbeat” with Min and Max

This example shows how to send periodic “heartbeat” data values with min and max for the period, via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show data readings once per minute, with the minimum and maximum values during that period.
    XBee MicroPython Send Heartbeat Min Max screenshot

Aggregate 10 Samples Then Send Batch

This example shows how to aggregate data before sending in a batch, via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show nothing for a minute, then you should see 10 data readings, sent all at once.
    XBee MicroPython Aggregate Then Send 10 Values screenshot

Aggregate Values Then Send Average

This example shows how to aggregate data before sending an average of the data, via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show nothing for a minute, then you should see an average of 10 samples.
    XBee MicroPython Aggregate Then Send Average screenshot

Suppress Duplicate Data

This example shows how to suppress sending too many duplicate readings, via an XBee in transparent mode.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show readings every minute, with changes greater than 10% triggering an immediate reading.
    XBee MicroPython Surpress Duplicate Data screenshot

Local Control

This example shows how to turn local input into local actions, with alert messages about important events delivered remotely.

  1. SETUP: Connect the XBee (configured to factory defaults), an LED and a sensor switch as shown below. You can use the XBee pyboard skin to easily connect the radio module, and a small breadboard connected with jumper wires for the LED, the switch and their resistors.
    XBee Pyboard Local Control_bbXBee Pyboard Local Control_schem
  2.  PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the alarm alerts. After the switch has been pressed continuously for 60 seconds, the alarm light will turn on. You will receive text alerts in the terminal every minute until the button is released.
    XBee MicroPython Local Control screenshot

Transform UART

This example shows how to accept incoming text strings and transform them to different outgoing text strings.

  1. SETUP: Use the hello world setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Next, using a terminal program like XCTUor CoolTerm, type “hw” into the active window followed by a return. You should receive “hello world” back as shown below. All text input that doesn’t contain “hw” should be ignored.
    XBee MicroPython Transform UART screenshot

Respond to Data Request

This example shows how to accept an incoming serial text command and respond with a sensor data reading.

  1. SETUP: Use the basic sensing setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Next, using a terminal program like XCTUor CoolTerm, type “temp” into the active window followed by a return. You should receive a temperature reading back as shown below. All text input that doesn’t contain “temp” should be ignored.
    XBee MicroPython Respond to Data Request screenshot

Control Pin via UART

This example shows how to turn an LED on and off using serial text input, via an XBee in transparent mode.

  1. SETUP: Connect the XBee (configured to factory defaults), and an LED as shown below. You can use the XBee pyboard skin to easily connect the radio module, and a small breadboard connected with jumper wires for the LED and resistor.
    XBee Pyboard Control Pin via UART_bbXBee Pyboard Control Pin via UART_schem
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Next, using a terminal program like XCTUor CoolTerm, type “on” into the active window followed by a return. You should receive “pin on” back as shown below, and the LED should turn on. Type “off” followed by a return to douse the LED and receive “pin off” as confirmation. All text input that doesn’t contain “on” or “off” should be ignored.
    XBee MicroPython Contol Pin via UART screenshot

Basic I2C Sensor Read

This example shows how to do a very basic read an I2C sensor (the BMP180), via an XBee in transparent mode.

  1. SETUP: Connect the XBee (configured to factory defaults), and the BMP180 as shown below (same connections as older BMP085). You can use the XBee pyboard skin to easily connect the radio module, and a small breadboard connected with jumper wires for the BMP180.
    XBee Pyboard Basic I2C Sensor Read_bbXBee Pyboard Basic I2C Sensor Read_schem
  2. PROGRAM: Load the code sample into your pyboard’s main.py file
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show raw, uncalibrated numbers from the temperature sensor since that’s all this example asks for. Warm the sensor with your hand to watch the numbers increase, then let it cool to see them decrease:
    XBee MicroPython Basic I2C Sensor Read screenshot

Full BMP180 I2C

This example shows how to fully read an I2C sensor (the BMP180), via an XBee in transparent mode.

  1. SETUP: Use the BMP180 setup.
  2. PROGRAM: Load the code sample into your pyboard’s main.py file, then download the BMP180 library and put it into the same directory.
  3. RESULTS: Connect a second XBee, also configured to factory defaults, to your computer. Then use a terminal program like XCTU or CoolTerm to receive the sensor data. The results will show calibrated temperature, pressure and altitude readings. Warm the sensor with your hand, and raise or lower it by a few meters to change the readings:
    XBee MicroPython Full BMP180 I2C screenshot


Resources

Simulating Limited RAM

To simulate 12KB of total RAM on the 192KB pyboard, use this block of code at the start of your program, or imported  in a config.py file.

Posted in General, Internet of Things, Networking, XBee
One comment on “XBee MicroPython Examples
  1. Gautam says:

    Thanks for sharing such valuable information.

    Can you please also guide:

    1. How to do a implementation using 16 bit and 64 addresses in API mode.
    2. One to many and vice versa communication.