All Projects → sergey-brutsky → Mi Home

sergey-brutsky / Mi Home

Licence: mit
С# API for Xiaomi Mi Home devices

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Mi Home

node-lumi-aqara
Control your Xiaomi Smart Home devices with this Lumi Aqara library
Stars: ✭ 45 (-47.67%)
Mutual labels:  gateway, xiaomi
ble2mqtt
Bluetooth to MQTT bridge, add your bluetooth-capable (including controllable) devices to your smart home
Stars: ✭ 46 (-46.51%)
Mutual labels:  smarthome, xiaomi
Valetudo
Cloud-free control webinterface for vacuum robots
Stars: ✭ 2,738 (+3083.72%)
Mutual labels:  smarthome, xiaomi
Openmqttgateway
MQTT gateway for ESP8266, ESP32, Sonoff RF Bridge or Arduino with bidirectional 433mhz/315mhz/868mhz, Infrared communications, BLE, Bluetooth, beacons detection, mi flora, mi jia, LYWSD02, LYWSD03MMC, Mi Scale, TPMS, BBQ thermometer compatibility, SMS & LORA.
Stars: ✭ 2,413 (+2705.81%)
Mutual labels:  xiaomi, gateway
Com.xiaomi Miio
Homey app to control Mi Home devices that implement the miIO protocol and Xiaomi gateway child devices
Stars: ✭ 318 (+269.77%)
Mutual labels:  xiaomi, gateway
ioBroker.mihome-vacuum
Control your Xiaomi vacuum cleaner
Stars: ✭ 96 (+11.63%)
Mutual labels:  smarthome, xiaomi
esp8266-deerma-humidifier
Cloud-free wifi humidification
Stars: ✭ 34 (-60.47%)
Mutual labels:  smarthome, xiaomi
Homebridge Mi Aqara
a homebridge plugin for XiaoMi Aqara plugin.
Stars: ✭ 990 (+1051.16%)
Mutual labels:  xiaomi, gateway
Homeassistantconfiguration
Home Assistant Config. For more info see link:
Stars: ✭ 260 (+202.33%)
Mutual labels:  smarthome, xiaomi
goodbye-mihome
Web UI and plugins to manage Xiaomi MiHome gateway, sensors and smart devices.
Stars: ✭ 92 (+6.98%)
Mutual labels:  smarthome, xiaomi
Zigbridge
Zigbee gateway implementation
Stars: ✭ 21 (-75.58%)
Mutual labels:  xiaomi, gateway
Homebridge Mi Gateway Fm
XiaoMi Gateway FM plugin for HomeBridge.
Stars: ✭ 43 (-50%)
Mutual labels:  xiaomi, gateway
Xiaomi Flashable Firmware Creator
Xiaomi Flashable Firmware Creator
Stars: ✭ 74 (-13.95%)
Mutual labels:  xiaomi
Badgeforappicon
The unread badges of the android launcher icon.
Stars: ✭ 83 (-3.49%)
Mutual labels:  xiaomi
Yi Hack V4
New Custom Firmware for Xiaomi Cameras based on Hi3518e Chipset. It features RTSP, SSH, FTP and more!
Stars: ✭ 1,183 (+1275.58%)
Mutual labels:  xiaomi
Adonetcore.aseclient
AdoNetCore.AseClient - a .NET Core DB Provider for SAP ASE
Stars: ✭ 72 (-16.28%)
Mutual labels:  netcore2
Fritzctl
A lightweight, easy to use console client for the AVM FRITZ!Box Home Automation.
Stars: ✭ 84 (-2.33%)
Mutual labels:  smarthome
Ng Alain .netcore
ng-alain 与 .net core 搭配
Stars: ✭ 82 (-4.65%)
Mutual labels:  netcore2
Streamdeck Homeassistant
🏠 Use the Elgato Stream Deck as Home Assistant controller. Call any available service and toggle lights or resume your music.
Stars: ✭ 69 (-19.77%)
Mutual labels:  smarthome
Microgateway
IBM API Connect Microgateway framework, built on Node.js & Nginx
Stars: ✭ 1,131 (+1215.12%)
Mutual labels:  gateway

C# Library for using xiaomi smart gateway in your automation scenarious

This library provides simple and flexible C# API for Xiaomi Mi Home devices.

Currently supports only Xiaomi Smart Gateway, Air Humidifier and several sensors. Please see the pictures below.

gateway humidifier

