Yeelight
Yeelight has a complete smart home lighting product line, covering home lighting series, desk lighting series, ambient lighting series, and smart control series. Combining cutting-edge technology with beautiful design has always been Yeelight’s pursuit. AI technology, BLE MESH technology, and whole-house smart lighting technology are widely applied in its products. All Wi-Fi products support smart voice control, so lighting changes are just a word away.
LAN Control
Yeelight supports Google Assistant and Amazon Alexa smart voice control. It also supports IFTTT, which is rarely supported domestically. It can better integrate with various network services such as social media and smart hardware. We will explain the related applications of IFTTT later. In addition, Yeelight also launched a third-party control protocol for technology enthusiasts, which can achieve personalized control within the LAN. The mPython Board controlling Yeelight smart lighting devices explained in this article uses this protocol.
Yeelight third-party control protocol: https://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
Yeelight LAN control
Preparation
First, we need a Yeelight smart lighting device. According to Yeelight’s official statement, all Wi-Fi lighting devices on the market and subsequent Wi-Fi products will support the LAN control protocol. I recommend the Yeelight LED bulb (color version), which is economical and can control colors.
Yeelight LED bulb (color version)
Before using the YeeLight smart bulb, you must use the YeeLight APP to configure and connect to Wi-Fi first, and turn on the “LAN Control” function.
Yeelight configuration process
The mPython Board provides the
yeelightdriver library, which can be obtained from awesome-mpython/libary/yeelight. It contains more detailed API descriptions of theyeelightmodule. Downloadyeelight.pyto the file system of the mPython Board.Connect the mPython Board to the same LAN as Yeelight.
Programming
Discovering Bulbs
After the mPython Board and the Yeelight bulb are in the same LAN, to control the bulb, we first need to know the bulb’s IP address. We can use the discover_bulbs() function:
from mpython import * # Import the mpython module
from yeelight import * # Import the yeelight module
my_wifi = wifi() # Connect to the same LAN as YeeLight
my_wifi.connectWiFi("ssid","password")
discover_bulbs() # Discover the device information of YeeLight in the LAN
The Yeelight bulb in the network responds and returns a dictionary containing the bulbs attribute:
>>> discover_bulbs()
[{'ip': '192.168.0.8', 'capabilities': {'rgb': '16711680', 'bright': '100', 'support': 'get_prop set_default set_power toggle set_bright start_cf stop_cf set_scene cron_add cron_get cron_del set_ct_abx set_rgb set_hsv set_adjust adjust_bright adjust_ct adjust_color set_music set', 'sat': '100', 'power': 'off', 'id': '0x0000000007e7544d', 'name': '', 'fw_ver': '26', 'color_mode': '2', 'hue': '359', 'ct': '3500', 'model': 'color'}, 'port': '55443'}]
The discover_bulbs() function can obtain the attributes of Yeelight devices in the network. From the above return, the IP address of the bulb is 192.168.0.8.
Switch Control
After knowing the IP address, we build a Bulb object to control the bulb switch:
bulb=Bulb("192.168.0.8") # Build the object
bulb.turn_on() # Turn on light command
bulb.turn_off() # Turn off light command
In addition to turn_on() and turn_off(), you can also use toggle() to reverse the state.
Brightness Adjustment
bulb.set_brightness(100)
set_brightness(brightness). The brightness parameter is the brightness value, with an adjustable range of 0~100.
Setting Colors
bulb.set_rgb(255,0,0) # Set the bulb color via RGB
bulb.set_hsv(180,100) # Set the bulb color via HSV
bulb.set_color_temp(1700) # Set the bulb color temperature
The yeelight module provides two functions: set_rgb(red, green, blue) and set_hsv(hue, saturation). The two color models “RGB” and “HSV” are used to set the bulb light color. The RGB color model is more commonly used, and I believe you are familiar with it. It obtains various colors through the changes of the three color channels red (R), green (G), and blue (B) and their superposition on each other.
HSV (Hue Saturation Value) color model: hue is the hue, measured in angles, with a value range of 0~359, calculated counterclockwise starting from red, where red is 0°, green is 120°, and blue is 240°. saturation represents the degree to which the color approaches the spectral color. The higher the saturation of the color, the deeper and more vivid it is. Range 0~100.
The Value brightness parameter is not supported. You only need to set the hue and saturation parameters. When making rainbow effects and color transitions, HSV is more natural.
You can also use the set_color_temp(degrees) function to set the bulb color temperature. The degrees color temperature parameter has a range of 1700~6500.
Yeelight HSV color model
mPython Board controlling Yeelight
Attention
Yeelight Wi-Fi smart devices currently support a maximum of 4 simultaneous TCP connections. Connection attempts beyond this will be rejected. For each connection, there is a command message quota limit, that is, 60 commands per minute. All LANs also have a total quota limit of 144.