Updated 2018-03-01 17:44:23 by ET

The Roku is a popular internet tv device, similar to a smart tv, but in a separate box. It can be controlled over a local network with simple commands.

You will need to have a static address on your lan to use the following code.

On my lan, I use my router's dhcp address reservation to provide a fixed ip address for my roku, 192.168.1.76 and the port number is 8060. Once you have the ip address your roku is using, you can send commands rather easily.

On linux, one can test out roku commands using the command line program curl.
curl -d '' http://192.168.1.76:8060/keypress/select

(replace your roku's ip address for mine, 192.168.1.76 here and below in the tcl code)

That command should send the select key.

I use mine to control the mlb.tv app on my roku. One useful command is to skip forward 2 minutes. Sending the "right" arrow command will cause it to skip 10 seconds for each one sent. However, if you send multiple ones too quickly it will not work. I find that 200 milliseconds is about right.

so, for example, to skip 2 minutes ahead, with 200 ms delay, between each:
toroku right 12 200
toroku select 1 ;# must send this also to restart playing

see this guide http://a-more-common-hades.blogspot.com/2011/07/control-roku-from-command-line.html

Here are a selection of commands I use, pretty much the intuitive name of the key you want to send. The 2 defaulted args are the number of times to send and an optional delay after each send.
toroku back
toroku down
toroku down $num 200
toroku InstantReplay
toroku left $n
toroku play ;# this one is the play/pause toggle
toroku right $n
toroku select
toroku up 1 250
toroku fwd  ;# fast forward
toroku rev  ;# this one is rewind

I build a small keypad using the following list :
"<-" back          
"^ " up            
"Ho" home          
"< " left          
"ok" select        
"> " right         
"rp" instantreplay 
"v " down          
"* " info          


package require http

proc toroku {key {rep 1} {wt 0}} {
    for {set m 1} {$m <= $rep} {incr m} {
        quickroku "http://192.168.1.76:8060/keypress/$key"
        if { $wt != 0 } {
            wait $wt
        }
    }
    return
}

proc quickroku {args} {
    set token [::http::geturl $args -method POST ]        
    ::http::reset $token
}
proc uniqkey { } {
    set key   [ expr { pow(2,31) + [ clock clicks ] } ]
    set key   [ string range $key end-8 end-3 ]
    set key   [ clock seconds ]$key
    return $key
}

proc wait { ms } {
    set uniq [ uniqkey ]
    set ::__sleep__tmp__$uniq 0
    after $ms set ::__sleep__tmp__$uniq 1
    vwait ::__sleep__tmp__$uniq
    unset ::__sleep__tmp__$uniq
}