MarkDown
# Getting Started with PyZigbee: Using Python to Control Smart Home Communication Protocols
Hello everyone! Today we are going to explore a particularly cool Python library—PyZigbee. This library allows us to easily control Zigbee devices with Python, creating our own smart home system. Even if you are a beginner in Python, following this article step by step will help you quickly get started with this interesting technology!
1. What is Zigbee?
Zigbee is like a “dialect” between smart devices; it is a wireless communication protocol specifically designed for the Internet of Things (IoT). It has low power consumption and a long range (about 10-20 meters indoors), making it very suitable for communication between smart home devices.
Imagine that your smart light bulbs, thermostats, and door locks might be “quietly talking” to each other via Zigbee. PyZigbee is the “translator” that allows us to join this conversation using Python.
2. Installing PyZigbee
First, we need to install this library. Open your terminal or command line and enter:
pip install pyzigbee
Tip: If you have both Python 2 and Python 3 installed, remember to use <span>pip3</span> to ensure it is installed in the Python 3 environment.
3. Connecting Zigbee Devices
Once installed, let’s try to connect a Zigbee device. The most common way is through a USB adapter:
from pyzigbee import ZigBee
# The serial port name is usually COM3 on Windows and /dev/ttyUSB0 on Linux/Mac
zb = ZigBee('/dev/ttyUSB0') # Replace with your actual serial port
print("Zigbee adapter connected successfully!")
Notes:
-
Please confirm that your device supports the Zigbee protocol before connecting.
-
The serial port name varies by operating system.
-
You may need to install additional serial port drivers.
4. Discovering Devices on the Network
Once connected, we can scan for Zigbee devices on the network:
def device_discovered(device):
print(f"Discovered new device: {device}")
zb.device_discovered = device_discovered
zb.start_scan() # Start scanning
This code will print out all discovered device information, including device type, address, etc. Just like meeting new friends at a party, your Python program can now “know” all Zigbee devices!
5. Controlling Smart Light Bulbs
Let’s take a smart light bulb as an example and see how to control it:
# Assuming we already know the address of the bulb
bulb_address = '00:12:4B:00:06:12:34:56'
# Turn on the light
zb.send_command(bulb_address, 'on')
print("The light bulb is turned on")
# Set brightness to 50%
zb.send_command(bulb_address, 'level', 50)
print("Brightness set to 50%")
Tip: In actual use, you need to replace it with the real device address. This information can usually be found in the device manual or management backend.
6. Receiving Device Messages

Zigbee devices can also actively send messages, such as sensor data. We can receive them like this:
def message_received(address, cluster, data):
print(f"Message received from {address}:")
print(f"Cluster: {cluster}, Data: {data}")
zb.message_received = message_received
# Keep the program running to receive messages
while True:
pass
This code will continuously listen for device messages, making it very suitable for receiving temperature and humidity sensor data or door lock status changes.
Practical Exercise
Now, let’s do a comprehensive exercise:
-
Scan and list all Zigbee devices
-
Find your smart light bulb
-
Write a function to create a breathing light effect that gradually brightens and dims the bulb
Tip code framework:
import time
# Connection and scanning code...
def breathing_light(address):
for i in range(0, 100, 5):
zb.send_command(address, 'level', i)
time.sleep(0.1)
for i in range(100, 0, -5):
zb.send_command(address, 'level', i)
time.sleep(0.1)
# Call the function...
Summary Review
Today we learned about:
-
The basic concepts of the Zigbee protocol
-
How to install and use the PyZigbee library
-
Methods to connect to Zigbee adapters
-
How to scan and identify network devices
-
How to control the switch and brightness of smart light bulbs
-
Techniques for receiving device messages
Friends, today’s Python learning journey ends here! Remember to get hands-on coding and connect all your smart home devices. Feel free to ask questions in the comments section. Wishing everyone a happy learning experience and continuous improvement in Python!
Previous Reviews
◆
◆
◆