Page 1 of 1
Chuangmi Ir remote and wifi Plug control scripts
Posted: Monday 01 January 2018 23:02
by goshi0
Hi
In a domoticz telegram chat we developed an approach to use the xiaomi's chuangmi ir universal remote. Since didnt find any approach in domoticz I made this little script
Steps to make it work :
1 /
Install python-miio from these tutorial.
https://python-miio.readthedocs.io/en/l ... stallation
2/
With the python miio installed check it works with these command
3/
Create the scripts
Code: Select all
#!/usr/bin/python3
import sys
import ipaddress
from typing import Any
from miio import ChuangmiIr,DeviceException
import time
import json
import os
import os.path
fichero ='./codes.txt'
ip= '192.168.1.128'
token = 'b14fd53b79ab7d17991e0093264b21aa'
try:
os.stat("codes.txt")
print ('Existe el fichero codes.txt')
except:
print ('No existe codes txt vamos a crearlo')
file = open("codes.txt", "w")
num_lines = sum(1 for line in open('codes.txt'))
print ('Stored Ir Keys ->'+ str(num_lines) )
id = num_lines + 1
ir = ChuangmiIr(ip,token)
ir.learn(key=1)
time.sleep(10)
print (ir.read(key=1).get("code"))
with open('codes.txt', 'a') as file:
try:
file.write(ir.read(key=1).get("code")+ '\n')
except:
print('cant save the Ir code readin error')
When you call this script the led in the device begins to blink and you have 10 seconds to send a the comand, the ir sequence is stored in the file codes.txt, its saved one sequence each line.
To send the ir codes we use the next script irsend.py. You have to specify a number which is the line number in codes.txt, for example if you want to use the tv off wich is the third line.
here the code
Code: Select all
#!/usr/bin/python3
import sys
import ipaddress
from typing import Any
from miio import ChuangmiIr,DeviceException
import time
############################################
##
## Config
##############################################
ip = 'device ip'
token = 'token'
####################NO MODIFICAR NADA MAS A PARTIR DE AQUI######
f=open('codes.txt')
codeline=f.readlines()
id = codeline[int(sys.argv[1])-1]
print('vamos a enviar el codigo numero'+ id)
try:
ir = ChuangmiIr(ip,token)
except:
print ('esto ha petado sin decir nada flipa!')
print('conexion con el ir Ok procedemos a enviar el codigo ->' )
ir.play(id,frequency='')
print ('todo ha ido bien creo!')
you can delete the print sentences were only for debugging.
now you can link it to a dummy device to send your desired commands
Now the wifis plugs
the same check the devices with
take note of the ip and token
and then to turn on the plug :
Code: Select all
miplug --ip [device ip] --token [device token] on
to turn it off:
Code: Select all
miplug --ip [device ip] --token [device token] off
now you can create a dummy switch and control these cheap plugs.
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Friday 05 January 2018 13:17
by dcliff1
Hello, thank you for making these scripts! Perfect timing as I'm looking at integrated the IR receiver.
I am having a small problem, as I'm not familiar with Python.. I've managed to install the miio library and can run "mirobo discover" to get the needed information.
However, when i create your scripts - get this (on Windows and Raspbian). Any suggestions?
Code: Select all
Traceback (most recent call last):
File "C:\temp\code.py", line 6, in <module>
from miio import ChuangmiIr,DeviceException
File "C:\Python36\lib\site-packages\miio\__init__.py", line 2, in <module>
from miio.protocol import Message, Utils
File "C:\Python36\lib\site-packages\miio\protocol.py", line 21, in <module>
from construct import (Struct, Bytes, Const, Int16ub, Int32ub, GreedyBytes,
File "C:\Python36\lib\site-packages\construct\__init__.py", line 24, in <module>
from construct.debug import Probe, ProbeInto, Debugger
File "C:\Python36\lib\site-packages\construct\debug.py", line 5, in <module>
import sys, traceback, pdb, inspect
File "C:\Python36\lib\pdb.py", line 76, in <module>
import code
File "C:\temp\code.py", line 6, in <module>
from miio import ChuangmiIr,DeviceException
ImportError: cannot import name 'ChuangmiIr'
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Monday 22 January 2018 0:00
by goshi0
It seems to me the script cant't locate some modules check your installation, are in the correct VENV ?
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Saturday 10 February 2018 21:41
by molnaratti
Hi! Thank you for your work! What could be the problem?
pi@raspberrypi:~/domoticz/scripts $ python3 irsend.py 2
vamos a enviar el codigo numeroZ6VJAAQCAAChAgAAZgYAAF0RAABGIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0AQEBASEBAQEhAQEhAQEBAQEBAQEBIQEBAQEBASEBIQEBIQEB
conexion con el ir Ok procedemos a enviar el codigo ->
Traceback (most recent call last):
File "irsend.py", line 25, in <module>
ir.play(id,frequency='')
TypeError: play() got an unexpected keyword argument 'frequency'
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Friday 16 March 2018 10:54
by nickmorgan
I will correct not much
In the script
irsend.py the last line should look like this
only so I sent the code
You can also send commands with the following code
Code: Select all
mirobo --ip 192.168.0.25 --token 689c4056fe28ebb3a2e8c2fe350e51ba raw_command miIO.ir_play '{"freq": 38400, "code": "base64..."}'
which is much easier to install into other scripts
Sorry for my bad english
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Sunday 18 March 2018 19:19
by molnaratti
nickmorgan wrote: ↑Friday 16 March 2018 10:54
I will correct not much
In the script
irsend.py the last line should look like this
only so I sent the code
You can also send commands with the following code
Code: Select all
mirobo --ip 192.168.0.25 --token 689c4056fe28ebb3a2e8c2fe350e51ba raw_command miIO.ir_play '{"freq": 38400, "code": "base64..."}'
which is much easier to install into other scripts
Sorry for my bad english
Thank you for your help. It works perfectly
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Sunday 18 March 2018 19:45
by DAVIZINHO
goshi0 wrote: ↑Monday 01 January 2018 23:02
Hi
In a domoticz telegram chat we developed an approach to use the xiaomi's chuangmi ir universal remote. Since didnt find any approach in domoticz I made this little script
Steps to make it work :
1 /
Install python-miio from these tutorial.
https://python-miio.readthedocs.io/en/l ... stallation
2/
With the python miio installed check it works with these command
3/
Create the scripts
Code: Select all
#!/usr/bin/python3
import sys
import ipaddress
from typing import Any
from miio import ChuangmiIr,DeviceException
import time
import json
import os
import os.path
fichero ='./codes.txt'
ip= '192.168.1.128'
token = 'b14fd53b79ab7d17991e0093264b21aa'
try:
os.stat("codes.txt")
print ('Existe el fichero codes.txt')
except:
print ('No existe codes txt vamos a crearlo')
file = open("codes.txt", "w")
num_lines = sum(1 for line in open('codes.txt'))
print ('Stored Ir Keys ->'+ str(num_lines) )
id = num_lines + 1
ir = ChuangmiIr(ip,token)
ir.learn(key=1)
time.sleep(10)
print (ir.read(key=1).get("code"))
with open('codes.txt', 'a') as file:
try:
file.write(ir.read(key=1).get("code")+ '\n')
except:
print('cant save the Ir code readin error')
When you call this script the led in the device begins to blink and you have 10 seconds to send a the comand, the ir sequence is stored in the file codes.txt, its saved one sequence each line.
To send the ir codes we use the next script irsend.py. You have to specify a number which is the line number in codes.txt, for example if you want to use the tv off wich is the third line.
here the code
Code: Select all
#!/usr/bin/python3
import sys
import ipaddress
from typing import Any
from miio import ChuangmiIr,DeviceException
import time
############################################
##
## Config
##############################################
ip = 'device ip'
token = 'token'
####################NO MODIFICAR NADA MAS A PARTIR DE AQUI######
f=open('codes.txt')
codeline=f.readlines()
id = codeline[int(sys.argv[1])-1]
print('vamos a enviar el codigo numero'+ id)
try:
ir = ChuangmiIr(ip,token)
except:
print ('esto ha petado sin decir nada flipa!')
print('conexion con el ir Ok procedemos a enviar el codigo ->' )
ir.play(id,frequency='')
print ('todo ha ido bien creo!')
you can delete the print sentences were only for debugging.
now you can link it to a dummy device to send your desired commands
Now the wifis plugs
the same check the devices with
take note of the ip and token
and then to turn on the plug :
Code: Select all
miplug --ip [device ip] --token [device token] on
to turn it off:
Code: Select all
miplug --ip [device ip] --token [device token] off
now you can create a dummy switch and control these cheap plugs.
Great job!!!!
congratulations
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Sunday 15 July 2018 15:35
by kunzai
I'm having some problems implementing this.
Files have been created:
/home/pi/domoticz/scripts/python/IR.py
/home/pi/domoticz/scripts/python/irsend.py
/home/pi/domoticz/scripts/python/codes.txt
When I run IR.py from command line, it gives an error when I don't record anything
Code: Select all
Traceback (most recent call last):
File "/home/pi/domoticz/scripts/python/IR.py", line 25, in <module>
print (ir.read(key=1).get("code"))
File "/usr/local/lib/python3.5/dist-packages/miio/chuangmi_ir.py", line 56, in read
return self.send("miIO.ir_read", {'key': str(key)})
File "/usr/local/lib/python3.5/dist-packages/miio/device.py", line 270, in send
raise DeviceError(m.data.value["error"])
miio.exceptions.DeviceError: {'code': -5003, 'message': 'learn timeout'}
When I do record it works flawless
When I load it from Domoticz with this line:
script:///usr/bin/python3 /home/pi/domoticz/scripts/python/IR.py
codes.txt is not updated when trying to record
I receive the error "Error executing script command (/usr/bin/python3). returned: 256".
After a restart of the system, the error disappeared but the codes.txt is still not updated.
Any ideas?
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Saturday 29 December 2018 17:35
by ArnieO
Thank you @goshi0, this works very well!
I currently use this for controlling a heat pump from Domoticz.
But of course when I use the hand remote to change it, Domoticz does not see it.
Can you see any way to get the Xiaomi IR remote to continuously read IR signals into Domoticz, and change Domoticz devices if a code corresponds to one that is stored in the codes.txt file?
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Saturday 29 December 2018 17:39
by DAVIZINHO
ArnieO wrote: ↑Saturday 29 December 2018 17:35
Thank you @goshi0, this works very well!
I currently use this for controlling a heat pump from Domoticz.
But of course when I use the hand remote to change it, Domoticz does not see it.
Can you see any way to get the Xiaomi IR remote to continuously read IR signals into Domoticz, and change Domoticz devices if a code corresponds to one that is stored in the codes.txt file?
this is a idea that many of us have sometime (in a spanish telegram domoticz group); but nobody do the work.
Basically one script that:
- listen for code
- if receive code, compare with a codes_bd.txt
- launch and action to domoticz with virtual switches
- if domoticz launch a code to de IR (normal use) it stop the script of listening for codes, and lauch the ir comand. after that put in listeing mode again
its importante to control the timeout in listeing mode and relaunch this mode.
this is my idea, but need someone to program and test
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Saturday 29 December 2018 18:36
by ArnieO
DAVIZINHO wrote: ↑Saturday 29 December 2018 17:39
ArnieO wrote: ↑Saturday 29 December 2018 17:35
Thank you @goshi0, this works very well!
I currently use this for controlling a heat pump from Domoticz.
But of course when I use the hand remote to change it, Domoticz does not see it.
Can you see any way to get the Xiaomi IR remote to continuously read IR signals into Domoticz, and change Domoticz devices if a code corresponds to one that is stored in the codes.txt file?
this is a idea that many of us have sometime (in a spanish telegram domoticz group); but nobody do the work.
Basically one script that:
- listen for code
- if receive code, compare with a codes_bd.txt
- launch and action to domoticz with virtual switches
- if domoticz launch a code to de IR (normal use) it stop the script of listening for codes, and lauch the ir comand. after that put in listeing mode again
its importante to control the timeout in listeing mode and relaunch this mode.
this is my idea, but need someone to program and test
Yes this sounds like a reasonable approach!
The remote should most of the time be in listening mode, and only stop listening when a code is sent, then go back to listening. Unfortunately, I am not skilled in python, so I am not able to program this.
But I have a feeling the bits and pieces of code needed are already in the code at the top of this thread.
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Wednesday 09 January 2019 16:55
by DoMoney
Please clarify regarding the token - where can I find it?
Should I take it from Xiaomi gateway?
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Wednesday 09 January 2019 21:07
by DAVIZINHO
DoMoney wrote: ↑Wednesday 09 January 2019 16:55
Please clarify regarding the token - where can I find it?
Should I take it from Xiaomi gateway?
Please, read the tutorials of the library, you can find it in the web of the library that the partner tell us to install:
https://python-miio.readthedocs.io/en/l ... -discovery
Re: Chuangmi Ir remote and wifi Plug control scripts
Posted: Friday 11 January 2019 21:13
by deennoo
Maybe my dev can help on this :
https://github.com/deennoo/domoticz-Xiaomi-Led-Lamp
This is using mihome python lib