rbaron / b-parasite

🌱💧 An open source DIY soil moisture sensor
1.85k stars 143 forks source link

Introduce an experimental/educactional/exploratory Zigbee firmware sample #81

Closed rbaron closed 1 year ago

rbaron commented 1 year ago

This sample is mostly adapted from the zigbee template from the nRF Connect SDK. It's a basic/educational/experimental/exploratory/fun firmware sample for b-parasite.

Fixes #56.

Tasks

Zigbee firmware sample

This sample is adapted from the zigbee_template from the nRF Connect SDK.

Clusters

These clusters are defined in the sample:

Cluster ID Name
0x0001 Power Configuration
0x0400 Illuminance Measurement
0x0402 Temperature Measurement
0x0405 Relative Humidity Measurement
0x0408 Soil Moisture Measurement

Pairing Mode

The sample will first boot and start looking for a Zigbee coordinator - in pairing mode. The onboard LED will be flashing once a second while in this mode. Once a suitable network is found, the LED will briefly flash 3 times and remain off.

Factory Reset

Most Zigbee devices provide a physical button to "factory reset" it - causing it to forget its joined network and look for a new one.

b-parasite has no physical buttons, and the implemented work around is to distinguish between two reset modes:

Power up mode

The device enters this mode when it is powered. For example, swapping an old battery or connecting to eternal power. This is the "usual" reset mode, and joined networks will be remembered.

Reset pin mode

If the device's RESET pin is briefly grounded, the device will effectively be factory reset. The device will leave its previous network and start looking for a new one.

Configs

Available options in Kconfig. Notable options:

Zigbee2MQTT & Home Assistant

This firmware sample has been tested with Zigbee2MQTT, an open source Zigbee bridge that connects seamlessly with Home Assistant.

The b-parasite.js file in this PR contains a converter that can be installed to Zigbee2MQTT to suppoort this sample. See Support new devices for instructions.

Here's how it looks in the Zigbee2MQTT addon inside Home Assistant:

image

Zigbee Home Automation (ZHA) and Home Assistant

The firmware sample has also been tested with the ZHA Home Assistant integration. With ZHA, no custom device parser is needed, and all sensor clusters are identified by ZHA out of the box.

Here is how b-parasite looks after being paired with ZHA:

image

Battery Life

While sleeping, the device consumes around 2 uA: sleeping

In the active cycle, it averages around 125 uA for 1 second: active

Building

Same instructions as in the Wiki.

Prebuilt binaries

There's now a zigbee_nrf52840_default.hex automatically built as part of the actions for this branch.

rbaron commented 1 year ago

Bonus pic: it's pretty cool to see the humble b-parasite among the heavyweights 😊

image
drspangle commented 1 year ago

This is pretty nifty. Do you expect that this will have a significant power savings over the BTLE-flavored firmware, or any other side benefits?

rbaron commented 1 year ago

Do you expect that this will have a significant power savings over the BTLE-flavored firmware, or any other side benefits?

The only power measurements I've done are the ones I shared in the PR description. I think both would last a couple of years, but real long term testing would be ideal.

Hedda commented 1 year ago

Nice! If keeps to official Zigbee specs then it should hopefully also work seamlessly with Home Assistant's official ZHA Integration:

https://www.home-assistant.io/integrations/zha

https://www.home-assistant.io/integrations/zha#knowing-which-devices-are-supported

https://github.com/zigpy/zha-device-handlers/blob/dev/README.md#what-are-these-specifications

What are these specifications

If deviating from the official Zigbee specs then may need a "quirk" in “ZHA Device Handlers" (which works similarly to a converter):

https://github.com/zigpy/zha-device-handlers

https://github.com/zigpy/zha-device-handlers/blob/dev/README.md

Note that while Home Assistant's official ZHA Integration has a smaller community than Zigbee2MQTT it is still quite popular:

https://analytics.home-assistant.io/integrations/

(Home Assistant Analytics is only registered via opt-in so Nabu Casa founders estimate that statistics only how about 1/5 of users).

PS: Another unrelated tip is that it can be a good idea to test with both Texas Instruments and Silicon Labs Zigbee Coordinator adapters if consider setting up a rig for regression testing as different Zigbee stacks can have a few odd compatibility bugs. Ex:

https://itead.cc/product/sonoff-zigbee-3-0-usb-dongle-plus/ <- Texas Instruments CC2652P based with Z-Stack firmware.

