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.

TCP Socket Connection for Legrand
#1
Hi,

We have Legrand system that we can send commands via TCP Socket. 

IP Address : 192.168.161
Port : 20000
Password : 12345 

On Command : *1*1*11##
Off Command : *1*0*11##

I tried to use some TCP socket examples in here but i failed. Can you help me for script regarding to information above.
Reply
#2
Do you have some description the legrand interface?
------------------------------
Ctrl+F5
Reply
#3
Hi,
We connected via f455 Legrand IP interface.
We used some Legrand softwares called Scenarx. https://www.myhome-project.it/software/scenarx
Via this software, we send Light on Command *1*1*11## and Light Off Command *1*0*11## and communicatin is worked.
Legrand system send commands via Open WebNet protocol based on TCP Socket communication.
There are open resources for openwebnet in here https://github.com/openwebnet . One is Java Client. You can see the communication example via Java.
One of the our competitor in this project make connection with this Java Client example. But we want to use LM and Luascript. We heard that our competitor just send simple tcp socket commands but via Java.
Regards,
Reply
#4
At least try connecting via telnet using Putty or similar client from your PC. Since there's a password specified some extra protocol implementation is required.
Reply
#5
Hi there ,
I will try. But if there is no password, can you write a simple script which connect to 192.168.161 address and 20000 port then it will send *1*1*11## code if the value is true.
Regards,
Reply
#6
Maybe you are missing additional \r\n after the command?
Reply
#7
Hi,
You can find some videos with putty connection.

https://www.youtube.com/watch?v=O2d_abVaqe0
https://www.youtube.com/watch?v=qDQftLB25Hc

Can you write an example script considering there is no password .
Regards,
Reply
#8
This is not a proper protocol implementation, but see if this works for you:
Code:
function openwebnetcmd(ip, cmd)
  local socket = require('socket')
  local eot = string.char(4)
  local sock = socket.tcp()

  sock:settimeout(1)

  local res, err = sock:connect(ip, 20000)
  if not res then
    alert('connect failed ' .. tostring(err))
    return
  end

  os.sleep(1)
  sock:send('*99*0##' .. eot)

  os.sleep(1)
  sock:send(cmd .. eot)

  os.sleep(1)
  sock:close()

  return true
end

openwebnetcmd('192.168.1.1', '*1*1*11##')
Reply
#9
Hi,

With a little change, it worked well.
Thank you very much.
Reply
#10
Hello,
As I said before above, sending command is working. Now I need to listen status information. For getting status,
I send *99*1## command first ,
Then I send *1*11## command. It means, I request status information for light address 11.
In putty, we get information like *1*1*11## .
In logic machine, how can i write this feedback in to a variable (Like value ) in script.

Regards,
Reply
#11
(07.05.2019, 16:00)savaskorkmaz Wrote: Hi,

With a little change, it worked well.
Thank you very much.

Hello,

Would you share the changes to the script and how you tied it to a password?

Thanks in advance!
Reply
#12
The password algorithm differs between gateways. Which one are you using?
Reply
#13
(11.04.2022, 12:14)admin Wrote: The password algorithm differs between gateways. Which one are you using?

Gateway is MH200N
Reply
#14
Try this, change PASS and IP as needed. The script handles the authentication part, after that you can issue commands, read status info etc.

Code:
function calcpass(pass, nonce)
  local flag = true
  local num1 = 0
  local num2 = 0
  local password = tonumber(pass, 10)

  for i = 1, #nonce do
    local c = nonce:sub(i, i)

    if c ~= '0' then
      if flag then
        num2 = password
      end

      flag = false
    end

    if c == '1' then
      num1 = bit.band(num2, 0xFFFFFF80)
      num1 = bit.rshift(num1, 7)
      num2 = bit.lshift(num2, 25)
      num1 = num1 + num2
    elseif c == '2' then
      num1 = bit.band(num2, 0xFFFFFFF0)
      num1 = bit.rshift(num1, 4)
      num2 = bit.lshift(num2, 28)
      num1 = num1 + num2
    elseif c == '3' then
      num1 = bit.band(num2, 0xFFFFFFF8)
      num1 = bit.rshift(num1, 3)
      num2 = bit.lshift(num2, 29)
      num1 = num1 + num2
    elseif c == '4' then
      num1 = bit.lshift(num2, 1)
      num2 = bit.rshift(num2, 31)
      num1 = num1 + num2
    elseif c == '5' then
      num1 = bit.lshift(num2, 5)
      num2 = bit.rshift(num2, 27)
      num1 = num1 + num2
    elseif c == '6' then
      num1 = bit.lshift(num2, 12)
      num2 = bit.rshift(num2, 20)
      num1 = num1 + num2
    elseif c == '7' then
      num1 = bit.band(num2, 0x0000FF00)
      num1 = num1 + bit.lshift(bit.band(num2, 0x000000FF), 24)
      num1 = num1 + bit.rshift(bit.band(num2, 0x00FF0000), 16)
      num2 = bit.rshift(bit.band(num2, 0xFF000000), 8)
      num1 = num1 + num2
    elseif c == '8' then
      num1 = bit.band(num2, 0x0000FFFF)
      num1 = bit.lshift(num1, 16)
      num1 = num1 + bit.rshift(num2, 24)
      num2 = bit.band(num2, 0x00FF0000)
      num2 = bit.rshift(num2, 8)
      num1 = num1 + num2
    elseif c == '9' then
      num1 = bit.bnot(num2)
    elseif c == '0' then
      num1 = num2
    end

    num2 = num1
  end

  if num1 < 0 then
    num1 = num1 + 4294967296
  end

  return tostring(num1)
end

function read(sock)
  local buf = {}

  while true do
    local ch, err = sock:receive(1)

    if ch then
      local pr = buf[ #buf ]

      buf[ #buf + 1 ] = ch

      if ch == '#' and pr == '#' then
        break
      end

    else
      return nil, err
    end
  end

  return table.concat(buf)
end

ACK = '*#*1##'
NACK = '*#*0##'
OPEN = '*99*0##'

PASS = '12345'
IP = '127.0.0.1'
PORT = 20000

function openwebnetinit()
  local sock = require('socket').tcp()
  local res, err

  sock:settimeout(1)

  res, err = sock:connect(IP, PORT)
  if not res then
    alert('connect failed ' .. tostring(err))
    return
  end

  res, err = read(sock)
  if res ~= ACK then
    alert('no initial ack ' .. tostring(res or err))
    return
  end

  sock:send(OPEN)

  res, err = read(sock)
  if not res then
    alert('no nonce ' .. tostring(err))
    return
  end

  local nonce = res:match('%d+')
  local pass = calcpass(PASS, nonce)

  sock:send('*#' .. pass .. '##')

  res, err = read(sock)
  if res ~= ACK then
    alert('auth failed ' .. tostring(res or err))
    return
  end

  return sock
end

sock = openwebnetinit()
if sock then
  alert('connection ok')
  -- do something here
  sock:close()
end
Reply
#15
Thank you very much for the answer and the example! I will try it on Saturday and give feedback.
Reply
#16
(13.04.2022, 12:02)admin Wrote: Try this, change PASS and IP as needed. The script handles the authentication part, after that you can issue commands, read status info etc.

Code:
function calcpass(pass, nonce)
  local flag = true
  local num1 = 0
  local num2 = 0
  local password = tonumber(pass, 10)

  for i = 1, #nonce do
    local c = nonce:sub(i, i)

    if c ~= '0' then
      if flag then
        num2 = password
      end

      flag = false
    end

    if c == '1' then
      num1 = bit.band(num2, 0xFFFFFF80)
      num1 = bit.rshift(num1, 7)
      num2 = bit.lshift(num2, 25)
      num1 = num1 + num2
    elseif c == '2' then
      num1 = bit.band(num2, 0xFFFFFFF0)
      num1 = bit.rshift(num1, 4)
      num2 = bit.lshift(num2, 28)
      num1 = num1 + num2
    elseif c == '3' then
      num1 = bit.band(num2, 0xFFFFFFF8)
      num1 = bit.rshift(num1, 3)
      num2 = bit.lshift(num2, 29)
      num1 = num1 + num2
    elseif c == '4' then
      num1 = bit.lshift(num2, 1)
      num2 = bit.rshift(num2, 31)
      num1 = num1 + num2
    elseif c == '5' then
      num1 = bit.lshift(num2, 5)
      num2 = bit.rshift(num2, 27)
      num1 = num1 + num2
    elseif c == '6' then
      num1 = bit.lshift(num2, 12)
      num2 = bit.rshift(num2, 20)
      num1 = num1 + num2
    elseif c == '7' then
      num1 = bit.band(num2, 0x0000FF00)
      num1 = num1 + bit.lshift(bit.band(num2, 0x000000FF), 24)
      num1 = num1 + bit.rshift(bit.band(num2, 0x00FF0000), 16)
      num2 = bit.rshift(bit.band(num2, 0xFF000000), 8)
      num1 = num1 + num2
    elseif c == '8' then
      num1 = bit.band(num2, 0x0000FFFF)
      num1 = bit.lshift(num1, 16)
      num1 = num1 + bit.rshift(num2, 24)
      num2 = bit.band(num2, 0x00FF0000)
      num2 = bit.rshift(num2, 8)
      num1 = num1 + num2
    elseif c == '9' then
      num1 = bit.bnot(num2)
    elseif c == '0' then
      num1 = num2
    end

    num2 = num1
  end

  if num1 < 0 then
    num1 = num1 + 4294967296
  end

  return tostring(num1)
end

function read(sock)
  local buf = {}

  while true do
    local ch, err = sock:receive(1)

    if ch then
      local pr = buf[ #buf ]

      buf[ #buf + 1 ] = ch

      if ch == '#' and pr == '#' then
        break
      end

    else
      return nil, err
    end
  end

  return table.concat(buf)
end

ACK = '*#*1##'
NACK = '*#*0##'
OPEN = '*99*0##'

PASS = '12345'
IP = '127.0.0.1'
PORT = 20000

function openwebnetinit()
  local sock = require('socket').tcp()
  local res, err

  sock:settimeout(1)

  res, err = sock:connect(IP, PORT)
  if not res then
    alert('connect failed ' .. tostring(err))
    return
  end

  res, err = read(sock)
  if res ~= ACK then
    alert('no initial ack ' .. tostring(res or err))
    return
  end

  sock:send(OPEN)

  res, err = read(sock)
  if not res then
    alert('no nonce ' .. tostring(err))
    return
  end

  local nonce = res:match('%d+')
  local pass = calcpass(PASS, nonce)

  sock:send('*#' .. pass .. '##')

  res, err = read(sock)
  if res ~= ACK then
    alert('auth failed ' .. tostring(res or err))
    return
  end

  return sock
end

sock = openwebnetinit()
if sock then
  alert('connection ok')
  -- do something here
  sock:close()
end
I get this as errors:

AS 15.04.2022 22:59:11
Resident script:19: bad argument #1 to 'band' (number expected, got nil)
stack traceback:
[C]: in function 'band'
Resident script:19: in function 'calcpass'
Resident script:132: in function 'openwebnetinit'

AS 15.04.2022 22:59:21
Resident script:19: bad argument #1 to 'band' (number expected, got nil)
stack traceback:
[C]: in function 'band'
Resident script:19: in function 'calcpass'
Resident script:132: in function 'openwebnetinit'

AS 15.04.2022 22:59:32
Resident script:19: bad argument #1 to 'band' (number expected, got nil)
stack traceback:
[C]: in function 'band'
Resident script:19: in function 'calcpass'
Resident script:132: in function 'openwebnetinit'

AS 15.04.2022 22:59:45
Resident script:19: bad argument #1 to 'band' (number expected, got nil)
stack traceback:
[C]: in function 'band'
Resident script:19: in function 'calcpass'
Resident script:132: in function 'openwebnetinit'

AS 15.04.2022 22:59:58
Resident script:19: bad argument #1 to 'band' (number expected, got nil)
stack traceback:
[C]: in function 'band'
Resident script:19: in function 'calcpass'
Resident script:132: in function 'openwebnetinit'
Reply
#17
What I managed to do so far is to redirect the data from MH200 to LM without a password, because obviously the error in the script is from the password. I'm trying to read what's going on the line using the first code, but to no avail. How do I make the codes coming out of the MH200N visible in the console and be able to associate them with virtual objects?
Reply
#18
Start by logging what data the gateway is sending. Modify read function like this:
Code:
function read(sock)
  local buf = {}

  while true do
    local ch, err = sock:receive(1)

    if ch then
      local pr = buf[ #buf ]

      buf[ #buf + 1 ] = ch

      if ch == '#' and pr == '#' then
        break
      end

    else
      return nil, err
    end
  end

  local ret = table.concat(buf)
  log(ret)
  return ret
end

For an event session you need to change OPEN command to *99*1##
Reply
#19
Code:
function read(sock)
  local buf = {}

  while true do
    local ch, err = sock:receive(1)

    if ch then
      local pr = buf[ #buf ]

      buf[ #buf + 1 ] = ch

      if ch == '#' and pr == '#' then
        break
      end

    else
      return nil, err
    end
  end

  local ret = table.concat(buf)
  log(ret)
  return ret
end
ACK = '*#*1##'
NACK = '*#*0##'
OPEN = '*99*1##'



function openwebnetinit()
  local socket = require('socket')
    local sock = socket.tcp()

sock:settimeout(1)

   local res, err = socket.connect('192.168.1.247', 20000)
  if not res then
    alert('connect failed ' .. tostring(err))
    return
  end

  res, err = read(sock)
  if res ~= ACK then
    alert('no initial ack ' .. tostring(res or err))
    return
  end

  sock:send(OPEN)

  res, err = read(sock)
  if not res then
    alert('no nonce ' .. tostring(err))
    return
  end

  local nonce = res:match('%d+')


  res, err = read(sock)
  if res ~= ACK then
    alert('auth failed ' .. tostring(res or err))
    return
  end

  return sock
end

sock = openwebnetinit()
if sock then
  alert('connection ok')
  -- do something here
  sock:close()
end
Code:
Resident script:5: calling 'receive' on bad self (tcp{client} expected, got userdata)
stack traceback:
[C]: in function 'receive'
Resident script:5: in function 'read'
Resident script:43: in function 'openwebnetinit'
Apparently I'm confusing something general?
Reply
#20
Line 37 is incorrect:
Code:
local res, err = socket.connect('192.168.1.247', 20000)

It should be:
Code:
local res, err = sock:connect('192.168.1.247', 20000)
Reply


Forum Jump: