|
1 |
| -# SwiftXBee |
2 |
| -⚡️ A Swift library for communicating with XBee radios in API mode |
| 1 | +# SwiftyXBee |
| 2 | + |
| 3 | +⚡️A Swift library for communicating with XBee (ZigBee Series 2) radios in API mode. |
| 4 | +<p> |
| 5 | +<img src="https://img.shields.io/badge/Architecture%20-ARMv6%20%7C%20%20ARMv7%2F8-red.svg"/> |
| 6 | +<img src="https://img.shields.io/badge/OS-Raspbian%20%7C%20Debian%20%7C%20Ubuntu-yellow.svg"/> |
| 7 | +<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift-4x-brightgreen.svg"/></a> |
| 8 | +<a href="https://raw.githubusercontent.com/samco182/SwiftySHT20/master/LICENSE"><img src="https://img.shields.io/badge/Licence-MIT-blue.svg" /></a> |
| 9 | +</p> |
| 10 | +<img 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"> |
| 11 | + |
| 12 | +## Summary |
| 13 | +This is a [SwiftyGPIO](https://github.com/uraimo/SwiftyGPIO) based library for communicating with XBee radios in **API mode**, with support for Series 2 **only**. |
| 14 | + |
| 15 | +This 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 RX/TX 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:. |
| 16 | + |
| 17 | +For more information regarding the RF module, you can consult its [datasheet](https://www.digi.com/resources/documentation/digidocs/pdfs/90002002.pdf). |
| 18 | + |
| 19 | +## API Mode |
| 20 | +XBees 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. |
| 21 | + |
| 22 | +According to the documentation: |
| 23 | +> 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: |
| 24 | +> - AP = 1: API operation. |
| 25 | +> - AP = 2: API operation (with escaped characters—only possible on UART). |
| 26 | +
|
| 27 | +⚠️ 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. |
| 28 | + |
| 29 | +## Hardware Details |
| 30 | +- The XBee should be powered using **3.3 V**. |
| 31 | +- 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. |
| 32 | + |
| 33 | +This is the pin assignment to manually connect (no XBee shield) the device to any of the compatible boards: |
| 34 | + |
| 35 | +| XBee Pin | Board UART | |
| 36 | +| ------------------------ | --------------- | |
| 37 | +| VCC = **1** | VCC 3.3V | |
| 38 | +| DOUT = **2** | RX / Receive | |
| 39 | +| DIN / CONFIG = **3** | TX / Transmit | |
| 40 | +| GND = **10** | GND | |
| 41 | + |
| 42 | +The 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. |
| 43 | + |
| 44 | +## Supported Boards |
| 45 | +Every board supported by [SwiftyGPIO](https://github.com/uraimo/SwiftyGPIO): RaspberryPis, BeagleBones, C.H.I.P., etc... |
| 46 | + |
| 47 | +To use this library, you'll need a Linux ARM board running [Swift 4.x](https://github.com/uraimo/buildSwiftOnARM) 🚗. |
| 48 | + |
| 49 | +The example below will use a Raspberry Pi 3B+ board, but you can easily modify the example to use one of the other supported boards. A full working demo project for the RaspberryPi3B+ is available in the **Example** directory. |
| 50 | + |
| 51 | +## Installation |
| 52 | +First of all, makes sure your board is running **Swift 4.x** ⚠️! |
| 53 | + |
| 54 | +Since Swift 4.x supports Swift Package Manager, you only need to add SwiftXBee as a dependency in your project's `Package.swift` file: |
| 55 | + |
| 56 | +```swift |
| 57 | +let package = Package( |
| 58 | + name: "MyProject", |
| 59 | + dependencies: [ |
| 60 | + .package(url: "https://github.com/samco182/SwiftyXBee", from: "1.0.0"), |
| 61 | + ] |
| 62 | + targets: [ |
| 63 | + .target( |
| 64 | + name: "MyProject", |
| 65 | + dependencies: ["SwiftyXBee"]), |
| 66 | + ] |
| 67 | +) |
| 68 | +``` |
| 69 | +Then run `swift package update` to install the dependency. |
| 70 | + |
| 71 | +## Usage |
| 72 | +### Initialization |
| 73 | +The 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: |
| 74 | + |
| 75 | +```swift |
| 76 | +import SwiftyXBee |
| 77 | + |
| 78 | +let serial = SerialConnection(speed: .S9600, bitsPerChar: .Eight, stopBits: .One, parity: .None) |
| 79 | +let xbee = SwiftyXBee(for: .RaspberryPi3, serialConnection: serial) |
| 80 | +``` |
| 81 | +You can also initialize the `XBee` object with the following method: |
| 82 | +```swift |
| 83 | +import SwiftyXBee |
| 84 | + |
| 85 | +let xbee = SwiftyXBee() |
| 86 | +``` |
| 87 | +This initializer defaults to `.RaspberryPi3` as the selected board and serial connection with `speed: .S9600`, `bitsPerChar: .Eight`, `stopBits: .One`, and `parity: .None)`. |
| 88 | + |
| 89 | +### Transmit Packets |
| 90 | +There 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. |
| 91 | +```swift |
| 92 | +// The destination XBee 64-bit serial number |
| 93 | +let deviceAddress = DeviceAddress(address: 0x0013A20012345678) |
| 94 | + |
| 95 | +// The destination XBee 16-bit network address |
| 96 | +let networkAddress = NetworkAddress(address: 0xFFFE) |
| 97 | + |
| 98 | +// The actual message to be sent |
| 99 | +let message = "This is my message to send!" |
| 100 | + |
| 101 | +xbee.sendTransmitRequest(to: deviceAddress, network: networkAddress, message: message) |
| 102 | +``` |
| 103 | +When a Transmit Request completes, the device sends 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: |
| 104 | +```swift |
| 105 | +do { |
| 106 | + let transmitStatus = try xbee.readTransmitStatus() |
| 107 | + print("Status received: \(readingPacket.frameData.deliveryStatus)") |
| 108 | +} catch let error { |
| 109 | + print("Error: \(error)") |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Receive Packet |
| 114 | +As 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. |
| 115 | +```swift |
| 116 | +do { |
| 117 | + let readingPacket = try xbee.readRFDataPacket() |
| 118 | + print("Packet received: \(readingPacket.frameData.receivedData)") |
| 119 | +} catch let error { |
| 120 | + print("Error: \(error)") |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Note 🔎 |
| 125 | +If 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&tag=xbapra-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0596807732Building). |
0 commit comments