wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Running Tira-2 via web interface

Sun Nov 24, 2013 7:31 pm

Hello,

I previously had a neat little home theater remote setup using a minimal ubuntu machine and a Tira-2 USB IR transceiver.
The old machine finally died after about 5 years of continuous operation, so I've replaced it with a Pi

The old setup worked like this:
web page button (form) press -> AJAX & Javascript -> CGI -> perl script -> Tira-2.py -> IR command transmitted

I've set up my web host (apache2), perl is up to date and python is up to date.
Naturally, it isn't working as planned.

In attempting to run the Tira-2.py script directly (bash)
I get the following errors:

Code: Select all

Tira-2.py: line 20:
 Control program for the Home Electronics Tira-2 IR transceiver
 Copyright © 2006,2007 Lincor Solutions… 
…See the GNU General Public License for more details.
: No such file or directory
Tira-2.py: line 60: import: command not found
Tira-2.py: line 62: syntax error near unexpected token `('
Tira-2.py: line 62: `def get_options():'
(I shortened the full text of the line 20 error dump for clarity)

The full script is still available here http://www.pixelbeat.org/programs/Tira-2/

Any help getting this much operating would be really appreciated!
After this I can get into making everything else cooperate, which will be many more questions.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running Tira-2 via web interface

Sun Nov 24, 2013 10:35 pm

Start by renaming your copy to Tira-2.py~

Code: Select all

mv Tira-2.py Tira-2.py~
Then pull a new copy with

Code: Select all

wget http://www.pixelbeat.org/programs/Tira-2/Tira-2.py
The compare them with

Code: Select all

diff -u Tira-2.py Tira-2.py~ 
That should show the changes between your version and the original version and you may be able to see what's gone wrong.

I pulled a copy and it runs OK for me when I run it with:

Code: Select all

python Tira-2.py
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Re: Running Tira-2 via web interface

Sun Nov 24, 2013 11:53 pm

Thanks for the help, Dougie!

I found that my problem was in my configs location; I had the wrong location specified. Once I corrected that, the script worked perfectly.

wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Re: Running Tira-2 via web interface

Mon Nov 25, 2013 6:02 am

Now that I've established that my python script works, it's time to make the link from the webpage to my python script.
For simplicity's sake, I'm leaving the site at Apache2's default of /var/www with my files in a subdirectory /var/www/html
The perl script pages (in a subdirectory within ../../html) call the Tira-2 command files at /home/web/remotes (these contain the IR codes to be transmitted)

I've added a few additional apache modules:
libapache2-mod-python
libapache2-mod-perl2
libapache2-mod-fcgid

I still can't get my webpage to fire off my scripts (which use javascript and ajax)
beyond these modules, am I missing anything? I can't easily determine if I have javascript available.

wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Re: Running Tira-2 via web interface

Mon Nov 25, 2013 9:11 pm

Here's my javascript

Code: Select all

<script type="application/x-javascript">
var xmlDoc = null;
var newPage = '';

if (navigator.userAgent.indexOf('iPhone') != -1) {
	addEventListener("load", function() {
		setTimeout(hideURLbar, 0);
	}, false);
}
function hideURLbar() {
	window.scrollTo(0, 1);
	doAction('volume.pl','');
}

function process() {
	if (xmlDoc.readyState == 4) {
		if (newPage != '') {
			window.location=newPage;
		}
		if (xmlDoc.responseText !='') {
			var vol = xmlDoc.responseText;
			var index = vol.indexOf("mute");
			if (index != -1) {
				vol = vol.substring(0, index);
				document.getElementById("muteText").className = "mute";
			} else {
				document.getElementById("muteText").className = "volume";
			}
			document.getElementById("volText").innerHTML = vol;
		}
	}
}

function doAction(actionParameter, page) {
	xmlDoc = new XMLHttpRequest();
	xmlDoc.onreadystatechange = process;
	xmlDoc.open("GET", actionParameter,  true);
	xmlDoc.send(null);
	newPage = page;
}

</script>
Which calls the associated perl file via cgi to run the Tira-2.py script

Code: Select all

#!/usr/bin/perl -w

require "cgi-lib.pl"

&ReadParse(*input);

print "Content-type: text/plain\n\n";

use CGI;

$query = new CGI;

if ($var = $query->param('action')) {
if ($var eq "BDPPowerOn")
{
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=kuro     --transmit=Power";
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=receiver --transmit=DVD"; 
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=oppoBDP  --transmit=power";
}
elsif ($var eq "CDPowerOn")
{
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=receiver --transmit=DVD"; 
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=oppoBDP  --transmit=power";
}
elsif ($var eq "cablePowerOn")
{
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=kuro     --transmit=Power";
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=receiver --transmit=CD"; 
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=cable    --transmit=power";
}
elsif ($var eq "XBOXPowerOn")
{
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=kuro     --transmit=Power";
	system "/usr/Tira-2.py --port=/dev/ttyUSB0 --configs=/home/web/remotes --remote=receiver --transmit=DSS";
}
}
Is there anything in these code snippets that would cause my scripts to not work? At the moment, all of my html works, none of the scripts go through.

wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Re: Running Tira-2 via web interface

Tue Nov 26, 2013 1:51 am

I'm still stumbling around in the dark on this one, trying to get closer to the original environment and having no real luck.
Since I've added a few modules to my Apache2 install, I get the following in my Apache2/error.log when I restart the server:

Code: Select all

[error] python_init: Python version mismatch, expected '2.7.2+', found '2.7.3'.
[error] python_init: Python executable found '/usr/bin/python'.
[error] python_init: Python path being used '/usr/bin/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload'.
[notice] mod_python: Creating 8 session mutexes based on 6 max processes and 25 max threads.
[notice] mod_python: using mutex_directory /tmp
[notice] Apache/2.2.22 (Debian) mod_fcgid/2.3.6 mod_python/3.3.1 Python 2.7.3 mod_perl/2.0.7 Perl/v5.14.2 configured -- resuming normal operations
attempting to run the script via the website does not generate any errors.
Do I need to change configurations to perl somewhere to make things operate?
Too many or conflicting modules on Apache?
my scripts and html files are set to www-data:www-data with 755 permissions (777 on the volume directory, files and scripts as they need to read and write)

wintermute
Posts: 6
Joined: Sun Nov 24, 2013 6:25 pm

Re: Running Tira-2 via web interface

Tue Nov 26, 2013 4:43 am

SOLVED!
I needed to add .cgi and .pl handlers to my apache2.conf file

Additionally I needed a very old, depreciated file: cgi-lib.pl
found here

With these key parts in place with proper permissions and ownership, everything works!

Return to “Beginners”