{"id":13667130,"url":"https://github.com/samco182/SwiftyXBee","last_synced_at":"2025-04-26T15:32:13.492Z","repository":{"id":63920464,"uuid":"198308913","full_name":"samco182/SwiftyXBee","owner":"samco182","description":"⚡️ A Swift library for communicating with XBee radios in API mode","archived":false,"fork":false,"pushed_at":"2020-01-15T23:26:45.000Z","size":71,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-05T01:48:47.965Z","etag":null,"topics":["iot","library","raspberry-pi","rf-module","s2c","swift","swifty","uart","xbee","xbee-api","zigbee"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samco182.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-22T22:09:19.000Z","updated_at":"2024-09-02T06:19:19.000Z","dependencies_parsed_at":"2023-01-14T14:00:37.541Z","dependency_job_id":null,"html_url":"https://github.com/samco182/SwiftyXBee","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samco182%2FSwiftyXBee","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samco182%2FSwiftyXBee/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samco182%2FSwiftyXBee/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samco182%2FSwiftyXBee/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samco182","download_url":"https://codeload.github.com/samco182/SwiftyXBee/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250751841,"owners_count":21481313,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iot","library","raspberry-pi","rf-module","s2c","swift","swifty","uart","xbee","xbee-api","zigbee"],"created_at":"2024-08-02T07:00:32.312Z","updated_at":"2025-04-26T15:32:13.443Z","avatar_url":"https://github.com/samco182.png","language":"Swift","funding_links":[],"categories":["Communication"],"sub_categories":["Networking, IoT, Bus Protocols, …"],"readme":"# SwiftyXBee\n\n⚡️A Swift library for communicating with XBee (ZigBee Series 2) radios in API mode.\n\u003cp\u003e\n\u003cimg src=\"https://img.shields.io/badge/Architecture%20-ARMv6%20%7C%20%20ARMv7%2F8-red.svg\"/\u003e\n\u003cimg src=\"https://img.shields.io/badge/OS-Raspbian%20%7C%20Debian%20%7C%20Ubuntu-yellow.svg\"/\u003e\n\u003ca href=\"https://developer.apple.com/swift\"\u003e\u003cimg src=\"https://img.shields.io/badge/Swift-4x-brightgreen.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://raw.githubusercontent.com/samco182/SwiftySHT20/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/Licence-MIT-blue.svg\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cimg src=\"https://www.digi.com/products/embedded-systems/rf-modules/2-4-ghz-modules/xbee-zigbee/product-images/xbee-s2c-zigbee\" height=\"300\" width=\"450\"\u003e\n\n## Summary\nThis is a [SwiftyGPIO](https://github.com/uraimo/SwiftyGPIO) based library for communicating with XBee radios in **API mode**, with support for Series 2 **only**.\n\nThis is a **work in progress**. I started coding the library for a project I am currently working on, so, as of now, it only supports ATCommand, ATCommandResponse, Tx, Rx, and Transmit Status API Frames. If you want to contribute to this noble cause, please submit a PR and I will be more than happy to review it and merge it :smile:.\n\nFor more information regarding the RF module, you can consult its [datasheet](https://www.digi.com/resources/documentation/digidocs/pdfs/90002002.pdf).\n\n## API Mode\nXBees support two operation modes: API and AT. API mode requires that the device communicate through a structured interface (that is, data is communicated in frames in a defined order). The API specifies how the device sends and receives commands, command responses, and module status messages using a serial port Data Frame. On the other hand, in AT (transparent) mode, the XBee radio simply relays serial data to the receiving XBee, as identified by the DH+DL address.\n\nAccording to the documentation:\n\u003e The XBee firmware supports two API operating modes: without escaped characters and with escaped characters. Use the AP command to enable either mode. To configure a device to one of these modes, set the following AP parameter values:\n\u003e -  AP = 1: API operation.\n\u003e -  AP = 2: API operation (with escaped characters—only possible on UART).\n\n⚠️ This library is designed to work only in API mode,  it **requires** the **AP mode set to 2 (escape bytes)**, as this setting offers the best reliability.\n\n## Hardware Details\n- The XBee should be powered using **3.3 V**.\n- This library requires the XBee's **UART** to be configured with compatible settings for the baud rate, parity, start bits, stop bits, and data bits.\n\nThis is the pin assignment to manually connect (no XBee shield) the device to any of the compatible boards:\n\n| XBee Pin                     | Board UART    |\n| ------------------------  | ---------------   | \n| VCC = **1**                 | VCC 3.3V        |\n| DOUT = **2**              | RX / Receive   |\n| DIN / CONFIG = **3** | TX / Transmit   | \n| GND = **10**              | GND                | \n\nThe UART pins on the RaspberryPi (pin 14 TXD, pin 15 RXD) need to be enabled via `raspi-config` before you can use them (restart required). By enabling the UART pins, you will lose the ability to access the login shell over serial.\n\n## Supported Boards\nEvery board supported by [SwiftyGPIO](https://github.com/uraimo/SwiftyGPIO): RaspberryPis, BeagleBones, C.H.I.P., etc...\n\nTo use this library, you'll need a Linux ARM board running [Swift 5.x](https://github.com/uraimo/buildSwiftOnARM) 🚗.\n\nThe examples below will use a Raspberry Pi 3B+  board, but you can easily modify the examples to use one of the other supported boards. Full working demo projects for the RaspberryPi3B+ are available under the **Examples** directory.\n\n## Installation\nFirst of all, makes sure your board is running **Swift 5.x** ⚠️!\n\nSince Swift 5.x supports Swift Package Manager, you only need to add SwiftXBee as a dependency in your project's `Package.swift` file:\n\n```swift\nlet package = Package(\n    name: \"MyProject\",\n    dependencies: [\n        .package(url: \"https://github.com/samco182/SwiftyXBee\", from: \"1.0.0\"),\n    ]\n    targets: [\n        .target(\n            name: \"MyProject\", \n            dependencies: [\"SwiftyXBee\"]),\n    ]\n)\n```\nThen run `swift package update` to install the dependency.\n\n## Usage\n### Initialization\nThe first thing is to initialize an instance of `SwiftyXBee` with the same UART configuration as the one you used to setup the XBee devices on the XCTU tool or through the AT command mode. Once you have your `xbee` object initialized, you can send and receive data packets between XBees:\n\n```swift\nimport SwiftyXBee\n\nlet serial = SerialConnection(speed: .S9600, bitsPerChar: .Eight, stopBits: .One, parity: .None)\nlet xbee = SwiftyXBee(for: .RaspberryPi3, serialConnection: serial)\n```\nYou can also initialize the `XBee` object with the following method:\n```swift\nimport SwiftyXBee\n\nlet xbee = SwiftyXBee()\n```\nThis initializer defaults to `.RaspberryPi3` as the selected board and serial connection with `speed: .S9600`, `bitsPerChar: .Eight`, `stopBits: .One`, and `parity: .None)`.\n\n### AT Command Packet\nAT-type commands can be sent via API frames to configure your local radio. They can query the settings on the local radio or set parameters. These are all the same commands you typed in transparent/command mode.\n\nFor this example, the local radio's *Node Identifier* parameter is been set and read.\n\nSetting AT Command parameter:\n``` swift\n// The new Node Identifier\nlet nodeIdentifier = \"XBee Test\"\n\nxbee.sendATCommand(.addressing(.ni(.write(nodeIdentifier))), frameId: .sendNoACK)\n```\nReading AT Command parameter:\n```swift\nxbee.sendATCommand(.addressing(.ni(.read)))\n\ndo {\n    let atCommandResponse = try xbee.readATCommandResponse()\n    print(\"AT Command Response received: \\(atCommandResponse.frameData.commandData) = \\(atCommandResponse.frameData.commandData.string)\")\n} catch let error {\n    print(\"Error receiving packet: \\(error)\")\n}\n```\n\n### Transmit Packets\nThere a several different types of transmit (TX) packets available. But as mentioned above, as of now, the library only allows **Transmit Request**  packets to be sent. A list of all TX packets can be found in the API [documentation](https://www.digi.com/resources/documentation/digidocs/pdfs/90002002.pdf). All classes that end in \"Request\" are TX packets.\n```swift\n// The destination XBee 64-bit serial number\nlet deviceAddress = DeviceAddress(address: 0x0013A20012345678)\n\n// The destination XBee 16-bit network address\nlet networkAddress = NetworkAddress(address: 0xFFFE)\n\n// The actual message to be sent\nlet message = \"This is my message to send!\"\n\nxbee.sendTransmitRequest(to: deviceAddress, network: networkAddress, message: message)\n```\nWhen a Transmit Request completes, if configured on the request's Frame Id, the receiver device will send a **Transmit Status** packet out of the serial interface. This message indicates if the Transmit Request was successful or if it failed. If you want to make sure your packet was correctly delivered, call the following code:\n```swift\ndo {\n    let transmitStatus = try xbee.readTransmitStatus()\n    print(\"Status received: \\(readingPacket.frameData.deliveryStatus)\")\n} catch let error {\n    print(\"Error: \\(error)\")\n}\n```\n\n### Receive Packet\nAs with transmit (TX) packets, there are also several different types of receive (RX) packets available. But as of now, the library only allows **Receive Packet** API frame to be read.\n```swift\ndo {\n    let readingPacket = try xbee.readRFDataPacket()\n    print(\"Packet received: \\(readingPacket.frameData.receivedData)\")\n} catch let error {\n    print(\"Error: \\(error)\")\n}\n```\n\n## Note 🔎\nIf you want to better understand how ZigBee communication protocol works, or the details (in a more comprehensive way) of the most common API frames XBee counts with, you could try reading [this book](https://www.amazon.com/gp/product/0596807732?ie=UTF8\u0026tag=xbapra-20\u0026linkCode=as2\u0026camp=1789\u0026creative=9325\u0026creativeASIN=0596807732Building).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamco182%2FSwiftyXBee","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamco182%2FSwiftyXBee","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamco182%2FSwiftyXBee/lists"}