CHAPTER 5 ■ COMMUNICATION 173 • Finally, remember that most system commands are blocking. That is, they don’t return until they’ve finished their task. So, when the task is being called from inside a web page, the user will be at a blank web page (with the “waiting” cursor) until the page has completed. Consequently, any output or error codes from the command cannot be shown on the page. Instead, you will have to write your software so that: • Your command executes asynchronously, using shell_exec("cmd &") or similar. • You can update the error or output status through Ajax. • You can retrieve error states through a secondary command, issued after an asynchronously command invocation. None of these are unsolved problems, but it is an extra layer of complexity for those wanting to write their own home automation web applications. Media Access One common piece of functionality is to provide access to your music collection from outside home, such as from the office. Several Apache modules are available to handle this; one of them is mod_musicindex (http://freshmeat.net/projects/musicindex/). Although capable of being used to list general-purpose directories (as it does for its own online documentation), it is capable of rendering music-specific icons to let you download and/or stream this music anywhere in the world and create playlists interactively for the current folder and all the subdirectories underneath it. To prepare an online portal for your music, first create a directory inside your web directory: mkdir music Then create an .htaccess file inside, granting permissions to whichever users you see fit. These permissions apply to this directory and every one underneath it, unless superseded by another .htaccess file. Since your music collection is likely to be stored outside of the web root, you must add a symlink to it: ln -s /net/media/mp3 mp3 This also highlights the reason you created a separate media directory in the root—it eliminates the need for web-specific files polluting the directory structure of our nonweb media file hierarchy. You can then add the appropriate configuration lines to your virtual site configuration file, such as 001- homecontrol: <Directory /var/www/sites/homecontrol/media> Options Indexes FollowSymLinks MultiViews MusicIndex On +Stream +Download +Search -Rss -Tarball MusicSortOrder filename album disc track artist title filetype filename MusicFields title artist album length bitrate MusicPageTitle Media Jukebox MusicDefaultCss musicindex.css </Directory> CHAPTER 5 ■ COMMUNICATION 174 Then reload the Apache configuration in the usual way. This provides a functional but less than beautiful page, such as that shown in Figure 5-2. Figure 5-2. An example of music index and your music being available anywhere Playback on the client side is a simple matter of installing a network-friendly media player, such as VLC. When your browser first encounters an unknown type (such as mp3 or m3u), it will ask for a suitable application to launch. If possible, you want to set this up so that each new song is queued in the media playlist, instead of launching a separate instance of the player. This is known as enqueuing. Some browsers (such as Firefox) are often supplied with media plug-ins that take control of all media and attempt to play the media in the browser itself. This is generally undesirable, so by using the File Types menu option, you can remove this association and apply it manually. An alternative package with similar scope is smb2www. As the name suggests, this provides access to all your Samba-related shares from across the Web. This has the advantage of being incredibly flexible and eliminates the need for specific symlinks to each folder you want to share but at the expense of opening a lot of your personal network to the outside world. Although I have this installed, I keep it switched off by default and switch it on (by entering through an SSH session) only when needed, that is, when I need to access a Windows machine that doesn’t allow remote connection. When your server is often under a heavy load, that is, when it’s used as a media server, then smb2www has the benefit of not requiring a reboot after changing its configuration. The new configuration is available immediately after editing the file: vi /etc/smb2www/smb2www.conf or using the following: dpkg-reconfigure smb2www SMS The lowest common denominator in bidirectional wireless communication is undoubtedly the Short Message Service (SMS), or text message. This protocol exists as part of the mobile phone network infrastructure, making it zero cost for the provider and therefore low cost to the consumer, with many networks providing free text messages as part of their monthly package deals. Despite the rise of mobile CHAPTER 5 ■ COMMUNICATION 175 Internet, the SMS remains a well-used protocol, especially among the young, for communication. To make use of SMS within your home, you can use one of two approaches to send and receive messages. The first and most obvious way is to perform all the processing with a secondary mobile phone connected to the computer. There is also the second method whereby a telecoms company provides you with a pseudomobile number that acts in same way as a physical phone, except you use it with an API rather than a keypad. In some cases this API is as simple as an SMTP gateway. In both cases, there are command-line tools to handle the telecoms data, so the method you choose comes down to financial preference. ■ Note Some hardware devices will control power lines on receipt of a mobile phone call (like the GSM Remote Control Switch; see http://www.gsm-auto.com), but their functionality is limited and often more expensive than the home-brew equivalent. Processing with a Phone This is the quickest way to experiment with a SMS-controlled home because most people have (at least) one old phone at home or one from a partner that can be borrowed for testing. Even without an existing device, the cost of a simple pay-as-you-go device is not that great. You will also need a valid subscriber identity module (SIM) card and a connecting cable to your computer. A number of phone shops (and even supermarkets) sell SIM cards containing very low credit and are ideal starting points if you don’t have a second card of your own. Most mobile phone packages come in one of two varieties, each with particular merits in the HA field. • Contract deals are cheap to begin with, because the cost of a (new) phone is subsidized but expensive to maintain. Unless you convert all your all e-mail to text messages, it is unlikely you will ever make full use of the “free SMS for life” deals to make it worth the money you pay out every month on the subscription. • Pay-as-you-go deals provide a comparatively cheap barrier to entry, since the bulk of the cost is up front and a virtually nil running cost. This is more true if you have a surplus phone from, say, a previous upgrade. The price of individual messages will be higher (than free!), but since most HA installations send very few messages, this is a worthwhile trade-off. If your software goes haywire and issues too many messages, however, you will quickly exhaust your credit, causing further (and potentially more vital) communications to be lost. ■ Note The quality or age of the phone isn’t important since it will be permanently plugged into a PC at home and unlikely to suffer the abuse of daily life. The specific make of phone will depend on the software used. There are a couple of open source projects here, with most supporting the majority of functionality present on the Nokia devices, along CHAPTER 5 ■ COMMUNICATION 176 with some Sony Ericsson handsets. Our basic requirements from a software point of view is that you should be able to send and receive messages to our phone. Access to the phones address book is useful but not necessary, since that can be better represented in software. It should also work as a command- line tool. Gnokii (http://www.gnokii.org) has been the leading software in this field for a while, and its technology has spawn several forks over the years. Its name presents the fact that the majority of supported devices are Nokia-based, although devices do work with a standard cable. (See http://wiki.gnokii.org/index.php/Config for a list of known good devices.) For others, you may have more luck using the Bluetooth driver. The setup, provided you have a compatible phone, involves a simple configuration file such as the following: [global] port = /dev/ttyACM0 model = AT connection = serial where the port can be determined by dmesg after plugging in your phone, although some others are chosen according the make and model of your phone. (Determine this from the web site at http://wiki.gnokii.org/index.php/Config.) Once it’s plugged in, you can issue the following to determine that the connection is working: gnokii identify Even though the phone might be able to communicate with Gnokii, the available functionality can vary. So, don’t make critical changes your phone (such as writing data into the address book) without a suitable backup. 14 The easiest functionality to test and demonstrate is that of sending a text message. This is also the most widely supported. echo "This is a test message" | gnokii sendsms myphonenumber The receiving of messages is no more involved, depending on what you want to do. To simply retrieve all of your messages, you can execute the following gnokii getsms ME 1 end This writes every SMS from your internal phone memory to the screen, where it could also be redirected into a file or parsed. There is a built-in parser, which will format text messages into that of an e-mail and append it in your inbox. gnokii getsms ME 1 end -a /var/mail/steev 14 Gnokii is able to provide this backup for you with gnokii getphonebook ME 1 end vcard >myphonebook.vcf . CHAPTER 5 ■ COMMUNICATION 177 Since this is an issued command, using received messages to control home devices takes a little work but is feasible, since you need to poll the phone periodically. An implementation would first require you need to keep a count of the messages you have in the inbox. This is not directly available, since the command reports all messages from every inbox: $ gnokii showsmsfolderstatus GNOKII Version 0.6.26 No. Name Id #Msg ============================================ 0 Internal memory ME 92 1 SIM card SM 0 However, since we’d be parsing each message anyway, it isn’t any more difficult and doesn’t matter that you might also download the same message you sent out previously. So, you get the number of total messages, like this: #!/usr/bin/perl my $status = `gnokii showsmsfolderstatus 2>/dev/null`; $status=~/ME\s+(\d+)/; my $count=$1; After retrieving the last total (held in whatever temporary or log file you decide to use), you can recall only the new messages and then process them accordingly: for(my $msg=$lastCount;$msg<=$count;++$msg) { my $txt = `gnokii getsms ME $msg $msg 2>/dev/null`; if ($txt=~/Inbox Message/) { $txt=~/Date\/time\:(.*?)\n.*?Sender\:(.*?)Msg.*?\n.*?Text\:\n(.*)/; my $date = $1; my $sender = $2; my $message = $3; # process here } } Using messages to control other devices requires us to create a standard format and stick to it. The core elements in an SMS—and indeed, any message—are, from address to address and message. You can use the from address to validate the user and the message to execute commands on the local machine. The case study for message systems comes in Chapter 7. ■ Note It is possible to connect two phones into one machine. This allows you to use one that transmits standard messages with your daily schedule or reminders and a second for any emergency “house alert” messages that need to get through. In this way, should the first run out of credit, you will still receive the high-priority messages. . http://www.gsm-auto.com), but their functionality is limited and often more expensive than the home- brew equivalent. Processing with a Phone This is the quickest way to experiment with a SMS-controlled. SMS-controlled home because most people have (at least) one old phone at home or one from a partner that can be borrowed for testing. Even without an existing device, the cost of a simple pay-as-you-go. file, such as 00 1- homecontrol: <Directory /var/www/sites/homecontrol/media> Options Indexes FollowSymLinks MultiViews MusicIndex On +Stream +Download +Search -Rss -Tarball MusicSortOrder