Skip to content

Xiaomi Door Window sensor (MCCGQ01LM)

Sergey edited this page Jul 1, 2024 · 2 revisions

For Xiaomi Gateway 2

using var gw2 = new XiaomiGateway2();
{
    gw2.OnDoorWindowSensor += (_, x) =>
    {
        Console.WriteLine(x.ToString());

        if(x.Sid == "<specific sid here>")
        {
            x.OnOpen += (_, __) => Console.WriteLine("Door/Window is open");
            x.OnClose += (_, __) => Console.WriteLine("Door/Window is close");
            x.NotClosedFor1Minute += (_, __) => Console.WriteLine("Door/Window is not closed for 1 minute");
            x.NotClosedFor5Minutes += (_, __) => Console.WriteLine("Door/Window is not closed for 5 minutes");
        }
    };
}   

For Xiaomi Gateway 3

using var gw3 = new XiaomiGateway3("<gateway ip>", "<gateway token>");
{
    gw3.OnDeviceDiscovered += x =>
    {
        if(x is XiaomiDoorWindowSensor device && device.Did == "<specific did here>")
        {
            Console.WriteLine(device.ToString());

            device.OnContactChanged += () => Console.WriteLine($"Door/Window is {device.Contact}");
            device.OnBatteryPercentChange += (oldBatteryValue) => Console.WriteLine($"Battery is {device.BatteryPercent}");
            device.OnChipTemperatureChange += (oldChipTemperatureValue) => Console.WriteLine($"Chip Temperature is {device.ChipTemperature}");
            device.OnVoltageChange += (oldVoltageValue) => Console.WriteLine($"Voltage is {device.Voltage}");
            device.OnLinkQualityChange += (oldLinkQualityValue) => Console.WriteLine($"Linq quality is {device.LinqQuality}");
        }
    };

    gw3.DiscoverDevices();
}