Luup code for switch on Sony Bravia

Hello,

I have a Vera plus since 5 years. I would like to create a Luup code to switch on my TV (Sony Bravia)
First I have to switch on the MyStrom Switch with http get request to switch on all my equipment pluged (TV, Sat …, wait for about 120 sec, send a tcp command to wake up the Sony Bravia, wait for 10 sec, and send a tcp command to choose a specifical function on my Sony Bravia.

I tested each commands separatly, it works. I have a problem to include wait function.

I wrote this code :

local socket = require("socket")
local host, port = "10.1.6.5", 20060
local tcp = assert(socket.tcp())
local delay1 = 120
local delay2 = 10
http = require("socket.http")
http.request{
url = "http://10.1.9.6/relay?state=1"
}
tcp:connect(host, port);
tcp:send("*SCPOWR0000000000000001\n");
luup.call_delay(delay1)
tcp:connect(host, port);
tcp:send("*SCIRCC0000000000000100\n");

But this code doesn’t work.

Anybody can help me to resolve it ?

Thanks in advance.

gvals

Just looking for the obvious stuff here; since I don’t have your equipment to test with, I have no idea if this is all that’s needed, but…

The tcp:connect() call returns a (client) socket object that you have to store in a variable. You then use that in the send() calls. You need to close sockets that you open or your system will eventually crash. Your call_delay doesn’t work the way you think it does (see docs). It calls a function, which you need to define, after a set number of seconds.

local socket = require("socket")
local host, port = "10.1.6.5", 20060
local tcp = assert(socket.tcp())
http = require("socket.http")
http.request{
url = "http://10.1.9.6/relay?state=1"
}

sock = tcp:connect( host, port )
function braviaDelay1()
    sock:send("*SCPOWR0000000000000001\n")
    luup.delay("braviaDelay2", 10) -- launch second delay function in 10 sec
end
function braviaDelay2()
    sock:send("*SCIRCC0000000000000100\n")
    luup.delay("braviaDelay3", 1) -- launch third function in 1 second
end
function braviaDelay3()
    sock:close() -- close open socket
end

-- Get things going by starting a 120 second day to the first function
luup.call_delay("braviaDelay1", 120) -- launch first delay function in 120 sec

Also, Lua isn’t JavaScript, Java, or C; no ending semicolons ; are required on lines.

Those are really long delays, so this seems kind of a ropey thing to do. There’s no way to cancel a delay, so once this thing gets going, only a restart of your system will stop it before it’s finished.

Also, use the log file and luup.log() for information about what’s happening and debug logging. You can read the log locally from a browser via http://your-vera-ip/cgi-bin/cmh/log.sh?Device=LuaUPnP

1 Like

You may be interested in the Sony Bravia Plugin?

Yeah, call_delay() needs to include the function. For example using a Global Cache iTach to change inputs on a TV 10 seconds after turning on that TV via a function I wrote called SendiTachCommand:

luup.call_delay("SendiTachCommand",10, "192.168.0.121,4998,sendir,1:1,1,38000,1,1,173,173,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,65,21,65,21,65,21,21,21,21,21,21,21,21,21,21,21,65,21,21,21,21,21,65,21,21,21,65,21,65,21,65,21,21,21,65,21,65,21,21,21,65,21,21,21,21,21,21,21,1832,\r") -- Switch Samsung TV to Input HDMI 1