Android and Linux

Monday, July 4, 2011

Control Roku from command line

I got a Roku to play Netflix Instant on my TV (as well as other channels available through Roku). It connects to your wifi or ethernet and streams video through a wire to your TV and is controlled with a remote control. The Roku has two output ports, one for RCA cables and one for HDMI cables. I have the HDMI going to the TV in the living room and RCA cables going to my bedroom TV so I can also watch the Roku in there. But, of course, the remote control doesn't work from my bedroom.

Luckily, the Roku can also be controlled over your home network. There is a Roku remote control app available for Android called RoMote which works great, except you have to tell it to connect and it loses the connection when you close the app. If you only need to push one button, like Pause, it's a bit of a hassle because you have to open the app, click connect, then tap Pause and close the app.

There is an SDK available that allows developers to create their own apps, which is obviously how the RoMote developer made his. This is an example of what the SDK uses for the general format for keypresses. This example uses the Home button and assumes the Roku IP is 192.168.1.6.

echo -e 'POST /keypress/Home/1.1\r\n\r\n' | ncat 192.168.1.6 8060

Yuck. Luckily again, we can make that a little nicer looking by using cURL or wget to post data.

curl -d "" "http://192.168.1.6:8060/keypress/Home"

wget --post-data "" "http://192.168.1.6:8060/keypress/play"

The Roku only seems to work with the HTTP POST method. The -d option tells cURL to post http data and obviously the "--post-data" option does the same for wget. The data is supposed to be between the first quotation marks and as you see, mine are empty, so it's posting, but it's posting nothing. The important part is the url that it posts to, in this case /keypress/Home. When the Roku detects a post to that url, it acts as if the Home key on the remote control has been pressed.

Here is a list of the available controls:

Home
Rev
Fwd
Play
Select
Left
Right
Down
Up
Back
InstantReplay
Info
Backspace
Search
Enter
Lit_

Lit_ is special, you enter a letter with Lit_x. For example, to search for movies containing "foo", you'd hit the following 5 urls in order:

Search
Lit_F
Lit_O
Lit_O
Enter

The /query/apps URL will give you a list of the apps installed on the Roku. Apps are basically channels. There is a Netflix app, WeatherChannel app, etc. The data comes back in xml format.
# curl http://192.168.1.6:8060/query/apps                              
<apps>
<app id="5127" version="1.0.10">Roku Spotlight</app>
<app id="11" version="2.1.22">Roku Channel Store</app>
<app id="12" version="2.3.20">Netflix</app>
<app id="2016" version="2.0.37">Movies on Demand</app>
<app id="2285" version="1.9.1">Hulu Plus</app>
<app id="28" version="1.0.24">Pandora</app>
<app id="1688" version="1.5.10">Roku Newscaster</app>
<app id="1756" version="1.5.5">Funny Videos and Pics by Break</app>
<app id="2115" version="1.4.1">SHOUTcast Internet Radio</app>
<app id="27" version="1.1.110207">Mediafly</app>
<app id="2898" version="0.0.0">Weather Underground</app>
<app id="2963" version="1.0.0">My Damn Channel</app>
<app id="4070" version="1.5.1306292789">TEDTalks</app>
<app id="45" version="1.5.0">Revision3</app>
<app id="4687" version="1.0.0">Inmoo </app>
<app id="6493" version="1.0.1">Sunlight Foundation White House Video Stream</app>
<app id="1980" version="1.1.100907">Vimeo</app>
<app id="2091" version="1.2.1">Warriors of War</app>
</apps>
#

If you want to watch a channel, just hit this url containing the numerical ID. For example, if you want to check the weather:

/launch/11?contentID=2898

And using the numerical ID, you can view that channel's icon using this url: /query/icon/2898.

That's pretty much all there is to imitating the Roku remote control. I'll probably be putting this all into a script and posting it later when I have more time. Being able to control it this way allows you to script your player. For example, Netflix has entire seasons of TV shows available but doesn't have a "play all" option, so you could use this to watch one, wait 23 minutes, watch another etc then you could have Weather Undergroud start playing at the time you wake up.

This can also be controlled through your Android phone. cURL isn't available on Android, but see wget is, and see my earlier post about using Tasker to post HTTP data.

Sunday, May 22, 2011

No internet

Ugh, if you move into a newly constructed house, make sure to sort everything early. I ended up moving before the cable was connected, but they were scheduled to do it Saturday. When the tech got everything wired up, he found that my side if the street wasn't tapped into the cable on the main street yet, so it's going to be at least another week until I get cable/internet. I guess everyone else on my side of the street is using satellite or something.

I have at least been able to get a little home automation done. I put rope lights above the kitchen cabinets. When watching a movie in the living room, they provide a very nice faint glow in the background. I can get up and get a drink without turning on any other lights.

I'm temporarily using X10 lamp modules to control them and have a cron job on the computer turn them on at 7am so I have a gentle glow to light my way when I wake up and stumble into the kitchen. I'll probably put in X10 outlets all throughout the house later and I already have several X10 switches that I need to wire in.

Not much else to report. I just wanted to check in. With no internet, I haven't felt like doing anything cool tech-wise. It ain't cool if you can't do it remotely.


Monday, May 9, 2011

Yay, new house.

I closed on the house today and it's been a frustrating ride. The builder had to fix a few things and on the second inspection... nothing had been fixed. Last week, they were working on it and no thanks to a lender that couldn't be bothered to do anything, I had to coordinate with the title company myself to get the closing done, and in the process forgot to do a final inspection.

Oh well, I'm sure they took care of it, right? Nope, I walked in to my new home to find that they still weren't done. But I got the keys and they're going to finish tomorrow and I will move this weekend.

With a brand new house and brand new furniture, I think it's time for some brand new computing power. I'm thinking a couple wifi repeaters, a server in the closet to run X10, storage, maybe even media and an Android tablet as a remote control for everything.

Maybe.

I'll soon be moved and hopefully have something worth blogging.

Monday, April 11, 2011

Light posting

Well, that's not new, I'm not a very prolific blogger, but posting may (or may not) be light for the next month. I'm buying a new house- literally, it just got built- and will be going crazy trying to get everything done and get moved. I'm not sure when I will move, I told someone that I will probably follow the internet. Once the internet gets turned off here and on there, I'm moving that day!

On the plus side, I will finally have an excuse to go nuts with X10. The house has tray ceilings in the living room and master bedroom, so I'm planning to put in some lighting like this which just begs to be controlled with X10 and a dimmer.



Then there's the rest of the house, I'm just itching to go X10 crazy on it.

Anyway, if I don't post much for the next month, don't get alarmed and call Dog The Bounty Hunter to look for me. I'll be around.

Tuesday, April 5, 2011

Get current url from Android browser

I've been using an app called Wave Launcher, which allows you to swipe a certain area of the screen to pop up a launch bar. The launch bar contains 5 icons which you can set up to open an app or shortcut. With Folder Organizer, you can have it open a folder of apps, or, even cooler, a folder full of Tasker tasks.

I wanted to create a Tasker task to open my computer's browser to the same url that my phone's browser is on. I can do this by copying the url and running a task to share it, but that's too much work. After much digging, I found a way to extract the current url from the browser database.
sqlite3 /data/data/com.android.browser/databases/browser.db "SELECT * FROM bookmarks;" | tr '|' '\n' | grep http | tail -n1

If you go to cnn.com on the phone and run that command, the output is http://m.cnn.com/.

Using that, I can make a short script to make it open on my home computer via ssh:

#! /system/bin/sh
url=$(sqlite3 /data/data/com.android.browser/databases/browser.db "SELECT * FROM bookmarks;" | tr '|' '\n' | grep http | tail -n1)
ssh -i /PATH/TO/SSHKEY USER@IP "firefox $url"

If I'm browsing a page on the phone and want to read it on the computer instead, I can swipe Wave Launcher and select the task that runs this command. This will grab the last url visited, so it doesn't matter if the browser is open or closed.

Saturday, April 2, 2011

shell launcher for sl4a scripts

Scripting Layer for Android, or SL4A is an app which allows you to run scripts such as perl and python on your phone. The cool thing about them is that they have full access to Android API calls, unlike the Android command line. But sl4a scripts can be invoked from the command line and here's a script to do it.

Everything from "am" to the end needs to be on one line, but it's too long to post it that way here and I've always found the typical way of splitting lines on the web with a backslash confuses people, so just make sure "am...}" goes on a single line or copy it from the QR code below.

To run it, just execute the script with the name of an sl4a script as an argument. e.g., if you name the script slsh you run "slsh test.py"

#! /system/bin/sh
[ -z "$1" ] && exec echo "please specify the name of a script to run"
am start -a com.googlecode.android_scripting.action.LAUNCH_BACKGROUND_SCRIPT
-n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher
-e com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/${1}

This could give you the ability to do virtually anything on the phone through the command line by invoking sl4a scripts. sl4a would do the hard work, of course, but this gives you a lower level access to it. You could, for example, probably use it to easily ssh into the phone from the computer and send a text message or get the GPS location.

Copy "slsh" to your phone's clipboard with this QR code:

Friday, April 1, 2011

New Tasker Forum

Pent has created a new discussion group called Tasker Pro in response to this thread where people lamented the lack of a good place where people could put their heads together and come up with cool Tasker solutions.

Followers