Raspberry Pi Series Tutorial: Infrared Remote Control

I found an infrared receiver (HX1838+NEC code infrared remote control) and an infrared remote control on the Internet, ready to put it on the Raspberry Pi to learn infrared control.

I don't know why the distance sensor with the light on the receiver is very close, I don't know why, there is no need for the light here, use the two pictures below, one receiver and controller



1. Aim at yourself, and then three from left to right The heads are as shown below, plug them into the corresponding Raspberry Pi


2. Install lirc and modify the configuration file
LIRC (Linux Infrared remote control) is an open source software package under the linux system. This software allows Linux systems to receive and send infrared signals.
sudo apt-get install lirc

Here I want to focus on explaining that after installation, basically the articles on the Internet say to modify the file /etc/lirc/hardware.conf, but after I installed the latest version of lirc (0.9.2), I couldn’t find this configuration file at all. The modification is not the hardware.conf file, but the /etc/lirc/lirc_options.conf file is modified, and the following parameters are modified in this file.
#driver          = devinput
driver		= default
#device		= /dev/lirc0
device          = auto

3. Modify the /boot/config.txt file, where 26 is the gpio interface corresponding to the bcm Raspberry Pi:
dtoverlay=lirc-rpi,gpio_in_pin=26

4. Re-enable the lirc function, and the lirc software is now configured.
sudo /etc/init.d/lircd restart
sudo modprobe lirc_rpi


5. Turn off the infrared reception function
First close the lirc software, and then execute the following command:
sudo kill $(pidof lircd)


6. Test infrared reception
mode2 -d /dev/lirc0

Using the infrared remote control, press any button on the receiver, the screen will print something similar to the following, indicating that the infrared receiving function is normal.
quote
space 16300
pulse 95
space 28794
pulse 80
space 19395


7. Infrared code recording
irrecord --list-namespace

You can check which key names are there and record them, and then execute the infrared code recording command
irrecord -d /dev/lirc0 ~/lircd.conf

At the beginning, you need to enter an alias, and finally the corresponding file name will be saved according to this name. I started here: pi
needs to enter the number of keys twice according to the prompt. I can’t remember how many times it needs to be. Press to the end of the prompt, the next step is to click the corresponding button on the controller according to the button name recorded above, and all the buttons on the controller correspond to a different button name. If all are completed, press Enter once more , the prompt is over, but you need to press the button again to save successfully. After success, the file pi.lircd.conf will be generated in the ~/ directory, just put this file in /etc/lirc/lircd.conf. d/ in this directory, execute:
sudo cp ~/pi.lircd.conf /etc/lirc/lircd.conf.d/

Below is the content of this file I generated pi.lircd.conf:
# Please take the time to finish this file as described in
# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/
# and make it available to others by sending it to
# <[email protected]>
#
# This config file was automatically generated
# using lirc-0.9.4c(default) on Sat Feb 24 13:30:19 2018
# Command line used: -d /dev/lirc0 /home/pi/lircd.conf
# Kernel version (uname -r): 4.9.59-v7+
#
# Remote name (as of config file): pi
# Brand of remote device, the thing you hold in your hand:
# Remote device model nr:
# Remote device info url:
# Does remote device has a bundled capture device e. g., a
#     usb dongle? :
# For bundled USB devices: usb vendor id, product id
#     and device string (use dmesg or lsusb):
# Type of device controlled
#     (TV, VCR, Audio, DVD, Satellite, Cable, HTPC, ...) :
# Device(s) controlled by this remote:

