Author Topic: Python and voxcommando with infrared signals  (Read 9441 times)

0 Members and 1 Guest are viewing this topic.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #15 on: May 05, 2013, 07:26:38 PM »
Work like a dream.

I can see where i was going wrong trying to get my script to run.
I didn't have it as a start up script, so VC can just call strings

Now i have the concept, i'll have a play about.

Thanks heaps for taking the time to whip this up.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #16 on: May 21, 2013, 09:30:53 AM »
Thanks to the wonderful help from jitterjames this is what we've come up with, and it works perfectly.

This process uses the Python plug-in and the TCP plug-in to be able to send pretty much any command to a network connected device. There is a few ways to achieve this, this is an example of how I go about it.

Step 1: Creating the python file.
Open a good text editor, “Notepad ++” works well.  Copy and paste in the notepad this command:

Code: [Select]
amp =  {
'PWRON'       : 'sendir,1:1,1,38000,1,1,341,170,21,21,21,64BCCCCBCBCBBBBCCBCCCBBBBCBBBCCC21,1517,341,85,21,3800',
'PWROFF'      : 'sendir,1:1,1,38000,1,1,341,170,21,21,21,64BCCCCBCBCBBBBCBCCCCBBBCBBBBCCC21,1517,341,85,21,3800',
}

paltv = {
'PWRTOGGLE': 'sendir,1:2,1,38226,1,1,341,170,22,21BBBBBBB22,64CCCCCCBBCBCBBBBCBCBCCCC22,764',
'EPGUIDE' : 'sendir,1:2,1,38226,1,1,341,170,22,21BBBBBBB22,64CCCCCCBBBBBBBBBCCCCCCCC22,764',
}

def ampcode(strCommand):
    data=amp[strCommand]+'\x0D'
    return data

def tvcode(Command):
    data=paltv[Command]+'\x0D'
    return data

Then save this file as controlcodes.py to the PY folder in the VoxCommando directory. Don't forget the .py on the end.

Step 2:
Next we need to tell voxcommando to load this file on start up, this is how we do it:

Code: [Select]
<command id="1436" name="load python startup code" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <cmdString>PY\controlcodes.py</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>load python startup code, load python startup script</phrase>
  <event>python ready</event>
</command>

Should have this:


Step 3:
So now have our python file running we need to find our device we need on the network and open a connection. We do that with the TCP Plugin:

Code: [Select]
<command id="673" name="First connect iTach" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>TCP.Client.Connect</cmdType>
    <cmdString>iTach&amp;&amp;IP.of.network.device&amp;&amp;port</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>VC.Loaded</event>
</command>

It should look like this:


Set the client name to what ever you feel like. Mine is connecting to a Global Cache iTach device so i called my client "iTach"
Set the ip address and port number of the device your connecting to.

So now we just need to set up some commands to make the magic happen.

From this point, using the key, we can call any value we like from the python dictionany. (any value inside the {})



Step 4:
Building the commands. Here is an example for sending the "power on" inferared code to my Amplifier:

Code: [Select]
<command id="1412" name="Amp Power On" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=ampcode('PWRON')</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Tcp.Client.WriteLn</cmdType>
    <cmdString>iTach&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Amplifier Power On</phrase>
</command>




By having a separate function for each dictionary name,

we can use the same keys for each dictionary if we wish.

What does it all mean?
-We can use any number of "TCP Clients" to send any command we want.
-We can build a multitude of "Dictionaries" for any "TCP Client" and any "TCP Client" can use any "Dictionary".

The real advantage,
Example: If i have 20 commands in VoxCommando to send infrared signals to my amplifier, should I change my amplifier, all i need to do is edit the "values" in the dictionary for the amplifier (which are neatly positioned in one spot). No need to go through and change each command in VoxCommando.

Extras
And since I'm on a roll here is a little trick for having the client repeat the command. Eg: send volume down 5 times to turn the volume down 5.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="1369" name="Amp Vol Down" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="" loopMax="" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=ampcode('VOLDOWN')</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Tcp.Client.WriteLn</cmdType>
    <cmdString>iTach&amp;&amp;{LastResult}&amp;&amp;225</cmdString>
    <cmdRepeat>5</cmdRepeat>
  </action>
  <phrase optional="true">turn, the</phrase>
  <phrase>Volume Down</phrase>
  <payloadRange>1,20</payloadRange>
</command>

Should look like this:


My amplifier needed a 225 millisecond delay between pluses to register each signal. Using a  Payload Range the number can be anything I say.


Have fun and enjoy :)

Python file and Python iTach Startup group attached if you wish to use it.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #17 on: May 21, 2013, 12:09:25 PM »
Wow.  Excellent information there, and very well presented with all the images etc.  Thanks very much.

Looks like someone needs an account so they can edit the wiki  :biglaugh :biglaugh :biglaugh

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python and voxcommando with infrared signals
« Reply #18 on: May 21, 2013, 08:24:19 PM »
Sure, hook me up ill see what I can do.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python and voxcommando with infrared signals
« Reply #19 on: July 08, 2013, 12:46:12 PM »
I finally got around to creating my version of an integrated dictionary for VoxCommando.  I'm calling it "maps" and using sqlite to store and access values.

Check it out...

http://voxcommando.com/forum/index.php?topic=1128.msg9485#msg9485