Apress - Smart Home Automation with Linux (2010)- P13

pdf
Số trang Apress - Smart Home Automation with Linux (2010)- P13 5 Cỡ tệp Apress - Smart Home Automation with Linux (2010)- P13 245 KB Lượt tải Apress - Smart Home Automation with Linux (2010)- P13 0 Lượt đọc Apress - Smart Home Automation with Linux (2010)- P13 0
Đánh giá Apress - Smart Home Automation with Linux (2010)- P13
4.9 ( 11 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CHAPTER 1 ■ APPLIANCE CONTROL ■ Note You may need an IR bypass kit when passing IR signals over coax cables because the messages get muddled when passing through distribution amplifiers. IR-RF-IR Gateways These devices relay IR data through the air, at the 433MHz radio frequency used by so much wireless equipment, before being replayed. For these devices, you have a choice between IR-only transmissions and TV senders. An IR-only transmitter, such as the Powermid XL, is the simplest of these devices and will allow you to remotely control devices without installing cables or sockets. They are fairly cheap but pass only IR data, so the controlled device must be able to have an impact on you when you’re in another room. TV senders are the wireless versions of the over-the-aerial cables or old TV distribution systems, which involved an aerial amplifier and a separate aerial cable into each TV in the house. The TV sender takes a single input and transmits it to whichever receivers are listening, encoding whatever IR signals it also saw. There are many variants on the market, including those with SCART sockets (instead of the oldschool coaxial aerial sockets) and RCA composite video. Even the cheaper models often have a “channel” switch on them, allowing multiple receiver-transmitter pairs to be used in the same house without the signals getting mixed up. And with these devices becoming more mainstream, some are almost as cheap as an IR-only transmitter, with the TV functionality becoming a free bonus feature. IR Over IP It is also possible to send data over your existing Ethernet cables, using devices such as the Keene IR Anywhere over IP (KIRA). This eliminates any distance or interference issues you might get from the other methods and also provides a way of remotely controlling IR devices from a computer, without needing to have the computer and its IR transmitter physically in range of the device. Being IP-controlled also means that IR signals can be sent via the Internet. Although this is pointless in itself (because you can’t derive any benefit from changing the TV channel when you’re not sitting watching it), it does provide an off-the-shelf way of controlling IR-based devices from a remote computer. And if something can be controlled from a computer, then it can be controlled from anything connected to the computer, such as a web page or cron job. Using KIRA to retransmit IR codes first requires that you teach it those codes in the first place. This is done by generating text files, using the software shown in Figure 1-15. 43 CHAPTER 1 ■ APPLIANCE CONTROL Figure 1-15. Configuring KIRA 15 This software is available from the Keene web site and has been thoughtfully written in Java, making it Linux-friendly. After attaching KIRA to your network and after it’s used your DHCP server to provide it with an IP address, you can add new commands. First you request that all the IR messages are 15 44 See www.keene.co.uk/electronics/multi.php?mycode=KIRA. CHAPTER 1 ■ APPLIANCE CONTROL sent to this machine, and then you press Learn before hitting the first key on your remote. This should present the code, such as the following, which can then be copied and pasted into a text file for later use: K 240C 037F 0369 03AC 034D 0624 035A 0378 0382 0378 0381 0396 0366 0377 0382 0396 0365 0396 06E0 03AF 034C 072C 0353 0378 2000 I have used a directory hierarchy for each device so that the on/off button for my TV is in the directory ir/tv/codes/on. Since the same button performs both functions, I created a symlink between off and on. Those with bigger houses and more TVs might like to use a more descriptive name than TV. Although KIRA has a web page, it isn’t very configurable and limits you to four prestored IR codes. Fortunately, it also listens for commands sent on UDP ports 30303 and 65432. The former is for device discovery and configuration, so consequently the port cannot be changed. The latter is the IR control port, which processes all the basic commands to control the various IR devices within range. All responses to these commands are returned by UDP also, so you need to run two instances of the Swiss Army knife of network utility, netcat, to handle it. Begin by creating two terminal windows, and start a UDP server in one of them by typing the following: nc -u -l -p 30303 This will now listen on port 30303 for any UDP messages sent to it. Now, in the other window, send a message to KIRA (whose IP has been determined as 192.168.1.111 by the DHCP server) on the same port. echo disD | nc -q 0 -u 192.168.1.111 30303 You should see the other window spring to life and report various stats about the device. If not, check that the ports are open and working with (that other Swiss Army knife of networking) netstat: netstat -ntpl With some averagely clever bash coding, you can achieve the same result with a script such as the following: #!/bin/bash TEMPFILE=`mktemp` nc -u -l -p 30303 >$TEMPFILE & PROCESS=$! echo disD | nc -q 0 -u 192.168.1.111 30303 # Wait for a second so the output has finished writing sleep 1 kill $PROCESS cat $TEMPFILE rm $TEMPFILE 45 CHAPTER 1 ■ APPLIANCE CONTROL The process for sending IR messages is the same, except you need to switch onto the IR port. Here’s an example: cat ir/codes/tv/off | nc -q 0 -u 192.168.1.111 65432 The only response sent to port 65432 is ACK, which can be safely ignored. However, if you do decide to listen to port 65432 (and you have requested that all IR messages are forwarded to your PC), then you will see the key codes appear. These can be copied from the terminal window (instead of using the web interface) into your own configuration files. There is a supplied API document detailing each of the commands that each port handles. Note that by using your Linux machine to maintain the IR codes, you don’t need to ever upload keycode files to one of the four slots on its web server. ■ Note You can several KIRA devices on your network, each transmitting messages to different devices, although for KIRA to receive messages and send them back to a PC, you must explicitly enable the functionality from its web interface and give it the IP address where netcat, or similar, will be listening. IR Control If you have a PC fairly close to the IR devices you want to control, it is easy to add a suitable USB-based transmitter or receiver to it. These can be either bought from places like RedRat or built from one of the circuit diagrams provided by the LIRC (Linux Infra-Red Remote Control), which also provides the software. ■ Caution Most PCs attach USB ports directly to the motherboard, and if you make a mistake when building your own device, you could destroy it. Purchasing a separate PCI board with USB sockets should provide some protection from mishaps. For IR transmission, you need to know the specific control codes of the device you want to control. If you have a standard TV or video, this data is usually available online (http://lirc.sourceforge.net/remotes/); otherwise, you will also need to purchase or build an IR receiver to teach LIRC the existing codes. Fortunately, if you took the earlier hint and bought a RedRat, you will have a receiver built in that, along with the supplied Windows software or LIRC, can be used to program the codes directly. 16 LIRC is the Linux-standard method for reading and transmitting IR data. It comprises a standard daemon, lircd, and a set of tools to record the input messages and transmit them back again. It adopts a 16 46 http://lirc.org CHAPTER 1 ■ APPLIANCE CONTROL modular approach to support the wide variety of LIRC devices available. Reproducing an installation guide here would be foolhardy, but suffice to say there are three main types of supported control: GPIO devices: These are generally supplied with TV cards, such as those from Hauppauge. The modules are usually compiled into the standard daemon build. Serial port device: This covers a wide range of different devices, including home-brew transmitters, and because they process serial data directly, they don’t need any specific driver code. Typical circuits are available from the LIRC web site. If you’re unsure about connecting your own electronics onto your PC motherboard, you can buy serial PCI cards, which offer a level of protection against rogue electronics. Kernel drivers: These, such as the RedRat3 device, require you to build LIRC from source and (in some cases) copy the new driver code into the LIRC directory. From here you can rebuild the setup (data2setup.sh) file and build as normal. These devices will make use of the /dev/lircd device, which should have ugo+rw privileges. 17 Once built, the drivers can be configured and prepared according to the table at LIRC, which also provides sample configuration files for the various devices that describe each button with a humanfriendly name and the details of the IR signal to be sent. From here, you can add specific commands to be triggered upon the various button presses within the .lircrc file, which has a format typical of this: begin prog = mythtv button = Rew config = Left end Each button on the remote is mapped to a function of the software (prog) in this fashion. One of the big benefits in using RedRat, and LIRC in general, is its inclusion in many standard media players, mixers, and TV applications. Consequently, if this is your primary purpose of IR, then you have completed your media installation already since those commands can trigger something useful in the existing software! LIRC also has a network mode whereby you can communicate with an LIRC daemon through a network socket, allowing an external application to act as if it were a local IR remote control. This is useful for testing and as a method of remotely a PC-based media player without writing new code. For more specific applications, you will need to make use of irexec, as shown here: begin prog = irexec button = ok config = /usr/bin/someprogram with arguments here end 17 www.lirc.org/html/table.html 47
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.