begin remote

  name  pi
  bits           32
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       8994  4467
  one           583  1641
  zero          583   570
  ptrail        583
  repeat       8994  2217
  gap          107314
  toggle_bit_mask 0x0
  frequency    38000

      begin codes
          KEY_0                    0x00FF6897 0x7ED25DBC
          KEY_1                    0x00FF30CF 0x7ED25DBC
          KEY_2                    0x00FF18E7 0x7ED25DBC
          KEY_3                    0x00FF7A85 0x7ED25DBC
          KEY_4                    0x00FF10EF 0x7ED25DBC
          KEY_5                    0x00FF38C7 0x7ED25DBC
          KEY_6                    0x00FF5AA5 0x7ED25DBC
          KEY_7                    0x00FF42BD 0x7ED25DBC
          KEY_8                    0x00FF4AB5 0x7ED25DBC
          KEY_9                    0x00FF52AD 0x7ED25DBC
          KEY_CHANNEL              0x00FF629D 0x7ED25DBC
          KEY_CHANNELDOWN          0x00FFA25D 0x7ED25DBC
          KEY_CHANNELUP            0x00FFE21D 0x7ED25DBC
          KEY_PLAY                 0x00FFC23D 0x7ED25DBC
          KEY_NEXT                 0x00FF02FD 0x7ED25DBC
          KEY_PREVIOUS             0x00FF22DD 0x7ED25DBC
          KEY_VOLUMEDOWN           0x00FFE01F 0x7ED25DBC
          KEY_VOLUMEUP             0x00FFA857 0x7ED25DBC
          KEY_EQUAL                0x00FF906F 0x7ED25DBC
          KEY_A                    0x00FF9867 0x7ED25DBC
          KEY_B                    0x00FFB04F 0x7ED25DBC
      end codes

end remote


After the completion, restart the Raspberry Pi

8. The next step is to write the code. I will give the code in two languages ​​here, one is in the c language and the other is in the python language, both of which are called through the

c code (the bcm2835 must be installed in advance The library, many from the Internet Baidu bar):
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
 
#define PIN 26
#define IO bcm2835_gpio_lev(PIN)
unsigned char i,idx,cnt;
unsigned char count;
unsigned char data[4];

static int flag = 0;
int exec_cmd(unsigned char key_val){
	switch(key_val) {
		case 0x45://CH-
		       	printf("Button CH-\n");
			break;
		case 0x46://CH
			printf("Button CH\n");
			break;
		case 0x47://CH+
			printf("Button CH+\n");
			break;
		case 0x44://PREV
			printf("Button PREV\n");
			system("mpc prev");
			break;
		case 0x40://NEXT
			printf("Button NEXT\n");
			system("mpc next");
			break;
		case 0x43://PLAY/PAUSE
			printf("Button PLAY/PAUSE\n");
			if (flag == 0) {
				flag = 1;
				printf("mpc play\n");
				//system("mpc play");
			}else{
				flag = 0;
				printf("mpc pause\n");
				//system("mpc pause");
			}
			break;
		case 0x07://VOL-
			printf("Button VOL-\n");
			system("mpc volume -1");
			break;
		case 0x15://VOL+
			printf("Button VOL+\n");
			system("mpc volume +1");
			break;
		case 0x09://EQ
			printf("Button EQ\n");
			break;
		case 0x16://0
			printf("Button 0\n");
			break;
		case 0x19://100+
			printf("Button 100+\n");
			break;
		case 0x0d://200+
			printf("Button 200+\n");
			break;
		case 0x0c://1
			printf("Button 1\n");
			break;
		case 0x18://2
			printf("Button 2\n");
			break;
		case 0x5e://3
			printf("Button 3\n");
			break;
		case 0x08://4
			printf("Button 4\n");
			break;
		case 0x1c://5
			printf("Button 5\n");
			break;
		case 0x5a://6
			printf("Button 6\n");
			break;
		case 0x42://7
			printf("Button 7\n");
			break;
		case 0x52://8
			printf("Button 8\n");
			break;
		case 0x4a://9
			printf("Button 9\n");
			break;
		default:
			break;
	}
	return 0;
}

