
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
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.confFollow 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
Now check the transmitter. We start the demon lirc. To send IR commands, execute:
xbian @ xbian ~ $ irsend SEND_ONCE TV KEY_POWERWhere,
SEND_ONCE - directive, send once
TV - The name of the TV from the file
/etc/lirc/remotes/tv_samsung.confKEY_POWER - the name of the command from the file
/etc/lirc/remotes/tv_samsung.confAfter 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
Making the script executable:
xbian @ xbian / var / www / cgi-bin $ sudo chmod + x main.cgiRun apache and try to turn on the TV from the browser:
192.168.1.23/cgi-bin/main.cgi?KEY_POWERif the server is working, we will get back:
Send IR code: KEY_POWERI 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 OpenRemoteAll comments and suggestions for improving the article accept in the comments!
Have a nice control!