Skip to content

MiControl

Melvin van Kalsbeek edited this page Jul 20, 2018 · 24 revisions

The MiControl library is a C# written class library for writing your own programs for controlling MiLight WiFi enabled light bulbs. The main class, Controller will interface directly with a MiLight WiFi controller on the local network to send commands to certain groups, or all linked MiLight light bulbs.

Using the MiControl library

The MiControl library can be used by referencing the compiled dll file or importing the project from the MiControl folder and then adding using MiControl; to the top of the file in which you wish to use the library.

Connecting to a MiLight controller

Making a connection to a MiLight controller is fairly easy by creating a new instance of the Controller class and supplying it with the IP-address of the MiLight WiFi controller.

// Create an instance of the Controller class for
// the controller connected to 192.168.0.123
var controller = new Controller("192.168.0.123");

If you're not sure on which IP address your MiLight WiFi controller(s) are located, there is a static method available for finding controllers on the local network.

// Discover controllers on the local network
// Returns a 'List<Controller>' with 'Controller' instances
var controllers = Controller.Discover();

Alternatively, when only using a single MiLight WiFi controller (with a dynamic IP address for instance), or if you want to send the same commands to multiple controllers, the Controller class can be instantiated with a subnet IP.

// Create a controller for this subnet
var controller = new Controller("255.255.255.255");

Sending commands to MiLight light bulbs

After creating an instance of the Controller class, sending commands is simple and intuitive by calling the controller class and the desired method of switching/changing. Documentation in the code should give you ample information about usage of methods.

Commands are built up according to the pattern Command(group, options). For the group a value between 0 and 4 must be supplied. Value 1 to 4 are specific groups of lights, 0 targets all groups.

There are different methods for RGBW, White MiLight light bulbs and older generation RGB light bulbs/LED strips. Commands should be sent with a minimum interval of 50ms to prevent commands being sent to fast and consequently not performed by the controller. This is done automatically unless the AutoDelay property is set to false. One can also change the duration of the delay with the Delay property.

Some examples for using the controller:

Switch 'on' all connected light bulbs

The Controller class has a few methods for controlling all connected lights (RGBW, White and RGB).

// Turn on all connected lights
controller.AllOn();

Switching all RGBW lights linked to this controller 'on'

Sending a command is simple by calling your instance of the Controller class and the desired method. Methods for different types of light bulbs are put in subclasses called RGBW, White and RGB.

// Turn on all RGBW lights
controller.RGBW.SwitchOn();

Switching RGBW group 1 and White group 2 'on'

Execution of multiple commands has an interval of 50ms to prevent commands being dropped by the controller. This Thread.Sleep(50) statement is handled in the Controller class. When sending lots of commands, a BackgroundWorker or other form of multithreading/tasking is advised to prevent the main thread from halting and becoming unresponsive.

// Turn 'on' RGBW group 1 and White group 2
controller.RGBW.SwitchOn(1);
controller.White.SwitchOn(2);

Alternatively, by setting the AutoDelay property of the Controller class to false, the 50ms delay is prevented and you can implement your own way of keeping some time between commands.

// Switch off the 50ms delay (switch to manual mode)
controller.AutoDelay = false;
    
// Turn RGBW group 1 and White group 2 'on'
controller.RGBW.SwitchOn(1);
Thread.Sleep(100);
controller.White.SwitchOn(2);

It is also possible to change the delay between commands by directly setting the Delay property of the Controller class.

// Set the delay to 250ms
controller.Delay = 250;

Switch White group 4 on and turn down brightness

Switching on a group of white lights and turning down the brightness. The brightness for white light sadly can not be set directly and is done in 'steps'.

// Switch on group 4 and set brightness one step down
controller.White.SwitchOn(4);
controller.White.BrightnessDown(4);

Set the hue of RGB lights

The color of a MiLight RGB bulb (old single channel model) or LED strip (possibly) can be set by giving a hue (0 - 360 degrees) for a specific group.

// Set the color of RGB light(s)/strip(s) to green
controller.RGB.SetHue(120.0);

Changing the color of all RGBW lights

Changing the color of all RGBW lights can be done with the SetColor method. This method will only use the hue of the color, and will therefore not be a very true representation of the selected color. It is therefore similar to the SetHue method.

// Set the color (hue) of all RGBW lights
controller.RGBW.SetColor(0, Color.Red);

Setting a true color for RGBW group 3

Setting the color of a MiLight RGBW light bulb can be done by passing the group and a System.Drawing.Color to set it to. This method will also change the bulb(s) to white light if the color is very dull (saturation below 15%) or very dark (brightness/value below 15%). It also adjust the brightness of the light to match the given color's intensity.

// Set the color for group 3 to mint cream. Very bright color
// with low saturation. Will produce white light.
controller.RGBW.SetTrueColor(3, Color.MintCream);

or

controller.RGBWSetTrueColor(3, Color.FromArgb(245, 255, 250));

Set the brightness for all RGBW lights

The brightness of the bulbs can be set by providing a percentage (value between 0 and 100) for a specified group. The percentage is translated to one of twenty-five steps.

// Set brightness for all lights (group 0) to 50%
controller.RGBWSetBrightness(0, 50);