So, it was put to me to find out what could be done to the Tivo box that is hooked up to the projector next to where the PCs live. Looking around on Google there are many references and a few PDFs about removing the main hard drive, booting a PC with a Linux live CD with the drive in and it installs a few applications that allow you to telnet into the box. Then, it is open access from there.

However, I lacked the coordination to unhook all the gear on Sunday so I went looking again, and found that the Tivo has an open telnet port (31339). Telnet with what ever program of choice to the box and you get presented with a one line status. Type quickly (or copy/paste) the following then press enter:

IRCODE TIVO - TIVO Button
SETCH 09 - Set channel to 09
IRCODE RECORD - getting pretty self explanatory

So, with a protocol that easy, it became a matter of choosing the method to execute the telnet command.

A copy of all executables/source is available for download here.
Included: /tivopython - Python scripts | /tivoweb - Tivo PHP pages | /tivodotnet - Modified Telnet Example
(UGLY, UNCLEAN CODE WARNING, BUT IT WORKS


Python & wbar - Linux

Python comes by default with Ubuntu (and most other dists) and is a easy language to ‘pick and paste’ code from the net. All of its modules can be installed with apt-get, and like all scripts can be run as a simple executable file.

I’ve made a folder  in my home directory called tivo/ and have placed each file inside.

danmar@danmar-desktop:~/tivo$ ls
cc  down  nine  pause  play  seven  ten  up
danmar@danmar-desktop:~/tivo$

Each file is an almost duplicate of each other - the only differences between each is the command that is sent. This could surely be compacted to one single Python script with command arguments, and I might just do that at some point to include all of the documented codes.

danmar@danmar-desktop:~/tivo$ cat ten
import sys
import telnetlib

HOST = “192.168.1.104″
PORT = “31339″
tn = telnetlib.Telnet(HOST, PORT)
tn.write(”SETCH 010″ + chr(13))
tn.close()
danmar@danmar-desktop:~/tivo$

Python is nice and easy to read. The Tivo IP address and port are specified, the connection is open, the command is written to the port, an enter is sent and the port closes.

To run:
danmar@danmar-desktop:~/tivo$ python ten
danmar@danmar-desktop:~/tivo$

The scripts do not output anything.

Now, copy and paste the same script until you have all the commands created in individual scripts. From there, the Tivo could be controlled via bash if you add ~/tivo to your $PATH. However, I’d like something to click and have always open on my Ubuntu bux, and I’ve always been a quiet fan of any docky type launcher.

A nice, simple dock is wbar, installed via
sudo apt-get install wbar
and, its configuration tool is located here.

Add a new icon for each (I googled the wikipedia page for each network) and the command to run (for example:TEN)
/usr/bin/python ~/tivo/ten

You should end up with something like this that you can click through your Tivo channels when the remote is lost!


PHP - Windows/Linux/Anything with a browser

Next step is to make it available to my friend who does not run Linux, therefore cannot run Python scripts on his computer. Since we are in the same network, I can host a webpage that when triggered can execute the Python script the same way the wbar icons do. On my Ubuntu PC I install LAMP (tasksel and choose LAMP Server), for Apache and PHP. Make a /var/www/tivo directory and create at least the following two files:

The main remote page (index.php):

danmar@danmar-desktop:/var/www/tivo$ cat index.php
<center>
<table>
<tr>
<td><a href="pause.php"><img src="Button-Pause-icon.png" width="64" height="64" border="0" alt=""></a></td>
<td><a href="play.php"><img src="play-icon.png" width="64" height="64" border="0" alt=""></a></td>
</tr>
<tr>
<td><a href="up.php"><img src="Arrow Up_64.png" width="64" height="64" border="0" alt=""></a></td>
<td><a href="down.php"><img src="Arrow Down_64.png" width="64" height="64" border="0" alt=""></a></td>
</tr>
</table>
<br><br>
<table>
<tr>
<td><a href="go.php">GO</a></td>
<td><a href="ten.php"><img src="header-ten-logo.gif" width="64" height="64" border="0" alt=""></a></td>
</tr>
<tr>
<td><a href="nine.php"><img src="Nine+logo+08.jpg.png" width="64" height="64" border="0" alt=""></a></td>
<td><a href="seven.php"><img src="seven logo.jpg" width="64" height="64" border="0" alt=""></a></td>
</tr>
</table></center>
danmar@danmar-desktop:/var/www/tivo$

The Pause Command (pause.php):

danmar@danmar-desktop:/var/www/tivo$ cat pause.php
<head><meta http-equiv="REFRESH" content="0;url=index.php"></head>....
<?php
echo exec('/usr/bin/python /home/danmar/tivo/pause');
?>
danmar@danmar-desktop:/var/www/tivo$

Same as before, copy and duplicate each file for each command, treating each .php file as an individual launcher.

With some tacky image and table skills, it should look like this:

http://1.2.3.4/tivo

Not only can PHP be used as a webpage, it also allows another input into the system. eg: Any request to http://1.2.3.4/tivo/pause.php will pause the system, so any program can access and run the script.


VB.Net - Windows

So now I could control the Tivo with my computer, and through his webbrowser on his laptop my friend could too. There was one problem though - what if I’m not around and my PC is turned off? Apache can’t serve a PHP script that runs a Python script that sends a telnet command to the Tivo’s IP because, well, there’s no power.

The next step was to provide a Windows executable that was self contained. If anything changed, I wanted to just have to copy a single .exe and since he has .NET installed for other applications (as most people do) there were no other requirements or components to install. So, if you don’t have it, get a copy of Visual Basic 2008 Express edition -it’s still free.. It’s less then 100mb to install and doesn’t take too long to get up and running with. I looked around for some example telnet code in vb.NET, and pieced together the following:

SendCommand(IP, Port, Command)

Private Sub SendCommands(ByVal PIPAddress As String, ByVal PPort As String, ByVal Command As String)
Dim RecvString As String = String.Empty
Dim NumBytes As Integer = 0
remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)
Dim RecvBytes(255) As [Byte]
Try
tnSocket.Connect(ep)
txtRecv.Text = “command : ” & Command
Catch oEX As SocketException
Exit Sub
End Try

Try
Wait(200)
If tnSocket.Connected Then
tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
Do
NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
Loop While NumBytes = 256
txtRecv.Text = RecvString
Wait(200)
tnSocket.Close()
End If
Catch oEX As Exception
End Try
remoteIPAddress = Nothing
ep = Nothing
tnSocket = Nothing
Command = Nothing
RecvString = Nothing
End Sub

So, to use the function I created ImageBox controls with the pictures of the buttons I had gotten earlier, for example, the Pause button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendCommands("192.168.1.104", "31339", "IRCODE PAUSE" & Chr(13))
End Sub

Continue this over all of the buttons required and run the program from the IDE and test - it shoud start working straight away. Once all the buttons are inserted, copy the executable out of the debug folder in the project and copy it to any Windows machine on the network. It might look something like this:


Conclusion:

Well, it was a start. Mostly I’ve been using the wbar buttons, and my friend has been used the vb.NET application - short cut on the desktop and it can always remain open. I want to clean the code up for all three, Python merged to one script with command arguments, PHP merged to one script with GET command arguments, and vb.NET application with modifiable settings/IPs/channels. This all happened because I needed something to do…

Add A Comment