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
#20
Hi,

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
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: