This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

UDP commands
#17
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
Reply


Messages In This Thread
UDP commands - by Domoticatorino - 08.12.2016, 14:42
RE: UDP commands - by admin - 08.12.2016, 14:48
RE: UDP commands - by Domoticatorino - 09.12.2016, 18:10
RE: UDP commands - by buuuudzik - 12.12.2016, 13:22
RE: UDP commands - by Erwin van der Zwart - 12.12.2016, 14:04
RE: UDP commands - by Domoticatorino - 12.12.2016, 14:23
RE: UDP commands - by admin - 12.12.2016, 14:26
RE: UDP commands - by Domoticatorino - 15.12.2016, 18:43
RE: UDP commands - by admin - 15.12.2016, 19:02
RE: UDP commands - by Domoticatorino - 15.12.2016, 20:48
RE: UDP commands - by Domoticatorino - 02.01.2017, 18:24
RE: UDP commands - by Erwin van der Zwart - 02.01.2017, 20:12
RE: UDP commands - by Domoticatorino - 03.01.2017, 12:07
RE: UDP commands - by admin - 03.01.2017, 13:22
RE: UDP commands - by Domoticatorino - 04.01.2017, 09:27
RE: UDP commands - by Domoticatorino - 04.01.2017, 16:01
RE: UDP commands - by Domoticatorino - 14.03.2017, 22:36
RE: UDP commands - by Erwin van der Zwart - 15.03.2017, 00:29
RE: UDP commands - by Domoticatorino - 15.03.2017, 15:57
RE: UDP commands - by Erwin van der Zwart - 15.03.2017, 21:45
RE: UDP commands - by Domoticatorino - 19.03.2017, 21:28
RE: UDP commands - by gilles38 - 01.05.2017, 12:38
RE: UDP commands - by Domoticatorino - 08.06.2017, 17:30

Forum Jump: