Logic Machine Forum
UDP commands - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Hardware (https://forum.logicmachine.net/forumdisplay.php?fid=12)
+--- Thread: UDP commands (/showthread.php?tid=500)

Pages: 1 2


UDP commands - Domoticatorino - 08.12.2016

Hi, I am working on integration with Milight.
I have to send UPD commands to manage the system.
Could you give me some details or example about the way to send UDP commands?
Thanks.


RE: UDP commands - admin - 08.12.2016

http://w3.impa.br/~diego/software/luasocket/udp.html


RE: UDP commands - Domoticatorino - 09.12.2016

Thank you Admin,
just a quick information. As you can see in the link (http://www.limitlessled.com/dev/) I have to send a datagram made of like below

UDP.SEND hex bytes: 20 00 00 00 16 02 62 3A D5 ED A3 01 AE 08 2D 46 61 41 A7 F6 DC AF (D3 E6) 00 00 1E <-- Send this to the ip address of the wifi bridge v6

In your opinion the following command can work? Because unfortunately it does not work:

local socket = require('socket')
local udp = socket.udp()

udpConfusedettimeout(200)

udpConfusedendto('31x00x00x08x04x02x00x00x00','192.168.1.32','5987')
data = udp:receive()

log(data)

if I use udpConfusedend it does not accepted.

Any suggestion?

Thanks.


RE: UDP commands - buuuudzik - 12.12.2016

You should type command in such form:
Code:
cmd = string.char(0x31,0x00,0x00,0x08,0x04,0x02,0x00,0x00,0x00
You can use this function for sending with feedback:
Code:
function send(cmd)
-- edit ip address and port
ip = '192.168.1.32'
port = 5987

 local sock = require('socket').udp()
 if sock then
   sock:sendto(cmd, ip, port)
   data = sock:receive()
   sock:close()
 end
return data
end



RE: UDP commands - Erwin van der Zwart - 12.12.2016

Hi,

I would add a timeout on the receive socket as now it can hang forever..

BR,

Erwin


RE: UDP commands - Domoticatorino - 12.12.2016

Erwing, you would add timeout setting after data = sock:receive ()?

Thanks.


RE: UDP commands - admin - 12.12.2016

It must be before receive


RE: UDP commands - Domoticatorino - 15.12.2016

ok. I tried this:

function cmd (mioff)
-- edit ip address and port
ip = '192.168.1.32'
port = 5987

 local sock = require('socket').udp()
 if sock then
   sockConfusedendto(cmd, ip, port)
sockConfusedettimeout(200)
   data = sock:receive()
   sock:close()
 end
return data
end
log(data)

as event function I used this following:

require("user.milight")
mioff = string.char(0x31,0x00,0x00,0x00,0x03,0x04,0x00,0x00,0x00,0x01)
cmd (mioff)

But I received the error below:

User Library "Milight", Line 8: bad argument #1 to 'sendto' (string expected, got function).

Thanks.


RE: UDP commands - admin - 15.12.2016

First argument of sendto must be mioff not cmd


RE: UDP commands - Domoticatorino - 15.12.2016

I am tired...too late to coding.
Thanks.


RE: UDP commands - Domoticatorino - 02.01.2017

Code:
function cmd (mioff)
ip = '192.168.1.255'
port = 48899

local sock = require('socket').udp()
if sock then
   sock:sendto(mioff, ip, port)
    sock:settimeout(200)
 data = sock:receive()
 sock:close()
end
return data
end

Hi there, I am going on to embadded Milight on HL unfortunately without success. User library above is not changed but HL error log tells me that on line 3:loop or previous error loading 'user.milight'.

Furthermore I tried to snif using wireshark and it seems that there is not any telegram sent by UDP to ny device. Event based script is the one shown below:

Code:
require("user.milight")
    mioff = string.char(0x80,0x00,0x00,0x00,0x11,0x00,0x31,0x00,0x00,0x08,0x04,0x02,0x00,0x00,0x00)
 cmd (mioff)
 Any suggestion?

Thanks.


RE: UDP commands - Erwin van der Zwart - 02.01.2017

Hi,

Can you try to put the function 'cmd' direct in your script (on top) without the require of a user lib?
Make sure there is no function called 'cmd' in your common functions to avoid a duplicate function.
BR,
Erwin


RE: UDP commands - Domoticatorino - 03.01.2017

Code:
mioff = string.char(0x80,0x00,0x00,0x00,0x11,0x00,0x31,0x00,0x00,0x08,0x04,0x02,0x00,0x00,0x00)
ip = '192.168.1.255'
port = 48899

local sock = require('socket').udp()
if sock then
   sock:sendto(mioff,ip,port)
    sock:settimeout(200)
 data = sock:receive()
 sock:close()
  end
    return data
Hi Erwin,
directly on event based I wrote this code but with Wireshark I cannot see any telegram leaving from HL to address 192.168.1.255.
What is wrong in your opinion?
Thanks.


RE: UDP commands - admin - 03.01.2017

You must add sock:setoption('broadcast', true) before sendto


RE: UDP commands - Domoticatorino - 04.01.2017

Thank you admin. It is working.
Now I can work on the telegrams. I'll keep you informed.
Regards.


RE: UDP commands - Domoticatorino - 04.01.2017

I am sending the 0x4c,0x69,0x6e,0x6b,0x5f,0x57,0x69,0x2d,0x46,0x69. It is part of the same char which I read with Wireshark when I command light using android app of Milight.
I do not understand the reason why when I send commands by Milight Android app telegram lenght is 52 instead sending telegram (just string above) lenght is 60.

The problem is thay I should send further data but telegram will be longer. What is your thoughs?

Thanks.


RE: UDP commands - Domoticatorino - 14.03.2017

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


RE: UDP commands - Erwin van der Zwart - 15.03.2017

Hi,

Could work, i looked short but already noticed that the socket declaration must be changed to HL socket connection.

Will check tommorow evening..

BR,

Erwin


RE: UDP commands - Domoticatorino - 15.03.2017

Thank you Erwin. I await for your response before test. It is a good opportunity to integrate another system of market.
Regards.


RE: UDP commands - Erwin van der Zwart - 15.03.2017

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