Zigbee Unit

Zigbee is a self-organizing network communication module launched by M5Stack. The module adopts the CC2630F128 solution, integrates the Zigbee protocol stack internally, and provides an open serial communication interface. It features an external antenna, with a stable single-node communication distance of up to 1 km and supports up to 200 levels of router depth. Through MESH networking, it can significantly extend the range of your IoT applications, offering both ultra-low power consumption and high sensitivity. The Zigbee network can support hundreds of nodes and has enhanced security features, providing a complete and interoperable IoT solution for home and building automation.

Support the following products:

ZigbeeUnit

Micropython TX Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import ZigbeeUnit
 9import time
10
11
12label0 = None
13zigbee_0 = None
14
15
16def setup():
17    global label0, zigbee_0
18
19    M5.begin()
20    Widgets.fillScreen(0x222222)
21    label0 = Widgets.Label("label0", 67, 43, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
22
23    zigbee_0 = ZigbeeUnit(1, port=(18, 17), verbose=True)
24    zigbee_0.set_module_param(
25        ZigbeeUnit.DEVICE_TYPE_COORDINATOR,
26        0x1620,
27        11,
28        ZigbeeUnit.TRANSFER_MODE_PASS_THROUGH,
29        0x6677,
30    )
31    label0.setText(str("start"))
32
33
34def loop():
35    global label0, zigbee_0
36    M5.update()
37    zigbee_0.p2p_transmission(0x0066, "p2p")
38    time.sleep(3)
39    zigbee_0.broadcast("broadcast")
40    time.sleep(3)
41
42
43if __name__ == "__main__":
44    try:
45        setup()
46        while True:
47            loop()
48    except (Exception, KeyboardInterrupt) as e:
49        try:
50            from utility import print_error_msg
51
52            print_error_msg(e)
53        except ImportError:
54            print("please update to latest firmware")

Micropython RX Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import ZigbeeUnit
 9
10
11label0 = None
12zigbee_0 = None
13
14
15zigbee_str_data = None
16zigbee_dest_address = None
17zigbee_src_address = None
18
19
20def zigbee_0_receive_event(dest_address, src_address, received_data):
21    global label0, zigbee_0, zigbee_str_data, zigbee_dest_address, zigbee_src_address
22    zigbee_dest_address = dest_address
23    zigbee_src_address = src_address
24    try:
25        zigbee_str_data = received_data.decode()
26    except:
27        zigbee_str_data = str(received_data)
28    label0.setText(str(zigbee_str_data))
29
30
31def setup():
32    global label0, zigbee_0, zigbee_str_data, zigbee_dest_address, zigbee_src_address
33
34    M5.begin()
35    label0 = Widgets.Label("label0", 21, 83, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
36
37    zigbee_0 = ZigbeeUnit(1, port=(33, 32), verbose=True)
38    zigbee_0.set_module_param(
39        ZigbeeUnit.DEVICE_TYPE_ROUTER, 0x1620, 11, ZigbeeUnit.TRANSFER_MODE_PASS_THROUGH, 0x0066
40    )
41    while not (zigbee_0.isconnected()):
42        pass
43    label0.setText(str(zigbee_0.get_custom_address()))
44    zigbee_0.receive_none_block(zigbee_0_receive_event)
45
46
47def loop():
48    global label0, zigbee_0, zigbee_str_data, zigbee_dest_address, zigbee_src_address
49    M5.update()
50
51
52if __name__ == "__main__":
53    try:
54        setup()
55        while True:
56            loop()
57    except (Exception, KeyboardInterrupt) as e:
58        try:
59            from utility import print_error_msg
60
61            print_error_msg(e)
62        except ImportError:
63            print("please update to latest firmware")

UIFLOW2 TX Example:

tx_example.png

UIFLOW2 RX Example:

rx_example.png

cores3_zigbee_tx_example.m5f2

stickc_plus2_zigbee_rx_example.m5f2

class ZigbeeUnit

Constructors

class ZigbeeUnit(id: Literal[0, 1, 2], port: list | tuple, verbose: bool = True)

Create a Zigbee unit.

Parameters:
  • id – The ID of the unit.

  • port – The port that the unit is connected to.

  • verbose – Print the log information. Default is True.

UIFLOW2:

init.png

Methods

ZigbeeUnit.set_module_param(device_type: int, pan_id: int, channel: int, transfer_mode: int, custom_address: int, ant_type: int, encryption_enable=ENCRYPTION_ENABLE, encryption_key=b'\x11\x12\x13\x14', node_type=DEVICE_TYPE_ROUTER, node_ant_type=ANT_TYPRE_ON_BOARD, node_transfer_mode=TRANSFER_MODE_PASS_THROUGH, node_custom_address=0x0066)
Parameters:
  • device_type (int) – The device type of the Zigbee module.

  • pan_id (int) – The PAN ID of the Zigbee module. The PAN ID is a 16-bit value that is used to identify the network.

  • channel (int) – The channel of the Zigbee module. The channel range is from 11 to 26

  • transfer_mode (int) – The transfer mode of the Zigbee module.

  • custom_address (int) – The custom address of the Zigbee module.

  • ant_type (int) – The antenna type of the Zigbee module.

  • encryption_enable (int) – The encryption status of the Zigbee module.

  • encryption_key (bytes) – The encryption key of the Zigbee module.

  • node_type (int) – The node type of the Zigbee module.

  • node_ant_type (int) – The antenna type of the Zigbee node.

  • node_transfer_mode (int) – The transfer mode of the Zigbee node.

  • node_custom_address (int) – The custom address of the Zigbee node.

Set the parameters of the Zigbee module.

UIFLOW2:

set_module_param.png

set_module_param1.png

set_module_param2.png

ZigbeeUnit.set_device_type(device_type: int)
Parameters:

device_type (int) – The device type of the Zigbee module.

Set the device type of the Zigbee module.

UIFLOW2:

set_device_type.png

ZigbeeUnit.set_pan_id(pan_id: int)
Parameters:

pan_id (int) – The PAN ID of the Zigbee module.

Set the PAN ID of the Zigbee module.

UIFLOW2:

set_pan_id.png

ZigbeeUnit.set_channel(channel: int)
Parameters:

channel (int) – The channel of the Zigbee module.

Set the channel of the Zigbee module.

UIFLOW2:

set_channel.png

ZigbeeUnit.set_transfer_mode(transfer_mode: int)
Parameters:

transfer_mode (int) – The transfer mode of the Zigbee module.

Set the transfer mode of the Zigbee module.

UIFLOW2:

set_transfer_mode.png

ZigbeeUnit.get_custom_address() int

Get the custom address of the Zigbee module.

Returns:

The custom address of the Zigbee module.

UIFLOW2:

get_custom_address.png

ZigbeeUnit.set_custom_address(custom_address: int)
Parameters:

custom_address (int) – The custom address of the Zigbee module.

Set the custom address of the Zigbee module.

UIFLOW2:

set_custom_address.png

ZigbeeUnit.set_ant_type(ant_type: int)
Parameters:

ant_type (int) – The antenna type of the Zigbee module.

Set the antenna type of the Zigbee module.

UIFLOW2:

set_ant_type.png

ZigbeeUnit.get_short_address() int

Get the short address of the Zigbee module.

Returns:

The short address of the Zigbee module.

UIFLOW2:

get_short_address.png

ZigbeeUnit.isconnected() bool

Check whether the Zigbee module is connected to the Zigbee network.

Returns:

True if the Zigbee module is connected to the Zigbee network, False otherwise.

UIFLOW2:

isconnected.png

ZigbeeUnit.receive_none_block(receive_callback)
Parameters:

receive_callback – The callback function that is called when the Zigbee module receives data.

Receive data from the Zigbee module.

UIFLOW2:

receive_none_block.png

receive_data_str_event.png

receive_data_event.png

ZigbeeUnit.stop_receive()

Stop receiving data from the Zigbee module.

UIFLOW2:

stop_receive.png

ZigbeeUnit.p2p_transmission(address: int, data: bytes)
Parameters:
  • address (int) – The custom address of the Zigbee module that the data is sent to.

  • data (bytes) – The data that is sent to the Zigbee module.

Send data to the Zigbee module.

UIFLOW2:

p2p_transmission.png

ZigbeeUnit.broadcast(data: bytes)
Parameters:

data (bytes) – The data that is sent to the Zigbee module.

Broadcast data to all Zigbee modules.

UIFLOW2:

broadcast.png