gateway temperature_sensor socket_plug motion_sensor motion_sensor_2 door_window_sensor aqara_door_window_sensor water_sensor smoke_sensor switch wired wall switch sensor_weather wireless dual wall switch aqara_cube_sensor

Warning : This is experimental version. It may be very unstable.

Installation

via nuget package manager

Install-Package MiHomeLib

or

dotnet add package MiHomeLib

or install via GitHub packages

Setup Gateway

Before using this library you should setup development mode on your gateway.

Here is an instruction --> https://www.domoticz.com/wiki/Xiaomi_Gateway_(Aqara)

Warning: If you bought a new revision of Mi Home Gateway (see picture bellow)
image
it could be possible that ports on your gateway required for UDP multicast traffic are closed.
Before using this library they must be opened.
Instruction

Warning: Mi Home Gateway uses udp multicast for messages handling.
So your app must be hosted in the same LAN as your gateway.
If it is not you have to use multicast routers like udproxy or igmpproxy or vpn briding

Warning : If your app is running on windows machine, make sure that you disabled virtual network adapters like VirtualBox, Hyper-V, Npcap, pcap etc.
Because these adapters may prevent proper work of multicast traffic between your machine and gateway

Usage examples

Get all devices in the network

public static void Main(string[] args)
{
    // pwd of your gateway (optional, needed only to send commands to your devices) 
    // and sid of your gateway (optional, use only when you have 2 gateways in your LAN)
    using var miHome = new MiHome();

    miHome.OnAnyDevice += (_, device) =>
    {
        Console.WriteLine($"{device.Sid}, {device.GetType()}, {device}"); // all discovered devices
    };

    Console.ReadLine();
}

Supported devices

1. Gateway

gateway

using var miHome = new MiHome();

miHome.OnGateway += (_, gateway) =>
{
    gateway.EnableLight();
    Task.Delay(3000).Wait();
    gateway.DisableLight();
    Task.Delay(3000).Wait();
    gateway.StartPlayMusic(1); // Track number 1 (tracks range is 0-8, 10-13, 20-29)
    Task.Delay(3000).Wait();
    gateway.StopPlayMusic();
};

2. Temperature and humidity sensor

temperature_sensor

using var miHome = new MiHome();

miHome.OnThSensor += (_, thSensor) =>
{
    if (thSensor.Sid == "158d000182dfbc") // sid of specific device
    {
        Console.WriteLine(thSensor); // Sample output --> Temperature: 22,19°C, Humidity: 74,66%, Voltage: 3,035V

        thSensor.OnTemperatureChange += (_, e) =>
        {
            Console.WriteLine($"New temperature: {e.Temperature}");
        };

        thSensor.OnHumidityChange += (_, e) =>
        {
            Console.WriteLine($"New humidity: {e.Humidity}");
        };
    }
};

3. Socket Plug

socket_plug

using var miHome = new MiHome();

miHome.OnSocketPlug += (_, socketPlug) =>
{
    if (socketPlug.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(socketPlug); // sample output Status: on, Inuse: 1, Load Power: 2.91V, Power Consumed: 37049W, Voltage: 3.6V

        socketPlug.TurnOff();
        Task.Delay(5000).Wait();
        socketPlug.TurnOn();
    }
};

4. Motion sensor or Aqara motion sensor

motion_sensor motion_sensor_2

using var miHome = new MiHome();

//miHome.OnAqaraMotionSensor += (_, motionSensor) =>
miHome.OnMotionSensor += (_, motionSensor) =>
{
    if (motionSensor.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(motionSensor); // sample output Status: motion, Voltage: 3.035V, NoMotion: 0s

        motionSensor.OnMotion += (_, __) =>
        {
            Console.WriteLine($"{DateTime.Now}: Motion detected !");
        };

        motionSensor.OnNoMotion += (_, e) =>
        {
            Console.WriteLine($"{DateTime.Now}: No motion for {e.Seconds}s !");
        };
    }
};

5. Door/Window sensor or Aqara open/close sensor

door_window_sensor aqara_door_window_sensor

using var miHome = new MiHome();

//miHome.OnAqaraOpenCloseSensor += (_, windowSensor) =>
miHome.OnDoorWindowSensor += (_, windowSensor) =>
{
    if (windowSensor.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(windowSensor); // sample output Status: close, Voltage: 3.025V

        windowSensor.OnOpen += (_, __) =>
        {
            Console.WriteLine($"{DateTime.Now}: Window opened !");
        };

        windowSensor.OnClose += (_, __) =>
        {
            Console.WriteLine($"{DateTime.Now}: Window closed !");
        };

    }
};

