From dd1cc00c9d6179ccd5a5985cac0f750078d26cf1 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 25 Dec 2020 23:08:42 +0100 Subject: [PATCH] Added Zigbee plugin based on Zigbee4java --- plugins/hal-zigbee/README.md | 56 +++++++++ plugins/hal-zigbee/build.gradle | 5 + .../plugin/zigbee/HalZigbeeController.java | 111 ++++++++++++++++++ .../src/se/hal/plugin/zigbee/plugin.json | 8 ++ 4 files changed, 180 insertions(+) create mode 100644 plugins/hal-zigbee/README.md create mode 100644 plugins/hal-zigbee/build.gradle create mode 100644 plugins/hal-zigbee/src/se/hal/plugin/zigbee/HalZigbeeController.java create mode 100644 plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json diff --git a/plugins/hal-zigbee/README.md b/plugins/hal-zigbee/README.md new file mode 100644 index 00000000..11df18bb --- /dev/null +++ b/plugins/hal-zigbee/README.md @@ -0,0 +1,56 @@ +# Plugin Configuration + +|Config Parameter |Value |Description | +|-----------------|-----------------------------|------------| +|zwave.com_port |Name or location of com port |The port where radio dongle is connected| + +# Hardware + +You need to buy the following items: + +1. http://www.ti.com/tool/cc2531emk + * Example item: + https://www.aliexpress.com/item/4001091370032.html +2. http://www.ti.com/tool/cc-debugger + * Example kit: + https://www.aliexpress.com/item/4001095299084.html + +## Flashing Dongle +Instructions are originally from: https://www.zigbee2mqtt.io/information/flashing_the_cc2531.html + +### Windows +1. Download and install the "SmartRF Flash Programmer" **(Not v2)** application from https://www.ti.com/tool/FLASH-PROGRAMMER + + +2. Connect CC debugger to the CC2531 USB sniffer. +3. Connect **BOTH** the CC2531 USB sniffer and the CC debugger to your PC using USB. + * If the light on the CC debugger is RED press set reset button on the CC debugger. The light on the CC debugger should now turn GREEN. If not use [CC debugger user guide](http://www.ti.com/lit/ug/swru197h/swru197h.pdf) to troubleshoot your problem. +4. Download the firmware [CC2531_DEFAULT_20190608.zip](https://github.com/Koenkk/Z-Stack-firmware/raw/master/coordinator/Z-Stack_Home_1.2/bin/default/CC2531_DEFAULT_20190608.zip) +4. Start SmartRF Flash Programmer, with the properties below: + * Flash image: Make sure to select the .hex file and not the .bin file. + * Uncheck "Retain IEEE address when reprogramming the chip. + +### Linux or MacOS + +1. Install prerequisites for CC-Tool using a package manager (e.g. Homebrew for macOS) + * Ubuntu/Debian: libusb-1.0-0-dev, libboost-all-dev, autoconf, libtool + * Fedora: dh-autoreconf, boost-devel, libusb1-devel, gcc-c++ + * Archlinux: dh-autoreconf, libusb, boost + * macOS: brew install autoconf automake libusb boost pkgconfig libtool + * Raspbian: dh-autoreconf, libusb-1.0-0-dev, libboost-all-dev + +2. Build cc-tool + + git clone https://github.com/dashesy/cc-tool.git + cd cc-tool + ./bootstrap + ./configure + make + +3. Connect **BOTH** the CC2531 USB sniffer and the CC debugger to your PC using USB. + * If the light on the CC debugger is RED press set reset button on the CC debugger. The light on the CC debugger should now turn GREEN. If not use [CC debugger user guide](http://www.ti.com/lit/ug/swru197h/swru197h.pdf) to troubleshoot your problem. +4. Download the firmware [CC2531_DEFAULT_20190608.zip](https://github.com/Koenkk/Z-Stack-firmware/raw/master/coordinator/Z-Stack_Home_1.2/bin/default/CC2531_DEFAULT_20190608.zip) +5. Flash your firmware: + + sudo ./cc-tool -e -w CC2531ZNP-Prod.hex + diff --git a/plugins/hal-zigbee/build.gradle b/plugins/hal-zigbee/build.gradle new file mode 100644 index 00000000..8d1dcdde --- /dev/null +++ b/plugins/hal-zigbee/build.gradle @@ -0,0 +1,5 @@ +dependencies { + implementation project(':hal-core') + + implementation 'org.bubblecloud.zigbee4java:zigbee-api:3.0.3' +} diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/HalZigbeeController.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/HalZigbeeController.java new file mode 100644 index 00000000..2888c099 --- /dev/null +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/HalZigbeeController.java @@ -0,0 +1,111 @@ +package se.hal.plugin.zigbee; + +import se.hal.HalContext; +import se.hal.intf.*; +import se.hal.struct.AbstractDevice; +import zutil.log.LogUtil; + +import java.util.List; +import java.util.logging.Logger; + +/** + * + */ +public class HalZigbeeController implements HalSensorController, HalEventController, HalAutoScannableController { + private static final Logger logger = LogUtil.getLogger(); + + private static final String CONFIG_ZIGBEE_PORT = ""; + + private SerialPort port; + private ZigBeeApiDongleImpl zigbeeApi; + + private HalSensorReportListener sensorListener; + private HalEventReportListener eventListener; + private List registeredDevices; + + + public HalZigbeeController() {} + + + @Override + public boolean isAvailable() { + return HalContext.containsProperty(CONFIG_ZIGBEE_PORT); + } + @Override + public void initialize() { + initialize(HalContext.getStringProperty(CONFIG_ZIGBEE_PORT)); + } + public void initialize(String comPort) { + byte[] networkKey = null; // Default network key + port = new SerialPortImpl(comPort); + zigbeeApi = new ZigBeeApiDongleImpl( + new ZigBeeDongleTiCc2531Impl(port, 4951, 11, networkKey, false), + false); + + zigbeeApi.startup(); + + ZigBeeDevice device = zigbeeApi.getZigBeeDevices().get(3); + + zigbeeApi.on(device); + Thread.sleep(1000); + zigbeeApi.color(device, 1.0, 0.0, 0.0, 1.0); + Thread.sleep(1000); + zigbeeApi.color(device, 0.0, 1.0, 0.0, 1.0); + Thread.sleep(1000); + zigbeeApi.color(device, 0.0, 0.0, 1.0, 1.0); + Thread.sleep(1000); + zigbeeApi.off(device); + } + + @Override + public void close() { + logger.info("Shutting down Zigbee port..."); + + zigbeeApi.shutdown(); + port.close(); + } + + + // -------------------------- + // Hal Overrides + // -------------------------- + + @Override + public void register(HalEventConfig event) { + + } + @Override + public void register(HalSensorConfig sensor) { + + } + + @Override + public void deregister(HalEventConfig event) { + registeredDevices.remove(event); + } + @Override + public void deregister(HalSensorConfig sensor) { + registeredDevices.remove(sensor); + } + + @Override + public int size() { + return registeredDevices.size(); + } + + @Override + public void send(HalEventConfig eventConfig, HalEventData eventData) { + + } + + @Override + public void setListener(HalEventReportListener listener) { + eventListener = listener; + } + @Override + public void setListener(HalSensorReportListener listener) { + sensorListener = listener; + } + + +} diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json new file mode 100644 index 00000000..79e46d95 --- /dev/null +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json @@ -0,0 +1,8 @@ +{ + "version": 0.1, + "name": "Hal-Zigbee", + "description": "A Zigbee plugin for directly connecting to a CC2531 device over serial port.", + "interfaces": [ + {"se.hal.intf.HalAutoScannableController": "se.hal.plugin.zigbee.deconz.zigbee.HalDeconzZigbeeController"} + ] +} \ No newline at end of file