28.01.2019, 08:19
You will need a resident script for receive side and several event scripts for sending requests. Keep in mind that UDP must be enabled separately: Enabled UDP function with DIP-switch DWS1.3 = ON.
Receive script, change 192.168.1.1 to IP address of P30. Since messages are sent via broadcast this script will receive messages that are sent from other scripts. Remove logging once receive is working.
Send script, change data request as needed:
Another thing to consider is that reply examples for reports are not valid JSON so you might have to add some extra code for parsing.
Receive script, change 192.168.1.1 to IP address of P30. Since messages are sent via broadcast this script will receive messages that are sent from other scripts. Remove logging once receive is working.
Code:
if not client then
require('socket')
require('ifinfo')
require('json')
ip = ifinfo().eth0.bcast
port = 7090
client = socket.udp()
client:setoption('broadcast', true)
client:setoption('reuseaddr', true)
client:setsockname(ip, port)
client:settimeout(1)
end
data, src = client:receivefrom()
if data then
log(data, src)
if src == '192.168.1.1' then
data = json.pdecode(data)
log(data)
end
end
Send script, change data request as needed:
Code:
require('socket')
require('ifinfo')
data = 'i'
ip = ifinfo().eth0.bcast
port = 7090
client = socket.udp()
client:setoption('broadcast', true)
client:setoption('reuseaddr', true)
client:sendto(data, ip, port)
client:close()
Another thing to consider is that reply examples for reports are not valid JSON so you might have to add some extra code for parsing.