ZigBee network on thermometers

We need to go deeper...

We need to go deeper…

NB: This is more of a comic article, do not take what is written seriously.

In the previous article, I described how you can make ZigBee from a Bluetooth sensor and add it to the network. A number of comments were received, some of which have been corrected. Now the battery works much more economically, although there is always room for improvement.

After I managed to get the sensor to connect to the network, I began to wonder what else can be made from a thermometer? The chip itself is quite functional, it has I2C, SPI, several UARTs, and even USB. The only pity is that in the version that is in the sensor, a little of what is inside the chip is transferred to the chip pins. And the layout of the board itself also imposes restrictions if we do not want to solder to the legs of the microcontroller or wire a separate board for it. Of course, there are modules with the TLSR8258 chip on Aliexpress, TB03F, TB04, but they require a separate board or surface mounting, so this is not our option.

So what, in theory, can be done? There are a number of free pins on testpoints. You can solder a reed switch and get an opening sensor. You can short the wires in water and make a leakage sensor. Similarly, you can make a button and turn something on. These are all end devices. You can also do something more serious, for example, an impulse counter from an electricity meter or water meters. They can still sleep until the next impulse, thereby allowing them to operate on battery power.

Add Mesh

There are also Zigbee routers. These are devices that, in addition to everything that end devices do, can also transmit messages from end devices to other routers or a coordinator. They require a constant presence on the network so as not to miss messages from the end devices that have connected to them. But here battery power is no longer suitable; it will need to be powered from the mains. Here, from LYWSD03MMC, you can come up with control of a relay through a transistor and make a smart switch, which, when the pins are closed, will send a command to turn it on and off. Or connect something to the built-in UART, for example PZEM-004T and transmit current parameters. Or connect an IKEA VINDRICTNING air sensor to your smart home. In Zigby, the SDK is usually responsible for the operation of the network, so the developer just needs to define the necessary clusters and fill them with values ​​at the right time. The Telink SDK contains examples of router devices: a light bulb, a switch, because if desired can be implemented. (These are all ideas for DIY, there are no ready-made solutions yet)

Great, in our imaginary networks from LYWSD03MMC already have end devices and routers.

ZigBee coordinator

There remains room for the main node of the zigbee network – the coordinator. For most manufacturers, the coordinator, like any Zigbee device, works on its own. Those. if there is already a network, then you can simply power it and it will somehow work with the network, route it, rebuild routes. Interaction with the outside world is carried out through sending and receiving commands via UART. If we need to allow adding devices to the network, we send a command. The device has connected – a message is received from the serial port in the appropriate format. The only trouble is that each chip manufacturer has its own message format.

As for our LYWSD03MMC, in revision B1.4 it has test points

The picture is taken from the pvvx/ATC_MiThermometer project for revision B1.4

The picture is taken from the pvvx/ATC_MiThermometer project for revision B1.4

We assemble the coordinator firmware from the example in the SDK, having previously specified the correct GPIOs for UART operation and enabled control via the serial port (you can also interact via the USB built into the chip, but we don’t have it in the thermometer). And we switch the wiring from the firmware via SWS to normal RX/TX for communication with the coordinator. Now we can send commands and the coordinating thermometer will respond to us.

The Telink SDK has an application for working with a COM port, compiled for Windows, but there are source codes. The application is written in python using PySide and Qt6.

ZGC tool

ZGC tool

The program is good, but we won’t manually manage the network. We need zigbee2mqtt.

Zigbee2mqtt actually consists of several parts: the program itself, which processes events from devices and sends them to MQTT and receives commands from there, an interface, a set of converters for working with devices not made according to the standard and zigbee-herdsman, which is responsible for low-level work with adapters and interaction with z2m in a format that is pleasant to him. This is something that needs to be improved. Herdsman already includes protocols for 4 different manufacturers. This means we need to add another one to work with telink.

I took the driver for zigate as a basis, because… I had a little hand in its creation. It’s visually a little familiar to me, although it’s still a bunch of incomprehensible code. Copied, renamed. We remove everything that relates to the zigate protocol, we will need to write everything again. We launch with debug messages: something is complaining. Comment, launch again. And so on in a circle. In short, our task is to describe in Herdsman format

  1. Possible messages that the program receives from the COM port, for example, the appearance of a device on the network

  2. Parsing the message and converting it into an object that zigbee2mqtt understands

It’s the same with the commands that we send to the coordinator. For example, turn on pairing mode for 250 seconds. It is necessary to convert the message from z2m into a command understandable to the coordinator and send it to the COM port. We look at the errors in the logs, add a parser and command processing, and try further. Looking at the ZGC tool code with one eye, and at the SDK code with the other, where the actual message processing takes place, I slowly add the necessary things to the Herdsman adapter code.

What is the result? I was able to implement a small part of the commands, but this was enough for zigbee2mqtt to start using the LYWSD03MMC thermometer as a coordinator, communicating with it via UART. I was able to connect the Xiaomi MCCGQ01LM door sensor to it, and it was even able to send its status – battery and open/closed. Not bad for a thermometer!

Of course, this work cannot be called complete; it is a proof-of-concept for exploring the capabilities of this chip in the Zigbee ecosystem. But the possibilities for customization are already encouraging.

I posted the work on initial support for the Telink protocol on GitHub.
https://github.com/devbis/zigbee-herdsman-telink/. Once again I want to note that this is the most basic support, 95% of the functions will not work, but you can improve it yourself.

For those who want to repeat the experiment, you need to clone the repository, compile typescript in javascript using npm run build, and then in the working z2m installation replace node_modules/zigbee-herdsman with the contents of the folder. You also need to add “telink” to the list of adapters in the settings.schema.json file in the “adapter” field so that it works ["deconz", "zstack", "zigate", "ezsp", "telink", "auto"]. After this, you can specify telink in the configuration file and launch it.

Why these perversions?!

I was interested in exploring the capabilities of a cheap device and, perhaps, motivating someone to make custom devices based on it that can work on a Zigbee network. After all, in some cases this can cover some of the issues that are solved by the popular ESP8266/32 and save someone from the wires needed to power the fast. True, there are no ready-made auto assemblers for this microcontroller, such as ESPHome or Tasmota, so the entry threshold will be higher. Of course, I don’t expect that this will encourage anyone to change their time-tested sticks and gateways to coordinators based on Telink chips. But zigbee2mqtt once started with support for a single stack from Texas Instruments and other adapters were added later. So anything is possible. 🙂

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *