Thursday, June 20, 2013

Tailing a File in Windows

There is an easy and very useful command in most Minix systems called tail.  Essentially tail shows the last bit of a file.  This can be really handy when you're trying to read a log file with 10,000 lines in it.  You can use tail to view the last 10 lines in the file.  While no such command exists for the native Windows command interpreter, there is an option in Windows PowerShell.  It's pretty easy to use and since PowerShell is built into all Windows systems XP and later, it's a handy way to get similar functionality without having to download or install anything.
However, the command isn't called tail, it's called Get-Content.  Here are some good examples and some of the options.  The one I like is:

This returns all lines of the file specified in [filename] and waits for the file to be changed.  When the file changes, the display is updated.  This is great for when you want to watch a log file for new entries.

Friday, June 7, 2013

NetVoyant Trap Processing

I decided to go through some of the content I've garnered over the years and make a few videos.  This one shows how NetVoyant goes through its processing of incoming traps.  Enjoy!

Monday, June 3, 2013

AirPrinting with an HP printer

If you have an iOS device and an HP printer, you might have been disappointed to find out that even though the HP printer uses AppleTalk (the protocol behind AirPrint), you can't actually print to the printer from you iOS device.  This has been one of the main reasons my wife hasn't completely abandoned her desktop PC.

Here's how I fixed it:
Use a little utility called AirPrint installer.  It installs a windows service that reshares the printers from your Windows PC using AppleTalk (the version that is compatible with iOS 6).  I tried doing it without the reg fix, but the printers wouldn't work.  After the reg fix, sure enough, I saw the two printers I have connected to my PC but they both had padlocks.  I used the recommended fix (enabling the guest account on my pc) and they started working.

Here's the utility: http://forums.macrumors.com/showthread.php?t=1293865

PiTunes

Previously, I posted my experience automating iTunes for use in the nursery.  That has been working very well and I wouldn't normally see any reason to change it.  However, since I recently acquired a Raspberry Pi, I've had several ideas about what to do with it.  The idea that won was to replace the nursery room computer (which was already a Dell Studio Hybrid small form factor PC).  The advantages would be that the Pi is fanless and quite a bit smaller.  I could also then reuse the Dell Hybrid for other things.

The same objectives apply:
  1. I want the music to come on and turn off by itself
  2. I don't want the music to start off at full blast in case we've put the boys down early.    
  3. I want the music to play whenever we put the boys to sleep, which could be as early as 7pm but as late as 9pm.
  4. I want to be able to rotate playlists so it's not the same thing every night.
  5. I want to be able to kick off the music any time.
  6. I want to be able to manually override either the current song or the current volume level.
So, of course the first thing I did was download the Rasbian image and get it booted up, connected via SSH, and downloading updates.  I'll assume if you're also trying to do this that you'll use the instructions at RaspberryPi.org to get you started with that.  To get the most recent updates, type sudo apt-get update && sudo apt-get upgrade.  This may take a while, depending on your internet speed.  The Raspberry Pi doesn't have that much CPU either, so you might just kick this one off and go to bed.

I also purchased a Netgear G54/N150 Wireless USB Micro Adapter and plugged it in.  I'm a Windows guy, so most of what Linux does appears magical to me.  I had a suspicion that the Raspberry Pi would get on my wifi all by itself.  Of course it didn't because at the very least it didn't have my SSID and WPA pre-shared key (the password).  I was lucky enough that the Raspbian distribution recognized it and fired it up without requiring me to load any drivers (thank the stars!).  I verified this by issuing an iwconfig command.  This showed me that it was all ready to go.  Since my network uses WPA2 encryption, I had to configure a built in utility called supplicant.  In the end, all I had to do was edit /etc/wpa_supplicant/wpa_supplicant.conf and add a couple lines:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Hobbiton"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="yeahright"
}

Much documentation online says all you have to do to get it working is bring the interface down and back up.  I wasn't able to get it to grab a DHCP address without rebooting the Pi. To get the interface to come online at boot time, I added ifup wlan0 to /etc/rc.local.

After I got the Pi up and running, it was time to install a few things that aren't included in the base image.  Mainly, I needed mplayer (that's the media player that will actually play the music) and ffmpeg (a bunch of codecs that will allow mplayer to play all kinds of files including mp3 and m4a).  I also wanted to be able to access the log file from wherever, so I decided to install a lightweight web server.  I'll log to index.html in the www folder.  Type sudo apt-get install lighttpd mplayer ffmpeg and sit back for a while.    

The next thing is to get the actual music onto the Raspberry Pi.  At first, I started out by putting all the music (and my scripts) on a USB stick.  In order to give the Pi access to a USB drive, just follow the instructions here.  I eventually went instead with a network attached hard drive, so I followed the instructions here to create a persistent mount to my NAS.  After a while, I noticed some jitter in the music, so I moved the data back to the SD card (where the OS is also installed).  I'm trying to listen now to see if that makes a difference to the jitter.

Now, on to the script.  The script runs as a cron job.  The comments should explain exactly what's happening.

By the way, the contents of /mnt/nas/PiTunes are: christmas/  nothing/  music/  play.sh  waves/.  Version 2 of the script will play the Christmas directory instead of the music directory during the month of December.

The next part involves running the script from CRON, which is equivalent to Windows' Scheduled Tasks.  To add jobs, just run crontab -e.  The format is one job per line, parameters separated by spaces:


Mine looks like this:


So, let's look at the objectives again:
  1. I want the music to come on and turn off by itself - check!
  2. I don't want the music to start off at full blast in case we've put the boys down early - check!
  3. I want the music to play whenever we put the boys to sleep, which could be as early as 7pm but as late as 9pm - check!
  4. I want to be able to rotate playlists so it's not the same thing every night - check!
  5. I want to be able to kick off the music any time - this can be done, although it's not as easy.  I have to ssh to the Raspberry Pi and kick off the script manually.  However, if it's a silent day, nothing will happen.  I'll probably look at a way to adjust the script so that when it's called from CRON it does its normal thing but when called from command line it always plays music.
  6. I want to be able to manually override either the current song or the current volume level - This can also be done, but it's not as easy.  While in ramp up or ramp down mode, the volume doesn't increase/decrease by 1, it gets set to a particular level.  Overriding the volume during ramp up/down won't make any difference because as soon as the script loops it will reset the volume to right back where it was.  I may be able to change this in the future.  While playing, I can change the volume by logging in via SSH and issuing amixer -q set PCM X% where X is the volume I want to set to.  However, during ramp down, the volume starts back at 100% before dropping down.  It may not take much, but the next version may have an increment/decrement instead of fixed volume levels.  If I get this working, this objective will be met.
Some of the plans I have for the future are an automatic switch over to Christmas music instead of regular music during the month of December, web service based controls, & incremental/decremental volume controls.  We'll see when I have time to work on it.