Skip to content

idatum/xbeesharp

Repository files navigation

xbeesharp

.NET 7 C# implementation of Digi XBee ZigBee API operations, focusing on transmit request, remote AT command request, and receive and IO packets. These cover all current scenarios of my XBee devices.

Initial applications include xbee2mqtt, which maps XBee receive packets to MQTT messages, as well as transmitting messages and remote AT commands from subscribed MQTT topics. It is implemented using MQTTnet. I use this to map all of my XBee device packets to MQTT messages. This is allows me to integrate with Home Assistant and other custom applications I use for home automation.

XBee for DIY

I use XBee devices for remote sensors (e.g. weather station), remote displays, and to open and close my garage door. Generally, I've found them to be great for DIY projects that need a wireless solution, including low power battery sensors. My DIY XBee garage door device replaces Chamberlain's myQ and all of its issues. Here is an image of the XBee opener in all its glory, spider webs included, hanging off my LiftMaster opener:

XBee garage door opener

I use Mosquitto for MQTT and take full advantage of TLS and ACLs for securily publishing messages from Home Assistant to control the XBee garage door. This takes advantage of xbee2mqtt's ability to map MQTT messages to XBee transmit packets.

I currently use a Teensy LC microcontroller which is comically overpowered for toggling a 3.3VDC relay. So I threw in a TMP36 sensor to use one of the ADC pins and periodically transmit its value using the XBee via one of the microcontroller's serial ports.

The need for a microcontroller is related to how the LiftMaster button works with the digital signal it generates, which requires a consistent on/off from the relay. Just sending ZBee AT commands to directly toggle the relay doesn't guarantee this.

Here's a Home Assistant Community discussion on using a LiftMaster button with a relay: MyQ Alternatives.

Usage

This initial working implementation has been solid so far, providing enough of an implementation to cover all of my current home automation needs. I run it in a Docker container and integrate with HA using the MQTT integration.

Here is a basic async example that reads the next receive packet on a coordinator, with support for a CancellationToken and ILogger<>:

var serialPort = new SerialPort("/dev/USB0", 115200);
serialPort.WriteTimeout = 500;
serialPort.Open();
var xbeeSerialAsync = new XbeeSerial(logger, cancellationToken);
while (!cancellationToken.IsCancellationRequested)
{
    XbeeFrame? xbeeFrame = await xbeeSerialAsync.ReadNextFrameAsync(serialPort, true);
    if (xbeeFrame != null && xbeeFrame.FrameType == XbeeFrame.PacketTypeReceive)
    {
        ReceivePacket? receivePacket;
        if (ReceivePacket.Parse(out receivePacket, xbeeFrame) && receivePacket != null)
        {
            var data = Encoding.Default.GetString(receivePacket.ReceiveData.ToArray());
            Console.WriteLine(data);
        }
    }
}

References

XBee/XBee-PRO® S2C Zigbee® RF Module User Guide

Deploy .NET apps to Raspberry Pi

MyQ Alternatives discussion in Home Assistant Community.