Chuangmi Ir remote and wifi Plug control scripts

Python and python framework

Moderator: leecollings

Post Reply
goshi0
Posts: 6
Joined: Sunday 31 December 2017 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Chuangmi Ir remote and wifi Plug control scripts

Post 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

Code: Select all

mirobo discover
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.

Code: Select all

python3 irsend.py 3
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

Code: Select all

mirobo discover
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.
dcliff1
Posts: 8
Joined: Tuesday 03 January 2017 3:35
Target OS: -
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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'
goshi0
Posts: 6
Joined: Sunday 31 December 2017 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by goshi0 »

It seems to me the script cant't locate some modules check your installation, are in the correct VENV ?
molnaratti
Posts: 34
Joined: Friday 02 February 2018 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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'
nickmorgan
Posts: 2
Joined: Friday 16 March 2018 10:25
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by nickmorgan »

I will correct not much
In the script irsend.py the last line should look like this

Code: Select all

ir.play_raw (id,frequency='')
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 :)
molnaratti
Posts: 34
Joined: Friday 02 February 2018 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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

Code: Select all

ir.play_raw (id,frequency='')
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 :)
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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

Code: Select all

mirobo discover
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.

Code: Select all

python3 irsend.py 3
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

Code: Select all

mirobo discover
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 ;-)
kunzai
Posts: 3
Joined: Saturday 04 March 2017 16:04
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Belgium
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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?
ArnieO
Posts: 15
Joined: Sunday 19 March 2017 16:08
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Norway
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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?
Electronics engineer partly out of practice, having turned the skills into hobby recently. Playing with RPi, Arduinos, ESP8266s etc. Installed Nexa units in the house, which I want to control with Domoticz / RPi3 / RFXtrx433.
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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 :-P
ArnieO
Posts: 15
Joined: Sunday 19 March 2017 16:08
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Norway
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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 :-P
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.
Electronics engineer partly out of practice, having turned the skills into hobby recently. Playing with RPi, Arduinos, ESP8266s etc. Installed Nexa units in the house, which I want to control with Domoticz / RPi3 / RFXtrx433.
DoMoney
Posts: 8
Joined: Friday 21 December 2018 11:09
Target OS: Windows
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by DoMoney »

Please clarify regarding the token - where can I find it?
Should I take it from Xiaomi gateway?
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post 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
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by deennoo »

Maybe my dev can help on this :

https://github.com/deennoo/domoticz-Xiaomi-Led-Lamp

This is using mihome python lib
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest