1. Trang chủ
  2. » Công Nghệ Thông Tin

Apress - Smart Home Automation with Linux (2010)- P9 pptx

5 319 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 0,94 MB

Nội dung

CHAPTER 1 ■ APPLIANCE CONTROL 23 Computer Control But far the most powerful and creative device available is a computer interface, such as the CM11, as shown in Figure 1-14. This is a transceiver that’s able to pass messages from the power line to the computer and send messages back from the computer onto the power line. Unlike most X10 devices, the power socket on the CM11 is not controllable by X10 and instead is a simple through port. Consequently, if you want to control your computer with X10, you have two options. ■ Caution Be wary about putting the computer’s power onto the normal house code, because you might accidentally switch it off when issuing an “all units off” message. First, you could assign the computer an unused unit code and configure the computer to issue a shutdown command when it is seen on the power line. (I’ll show you how shortly.) Second, you could use a separate appliance module and simply plug the computer into it. This is a workable although poor solution, since you’re likely to have the machine plugged into an uninterruptible power supply unit (UPS). Figure 1-14. The CM11EFL In addition to being a controller, this device can also act as an event scheduler and message-relay system, even when not connected to a computer. Therefore, you can use the software (that is, the supplied Microsoft Windows version or a Linux equivalent, such as Heyu) to program the device and let it run stand-alone, since this programmed information now lives within its own EEPROM, which retains the data even if there is no power, allowing it to be moved from one place to another without reprogramming. (This also means it’s possible to have a—slightly—automated house without a single computer!) However, you must keep a copy of the file and data that you uploaded to the CM11, since it is impossible to download it from the device. CHAPTER 1 ■ APPLIANCE CONTROL 24 ■ Caution When unplugging the CM11U from either the mains or the computer, always remove the serial cable from the device first, because stray noise from the cable can affect the internal memory and its settings. The event scheduler allows you to send any X10 messages at any time of the day, on any days of the week, between any dates of the year. On its own, the device doesn’t have the ability to vary the times randomly, but it does have a dusk and dawn setting that works after you’ve given it details of your physical location as a longitude and latitude. You can find your longitude/latitude from an atlas or (if we’re being serious for a moment) one of the many geo sites on the Web. Your IP address is often accurate enough for these calculations and is available from sites such as the following: http://api.hostip.info/get_html.php?position=true http://whatismyipaddress.com In CM11 parlance, the message-relay system is termed a macro. This allows an X10 message (such as “bedroom light on”) to spawn additional custom messages to any, or all, of your other equipment. A typical macro might consist of “landing light to 50 percent,” “bathroom light on,” and so on. These messages can be separated in time, allowing a single “bathroom light on” message to become a short program such as this: Bathroom light on Stairs light to 50% Wait 5 minutes Toilet light off Wait 2 minutes Stairs light off So, in short, the CM11 can provide most of the functionality an automated house could want, albeit in a very static way. For your CM11 to dynamically process X10 messages, you’ll need the computer on permanently and some software. Unfortunately, the software with which CM11 currently ships is for Microsoft Windows only. So instead, you can call on the community for software such as Heyu, which works as a replacement. Heyu Heyu is a simple command-line tool, available in most Linux distributions, capable of performing two- way communication with an X10 computer module and of programming the EEPROM with macros and scheduled events. You can also download it from the home page at www.heyu.org. This is not free software or open source as the OSI would consider it, but the source is available for free, and it is free to use. Once installed, the software autoconfigures itself when first run. This takes a few seconds and involves opening the serial port (/dev/ttyS0 by default) and verifying that the CM11 is truly plugged in and working correctly. The best way of doing this is to include Heyu in the startup sequence by running the following command: heyu engine CHAPTER 1 ■ APPLIANCE CONTROL 25 This ensures that the Heyu background process is running, which allows incoming messages to be picked up, triggering external scripts. The engine parameter also starts the state machine inside Heyu, allowing it to remember the last setting for each lamp and appliance, which is useful since many devices (especially the cheaper ones) do not let you query their status. In a noncomputerized environment, this feedback loop is unnecessary since, as a human, you can see whether the light came on when you pressed the button, so you can see if you need to try again. A computer is not as talented. It is also good design practice for any computer interface to indicate the module’s current state, making this feature more important. If you are likely to be using a lot of computer-based interfaces in your home (say through a web page), then it can be worth upgrading to the two-way lamp and appliance modules covered earlier. Configuration The configuration is held within various files inside /etc/heyu, specifically x10.conf, which holds the serial device, default house code, aliases, scenes, and scripts. By default all log information is written to /var/log/heyu. Aliases, as the name suggests, provide a human-friendly form of the house and unit codes for each device you want to set up in the x10.conf file along with whether the device is a lamp module (StdLM) or an appliance module (StdAM). ALIAS lounge e5 StdLM ALIAS stereo e6 StdAM Once specified, the alias can be used within the configuration file and in the commands issued upon it. This abstraction reduces the number of changes necessary should you ever need to renumber your house appliances. Scenes are Heyu’s way of describing relay messages or macros. Each line contains a label name and a list of semicolon-separated commands. SCENE movie_mode on tv; on stereo; dimb lounge 10; The dim range in Heyu is between 1 and 22 and is supported by relative and absolute brightness change commands (dim and dimb, respectively). Note that if you change the Heyu configuration file while it’s running, you must issue the following command to refresh the parameters: heyu restart Sending Messages This is done simply with commands such as the following: heyu turn studio on heyu onstate studio heyu bright lounge 5 heyu lightsoff _ # the underscore means current house code which can be placed in larger shell scripts, called out from other languages, or triggered through a web site with CGI. Note that these are all blocking commands, so the rest of the script won’t execute until the X10 messages have been sent—unless you begin the task in background mode, of course. CHAPTER 1 ■ APPLIANCE CONTROL 26 heyu turn studio on & These commands can also be placed in your crontab, saving the need to upload changes to the CM11U’s internal EEPROM. export EDITOR=vi crontab -e Then as a sample line, add the following: 30 9 * * 1-5 /usr/bin/play /usr/share/sounds/alsa/Noise.wav This adds an alarm call at 9:30 a.m. (when else!?) on every day of the month (the first wildcard) in every month of the year (second wildcard) when it’s also a weekday (Monday=1, Friday=5). If you want to add a random element, say within half an hour of 9:30, then you can use some simple bash to instead call this: 00 9 * * 1-5 sleep `echo $((RANDOM%60))m`; /usr/bin/play  /usr/share/sounds/alsa/Noise.wav Note that I’ve begun the delay 30 minutes earlier but created a random value that lasts up to 60 minutes. Receiving Messages Whenever a command is received, Heyu is able to launch an external script as specified in the configuration file. In many cases, this might be to switch on additional lights, acting like a scene or macro: SCRIPT bedroom on :: /usr/local/bin/heyu turn bedside_light1_mine on SCRIPT bedroom on :: /usr/local/bin/heyu turn bedside_light2_theirs on Instead of controlling only lights, it could run an external script. This has the benefit of being editable by a user other than root, it doesn’t require a heyu restart, and it provides a lot of flexibility that can’t be squashed onto a single line. Code such as the following: SCRIPT bedroom off :: ~steev/bin/housenight will run a private script that switches off all the important lights and appliances in the house (remember that there is no “all units off” command), post something to your Twitter feed, and play a “Good night” sound effect. #!/bin/bash wavplayer default play ~steev/media/good-night-gorgeous.wav tweet Good night all /usr/local/bin/heyu turn studio_light off /usr/local/bin/heyu turn kitchen_light off CHAPTER 1 ■ APPLIANCE CONTROL 27 /usr/local/bin/heyu turn lounge_light off shutdown –h now Alternatively, it can detect a message sent by a sensor transmitter that goes to no device at all but is relayed to several others: SCRIPT pir_detect_msg on :: /usr/local/bin/heyu lightson _ Instead of controlling X10 devices, you can also control the PC itself. This example intercepts the messages from a single switch to control the volume of the PC to which the CM11 is connected: SCRIPT E6 on :: /usr/local/minerva/bin/mixer default dec master 10 SCRIPT E6 off :: /usr/local/minerva/bin/mixer default inc master 10 These commands are run with the same user privileges as whoever issued the initial command: heyu engine This ensures the commands and devices (such as /dev/dsp) are available to this user. It is possible to build complex scripts and interactions solely using X10 messages. In Chapter 7 I’ll discuss Cosmic. Programming the EEPROM All the functionality of the CM11’s EEPROM is available for programming through Heyu. You simply create a text file called /etc/heyu/x10.sched (there is a sample file in this directory also) with a suitable list of commands and type while the CM11U is connected: heyu upload The process will convert this text file into a suitable binary image and upload it to the device through the existing serial cable. Since it is impossible to retrieve this data from the CM11, you will want to ensure you keep a backup of the x10.sched file or the resultant image for later use: /etc/heyu/x10image The full details of the x10.sched file format are available in the manual, including how to switch date formats to DMY from the default YMD. For now, I’ll include some fragments of my own schedule by way of an example: macro movies_on 0 dimb lounge 22; 0 on tv; 0 on stereo; macro lounge_off 0 off lounge; 0 off lounge_table; 0 off tv; 0 off stereo; timer wt 01/01-12/31 21:00 00:02 movies_on lounge_off trigger e1 on movies_on . is a simple command-line tool, available in most Linux distributions, capable of performing two- way communication with an X10 computer module and of programming the EEPROM with macros and scheduled. supplied Microsoft Windows version or a Linux equivalent, such as Heyu) to program the device and let it run stand-alone, since this programmed information now lives within its own EEPROM, which retains. If you are likely to be using a lot of computer-based interfaces in your home (say through a web page), then it can be worth upgrading to the two-way lamp and appliance modules covered earlier.

Ngày đăng: 03/07/2014, 20:20

TỪ KHÓA LIÊN QUAN