5. Water leak sensor

water_sensor

using var miHome = new MiHome();

miHome.OnWaterLeakSensor += (_, waterLeakSensor) =>
{
    if (waterLeakSensor.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(waterLeakSensor); // Status: no_leak, Voltage: 3.015V

        waterLeakSensor.OnLeak += (_, __) =>
        {
            Console.WriteLine("Water leak detected !");
        };

        waterLeakSensor.OnNoLeak += (_, __) =>
        {
            Console.WriteLine("NO leak detected !");
        };

    }
};

6. Smoke sensor

smoke_sensor

using var miHome = new MiHome();

miHome.OnSmokeSensor += (_, smokeSensor) =>
{
    if (smokeSensor.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(smokeSensor); // sample output Alarm: off, Density: 0, Voltage: 3.075V

        smokeSensor.OnAlarm += (_, __) =>
        {
            Console.WriteLine("Smoke detected !");
        };

        smokeSensor.OnAlarmStopped += (_, __) =>
        {
            Console.WriteLine("Smoke alarm stopped");
        };

        smokeSensor.OnDensityChange += (_, e) =>
        {
            Console.WriteLine($"Density changed {e.Density}");
        };
    }
};

7. Wireless dual wall switch

wireless dual wall switch

using var miHome = new MiHome();

miHome.OnWirelessDualWallSwitch += (_, wirelessDualSwitch) =>
{
    if (wirelessDualSwitch.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(wirelessDualSwitch);

        wirelessDualSwitch.OnLeftClick += (_) =>
        {
            Console.WriteLine("Left button clicked !");
        };

        wirelessDualSwitch.OnRightDoubleClick += (_) =>
        {
            Console.WriteLine("Right button double clicked !");
        };

        wirelessDualSwitch.OnLeftLongClick += (_) =>
        {
            Console.WriteLine("Left button long clicked !");
        };

    }
};

8. Aqara cube

aqara_cube_sensor

using var miHome = new MiHome();

miHome.OnAqaraCubeSensor += (_, aqaraQube) =>
{
    if (aqaraQube.Sid == "158d00015dc6cc") // sid of specific device
    {
        Console.WriteLine(aqaraQube);

        aqaraQube.OnStatusChanged += (sender, eventArgs) =>
        {
            Console.WriteLine($"{sender} | {eventArgs.Status}");
        };

    }
};

9. Air Humidifier

humidifier

Before using the library you need to know IP and TOKEN of your air humidifier. If you don't know these parameters try to use the following code in order to discover air humidifiers in your LAN

AirHumidifier.OnDiscovered += (_, humidifier) =>
{
    Console.WriteLine($"ip: {humidifier.Ip}, token: {humidifier.Token}");
    // sample output ip: 192.168.1.5, token: 4a3a2f017b70097a850558c35c953b55
};

AirHumidifier.DiscoverDevices();

If your device hides his token follow these instructions in order to extract it.

Basic scenario

var airHumidifier = new AirHumidifier("192.168.1.5", "token here");
Console.WriteLine(airHumidifier);
/* sample output
Power: on
Mode: high
Temperature: 32.6 °C
Humidity: 34%
LED brightness: bright
Buzzer: on
Child lock: off
Target humidity: 50%
Model: zhimi.humidifier.v1
IP Address:192.168.1.5
Token: 4a3a2f017b70097a850558c35c953b55
*/

Functions

var airHumidifier = new AirHumidifier("192.168.1.5", "token here");
airHumidifier.PowerOn(); // power on
airHumidifier.PowerOff(); // power off
airHumidifier.SetMode(AirHumidifier.Mode.High); // set fan mode high/medium/low
airHumidifier.GetTemperature(); // get temperature
airHumidifier.GetHumidity(); // get humidity
airHumidifier.SetBrightness(AirHumidifier.Brightness.Bright); // set brighness bright/dim/off
airHumidifier.BuzzerOn(); // set buzzer sound on
airHumidifier.BuzzerOff(); // set buzzer sound off
airHumidifier.ChildLockOn(); // set child lock on
airHumidifier.ChildLockOff(); // set child lock oаа
airHumidifier.GetTargetHumidity(); // get humidity limit 20/30/40/50/60/70/80 %

Async versions of the operations above also supported.

When I buy more devices I will update the library

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].