https://itead.cc/product/zigbee-3-0-usb-dongle/ <- Silicon Labs EFR32MG21 based with EmberZNet firmware.

rbaron commented 1 year ago

@Hedda I am surprised and super happy that it worked out of the box with ZHA (I have a CC2531 USB stick) 🚀

Here's what ZHA finds once you tap "add devices" in the ZHA coordinator inside the Home Assistant integration:

image

And then the b-parasite is automatically detected with all its sensors (no custom device parser needed as in Zigbee2MQTT):

image

I don't have any other Zigbee coordinator hardware, but I would say these tests are enough for a first experimental firmware.

Hedda commented 1 year ago

@Hedda I am surprised and super happy that it worked out of the box with ZHA (I have a CC2531 USB stick) 🚀 ... I don't have any other Zigbee coordinator hardware, but I would say these tests are enough for a first experimental firmware.

FYI, Home Assistant's ZHA integration is dependent on zigpy (open source Python library) which is a hardware-independent Zigbee stack that acts as an abstraction layer for manufacturer-specific Zigbee Coordinator adapters/firmware, so if works with one brand of Zigbee Coordinator adapters then it should in theory also work the same with all other brands Zigbee Coordinator adapters, including those based on SoC's and firmware from different manufacturers, and long as its Zigbee Coordinator radio interface is supported by zigpy.

https://github.com/zigpy/zigpy

The exception might be if the firmware Zigbee Coordinator adapter does not support Zigbee 3.0 and your device is using a Zigbee 3.0 profile. I am guessing that your CC2531 USB stick probably does not have Zigbee 3.0 firmware as it is old obsolete hardware that does not officially support it. Highly recommend also buying a newer Zigbee Coordinator so that you can test both an old Zigbee Coordinator that do not support Zigbee 3.0 and a new that does support Zigbee 3.0 specifications.

By the way, two other Zigbee implementations that are also based on the zigpy library is the "Zigbee Plugin for Domoticz" and the "Zigbee Plugin for Jeedom"

Zigbee Plugin for Domoticz

https://www.domoticz.com/wiki/ZigbeeForDomoticz https://github.com/zigbeefordomoticz/Domoticz-Zigbee/ https://zigbeefordomoticz.github.io/wiki/

Zigbee Plugin for Jeedom

https://doc.jeedom.com/en_US/plugins/automation%20protocol/zigbee/ https://doc.jeedom.com/en_US/plugins/automation%20protocol/zigbee/changelog https://market.jeedom.com/index.php?v=d&p=market_display&id=4050 https://blog.jeedom.com/5183-tout-ce-quil-faut-savoir-sur-le-plugin-officiel-zigbee/ https://blog.jeedom.com/category/protocole/zigbee/

PS: Another very popular open-source home automation with built-in Zigbee ("binding") implementation is OpenHAB:

https://www.openhab.org/ https://www.openhab.org/addons/bindings/zigbee/

Hedda commented 1 year ago

Here's what ZHA finds once you tap "add devices" in the ZHA coordinator inside the Home Assistant integration:

image

And then the b-parasite is automatically detected with all its sensors (no custom device parser needed as in Zigbee2MQTT):

image

Home Asssistant's ZHA integration should also show battery level but does not due to bug @oleo65 mentioned in PR or? -> https://github.com/rbaron/b-parasite/pull/84

rbaron commented 1 year ago

@oleo65 FYI I found a reasonable solution to add a Kconfig to prstlib. With this approach we can even use the "subsys/logging/Kconfig.template.log_config" to setup logging config just like most Zephyr subsystems.

Samples that rely on prstlib should now add rsource "../../prstlib/Kconfig", and all prstlib configs are available in the sample's Kconfig/prj.conf. I've updated all the samples and it seems to work okay.

oleo65 commented 1 year ago

Awesome, this looks really neat. This is scalable and concise with the Zephyr logging. Good job digging that up.

I really like the way this PR is going. 😊

Hedda commented 1 year ago

The b-parasite.js file in this PR contains a converter that can be installed to Zigbee2MQTT to support this sample. See Support new devices for instructions.

@rbaron Will you submit your b-parasite.js converter file as a PR to zigbee-herdsman-converters repo for inclusion by default?

https://github.com/Koenkk/zigbee-herdsman-converters

@stanvn if you base your firmware on the same code then you should be able to extend that converter file with your ID too?

https://github.com/rbaron/b-parasite/tree/main/code/nrf-connect/samples/zigbee

https://github.com/stanvn/zigbee-plant-sensor