connect the hardware
https://pi4j.com/1.2/images/j8header-zero-large.png, https://pi4j.com/1.2/images/j8header-photo-zero-large.png or following command show how to connect the PINs.
sudo apt install python3-gpiozero
pinout
I got a KY-022 IR receiver and a unknown type IR sender. I think the symbol S
or DAT
mean signal or data to be send, should connect to any GPIO pins in raspberry pi, while GND
or -
stand for Ground
or 0 votage. VCC
should be the positive votage, here in my case are both 5V.
install lircd service
I have seen people use pulseio
or pigpio
to control the IR. But in most case, lirc
should be a default choose.
sudo apt install lircd
tee -a /boot/config.txt <<EOF
dtoverlay=lirc-rpi
dtparam=gpio_in_pin=14
dtparam=gpio_out_pin=15
EOF
Now the system should reboot.
configure the lircd remote
One may create a remote device file /etc/lirc/lircd.conf.d/midea.lircd.conf
to define the parameters/signal sequence for the remote device.
begin remote
name midea
flags RAW_CODES
eps 30
aeps 100
ptrail 0
repeat 0 0
gap 108229
begin raw_codes
name toogle
8922 4396 581 1615 598 531 577 528 579 528 578 529 577 535 602 501 575 532 576 532 604 1587 599 1611 601 1612 606 1609 604 1608 603 1612 603 1610 607 1606 601 1615 598 529 580 530 573 531 573 534 598 508 578 532 572 535 574 530 577 1617 597 1617 595 1616 599 1612 597 1615 603 1609 610 1611 596 1619 594 528 580 530 576 531 588 516 577 527 603 507 578 529 578 527 576 1623 615 1587 628 1608 581 1616 607 1602 601 1609 604
end raw_codes
end remote
man lircd.conf
to see the meaning of those configurations.
There mighe be already a configuration devinput.lircd.conf
which can be removed or disabled (rename to *.dist) without any concern.
Notice the raw_codes
integer sequence, to understand it, see something like the NEC/RC-5 protocol.
For the IR sender follow NEC protocol, command irrecord -d /dev/lirc0 ~/base.lircd.conf
can be used to record the buttons. But in my case, the hardware do not send NEC codes, so when using irrecord
, I can not complete the collection parameter stage(the one irrecord
need you to randomly push buttons to get two rows of dots). People might suggest use irrecord -f
to force the raw codes mode, but it still do not work for me.
So I stopped the lirc with command sudo systemctl stop lircd
, and then use mode2 -d /dev/lirc0
to collect the pulse/space data myself, that is how I got the raw_codes
above. The codes usually start with a pulse last for 890 ms and end with a pulse, then might be a space with randomly duration(should be the space between you to button press).
For a complex IR sender, like the one for Air Conditioners, the raw codes should be carefully analysised because when you press the same button twice, you might get different signals.
I attached following python script when plotting the signals.
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from itertools import chain, islice
signal_seq = []
with open('/tmp/midea.history', 'r') as fp:
for l in fp:
s, t = l.split(' ')
if s == 'space' and int(t) > 10000:
signal_seq.append([])
else:
signal_seq[-1].append((len(signal_seq), 1 if s=='pulse' else 0, int(t)))
dat = pd.DataFrame(list(chain.from_iterable(signal_seq)), columns=['id', 'signal', 'period'])
fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(20, 10))
for i, d in islice(dat.groupby(dat.id), 300):
axs.set_ylim([-0.2, 3.5])
#axs.set_xlim([-1, 10000])
s = list(chain.from_iterable(((l-e, s), (l, s))
for s, l, e in zip(d.signal, np.cumsum(d.period), d.period)))
axs.plot(*zip(*[(t, s*2+i*0.1) for t, s in s]), '-')
code = ''.join((d.period[1::2][1:]>1000).astype(np.int).astype(str))
print(code[:16], code[16: (16+(len(code)-16)//2)]==code[-(len(code)-16)//2:])
fig.show()
Simulate press button
irsend SEND_ONCE midea toogle