Function blocks and Mosaic libraries Yeelight 13.07.2020 09:43 13.07.2020 09:43

Yeelight is the world's leading brand of intelligent lighting, originally under the auspices of Xiaomi. The company offers an inexhaustible number of types of lighting. From the lighting sources themselves, ie monochrome or RGB bulbs, spot lights, LED strips, to complete ceiling lights or lamps. All these elements can be connected to a smart home (usually via wifi) and controlled using the supplied mobile application. Of course, Yeelight also offers integration into the Apple HomeKit, Google Assistant and Amazon Alexa platforms, with the option of voice control.

Due to the high popularity of these lights among users, the YeelightLib library was implemented in the Mosaic environment for the possibility of controlling them from the Tecomat PLC application program. This allows a defined application interface for LAN communication that each Yeelight has implemented. If the user wants to make his light available for control by the PLC application program, he must enable LAN control for the given light in the mobile application.

To use the YeelightLib library to control lights, it is important that the user has a static IP address reservation set on his wifi router in the DHCP server configuration for the MAC address of the given lighting device. In other words, the light must always obtain the same IP address from the DHCP server after logging in to the network. The MAC address of the device for which an IP address reservation must be made can be read from the Yeelight mobile application.

If we have the lights ready this way, we can start programming in the Mosaic environment. In the example, we will demonstrate the control of one RGB bulb (Yeelight RGB Smart Light Bulb) and one ceiling light with ambient lighting function (Yeelight JIAOYUE 650).

We will first add the YeelightLib library to the relevant project:


The use of the library can be demonstrated on the following simple program:

PROGRAM prgYeelight
    yeelight : fbYeelightControl;

      (control := (enabled:= true), network:=(ip:='', name:='Ceiling light')),
      (control := (enabled:= true), network:=(ip:='', name:='Color Bulb'))


  yeelight(ethCode := ANY_UNI, pingEnab := TRUE, yeeLightUnits := YEELIGHT_Units, refreshTime := T#2s);

First it is necessary to declare Yeelight devices that we want to control with our program. The YeelightLib library defines a structure for this purpose T_YEELIGHT_UNIT.

    network          : T_YEELIGHT_UNIT_NETWORK;
    status           : T_YEELIGHT_UNIT_STATUS;
    control          : T_YEELIGHT_UNIT_CONTROL;

It consists of three parts:

Network structure network - specifies parameters for connection to Yeelight light. Above all, the IP address of the device. It also serves to signal the connection status. You can use the name variable to name the instance, if necessary.

    online           : bool;            // the signaling device is online
    lastSee          : dt;              // time of last device detection 
    ip               : string [20];     // time of last device detection
    port             : uint := 55443;   // communication port (default 55443)
    name             : string [24];     // device naming

Status zone status - a structure maintaining the current state of the device, ie setting the parameters of a given light, which is periodically read. Structure elements are read-only

    power            : bool;                 // device on (true) / off (false)
    bright           : usint;                // brightness (0 - 100%)
    ct               : uint;                 // color temperature (Kelvin)
    rgb              : udint;                // RGB 
    hue              : uint;                 // shade
    sat              : usint;                // satiety
    color_mode       : TYeelightColorMode;   // set light mode:
                                             // YEELIGHT_RGB_MODE - RGB mode
                                             // YEELIGHT_CT_MODE - color temperature mode
                                             // YEELIGHT_HSV_MODE - HSV mode
    bg_power         : bool;                 // backlight on (true) / off (false)
    bg_bright        : usint;                // backlight brightness (0 - 100%)
    bg_ct            : uint;                 // backlight color temperature (Kelvin)
    bg_rgb           : udint;                // RGB backlight
    bg_hue           : uint;                 // backlight shade
    bg_sat           : usint;                // backlight saturation
    bg_lmode         : TYeelightColorMode;   // backlight mode set
    moon             : bool;                 // night mode (Moon mode)
    moon_bright      : usint;                // night mode brightness
    name             : string [24];          // set device name

Control zone control - a structure enabling light control. Used in an application program to initiate a request to change the state of a device.

    enabled          : bool;   // total permission to operate the light
    power            : bool;   // turn the light on or off
    toggle           : bool;   // light state switching
    bright           : usint;  // brightness level adjustment (0-100%)
    ct               : uint;   // color temperature setting (1700-6500K)
    rgb              : udint;  // adjust the color of the light using the RGB components
    hue              : uint;   // light shade adjustment (HSV)
    sat              : usint;  // light saturation adjustment (HSV)
    color_mode       : TYeelightColorMode; // light color mode setting
    smooth           : bool;   // turn on gradual change
    duration         : udint;  // duration of the gradual change
    bg_power         : bool;   // turn the backlight on or off
    bg_bright        : usint;  // backlight brightness level adjustment (0-100%)
    bg_ct            : uint;   // backlight color temperature setting (1700-6500K)
    bg_rgb           : udint;  // setting the backlight color using RGB folders
    bg_hue           : uint;   // backlight shade adjustment (HSV)
    bg_sat           : usint;  // backlight saturation (HSV) adjustment
    moon             : bool;   // turn night light mode on or off (Moon mode)
    moon_bright      : usint;  // night light brightness level adjustment (0-100%)
    default          : bool;   // setting the default state after power on

Pomocí knihovny YeelightLib je možné řídit až 16 takových zařízení definovaných strukturou T_YEELIGHT_UNIT In the form of a T_YEELIGHT_UNIT_LIST list, these declarations are passed in the application program to the fbYeelightControl, function block, which is the main and only function block in the YeelightLib library. In the above example, it is clear that to initialize the units in the list, it is enough to write the IP addresses of the end devices and set the enabled flag to true in the control structure true.

When calling the T_YEELIGHT_UNIT function block. In the form of a T_YEELIGHT_UNIT_LIST list, these declarations are passed in the application program to the fbYeelightControl, function block, which is the main and only function block in the YeelightLib library. In the above example, it is clear that to initialize the units in the list, it is enough to write the IP addresses of the end devices and set the enabled flag to true in the control structure.

When calling the fbYeelightControl function block, you only need to specify the ethernet channel to be used for communication. The channel should be in UNI mode with a minimum transmit and receive zone size of 512 B. With the Foxtrot 2 system, the ANY_UNI, constant can be used to specify the channel, ensuring the automatic creation of a communication channel in the body of the function block. It is then not necessary to create it in a HW configuration. The remaining input parameters of the function block can be used to specify various time periods for checking the network connection, cyclic reading of status data, or intensity of command transmission. These parameters must be selected with caution, as incorrect parameter settings can cause temporary blockage of communication with the terminal. According to the specification of the application interface, Yeelight devices have a limit of a maximum of 60 accesses per 1 minute. It is therefore recommended to keep the time parameters of the function block at their default values, which have been designed with this limitation in mind.

After successful start of the application program in the PLC, the function block will verify the network availability of all devices defined by the IP address and with the enabled flag set in the control structure in a specified interval. All entries in the T_YEELIGHT_UNIT_LIST list with the enabled flag not set are ignored. If the device is available, the corresponding item in the device list in the network structure is automatically flagged online. Status data, ie the currently set light parameters, are then periodically read from all detected devices in the network. Due to the lack of support for IP multicast technology in the communication channels of the Tecomat system, which the Yeelight lights use to report status changes, the function block uses periodic data reading methods. The acquired status data is stored in the corresponding status zones. The selected light can be controlled by writing the required parameters to the control structure in the appropriate item in the device list. As soon as the fbYeelightControl function block detects different parameter settings in the status and control structure, it creates and then sends the corresponding command to the given light according to the control structure.

Some parameters of the control structure can be controlled directly from the system website. For example, you can control the light on / off by using the Two-state Image element in WebMaker in Mosaic. Simply set the Variable attribute to the corresponding item of the control structure of the selected light for this element.


The resulting control via the web interface by combining various elements and using the CanvasLib library can look like this: