Lua Script causing intermittent Luup restarts

I wrote this lua script to process TV channel processing, basically, it takes a string e.g. 7.1xxxxx and passed to multistring and splits the characters into individual variables which are then evaluated with an IF conditional which fires an infrared signal command when true. I found the x’s are needed as place holders since there are 6 variables to accommodate a channel such as 102.1 and none of the variables get populated if the string at least 6 characters. Reactor triggers the script on a value change on the multi-string variable and I have tried to run the lua in both reactor and passing it off to luup using a native UI7 scene.

It works well, however, I will get and intermittent luup restart every now and then, enough to be a problem. I am a novice at lua so put this together with trial and error. I have no idea is there is a better or more efficient way. I have read in the past that using luup sleep is not a great idea but I need a delay so the IR signals dont step on each other.

I guess at this point I am trying to get to the bottom of the luup restarts.

Script is as follows:

local channel, ts = luup.variable_get("urn:upnp-org:serviceId:VContainer1","Variable1", 477)
d1, d2, d3, d4, d5, d6 = channel:match("(.)(.)(.)(.)(.)(.)")
if (d1=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d1=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d1=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d1=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d1=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d1=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d1=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d1=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d1=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d1=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
if (d2==".") then
 --[[Sony Digit Separator----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,48,24,48,24,24,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,48,770" .. "\r\n")
end
if (d2=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d2=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d2=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d2=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d2=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d2=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d2=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d2=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d2=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d2=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
if (d3=="x") then
tcp:close()
return
end
if (d3==".") then
 --[[Sony Digit Separator----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,48,24,48,24,24,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,48,770" .. "\r\n")
end
if (d3=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d3=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d3=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d3=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d3=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d3=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d3=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d3=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d3=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d3=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
if (d4=="x") then
tcp:close()
return
end
if (d4==".") then
 --[[Sony Digit Separator----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,48,24,48,24,24,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,48,770" .. "\r\n")
end
if (d4=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d4=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d4=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d4=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d4=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d4=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d4=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d4=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d4=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d4=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
if (d5=="x") then
tcp:close()
return
end
if (d5=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d5=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d5=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d5=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d5=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d5=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d5=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d5=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d5=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d5=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
if (d6=="x") then
tcp:close()
return
end
if (d6=="1") then
 --[[Sony Digit 1----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125" .. "\r\n")
end
if (d6=="2") then
--[[Sony Digit 2----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d6=="3") then
--[[Sony Digit 3----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d6=="4") then
--[[Sony Digit 4----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d6=="5") then
 --[[Sony Digit 5----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d6=="6") then
--[[Sony Digit 6----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d6=="7") then
--[[Sony Digit 7----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
if (d6=="8") then
--[[Sony Digit 8----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035" .. "\r\n")
end
if (d6=="9") then
--[[Sony Digit 9----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080" .. "\r\n")
end
if (d6=="0") then
--[[Sony Digit 0----]]
local socket = require("socket")
tcp = assert(socket.connect("192.168.255.204", 4998))
tcp:send("sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058" .. "\r\n")
end
luup.sleep(1500)
tcp:close()

If you’re not closing all of your sockets you will exhaust the limited resource and get a reboot. Hard to tell if this code is doing a complete job of managing those resources.

Very, very bad idea to use luup.sleep().

A number of other code refinements possible to simplify this whole thing. If something seems long and tedious to script, then there’s almost definitely a better way of doing it.

1 Like

Thanks Patrick I put a tcp:close() after each digit evaluation and it seems completely stable now. I fired off 20 consecutive channels one right after the other without any luup restarts.

@akbooer, yes I heard that about Luup.Sleep(). I am not sure how else to handle a delay. I also thought about doing a do while loop to evaluate digits 0 thru 9 + the digit separator which is a period. The idea would be to change the evaluated variable to the next digit in the string at the top of the loop until it tests true for an x at which point the script would be terminated. Need to look into it since it would significantly shorten the code.

Good use of tables and functions will drastically reduce the size and increase the reliability of your code.

For example, the first chunk of your code processing the first digit can simply be:

local channel, ts = luup.variable_get("urn:upnp-org:serviceId:VContainer1","Variable1", 477)
local socket = require("socket")
local tcp = assert(socket.connect("192.168.255.204", 4998))
local d1, d2, d3, d4, d5, d6 = channel:match("(.)(.)(.)(.)(.)(.)")

local Digit1 = {
  ['1'] = "sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125",
  ['2'] = "sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['3'] = "sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['4'] = "sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['5'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['6'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['7'] = "sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['8'] = "sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035",
  ['9'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['0'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
}

tcp:send(Digit1[d1] .. "\r\n")

I have not looked at all the strings you are sending, but it looks as though most are duplicates, so you can simply reuse this table of responses for the other digits.

The proper way to code a delay is with the function luup.call_delay() with an associated callback function. Documentation is here:

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_call_delay

I implemented the table code you supplied and it works great with Luup.Sleep() but I can’t get it to work with luup.call_delay. I put the code together the way I understood some of the luup.call_delay documentation I looked at but I get no response from the code. It does pass the lua test.

local channel, ts = luup.variable_get("urn:upnp-org:serviceId:VContainer1","Variable1", 477)

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

local d1, d2, d3, d4, d5, d6 = channel:match("(.)(.)(.)(.)(.)(.)")

function tcp_close (TCP_Close)

local Digit1 = {

['1'] = "sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125",

['2'] = "sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",

['3'] = "sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",

['4'] = "sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",

['5'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",

['6'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",

['7'] = "sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",

['8'] = "sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035",

['9'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",

['0'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",

['.'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,48,24,48,24,24,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,48,770",

}

tcp:send(Digit1[d1] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

tcp:send(Digit1[d2] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

tcp:send(Digit1[d3] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

tcp:send(Digit1[d4] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

tcp:send(Digit1[d5] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

local socket = require("socket")

local tcp = assert(socket.connect("192.168.255.204", 4998))

tcp:send(Digit1[d6] .. "\r\n")

luup.call_delay( 'tcp_close', 2 )()

end

You are not using luup.call_delay() correctly.
Also, you don’t need multiple require statements.

Its not for a lack of effort. I tried to work thru this for quite some time this morning. I am afraid the wiki and other documentation I had found on luup.call_delay ()leaves a lot to be desired for someone like myself that is not a programmer.

It wasn’t a criticism… I’m just trying to help!

Seriously, though, if you’re trying to write code, then you ARE a programmer. It’s worth learning a little more about Lua in general, and Luup in particular. We all share the struggle with you of woefully inadequate documentation.

In outline, you need to be aiming for a code structure like this:

socket = require("socket")    -- only need this once at the start of the code

function MyDelayedCode (parameter)
  local tcp = socket.connect("192.168.255.204", 4998)
  if tcp then
    -- do a delayed action like sending TCP here
    tcp: close()
    -- possibly initiate another delayed call to this same function here
  end

  luup.call_delay (MyDelayedCode, 2, "string parameter")  -- call the delayed code
  -- this next line gets executed immediately, not waiting for the delay

If it’s still as clear as mud, please do ask again.

AK

Thanks, I did not take it as criticism, I just wanted to indicate that I was at a dead-end. I truly appreciate the help. Like I had said I have this table method working with Luup.Sleep() but I figured I should get to the bottom of luup.call_delay since I am using it in other scripts as well. In all honesty all the luup.sleep delays I am using are not more 1500 milliseconds. I thought I read that it was a danger for longer delays. In any case, I never had an issue from it as far as I know.

I do have some confusion and questions.

function MyDelayedCode (parameter) What goes in (parameter) ??

if tcp then Not sure I get this, What are we testing for here?

luup.call_delay (MyDelayedCode, 2, “string parameter”) Not sure what “string parameter” refers to.

– this next line gets executed immediately, not waiting for the delay. Not sure if this would be a problem since the firing of IR codes needs to be asynchronous.

Thanks

Fair comment. If it currently works for you, then fine. It’s just not a pattern to repeat too many times elsewhere and may cause problems which will be difficult to diagnose later.

Anyway, my last example was a step too far, but I think you can improve the code incrementally to get there in the end. Here’s a rework of your code using luup.sleep() and a function and a loop which cycles through a table of the six characters. Once you understand the basic Lua constructs, this code is much easier to look at (and much shorter) than your original, and is a stepping stone on the way to using luup.delay().

local socket = require("socket")

local Digit1 = {
  ['1'] = "sendir,1:3,1,40000,3,1,97,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1125",
  ['2'] = "sendir,1:3,1,40000,3,1,97,24,48,24,24,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['3'] = "sendir,1:3,1,40000,3,1,97,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['4'] = "sendir,1:3,1,40000,3,1,97,24,48,24,48,24,24,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['5'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['6'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['7'] = "sendir,1:3,1,40000,3,1,96,24,24,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['8'] = "sendir,1:3,1,40000,3,1,96,24,48,24,48,24,48,24,24,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1035",
  ['9'] = "sendir,1:3,1,40000,3,1,96,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1080",
  ['0'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,24,24,48,24,24,24,24,24,24,24,48,24,24,24,24,24,24,24,24,1058",
  ['.'] = "sendir,1:3,1,40000,3,1,96,24,48,24,24,24,48,24,48,24,48,24,24,24,24,24,48,24,48,24,48,24,24,24,48,24,24,24,24,24,48,770",
}

function tcp_send (digit)
  local tcp = socket.connect("192.168.255.204", 4998)
  if tcp then
    tcp: send(Digit1[digit] .. "\r\n")
    tcp: close()
  end
end

local channel = luup.variable_get("urn:upnp-org:serviceId:VContainer1","Variable1", 477)
local digits = {channel:match("(.)(.)(.)(.)(.)(.)")}

for _,d in ipairs (digits) do
  tcp_send(d)
  luup.sleep (1500)
end