14.03.2017, 22:36
Hi, I found on web this script to integrate Milight led. There are udp commends. Could you let me know if it is suitable for HL or some instruction should be modified?
Thanks.
local IP = 192.168.1.2
local Port = 5987
local ZoneID = string.char(0x01)
--List of Commands
local Init = string.char (0x20,0x00,0x00,0x00,0x16,0x02,0x62,0x3A,0xD5,0xED,0xA3,0x01,0xAE,0x08,0x2D,0x46,0x61,0x41,0xA7,0xF6,0xDC,0xAF,0xFE,0xF7,0x00,0x00,0x1E)
local Preample = string.char (0x80,0x00,0x00,0x00,0x11)
local Filler = string.char (0x00)
local On = string.char (0x31,0x00,0x00,0x08,0x04,0x01,0x00,0x00,0x00)
local Off = string.char(0x31,0x00,0x00,0x08,0x04,0x02,0x00,0x00,0x00)
--Generate random Sequential Byte just helps with keeping commands in the correct order, and it helps to ignore duplicate packets already received.
math.randomseed(os.time())
math.random(); math.random(); math.random()
local RandomNumber = math.random(255)
local SequentialByte = string.char(RandomNumber)
--Action
local Command = On
--Send Init and wait for response
local Response={}
socket = Net.FUdpSocket()
socket:setBroadcast(true)
bytes, ErrorCode = socket:write (Init, IP, Port)
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=22) do
Response, ErrorCode = socket:read()
ResponseLength = string.len(Response)
end
end
--Extract data from UDP response
local MAC = Response:sub(0x09,0x0E)
local WifiBridgeSessionID1 = Response:sub(0x14,0x14)
local WifiBridgeSessionID2 = Response:sub(0x15,0x15)
local Payload = Preample..WifiBridgeSessionID1..WifiBridgeSessionID2..Filler..SequentialByte..Filler..Command..ZoneID..Filler
local ChksumString = Payload:sub(0x0B)
--Calculate Checksum
local i = 1
local Chksum = 0
while i <= ChksumString:len() do
Number = ChksumString:sub(i,i)
Chksum = Chksum + tonumber(Number:tohex(),16)
i = i + 1
end
--Calculate remainder Chksum%256 same as &0xFF
Chksum = (Chksum - math.floor(Chksum/256)*256)
local Chksumchar = string.char(Chksum)
Payload = Payload..Chksumchar
Response={}
bytes, ErrorCode = socket:write (Payload, IP, Port)
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=8) do
Response, ErrorCode = socket:read()
ResponseLength = string.len(Response)
end
end
socket = nil
--Extract data from UDP response
local Result = ""
local ResultByte = Response:sub(0x08,0x08)
local ResponseSequentialByte = Response:sub(0x07,0x07)
if (ResponseSequentialByte==SequentialByte and ResultByte==Filler) then
Result = "Successfull"
else
Result = "Failed"
end
Thanks.
local IP = 192.168.1.2
local Port = 5987
local ZoneID = string.char(0x01)
--List of Commands
local Init = string.char (0x20,0x00,0x00,0x00,0x16,0x02,0x62,0x3A,0xD5,0xED,0xA3,0x01,0xAE,0x08,0x2D,0x46,0x61,0x41,0xA7,0xF6,0xDC,0xAF,0xFE,0xF7,0x00,0x00,0x1E)
local Preample = string.char (0x80,0x00,0x00,0x00,0x11)
local Filler = string.char (0x00)
local On = string.char (0x31,0x00,0x00,0x08,0x04,0x01,0x00,0x00,0x00)
local Off = string.char(0x31,0x00,0x00,0x08,0x04,0x02,0x00,0x00,0x00)
--Generate random Sequential Byte just helps with keeping commands in the correct order, and it helps to ignore duplicate packets already received.
math.randomseed(os.time())
math.random(); math.random(); math.random()
local RandomNumber = math.random(255)
local SequentialByte = string.char(RandomNumber)
--Action
local Command = On
--Send Init and wait for response
local Response={}
socket = Net.FUdpSocket()
socket:setBroadcast(true)
bytes, ErrorCode = socket:write (Init, IP, Port)
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=22) do
Response, ErrorCode = socket:read()
ResponseLength = string.len(Response)
end
end
--Extract data from UDP response
local MAC = Response:sub(0x09,0x0E)
local WifiBridgeSessionID1 = Response:sub(0x14,0x14)
local WifiBridgeSessionID2 = Response:sub(0x15,0x15)
local Payload = Preample..WifiBridgeSessionID1..WifiBridgeSessionID2..Filler..SequentialByte..Filler..Command..ZoneID..Filler
local ChksumString = Payload:sub(0x0B)
--Calculate Checksum
local i = 1
local Chksum = 0
while i <= ChksumString:len() do
Number = ChksumString:sub(i,i)
Chksum = Chksum + tonumber(Number:tohex(),16)
i = i + 1
end
--Calculate remainder Chksum%256 same as &0xFF
Chksum = (Chksum - math.floor(Chksum/256)*256)
local Chksumchar = string.char(Chksum)
Payload = Payload..Chksumchar
Response={}
bytes, ErrorCode = socket:write (Payload, IP, Port)
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=8) do
Response, ErrorCode = socket:read()
ResponseLength = string.len(Response)
end
end
socket = nil
--Extract data from UDP response
local Result = ""
local ResultByte = Response:sub(0x08,0x08)
local ResponseSequentialByte = Response:sub(0x07,0x07)
if (ResponseSequentialByte==SequentialByte and ResultByte==Filler) then
Result = "Successfull"
else
Result = "Failed"
end