En route to a smart home with the Z-Wave protocol

On and Off

To turn on the energy switch, Listing 1 sends a byte sequence of 0x00 0x13 as the payload, followed by the node number for the target device to the PC's USB port interface. The protocol then needs 0x03, 0x20, 0x01 followed by the switch's dimmer setpoint (0=off, 255=on) and by a byte with a value of 0x05.

As you can see in the output in Figure 5 from switching on and off, the CPAN module creates a packet from the payload by prepending a header with a value of 0x01, followed by the number of following bytes, rounded off by a checksum (0x3e or 0xc1; Figure 5). The Z-Wave protocol computes the checksum by XORing all the bytes in the packet, but without the first header byte.

Figure 5: The script in Listing 1 controls a Z-Wave consumer (on/off) and also prints out the bytes transmitted.

The checksum is then negated and appended to the packet so that the receiver can check the message for bit flips in the wireless transmission. The checksum is one of the protocol's weak spots; given only 255 different values and the simple logic, it is unable to detect more than the most simple error conditions.

The script in Listing 1 initializes Log4perl with a log level of $DEBUG, which explains why the bytes are printed out. If you don't do this, the whole process is non-verbose.

Handshake for Reliability

One positive aspect of the Z-Wave protocol is the handshake method where the receiver always returns an ACK packet to the transmitter to tell it that the message arrived and the corresponding action was triggered. If the controller is switching on a water pump, the application will almost certainly want to know whether the action succeeded and the valve is now open or whether something has gone wrong, so it can take corrective action if necessary.

Z-Wave can do much more than just switch consumers on and off, however. The energy switch can be configured to transmit packets regularly that show how much power is currently crossing the wire. The control software can thus trigger actions, keep track of them, check the power consumption and how much it costs, and – if needed – alert you if the switch was manually unplugged and stopped sending reports.

Lock and Key

Additionally, some Z-Wave sensors and actuators can even do without a mains power source – for example, you can purchase wireless door locks. To avoid the battery discharging too quickly, the Z-Wave protocol needs to make sure that the lock repeatedly wakes up, checks whether a signal is present, and goes back to sleep immediately if there is nothing on the airwaves. If it does discover a signal, however, the electronics start to investigate. If the signal comes from a controller that it trusts, the actuator responds to the transmitted commands. Z-Wave also extends a controller's range with a routing protocol that gives other Z-Wave components the ability to forward the signal until it is in range of the target actuator, which can then respond.

For further reading, you might like to pick up Z-Wave Basics: Remote Control in Smart Homes. This must have been written by a German author – no one else would print the details of their PhD on the spine – and some Amazon reviewers have complained about various grammatical errors and strange language. It's true and should have been corrected, but it's worth a read anyway. The Kindle version has been updated compared with the paperback edition and contains some revised chapters [8].

The book describes the historic development of the protocol and discusses the advantages and disadvantages of competing approaches. Specialists will enjoy the precise descriptions of the technical details of the protocol, although the book does not discuss the individual byte sequences. You will either need to contact the Z-Wave distributor, Sigma Designs, and purchase the SDK for these, or try the free OpenZWave [9] project.

Mike Schilli

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.


  1. "Perl: X10 Module" by Mike Schilli, Linux Magazine, issue 78, May 2007, pg. 72: http://perlmeister.com/lme/prod-0705.pdf
  2. "Perl: Linux-based Gardening" by Mike Schilli, Linux Magazine, issue 77, April 2007, pg. 68: http://perlmeister.com/lme/prod-0704.pdf
  3. Home automation: https://en.wikipedia.org/wiki/Home_automation
  4. Aeon Labs DSA02203-ZWUS Z-Wave Z-Stick Series 2 USB Dongle: http://www.amazon.com/gp/product/B003MWQ30E
  5. Aeon Labs DSC06106-ZWUS Z-Wave Smart Energy Switch: http://www.amazon.com/gp/product/B007UZH7B8
  6. Test script for actuating the Z-Wave USB Dongle with Perl: http://www.bigsister.ch/zwave/zwave_s
  7. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/184
  8. Paetz, Christian. Z-Wave Basics: Remote Control in Smart Homes, CreateSpace, 2013: http://www.amazon.com/dp/1490537368
  9. OpenZWave project: http://www.openzwave.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Z-Wave

    Z-Wave connects components in the smart home and ensures that remote commands from the control station take effect on real household appliances. We look at how it works, its range, the security of the protocol, and some basics that every Z-Waver should know.

  • Programming Snapshot – Power Outages

    A power failure can cause the IQ of a smart home to plummet suddenly. An emergency power supply and a script on the SmartThings platform can prevent a total outage and inform the owner. The polyglot Perlmeister embarks on a foray into the territory of the Groovy scripting language in this issue.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95