Controling Xiaomi Mi Robot with Domoticz

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Controling Xiaomi Mi Robot with Domoticz

Post by thps »

He there,

i managed to control my Xiaomi Mi Robot Vacuum with Domoticz, without catching UDP-packages or something like this. Lets look at it:

Steps:

1. Get the token of your mi robot with the MiToolkit :

https://github.com/ultrara1n/MiToolkit/releases

Sorry, its a german page, but Google-Translator is your friend ;). Your phone has to be in devmode and usb-debugging is activated. Connect your phone via usb to your pc.

2. Install "mirobo" (https://github.com/rytilahti/python-mirobo) on your domoticz-server. i´ve used this tutorial to manage this: http://blog.gurskiy.com/?p=62

This is a russian page, but with google translator, its is easy to handle.

After installing it, you should be able to control your robot with:

"sudo mirobo --ip [yourrobotiphere] --token [yourrobottokenhere] start"

3. Create a virtual switch in domoticz (On/Off).

4. Check the state with a device-based luascript and trigger "mirobo"-application like this:

Code: Select all

commandArray = {}
for deviceName,deviceValue in pairs(devicechanged) do
    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
    if (deviceName=='[nameofyourvirtualswitch]' ) then
        if tostring(deviceValue) == "On" then
            os.execute("sudo mirobo --ip [yourrobotiphere] --token [yourrobottokenhere]  start")
        elseif tostring(deviceValue) == "Off" then
            os.execute("sudo mirobo --ip [yourrobotiphere] --token [yourrobottokenhere]  home")
        end
    end
end

return commandArray
Thats all!
With controlicz i´m now able to call "Alexa, turn the vacuum on!", and my Xiamo Mi Robot will start going to work ;).

Just notice, that this is only an basic configuration. "Mirobo" is much more powerfull, like read the state of the xiaomi, manage dnd-mode, setting fanspeed and so on...

edit: i´ve added a icon for the xiaomi
Attachments
domoticz_custom_icon_MiRobot.zip
(5.05 KiB) Downloaded 147 times
Sevift
Posts: 31
Joined: Monday 21 September 2015 16:02
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by Sevift »

thps wrote: Sunday 13 August 2017 13:58
Sorry, its a german page, but Google-Translator is your friend ;). Your phone has to be in devmode and usb-debugging is activated. Connect your phone via usb to your pc.

i´ve used this tutorial to manage this: http://blog.gurskiy.com/?p=62

This is a russian page, but with google translator, its is easy to handle.
The source of the instructions above is the forum
http://4pda.ru/forum/index.php?s=&showt ... p=60716375
http://4pda.ru/forum/index.php?s=&showt ... p=60736476

The same script for dzVents
http://4pda.ru/forum/index.php?s=&showt ... p=63937624
Spoiler: show

Code: Select all

return {

	active = true,
	on = {
		devices = {
			-- девайсы которые слушаем
			'Mi Robot'
		},
	},	
	logging = {
        level = domoticz.LOG_ERROR
    },
	execute = function(domoticz, device)	    
		local command = 'mirobo --ip 192.168.000.00 --token 66664b79426a788f49397734736f3934 '
		os.execute(command..string.lower(device.levelName))			
	end
}
And a script to check the current state of the robot
http://4pda.ru/forum/index.php?s=&showt ... p=63937624
Spoiler: show

Code: Select all

return {
	active = true, 
	on = {
		timer = {'every 2 minutes'}
	},	
	logging = {
        level = domoticz.LOG_ERROR
    },
	execute = function(domoticz)    
		local command = 'mirobo --ip ip_пылика --token токен_пылика'
		local miRobotState = domoticz.devices('MiRobot State')		
		local f = io.popen(command)
		local line = f:read("*l")
		local state = string.gsub(line, "State: ", "")
		miRobotState.updateText(state)
		f:close()
	end
}
thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by thps »

thanks for this advice.

I´vbuild the control and info-scripts by myself in lua.
domo.PNG
domo.PNG (255.28 KiB) Viewed 6398 times
My Xiaomi is called "Bumbee", dont wonder. The Switch "Staubsauger"(Germ. vacuum) is just a workarround for use with alexa.
"Bumbee" and "Bumbee Fanspeed" control the vacuum.
I´ve build a script to poll the status-information every minute and refresh the state of those 2 switches and the battery capacity.
ebretteville
Posts: 39
Joined: Sunday 10 January 2016 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Cherbourg - Caen | France
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by ebretteville »

Nice, just received mine yesterday, can't wait to configure it soon. Did you "just follow" the first post tutorial?!
thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by thps »

i´ve followed the steps i wrote down. THen you are able to turn on/off your vacuum. everything else, i´ve build in lua after writing this steps down.
ebretteville
Posts: 39
Joined: Sunday 10 January 2016 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Cherbourg - Caen | France
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by ebretteville »

Thank you
DennisD
Posts: 51
Joined: Friday 18 September 2015 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by DennisD »

thps wrote: Thursday 17 August 2017 9:46 thanks for this advice.

I´vbuild the control and info-scripts by myself in lua.
domo.PNG

My Xiaomi is called "Bumbee", dont wonder. The Switch "Staubsauger"(Germ. vacuum) is just a workarround for use with alexa.
"Bumbee" and "Bumbee Fanspeed" control the vacuum.
I´ve build a script to poll the status-information every minute and refresh the state of those 2 switches and the battery capacity.
Would you mind sharing the script to poll the status information and the lua scripts you have. Would be greatly appreciated
ebretteville
Posts: 39
Joined: Sunday 10 January 2016 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Cherbourg - Caen | France
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by ebretteville »

+1

Sent from my MI MAX using Tapatalk

corederoma82
Posts: 85
Joined: Saturday 14 January 2017 21:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by corederoma82 »

But is only for raspberry and linux?
Raspberry PI 3, 3x Yeelight bulbs, 1x Yeeligh led strip, 1x Sonoff with ESP826, Controlicz, Kodi
blacksn0w
Posts: 99
Joined: Friday 10 March 2017 22:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by blacksn0w »

thps wrote: Sunday 13 August 2017 13:58 He there,

i managed to control my Xiaomi Mi Robot Vacuum with Domoticz, without catching UDP-packages or something like this. Lets look at it:

Steps:

1. Get the token of your mi robot with the MiToolkit :

https://github.com/ultrara1n/MiToolkit/releases

Sorry, its a german page, but Google-Translator is your friend ;). Your phone has to be in devmode and usb-debugging is activated. Connect your phone via usb to your pc.
Creator of MiToolkit here, currently integrating multi-language so you dont have to bother with German :D
Should be ready within the next week.
Migration done, hello Home Assistant.
ebretteville
Posts: 39
Joined: Sunday 10 January 2016 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Cherbourg - Caen | France
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by ebretteville »

Great thanks, if you need French, I can help! :-)

Sent from my MI MAX using Tapatalk

thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by thps »

Here is my script to poll infos from the robot.

Maybe there is some otherhead you will not need( like sending a message, when the mirobot finished his sheduled run), but take a look at it:

My Mirobot is called Bumbee ;)

Code: Select all

function mysplit(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                t[i] = str
                i = i + 1
        end
        return t
end


commandArray = {}

hour = os.date("%H") 


if(tonumber(hour) > 7 and tonumber(hour) < 22) then
    
    local handle = io.popen("sudo mirobo --ip XXXXXXX --token XXXXXXXX")
    local result = handle:read("*a")
    handle:close()

    print("Result:"..result)
    
    local t = mysplit(result,"\n")
    

    --Status
    if(t[1] ~= nil) then
        local status = string.gsub(t[1],"State: ","")

        
        if(status == "Cleaning" and otherdevices_svalues['Bumbee'] ~= '10') then
            commandArray['Bumbee'] = 'Set Level 10';
        end
            
        if(status == "Charging" and otherdevices_svalues['Bumbee'] ~= '30') then
            if(otherdevices_svalues['Bumbee'] == '10' and uservariables['bCleanToday'] == 10) then
                if(t[4] ~= nil and t[5] ~= nil) then
                    -- Benachrichtigung
                    local cleantime = string.gsub(t[4],"Cleaning since: ","")
                    local cleanspace =  string.gsub(string.gsub(t[5],"Cleaned area: ","")," m.","")
                    commandArray['SendNotification']='Reinigung abgeschlossen#'.."Bumbee hat "..cleanspace..' m² in '..cleantime..' erfolgreich gereinigt und kehrt mit einem Akkuladezustand von '..otherdevices_svalues['Bumbee Akkuladezustand']..' % zum Dock zurück.'..'#0'
                    uservariables['bCleanToday'] = 11
                end
            end
            commandArray['Bumbee'] = 'Set Level 30';
        end
            
        if((status == "Paused" or status == "Charger disconnected" or status =="Idle") and otherdevices_svalues['Bumbee'] ~= '20') then

            commandArray['Bumbee'] = 'Set Level 20';
        end   
        if(status == "Spot cleaning" and otherdevices_svalues['Bumbee'] ~= '40') then

            commandArray['Bumbee'] = 'Set Level 40';
        end   
    end
    -- Batterieladestand
    if(t[2] ~= nil) then
        local battery= string.gsub(string.gsub(t[2],"Battery: ","")," %%","") 
        commandArray['UpdateDevice']='1327|0|'..battery
    end
    
    --Fanspeed  
    if(t[3] ~= nil) then
        local fanspeed= string.gsub(string.gsub(t[3],"Fanspeed: ","")," %%","")

        
        if(fanspeed == "38" and otherdevices_svalues['Bumbee Fanspeed'] ~= '10') then
            commandArray['Bumbee Fanspeed'] = 'Set Level 10';
        end
        
        if(fanspeed == "60" and otherdevices_svalues['Bumbee Fanspeed'] ~= '20') then
            commandArray['Bumbee Fanspeed'] = 'Set Level 20';
        end

        if(fanspeed == "77" and otherdevices_svalues['Bumbee Fanspeed'] ~= '30') then
            commandArray['Bumbee Fanspeed'] = 'Set Level 30';
        end
        
        if(fanspeed == "90" and otherdevices_svalues['Bumbee Fanspeed'] ~= '40') then
            commandArray['Bumbee Fanspeed'] = 'Set Level 40';
        end
        
    end
    
elseif(uservariables['bCleanToday'] ~= 0) then
    uservariables['bCleanToday'] = 0
    
end

commandArray['Variable:bCleanToday'] = tostring(uservariables['bCleanToday'])


return commandArray
DennisD
Posts: 51
Joined: Friday 18 September 2015 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by DennisD »

Thank you very much, gonna play with it :)
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by cattoo »

And if you don't have access to a rooted android phone? How to do then?
blacksn0w
Posts: 99
Joined: Friday 10 March 2017 22:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by blacksn0w »

cattoo wrote: Tuesday 22 August 2017 9:14 And if you don't have access to a rooted android phone? How to do then?
With MiToolkit you only need an phone with Android 4 or higher.
It can be rooted, but dont have to. The method works completely without root.
Migration done, hello Home Assistant.
ebretteville
Posts: 39
Joined: Sunday 10 January 2016 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Cherbourg - Caen | France
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by ebretteville »

I tried MKIToolKit but can't get the token. Will try to find a video explaining how this app works.
thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by thps »

In generell you don´t need a rootet phone.

There are also other ways to get the token. You could get the token also with mirobo. Therefore you need to reset your robot and connect your Raspi to the Robo-Wifi. This way is descriped in the github-link above.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by cattoo »

Well ok, I was not clear. I don't have an Android, only iPhone. And my raspberry don't have wifi.

Ive managed to extract the file from the phone, but there is 1536 lines of code. So how will ill now what line is correct?

"EDIT"
I have now manage to get the token.
I used this guide,(its a German site) http://technikzeugs.de/xiaomi-mirobot-s ... n/?fref=gc
So ill hope this help someone.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by cattoo »

thps wrote: Sunday 13 August 2017 13:58 He there,

i managed to control my Xiaomi Mi Robot Vacuum with Domoticz, without catching UDP-packages or something like this. Lets look at it:

Code: Select all

commandArray = {}
for deviceName,deviceValue in pairs(devicechanged) do
    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
    if (deviceName=='[nameofyourvirtualswitch]' ) then
        if tostring(deviceValue) == "On" then
            os.execute("sudo mirobo --ip [yourrobotiphere] --token [yourrobottokenhere]  start")
        elseif tostring(deviceValue) == "Off" then
            os.execute("sudo mirobo --ip [yourrobotiphere] --token [yourrobottokenhere]  home")
        end
    end
end

return commandArray
When ill take this script, an error shows up. Whats wrong?

Code: Select all

2017-08-24 19:24:00.702 Error: EventSystem: in Inga On/Off: [string "commandArray = {}..."]:2: bad argument #1 to 'pairs' (table expected, got nil)

Code: Select all


commandArray = {}
for deviceName,deviceValue in pairs(devicechanged) do
    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
    if (deviceName=='Inga' ) then
        if tostring(deviceValue) == "On" then
            os.execute("sudo mirobo --ip IPADRESS --token ffffffffffffffffffffffffff  start")
        elseif tostring(deviceValue) == "Off" then
            os.execute("sudo mirobo --ip IPADRESS --token ffffffffffffffffffffffffff  home")
        end
    end
end

return commandArray
thps
Posts: 19
Joined: Monday 26 September 2016 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Controling Xiaomi Mi Robot with Domoticz

Post by thps »

thats strange...

did you choose "lua" and "Device" as script options?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest