Chuangmi Ir remote and wifi Plug control scripts

Python and python framework
Post Reply
goshi0
Posts: 6
Joined: Sunday 31 December 2017 18:43
Target OS: Raspberry Pi
Domoticz version:
Contact:

Chuangmi Ir remote and wifi Plug control scripts

Post by goshi0 » Tuesday 02 January 2018 0: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.

dcliff1
Posts: 8
Joined: Tuesday 03 January 2017 4:35
Target OS: -
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by dcliff1 » Friday 05 January 2018 14:17

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 18:43
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by goshi0 » Monday 22 January 2018 1:00

It seems to me the script cant't locate some modules check your installation, are in the correct VENV ?

molnaratti
Posts: 8
Joined: Friday 02 February 2018 17:21
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Chuangmi Ir remote and wifi Plug control scripts

Post by molnaratti » Saturday 10 February 2018 22:41

Hi! Thank you for your work! What could be the problem?

[email protected]:~/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'

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests