15.03.2017, 21:45
(This post was last modified: 15.03.2017, 21:55 by Erwin van der Zwart.)
Hi,
Just checked your script but it's incomplete, i changed the socket and added comments on socket changes and to your missing functions..
You probably will get a reply as 'Response' but the extraction of the reply data would need to be rebuild without the additional functions ..
BR,
Erwin
Just checked your script but it's incomplete, i changed the socket and added comments on socket changes and to your missing functions..
Code:
require('socket') -- added socket lib
local IP = '192.168.1.2' -- added set ip into ''
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={}
local client = socket.udp() -- added socket client
clientConfusedetBroadcast(true) -- missing function don't know what it should do
bytes, ErrorCode = client:write (Init, IP, Port) -- changed socket request
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=22) do
Response, ErrorCode = client:read() -- changed socket request
ResponseLength = string.len(Response)
end
end
--Extract data from UDP response
local MAC = ResponseConfusedub(0x09,0x0E) -- missing function don't know what it should do
local WifiBridgeSessionID1 = ResponseConfusedub(0x14,0x14) -- missing function don't know what it should do
local WifiBridgeSessionID2 = ResponseConfusedub(0x15,0x15) -- missing function don't know what it should do
local Payload = Preample..WifiBridgeSessionID1..WifiBridgeSessionID2..Filler..SequentialByte..Filler..Command..ZoneID..Filler --missing ID1 & ID2 result due to missing functions
local ChksumString = PayloadConfusedub(0x0B) -- missing function don't know what it should do
--Calculate Checksum
local i = 1
local Chksum = 0
while i <= ChksumString:len() do
Number = ChksumStringConfusedub(i,i)
Chksum = Chksum + tonumber(Number:tohex(),16) -- i think this must be lmcore.intohex()
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 = client:write (Payload, IP, Port) -- changed socket request
if ErrorCode == 0 then
local ResponseLength = 1
while (ErrorCode==0 and ResponseLength>0 and ResponseLength~=8) do
Response, ErrorCode = client:read() -- changed socket request
ResponseLength = string.len(Response)
end
end
client = nil
--Extract data from UDP response
local Result = ""
local ResultByte = ResponseConfusedub(0x08,0x08) -- missing function don't know what it should do
local ResponseSequentialByte = ResponseConfusedub(0x07,0x07) -- missing function don't know what it should do
if (ResponseSequentialByte==SequentialByte and ResultByte==Filler) then -- missing Byte due to missing function
Result = "Successfull"
else
Result = "Failed"
end
You probably will get a reply as 'Response' but the extraction of the reply data would need to be rebuild without the additional functions ..
BR,
Erwin