GatewayH2 Unit

This library is the driver for Unit Gateway H2, and the unit communicates via UART.

Support the following products:

Unit Gateway H2

Note

When using this unit, you need to flash the NCP firmware to the unit. For details, refer to the ESP Zigbee NCP documentation.

UiFlow2 Example

Switch Control

Note

To use this example, you need to flash this program onto an ESP32C6 or similar unit as a light node device. For details, refer to HA_on_off_light

Open the cores3_switch_endpoint_example.m5f2 project in UiFlow2.

The example demonstrates group control and targeted device operation for light nodes through SwitchEndpoint of Gateway H2 unit.

UiFlow2 Code Block:

cores3_switch_endpoint_example.png

Example output:

None

MicroPython Example

Switch Control

The example demonstrates group control and targeted device operation for light nodes through SwitchEndpoint of Gateway H2 unit.

MicroPython Code Block:

  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 GatewayH2Unit
  9
 10
 11title0 = None
 12label0 = None
 13label1 = None
 14label2 = None
 15label_addr = None
 16gateway_h2_0 = None
 17gateway_h2_0_ep = None
 18device_addr = None
 19device_count = None
 20device_list = None
 21
 22
 23def first_index(my_list, elem):
 24    try:
 25        index = my_list.index(elem) + 1
 26    except:
 27        index = 0
 28    return index
 29
 30
 31def gateway_h2_0_ep_bind_event(addr):
 32    global \
 33        title0, \
 34        label0, \
 35        label1, \
 36        label2, \
 37        label_addr, \
 38        gateway_h2_0, \
 39        gateway_h2_0_ep, \
 40        device_addr, \
 41        device_count, \
 42        device_list
 43    device_addr = addr
 44    print(device_addr)
 45    if first_index(device_list, device_addr) == 0:
 46        device_list.append(device_addr)
 47        device_count = device_count + 1
 48        label_addr.setText(str((str("new device addr: ") + str(device_addr))))
 49        gateway_h2_0_ep.off(device_addr)
 50
 51
 52def btnPWR_wasClicked_event(state):
 53    global \
 54        title0, \
 55        label0, \
 56        label1, \
 57        label2, \
 58        label_addr, \
 59        gateway_h2_0, \
 60        gateway_h2_0_ep, \
 61        device_addr, \
 62        device_count, \
 63        device_list
 64    if not not len(device_list):
 65        gateway_h2_0_ep.toggle()
 66
 67
 68def setup():
 69    global \
 70        title0, \
 71        label0, \
 72        label1, \
 73        label2, \
 74        label_addr, \
 75        gateway_h2_0, \
 76        gateway_h2_0_ep, \
 77        device_addr, \
 78        device_count, \
 79        device_list
 80
 81    M5.begin()
 82    Widgets.fillScreen(0x222222)
 83    title0 = Widgets.Title("GatewayH2Unit Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
 84    label0 = Widgets.Label(
 85        "if has device connect", 2, 26, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
 86    )
 87    label1 = Widgets.Label(
 88        "press the power button toggle", 2, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
 89    )
 90    label2 = Widgets.Label(
 91        "connect device: ", 2, 90, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
 92    )
 93    label_addr = Widgets.Label("None", 5, 115, 1.0, 0x00FF00, 0x222222, Widgets.FONTS.DejaVu18)
 94
 95    BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btnPWR_wasClicked_event)
 96
 97    gateway_h2_0 = GatewayH2Unit(1, port=(1, 2))
 98    gateway_h2_0_ep = gateway_h2_0.create_switch_ep()
 99    gateway_h2_0_ep.set_bind_callback(gateway_h2_0_ep_bind_event)
100    device_count = 0
101    device_list = []
102
103
104def loop():
105    global \
106        title0, \
107        label0, \
108        label1, \
109        label2, \
110        label_addr, \
111        gateway_h2_0, \
112        gateway_h2_0_ep, \
113        device_addr, \
114        device_count, \
115        device_list
116    M5.update()
117
118
119if __name__ == "__main__":
120    try:
121        setup()
122        while True:
123            loop()
124    except (Exception, KeyboardInterrupt) as e:
125        try:
126            from utility import print_error_msg
127
128            print_error_msg(e)
129        except ImportError:
130            print("please update to latest firmware")

Example output:

None

API

GatewayH2Unit

class unit.gateway_h2.GatewayH2Unit

Create an GatewayH2Unit object.

Parameters:
  • id (int) – The UART ID for communication with the GatewayH2 Unit. It can be 1, 2.

  • port – A list or tuple containing the TX and RX pins for UART communication.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from unit import GatewayH2Unit

gateway_h2_unit = GatewayH2Unit(2, port=(1, 2))
create_switch_endpoint()

Create Switch Endpoint.

Returns SwitchEndpoint:

zigbee switch endpoint object.

Return type:

SwitchEndpoint

UiFlow2 Code Block:

init.png

MicroPython Code Block:

h2_switch_endpoint = gateway_h2_unit.create_switch_endpoint()

Refer to SwitchEndpoint for more details about SwitchEndpoint.