Send UDP with lua

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:

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

sendto() requires a binary string.
You can build it up using string.char():

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().

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.

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] 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())

[/code]

I’ve added the following function to the startup Lua:

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:

sendUDP("garage door open")

On the listener (simple perl script) side I have routines which do different things dependent on different text.

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

got my answer :slight_smile:

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()