Wireless Protocol VI: Essentials of Zigbee
1. Introduction | 2. Objectives | 3. Basic Concepts | 4. Glossary | Related Components | Take the Quiz
Zigbee is a popular communication protocol used in many modern sensor and control networks, including home and industrial automation. Zigbee was developed as an open connectivity standard for wireless IoT networks. Its growing popularity stems from a variety of useful features, including low latency, support for multiple network topologies, and low power consumption; some devices can have battery life lasting several years. Wireless devices that use Zigbee operate in unlicensed radio bands, including 868 MHz, 915 MHz, and 2.4 GHz frequency bands. This learning module is an introduction to the Zigbee protocol, spanning the technology, architecture, applications, and development tools.
For more information on Home Automation, take a look at our eBook: Home Automation with the Raspberry Pi.
2. Objectives
Upon completion of this module, you will be able to:
- Describe Zigbee technology and communication protocols
- Understand the concepts underlying the technology
- Discuss various applications of Zigbee
- Discuss Zigbee development, including hardware and software
The Zigbee network layer supports star, tree, and mesh topologies. In mesh and tree topologies, the Zigbee coordinator forms the personal area network (PAN) and chooses specific key network parameters, such as assigning addresses. Mesh networks allow full peer-to-peer communication.
Figure 1: Zigbee Topology
- Analysis
The Zigbee architecture is a protocol stack with five layers.
- Application Layer
- Security Layer
- Network Layer
- Medium Access Control (MAC) Layer
- Physical (PHY) Layer
- Physical (PHY) layer: The PHY layer is the lowest layer and closest to the hardware. This layer directly controls and communicates with the Zigbee radio. The physical layer translates the data packets in over-the-air bits for transmission and vice-versa during reception.
- Medium Access Control (MAC) layer: The MAC layer works as an interface between the network and physical layer, controlling access and resolving conflict between competing nodes. The MAC layer provides PAN ID and also network discovery via beacon requests.
- Network Layer: The Network Layer is responsible for routing and establishing the different Zigbee topologies. The network layer acts as an interface between the MAC and application layers.
- Security Layer: The Security Layer handles encryption; with security enabled, data for Zigbee devices is encrypted with a 128-bit AES (Advanced Encryption Standard) network key.
- Application Layer: The Application Layer (or Application Framework) is the highest protocol layer in the Zigbee stack. It comprises the Application Support (APS) sub-layer and Zigbee Device Object (ZDO). The Application Layer contains manufacturer-defined applications, and is responsible for the discovery and binding of services.
- Zigbee Networking
The Zigbee networking layer manages the formation of the network, which can be one of two networking topologies: star and peer-to-peer. In the star topology, every device in the network communicates only with the PAN coordinator. Zigbee uses the Ad-hoc On-demand Distance Vector (AODV) routing protocol. Zigbee also supports the tree topology. A Zigbee coordinator (PAN coordinator) establishes the initial network, and Zigbee routers form the branches and relay the messages to devices.
In a peer-to-peer topology, each device can communicate directly with the other devices on the network, as long as the placement of these devices is sufficiently close to establish a successful communication link. Any Fully Functional Device (FFD) in a peer-to-peer network can play the role of the PAN coordinator. A peer-to-peer network can take different shapes by defining restrictions on the devices that can mutually communicate. An unrestricted peer-to-peer network is known as mesh topology.
A PAN coordinator always creates an IEEE 802.15.4 network, regardless of its topology. The PAN coordinator controls the network and performs duties such as allocating a unique address (16-bit or 64-bit) to each device in the network, initiating, terminating, and routing the messages throughout the network and selecting a unique PAN identifier for the network. The unique PAN identifier permits the devices within a network to use the 16-bit short-addressing method and still communicate with other devices across independent networks.
- Zigbee Security
The Zigbee network uses a 128-bit (16 Bytes) AES network key to encrypt all data transmission. Every node must obtain the network key in order to join the network. The network key is regenerated and distributed through the network at different intervals. Zigbee also implements link keys between devices. Link keys are unique keys shared by a pair of nodes, used to encrypt the data transmitted between the two devices. Link keys are generated by the trust center, the device assigned the responsibility of authenticating new nodes and distributing the network key.
- Zigbee Applications
A wide variety of industries and applications can take advantage of Zigbee wireless networking; these include home automation, industrial applications, health care, and inventory tracking.
Home Automation
A home automation network typically does not need high data rates; the standard data rate in home automation is only 10 kbps, well within the Zigbee specification. Home automation applications include security, light control, meter reading, irrigation, and multi-zone HVAC systems.
Industrial Automation
Zigbee mesh networking can support the data transfer required for energy management, livestock tracking, light control, process control, and asset management.
Consumer Electronics
Zigbee finds use in game controllers, wireless remote controls, wireless peripherals for personal computers, and many other applications.
Healthcare
In a system for remote patient monitoring, data from wearables and other sensors connected to patients can be collected wirelessly. Data is encrypted and securely transferred via Zigbee.
- The Zigbee Development Environment
A Zigbee Certified Platform (ZCP) is a platform certified by the Connectivity Standards Alliance (formerly the Zigbee Alliance), and comprises a radio, Zigbee stack, and a microcontroller. Tools are available on PC for building and compiling applications, which are then transferred into target boards for debugging, generally through USB or Ethernet (wireless download is enabled in some platforms).
All vendors of Zigbee radios offer development kits, complete with two or more development boards, a USB, serial or Ethernet connection to the boards, and a debug connection (BDM or JTAG) to download new binary images into the boards and to debug applications. Development kits include all the necessary hardware and software to develop Zigbee, as well as sample applications.
Digi XBee 3 Zigbee Mesh Kit
ONsemi Strata enabled Zigbee Green Power Kit
Digi Xbee 2.4GHz
OM15080-K32W USB Dongle
Application Development
This tutorial demonstrates basic application development, using the Raspberry Pi to control Zigbee communication. Special thanks go to Dr. Derek Schuurman, Computer Science Professor at Calvin University, who created pi-home, a home automation project that includes the control of lights, as well as the logging of temperature, humidity, pressure, and water leak sensors. This tutorial will go over pi-lights, a smaller version of pi-home, which focuses on the control of lights.
Light control using Raspberry Pi and Zigbee
Pi-lights enables the automation of lighting in the home, automatically turning on lights at dusk, and turning them off at a preset time. The project also includes the ability to control smart outlets. Configuration and manual operation can be performed via web browser.
The project was written in Python 3, and uses the Zigbee2MQTT library to control Zigbee devices using MQTT (Messaging Queue Telemetry Transport), a lightweight messaging protocol popular in IoT systems. Zigbee2MQTT works with a variety of Zigbee USB adapters and devices. Although the code was developed for a Raspberry Pi with a Zigbee USB adapter, it can be run on other POSIX (Portable Operating System Interface) compliant systems.
Hardware
Figure 3: Zigbee USB Adapter
A Zigbee adapter is the interface that enables Zigbee on the computer (or server). Several types of Zigbee adapters are available, include USB, GPIO or remote via WIFI or Ethernet.
Figure 4: Raspberry Pi Server
A computer or server executes the code. Most Raspberry-Pi models are able to support Zigbee. With the proper hardware and software, many other platforms also support Zigbee networks, including Linux, Windows, and macOS.
Figure 5: IKEA Light bulb. Image source: IKEA
Finally, choose the Zigbee devices that the system will control, such as the IKEA LED2035G10.
Software
The source code for the project can be downloaded at the pi-lights GitHub. Included in the package are installation instructions, as well as templates for the web interface. The two files containing source code are pi-lights.conf and pi-lights.py.
pi-lights.conf is the file containing the configuration settings used by the source code. This includes connection addresses and ports, as well as preferences such as location (based on city) and brightness range of the light.
pi-lights.py is the main source code written in Python 3. This file contains the functions performing all of the operations in the application.
The following is an analysis of some of the code in pi-lights.py.
turn_on_bulbs (line 55) and turn_off_bulbs (line 67) are the methods that turn the bulb(s) on and off. These methods are located in the State class, which manages device state for the bulbs and outlets.
def turn_off_bulbs(self):
Using a for loop, the function sends a command to each bulb via zigbee2mqtt. In the turn_on_bulbs method, that command is:
(rc, msg_id) = self.client.publish(f'zigbee2mqtt/{bulb}/set/state', 'ON')
rc is the return code; this is logged if there is an error.
self.lock.acquire() and self.lock.release() are primitive locks to control access to shared resources. acquire() changes the state of the object to locked, blocking all other calls to it until release() is called, changing it back to the unlocked state. Locking prevents multiple methods from access a shared resource at the same time, potentially resulting in corrupted data.
self.light_state is a variable that is set to remember if the lights are on or off.
Similarly, there are methods to turn smart outlets on and off (lines 79 and 91).
def turn_off_outlets(self):
set_brightness (line 103) sets the brightness of the bulbs.
def set_brightness(self, value):
At the bottom of pi-lights.py (line 326) is the code that executes when the application is run. First, the settings are imported from pi-light.conf. Next, the connection to the MQTT broker (line 389) is established.
ret = client.connect(BROKER_IP, BROKER_PORT, MQTT_KEEPALIVE)
if ret != 0:
In this code, BROKER_IP, BROKER_PORT, and MQTT_KEEPALIVE are connection settings imported from pi-lights.conf. BULBS, OUTLETS, and BRIGHTNESS are also imported from pi-lights.conf, where the different bulbs, outlets, and the brightness range of the bulbs are specified.
Finally, scheduler and light_timer objects are created in order to turn lights and outlets on and off at the correct times. light_timer calls the LightTimer class (line 118), and scheduler calls Python’s scheduler class, a generic interface for scheduling events.
The Flask framework is used to enable the web interface. Flask is configured in the FlaskThread class (line 223).
- AES (Advanced Encryption Standard): a specification for the encryption of data. AES was established by NIST (National Institute of Standards and Technology) in 2001.
- Connectivity Standards Alliance (formerly the Zigbee Alliance): a group of companies that maintains and publishes the Zigbee standard. The group contains over 500 members.
- IEEE 802.15.4: a standard that defines the specifications for low data rate wireless personal area networks. Zigbee is an enhancement to the IEEE 802.15.4 standard.
- Internet-of-Things (IoT): a network of connected electronic devices, where devices can communicate with each other and the cloud.
- Medium Access Control (MAC) layer: the layer that works as an interface between the network and the physical layer. The MAC layer controls access, performs error correction, and resolves conflicts between competing nodes. The MAC layer also performs activities such as framing and addressing.
- Mesh Topology: a network topology where all computers and network devices are interconnected and communicate directly with one another.
- MQTT (Messaging Queue Telemetry Transport): a lightweight and efficient messaging transport protocol with small code footprint and minimal network bandwidth requirements.
- Physical (PHY) layer: the layer that is lowest and closest to the hardware, interacting with the hardware to perform basic network functionality, such as transmitting and receiving.
- Star Topology: a network topology where every computer or network device is connected directly to a central device, such as a hub or router.
- Tree Topology: a network topology where multiple networks of star topology are connected via busses, similar to the branches on a tree.
Wireless connectivity via Zigbee is growing in popularity, especially for industrial and home automation applications. There are a variety of hardware components and software now available to help with Zigbee development.
Take the QuizBack to Top
Are you ready to demonstrate your Zigbee knowledge? Then take this 10-question quiz. To earn the Wireless Protocol VI Badge, read through the module, attain 100% in the quiz, and leave us some feedback in the comments section.