This post continues upon the last blog post, in the last post we looked at some z-wave protocol basics.
At the same time we had a look at a bitwise operation (XOR), in this post some more bitwise tricks will be used to do z-wave node discovery.
Node discovery is essential to get information about the members of a z-wave network.
The serial port sniff in the previous post, showed the FUNC_ID_SERIAL_API_GET_INIT_DATA command. Let’s dive into this packet…
Here’s the packet response again:
The first 4 bytes are not relevant for this post, I have to look into them later on. The fun starts at byte “02”, this byte represents the function response code (in this case FUNC_ID_SERIAL_API_GET_INIT_DATA). The byte following (05) is the z-wave protocol version, in this case v5. Next byte is “00” which is the controller type (primary or secondary)
“1D” indicates the amount of node bytes. 1D is 29 decimal.
Z-wave uses these node bytes to indicate whether or not a node is in use within the network (the bytes are bit masked). For example the first node byte value 03, is as follows in binary:
This means node 1 and 2 are in use, another example:
This would indicate node 4, 2 and 1 being used. So, how do we figure this out from code? This is where bitwise AND comes into play. A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0.
We can use this from our python code in the following matter:
length = ord(response) - 1 index = 1 for i in range(0, length): buffer_index = response[8+i] num = 1 j = 0 while j <= 7: if (ord(buffer_index) & num > 0): print "node exists!", index else: print "node does not exist", index index = index+1 if j < 7: num = num * 2 j = j+1
And does it work?
You have probably been using Home Assistant for quite a while, but lately you have wondered how to enable remote access to your Home Assistant installation. This is a very common question and one way...
As you might know, Google has shut down the Works with Nest program since the 31st of August 2019. Since then there was no official way to integrate Nest products with Home Assistant. Lets not talk...