GithubHelp home page GithubHelp logo

idatum / xbee-mpy Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 7 KB

MicroPython code to control Digi XBee Series 2C devices with a microcontroller.

Python 98.88% Shell 1.12%
micropython micropython-rpi-pico raspberry-pi-pico rp2040 xbee xbee-radio zigbee xbee-api xbee-zb

xbee-mpy's Introduction

XBee MicroPython

MicroPython code to control Digi XBee Series 2C devices with a microcontroller.

Digi's newer XBee3 already has MicroPython and I've found it to be a great little microcontroller. One use is monitoring my laundy: xbee3-laundry. However, I still have a couple XBee Series 2C devices around that I like to interface with various microcontrollers. I recently spent a Saturday giving micropython a try on my Raspberry Pi Pico and couldn't find an existing XBee API implementation. xbee-mpy is my own implementation based on Digi's documentation of their ZigBee API. I did something similar for C# with xbeesharp.

xbee-mpy allows my Pico to transmit data as well as receive messages over an XBee ZigBee mesh network. Specifically, this implementation handles transmit and receive packets, both escaped and unescaped. Any other microcontroller running MicroPython with a UART should be able to use this code with an Xbee.

Here is a simple example that waits to receive a "blink" message to toggle the Pico LED and transmits an acknowledge back:

from time import sleep
from machine import Pin
from machine import UART
from xbee_serial import XbeeSerial
import xbee_frame
import xbee_frame_builder as builder

led = Pin("LED", Pin.OUT)
xbs = XbeeSerial(UART(0, baudrate=115200), escaped=True)
rx_frame = xbs.receive()
if rx_frame.get_rame_type() == xbee_frame.FRAME_TYPE_RECEIVE:
    src_addr = rx_frame.get_source_address()
    msg = rx_frame.get_receive_data().decode()
    if msg == "blink":
        led.toggle()
        sleep(.5)
        led.toggle()
        tx_frame = builder.create_transmit_frame(address=src_addr,
                                                 frame_id=1,
                                                 tx_data="ACK blink".encode(),
                                                 escaped=True)
        xbs.transmit(tx_frame)

It can use some optimization around allocating the bytes, maybe pre-allocating a bytearray once the packet length field is received.

xbee-mpy's People

Contributors

idatum avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.