📜 ⬆️ ⬇️

Manage any AV-technology from the phone. IR transceiver for Raspberry


Slowly making my house a little smarter. First I made the light control from the phone using the RaZBerry (Z-Wave) expansion card for Raspberry, then I got into programming the AVR microcontrollers and assembled a small weather station showing the temperature on the LED display. Now the turn has come to control TV using the iPhone.
I use Raspberry with the Xbian distribution as a media player, so I immediately understood how I would control the TV. Raspberry stands on the shelf under the TV in direct line of sight. On one of the GPIO pins, you need to install the IR LED from the remote and give it signals that my TV understands. In the implementation of the idea was not difficult!
Next, we will discuss how to solder the IR transceiver card for the Raspberry and how to set up the software to switch channels from the phone, adjust the volume, turn the TV on and off.

To control TV, you need an IR transmitter, and to find out which signals to transmit TV you need to scan its remote control, for this you need an IR receiver, so the Raspberry board will contain both a receiver and a transmitter. The board should take up little space and be worn on the GPIO. The receiver uses GPIO 11, a GPIO 9 transmitter, plus 3.3V power and ground, for a total of 4 pins are involved.
Here is a basic circuit diagram:

It includes:
1) R1 - 1 kΩ (resistor, limits the base current to 2.5 mA, the maximum base current that can withstand the GPIO and not burn 16mA)
2) R2 - 33 ohms (resistor, limits the current on the LED to 50mA, thanks to this, the signal is strong, the range is more than 5 meters)
3) Q1 - BC547 (transistor for signal amplification)
4) D1 - IR LED from the 36-38 kHz panel (IR transmitter)
5) IR - TSOP1738 (IR receiver)

The figure indicates which GPIO zone the module occupies and which outputs it uses.


Further, with the help of technology LUT manufactured a small board. Soldered:

')
And installed in the Raspberry, the module takes the bottom 5x2 GPIO pins to hold tight. You can take and 5x1 conclusions.


The board is ready and installed, as a minus, I note that the IR receiver is too bulky and does not fit in a transparent Raspberry case, you can use any other more compact IR receiver at 38KHz.

Now the software, in order, I will decompose what we need:
1) Lirc for reading the console and for transmitting IR commands
2) A server that accepts HTTP commands sends IR signals via lirc
3) A client phone application that sends HTTP commands to the server

To begin with, let us tell you what conclusions lirc should use to receive and send IR signals, for this we fix / etc / modules so that the necessary modules are loaded:
xbian @ xbian ~ $ cat / etc / modules
# /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be loaded # at boot time, one per line. Lines beginning with "#" are ignored. # Parameters can be specified after the module name. lirc_dev lirc_rpi gpio_in_pin=11 gpio_out_pin=9 

To create the interface / dev / lirc0 add to / boot / config.txt :
 dtoverlay=lirc-rpi,gpio_in_pin=11,gpio_out_pin=9 

Reboot, install lirc, if its daemon is running, then stop it. Check if our module is working with the command:
xbian @ xbian ~ $ sudo mode2 -d / dev / lirc0
 space 16777215 pulse 4534 space 4421 pulse 621 space 1642 pulse 582 space 1648 

If the numbers crawled when you press the buttons, then the IR receiver works.
Now we consider the remote control command:
xbian @ xbian ~ $ sudo irrecord tv_samsung.conf
Follow the prompts of the program and you will receive a config tv_samsung.conf with a set of codes from your console.
We move the received config in / etc / lirc / remotes / .
Here is my config for the main buttons:
xbian @ xbian ~ $ cat /etc/lirc/remotes/tv_samsung.conf
 # Please make this file available to others # by sending it to <lirc@bartelmus.de> # # this config file was automatically generated # using lirc-0.9.1-git(default) on Tue Sep 3 19:29:12 2013 # # contributed by # # brand: /home/xbian/lircd2.conf # model no. of remote control: # devices being controlled by this remote: # begin remote name TV bits 16 flags SPACE_ENC|CONST_LENGTH eps 30 aeps 100 header 4532 4422 one 591 1650 zero 591 540 ptrail 595 pre_data_bits 16 pre_data 0xE0E0 gap 107530 toggle_bit_mask 0x0 begin codes KEY_POWER 0x40BF KEY_UP 0x48B7 KEY_DOWN 0x08F7 KEY_VOLUMEUP 0xE01F KEY_VOLUMEDOWN 0xD02F KEY_MUTE 0xF00F end codes end remote 


Now check the transmitter. We start the demon lirc. To send IR commands, execute:
xbian @ xbian ~ $ irsend SEND_ONCE TV KEY_POWER
Where,
SEND_ONCE - directive, send once
TV - The name of the TV from the file /etc/lirc/remotes/tv_samsung.conf
KEY_POWER - the name of the command from the file /etc/lirc/remotes/tv_samsung.conf

After executing this command, the TV should turn on or off, if everything works, we proceed to control from the phone.
We need a server that will accept HTTP requests and execute the irsend command, I picked up the Apache http server and set up the execution of cgi-bin scripts. In my Xbian distribution, this is done in the / etc / apache2 / sites-enabled / 000-default file, allowed the execution of cgi-bin scripts and indicated where to look for them
 .... ScriptAlias /cgi-bin/ /var/www/cgi-bin/ <Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> .... 


Then I wrote a script on bash that processes the HTTP request and executes the irsend command:
xbian @ xbian ~ $ cat /var/www/cgi-bin/main.cgi
 #!/bin/bash echo "Content-type: text/html" echo "" echo "<html><head><title>Welcome</title></head>" echo "<body>" echo "Send IR code: " echo $QUERY_STRING irsend SEND_ONCE TV $QUERY_STRING echo "</body></html>" 


Making the script executable:
xbian @ xbian / var / www / cgi-bin $ sudo chmod + x main.cgi

Run apache and try to turn on the TV from the browser:
192.168.1.23/cgi-bin/main.cgi?KEY_POWER
if the server is working, we will get back:
Send IR code: KEY_POWER
I think it is clear that after /cgi-bin/main.cgi? you need to enter the command from the /etc/lirc/remotes/tv_samsung.conf file

HTTP API is, now you can control the TV from any device, for the browser you can write a simple application with links to the TV control commands. I wrote a simple application for the iPhone in 15 minutes.


If you do not have programming skills for mobile platforms, but you really want to, then you can use the OpenRemote software product, use the OpenRemote designer to design the application, arrange the buttons, and then specify what HTTP command to execute in the button properties, how I use this software in the article Mobile applications for managing smart home based on Z-Wave using OpenRemote

All comments and suggestions for improving the article accept in the comments!
Have a nice control!

Source: https://habr.com/ru/post/193572/


All Articles