int main(int argc, char const *argv[]){
	if (!bcm2835_init()){
		return 1;
	}
	bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
	bcm2835_gpio_set_pud (PIN, BCM2835_GPIO_PUD_UP);
	printf("IRM Test Program ... \n");
	while (1) {
		if (IO == 0) {
			count = 0;
			while (IO == 0 && count++ < 200)   //9ms
				delayMicroseconds(60);

			count = 0;
			while (IO == 1 && count++ < 80)    //4.5ms
				delayMicroseconds(60);

			idx = 0;
			cnt = 0;
			data[0]=0;
			data[1]=0;
			data[2]=0;
			data[3]=0;
			for (i =0;i<32;i++) {
				count = 0;
				while (IO == 0 && count++ < 15)  //0.56ms
					delayMicroseconds(60);

				count = 0;
				while (IO == 1 && count++ < 40)  //0: 0.56ms; 1: 1.69ms
					delayMicroseconds(60);

				if (count > 25)
					data[idx] |= (1<<cnt);

				if (cnt== 7) {
					cnt=0;
					idx++;
				}else{
					cnt++;
				}
			}
			if ((data[0]+data[1] == 0xFF) && (data[2]+data[3]==0xFF)) {    //check
				printf("Get the key: 0x%02x\n",data[2]);
				exec_cmd(data[2]);
			}
		}
	}

	bcm2835_close();
	return 0;
}

After writing, compile and run the c file:
gcc -Wall irm.c -o irm -lbcm2835
sudo ./irm

Press the remote control button, the terminal will display the key value received by the button.

Let talk about the python code:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time

PIN = 26;

GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN,GPIO.IN,GPIO.PUD_UP)
print("irm test start...")

def exec_cmd(key_val):
    if(key_val==0x45):
        print("Button CH-")
    elif(key_val==0x46):
        print("Button CH")
    elif(key_val==0x47):
        print("Button CH+")
    elif(key_val==0x44):
        print("Button PREV")
    elif(key_val==0x40):
        print("Button NEXT")
    elif(key_val==0x43):
        print("Button PLAY/PAUSE")
    elif(key_val==0x07):
        print("Button VOL-")
    elif(key_val==0x15):
        print("Button VOL+")
    elif(key_val==0x09):
        print("Button EQ")
    elif (key_val == 0x16):
        print("Button 0")
    elif(key_val==0x19):
        print("Button 100+")
    elif(key_val==0x0d):
        print("Button 200+")
    elif(key_val==0x0c):
        print("Button 1")
    elif(key_val==0x18):
        print("Button 2")
    elif(key_val==0x5e):
        print("Button 3")
    elif(key_val==0x08):
        print("Button 4")
    elif(key_val==0x1c):
        print("Button 5")
    elif(key_val==0x5a):
        print("Button 6")
    elif(key_val==0x42):
        print("Button 7")
    elif(key_val==0x52):
        print("Button 8")
    elif(key_val==0x4a):
        print("Button 9")

try:
    while True:
        if GPIO.input(PIN) == 0:
            count = 0
            while GPIO.input(PIN) == 0 and count < 200:
                count += 1
                time.sleep(0.00006)

            count = 0
            while GPIO.input(PIN) == 1 and count < 80:
                count += 1
                time.sleep(0.00006)

            idx = 0
            cnt = 0
            data = [0,0,0,0]
            for i in range(0,32):
                count = 0
                while GPIO.input(PIN) == 0 and count < 15:
                    count += 1
                    time.sleep(0.00006)

                count = 0
                while GPIO.input(PIN) == 1 and count < 40:
                    count += 1
                    time.sleep(0.00006)

                if count > 8:
                    data[idx] |= 1<<cnt
                if cnt == 7:
                    cnt = 0
                    idx += 1
                else:
                    cnt += 1
            if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF:
                 print("Get the key: 0x%02x" %data[2])
                 exec_cmd(data[2])
except KeyboardInterrupt:
    GPIO.cleanup();

Press the remote control key, the terminal will display the key value received by the key

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326094454&siteId=291194637