Page 1 of 2

Controling Xiaomi Mi Robot with Domoticz

Posted: Sunday 13 August 2017 13:58
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

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 15 August 2017 10:31
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
}

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Thursday 17 August 2017 9:46
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 6389 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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Thursday 17 August 2017 9:51
by ebretteville
Nice, just received mine yesterday, can't wait to configure it soon. Did you "just follow" the first post tutorial?!

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 19 August 2017 15:59
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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 19 August 2017 17:25
by ebretteville
Thank you

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 19 August 2017 20:26
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

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 19 August 2017 21:20
by ebretteville
+1

Sent from my MI MAX using Tapatalk


Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 19 August 2017 23:03
by corederoma82
But is only for raspberry and linux?

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Sunday 20 August 2017 6:04
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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Sunday 20 August 2017 10:03
by ebretteville
Great thanks, if you need French, I can help! :-)

Sent from my MI MAX using Tapatalk


Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Monday 21 August 2017 13:09
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

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Monday 21 August 2017 21:49
by DennisD
Thank you very much, gonna play with it :)

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 22 August 2017 9:14
by cattoo
And if you don't have access to a rooted android phone? How to do then?

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 22 August 2017 9:38
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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 22 August 2017 9:40
by ebretteville
I tried MKIToolKit but can't get the token. Will try to find a video explaining how this app works.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 22 August 2017 9:55
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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Tuesday 22 August 2017 16:45
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.

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Thursday 24 August 2017 19:28
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

Re: Controling Xiaomi Mi Robot with Domoticz

Posted: Saturday 26 August 2017 20:38
by thps
thats strange...

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