Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: Webserver IR Example

  1. #1
    Join Date
    Mar 2005
    Location
    Los Angeles, CA
    Posts
    1,003

    Default Webserver IR Example

    There's been interest in using Girder's webserver in place of NetRemote, in order to support a broader variety of devices (iPhone/iPod Touch/iPad, Android, Windows Phone 7, etc). Girder's web server is very flexible, and you have the ability to completely customize the content that it serves up.

    Before I get into the subject of creating your own pages though, you should be aware that:
    - The default web pages that come with the webserver may be immediately useful, so be sure to check them out first.
    - There are already custom pages for iPhone/iPod Touch, for which there is a demonstration video here: http://www.promixis.com/forums/showthread.php?t=17822

    Assuming that you want to roll your own page layouts, here is a quick example to start you thinking. This example presents a button, which generates an infrared command from a GlobalCache device when pressed. The example consists of three pieces:
    1. example.lhtml - A web-page (in HTML and Javascript) that provides the user interface
    2. ajax_sendir.lhtml - Another web-page (in LUA) that provides the back-end functionality
    3. On.png & On_d.png - Button graphics, in two separate PNG files, one for the up state, and one for the down state

    To keep things simple, the user interface for this example is just a blank page with one button. You can use standard HTML, JavaScript, and CSS to your heart's content to build from there.

    example.lhtml
    Code:
    <html>
    	<head>
    		<!-- Include the jQuery library -->
    		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    
    		<!-- Custom JavaScript functions here -->
    		<script type="text/javascript">
    		
    			// Function to change the image of a specified HTML element immediately
    			function SetImage( id, src )
    			{
    				var Image = document.getElementById( id );
    				if ( Image )
    					Image.src = src;
    
    				return false;
    			}
     
    			// Function to change the image of a specified HTML element after 100 milliseconds
    			function SetImageDelayed( id, src )
    			{
    				setTimeout ( "SetImage(\""+id+"\",\""+src+"\")", 100 );
    				return false;
    			}
    			
    			// Function to invoke an IR command via AJAX to another web page
    			function SendIR( szCommand )
    			{
    				// This relies on having ajax_sendir.lhtml in the same directory as this file
    				$.ajax({
    					url: "ajax_sendir.lhtml",
    					data: "szCommand=" + szCommand,
    					cache: false,
    					async: true
    				});
    
    				return false;
    			}
    		</script>
    	</head>
    		
    	<!-- HTML layout here -->
    	<body>
    
    		<!-- A simple button -->
    		<!-- Relies on having On.png (up state) and On_d.png (down state) in the same directory as this file -->
    		<a href='' OnClick='return SendIR("ON");' OnMouseDown='return SetImage("OnButton", "On_d.png");' OnMouseUp='return SetImageDelayed("OnButton", "On.png");' onmouseout='return SetImageDelayed("OnButton", "On.png");'>
    			<IMG src='On.png' border='0' id='OnButton' />
    		</a>
    	</body>
    <html>
    ajax_sendir.lhtml
    Code:
    <%
    	-- Allocate a local variable to be used later
    	local szCommand;
    
    	-- Retrieve the JavaScript variables that were passed into a Girder array
    	local cgivars = webserver:GetCGI( );
    	
    	-- Loop through each JavaScript variable
    	for n,v in pairs( cgivars ) do
    	
    		-- If the variable is named 'szCommand', then create a Girder variable 
    		-- with the same name, and assign it the passed value
    		if ( n == "szCommand" ) then
    			szCommand = string.upper( v );
    		end;
    	end;
    
    	-- Set up GlobalCache parameters
    	local iAddress = "192.168.1.70";
    	local iPort = 1;
    	
    	-- Set up the array of IR commands (these examples are for an LG television, but replace with your own commands)
    	local Command = { };
    	Command[ "ON" ] = "0000 006D 0022 0002 0157 00AB 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 05F3 0156 0055 0016 0E4D";
    	Command[ "OFF" ] = "0000 006D 0022 0002 0157 00AB 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 05F3 0156 0055 0016 0E4D";
    
    	-- Bail out if the desired command is not found
    	if ( Command[ szCommand ] == nil ) then
    		print( "ERROR: Command not found!" );
    		return;
    	end;
    	
    	-- Initialize the GlobalCache
    	local a;
    	if ( iAddress ~= nil ) then
    		a = globalcache.New( iAddress );
    	end;
    
    	-- Bail out if the connection was not established
    	if ( not a ) then
    		print( "ERROR: GlobalCache failed to initialize!" );
    		return;
    	end;
    
    	-- Send the command to the GlobalCache
    	a:SendIR( Command[ szCommand ], 1, 2, iPort );
    
    	-- Over and out
    	return;
    %>
    The first file, example.lhtml, draws a button. When the button is pressed, it momentarily shows a down state (to provide some visual feedback), and it makes an AJAX call to the second file.

    When the second file receives that AJAX call, it looks up the specified command from a table, and sends it to a GlobalCache IR transmitter (you could make different LUA calls if you wanted to use a USB-UIRT).

    If you have a GlobalCache and you want to try running this example, simply paste the two sections of code into files named example.lhtml and ajax_sendir.lhtml, download the two PNG files attached to this post, and make sure that everything is in the root directory for the Girder webserver. Be sure to set the correct IP address, port, and change the codes in ajax_sendir.lhtml so that the example will work with your own devices.

    Hopefully this is enough of a primer to get started!

    Best Regards,
    Tim
    Attached Images Attached Images   
    Last edited by tmorten; June 24th, 2010 at 12:02 PM.

  2. #2
    Join Date
    Dec 2006
    Posts
    291

    Default

    Nice post thanks Tim. I'll be having a crack at this tonight. Expect a bunch of questions

  3. #3
    Join Date
    Aug 2009
    Location
    Toronto, Canada
    Posts
    110

    Default

    Hi Tim,

    Giving this a try with new IPAD and USBUIRT.
    Can't get the button action to send IR code. I have changed the ajax file as per attached.(I added an extra .txt to get it uploaded)
    Could you take a quick look and point me in the right direction?
    Thanks

    Shaffique
    Attached Files Attached Files

  4. #4
    Join Date
    Mar 2005
    Location
    Los Angeles, CA
    Posts
    1,003

    Default

    Are you sure that you want the fourth IR port (the iPort value)? As a test, maybe try outputing to the first port, and check if that emitter blinks (or if the network activity light on the GlobalCache blinks at least). Otherwise it looks okay to me - are you seeing the "success" message?

    Cheers,
    Tim

    PS - Also, can you verify that your GlobalCache is configured to have IR on module 2? That's the third parameter in the SendIR command, so you can still address it even if yours is set to a different value.

  5. #5
    Join Date
    Aug 2009
    Location
    Toronto, Canada
    Posts
    110

    Default

    Thanks Tim,

    I am actually using USBUIRT and not Global cache. I chose Port 4 thinking that was the Com port on the computer that the USBUIRT is attached to.

    I did not get the success, or any other message.

    Now that you know I have the USBUIRT, do I need to make other code changes?

    I will try using Port 1 etc when I get home this evening.

    Shaffique

  6. #6
    Join Date
    Mar 2005
    Location
    Los Angeles, CA
    Posts
    1,003

    Default

    The USBUIRT is not compatible with the GlobalCache - you will need to change the LUA associated with sending the IR.

    It appears that the syntax for the USBUIRT is as follows:

    Code:
    -- Send the command to the USBUIRT
    usbuirt.TransmitIR( string );
    So the function would look like this:

    ajax_sendir.lhtml
    Code:
    <%
    	-- Allocate a local variable to be used later
    	local szCommand;
    
    	-- Retrieve the JavaScript variables that were passed into a Girder array
    	local cgivars = webserver:GetCGI( );
    	
    	-- Loop through each JavaScript variable
    	for n,v in pairs( cgivars ) do
    	
    		-- If the variable is named 'szCommand', then create a Girder variable 
    		-- with the same name, and assign it the passed value
    		if ( n == "szCommand" ) then
    			szCommand = string.upper( v );
    		end;
    	end;
    
    	-- Set up the array of IR commands (these examples are for an LG television, but replace with your own commands)
    	local Command = { };
    	Command[ "ON" ] = "0000 006D 0022 0002 0157 00AB 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 05F3 0156 0055 0016 0E4D";
    	Command[ "OFF" ] = "0000 006D 0022 0002 0157 00AB 0016 0015 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 003F 0016 0015 0016 003F 0016 0015 0016 0015 0016 0015 0016 003F 0016 003F 0016 0015 0016 003F 0016 0015 0016 003F 0016 003F 0016 003F 0016 0015 0016 0015 0016 05F3 0156 0055 0016 0E4D";
    
    	-- Bail out if the desired command is not found
    	if ( Command[ szCommand ] == nil ) then
    		print( "ERROR: Command not found!" );
    		return;
    	end;
    	
    	-- Send the command to the USBUIRT
    	usbuirt.TransmitIR( Command[ szCommand ]);
    %>
    I don't have a USBUIRT setup to test with, so let me know how that works out for you.

    Cheers,
    Tim
    Last edited by tmorten; June 24th, 2010 at 12:01 PM.

  7. #7
    Join Date
    Aug 2009
    Location
    Toronto, Canada
    Posts
    110

    Default

    Hi Tim,

    Thanks for taking the time to research this for me! I copied and pasted your USBUIRT code and added my IR commands. No success!

    I added a return; to the very end of your code but that did not fix the problem. I added my Print: Success line and did not see that message when I pressed the Green Power button on my iPad.

    Do I need to make any changes to the example.html program? I see some settings there for Global cache.

    Shaffique

  8. #8
    Join Date
    Mar 2005
    Location
    Los Angeles, CA
    Posts
    1,003

    Default

    Two questions:
    1. Is your USBUIRT working in other circumstances, just not when you use the example?
    2. When you look at the Girder variable display, do you see an entry called usbuirt, with a function called TransmitIR underneath it?

    Cheers,
    Tim

  9. #9
    Join Date
    Mar 2008
    Location
    Nottingham, England
    Posts
    406

    Default

    Hi Tim,

    Im playing around with this as we speak....i have usbuirt

    i have changed the Ip address but not the port number? is this the number in the webserver config page (ie 80) or does this refer to the usbuirt unit itself?

    my unit it working and transmitting and it shows in the variable inspector

    i have saved all the files in the HTTPD file but dont see the buttons

    do i need to create a 'index' page with these buttons on?

    cheers

    Mark
    Last edited by Solosid; June 24th, 2010 at 11:38 AM. Reason: spelling mistake
    some people learn by writing, some by reading, some by listening and some by doing?...me I'm not learning at all

  10. #10
    Join Date
    Mar 2005
    Location
    Los Angeles, CA
    Posts
    1,003

    Default

    Quote Originally Posted by Solosid View Post
    Hi Tim,

    Im playing around with this as we speak....i have usbuirt

    i have changed the Ip address but not the port number? is this the number in the webserver config page (ie 80) or does this refer to the usbuirt unit itself?

    my unit it working and transmitting and it shows in the variable inspector

    i have saved all the files in the HTTPD file but dont see the buttons

    do i need to create a 'index' page with these buttons on?

    cheers

    Mark
    It sounds like you're using the GlobalCache code instead of the USBUIRT code. There shouldn't be any reference to an IP address or a port number for USBUIRT - the USBUIRT is not compatible with the LUA for the GlobalCache unfortunately. Replace the code in ajax_sendir.lhtml with the USBUIRT example code from a few posts back, and then hopefully you'll get some IR action.

    Assuming that each code sample has the filename specified in bold above it, and the buttons were placed in the same directory with them with their original filenames, you should see a single green button when you load the page.

    Cheers,
    Tim
    Last edited by tmorten; June 24th, 2010 at 12:03 PM.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •