We have moved at community.getvera.com

Author Topic: Send UDP with lua  (Read 5181 times)

Offline shaked

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Send UDP with lua
« on: May 02, 2015, 04:00:06 pm »
Hello,

I have this Chinese wifi wall plug that is controlled with UDP packets.
what I've tried so far is to send Hex / ASCII strings like so:

Code: [Select]
socket = require("socket")
local s = socket.udp()
s:sendto("68 64 00 1e 63 6c ac cf 23 44 fa ce 20 20 20 20 20 20 c0 19 24 23 cf ac", "10.0.0.1", 10000) -- start session
s:sendto("68 64 00 17 64 63 ac cf 23 44 fa ce 20 20 20 20 20 20 00 00 00 00 01 ", "10.0.0.1", 10000) --on
s:close()

As you can see, the port is 10000 and before sending the ON command , I need to start a session with the device.

btw, is sendto function requires Hex or ASCII ?

No luck so far, can anyone help?
thanks

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3260
  • Karma: +192/-9
Re: Send UDP with lua
« Reply #1 on: May 05, 2015, 08:08:45 pm »
sendto() requires a binary string.
You can build it up using string.char():
Code: [Select]
local hex = {
  "68", "64", "00", "1e" -- and so on
}
local binary = "";
for i, v in ipairs(hex) do
  binary = binary .. string.char(tonumber(v, 16))
end
Now the variable contains the string you need to provide to sendto().

Offline brewymatter

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Re: Send UDP with lua
« Reply #2 on: November 23, 2016, 10:01:57 pm »
I have the same wifi switch I think:
Orvibo S20

Anybody have luck with sending that udp hex string? I can't get it to work.

Offline martin12345

  • Sr. Member
  • ****
  • Posts: 321
  • Karma: +48/-4
Re: Send UDP with lua
« Reply #3 on: December 06, 2016, 03:45:35 pm »
sendto uses ascii in my experience, e.g. here is code I wrote for a different use case to send the text D:101:E to turn on device 01 for example;

Code: [Select]
        local socket = require "socket"
        local udp = socket.try(socket.udp())
        local broadcast_ip = '255.255.255.255'
        local port = 53008
        assert(udp:setoption("broadcast", true))
udp:settimeout (10)                  -- wait 10 seconds
socket.try (udp:setsockname ('*', port))

        local device_address=luup.devices[lul_device].mac

        luup.variable_set( "urn:upnp-org:serviceId:SwitchPower1", "Status", lul_settings.newTargetValue, lul_device)
        if (lul_settings.newTargetValue=="1") then
          socket.try(udp:sendto("D:1"..device_address..":E", broadcast_ip, port))
        else
          socket.try(udp:sendto("D:0"..device_address..":E", broadcast_ip, port))
        end
       
        socket.try(udp:close())

Offline Quinten

  • Sr. Member
  • ****
  • Posts: 273
  • Karma: +2/-0
Re: Send UDP with lua
« Reply #4 on: December 06, 2016, 03:50:53 pm »
I've added the following function to the startup Lua:

Code: [Select]
function sendUDP(msg)
  local socket = require "socket"
  local udp = assert(socket.udp())
  assert(udp:sendto(msg, "<ip address here>", <port number here>))
  assert(udp:close())
end

and in my scenes I just use:

Code: [Select]
sendUDP("garage door open")

On the listener (simple perl script) side I have routines which do different things dependent on different text.
VeraLite - UI7 1.7.947 | 8x Fibaro FGD211 | 2x Fibaro FGS221 | 1x Fibaro FGBS321 | 3x Fibaro FGRGBW | 1x Everspring SP814 | 2x Everspring AN157-3 | 2x Heatmiser PRT-TS WiFi | 1x Logitech Harmony Hub | 1x Philips LivingColour

Offline Tlex

  • Sr. Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
Re: Send UDP with lua
« Reply #5 on: March 05, 2019, 07:40:48 am »
I was wondering if someone could help me achieve the same kind of thing but for tcp packet ?
I need to send the ascii code "!000D000c;" to 192.168.1.101:9000 in TCP from my vera controller.
I tried multiple things without success yet.

Thanks :)

Offline Tlex

  • Sr. Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
Re: Send UDP with lua
« Reply #6 on: March 05, 2019, 09:55:13 am »
got my answer :)

local IP = "xxx.xxx.xxx.xxx"
local Port = "9000"
local Command = "!000D000c;\r"
local socket = require("socket")
c = assert(socket.connect(IP, Port))
c:settimeout(5)
local sres, serr = c:send(Command)
c:close()