Script Send UDP Problem

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
tonymer
Posts: 16
Joined: Wednesday 15 June 2022 16:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Script Send UDP Problem

Post by tonymer »

Hello, I try to send a request to my investor and the response seems cut off, I should receive 148 Bytes. Can you think what could be the problem? I send that same package by Packet Sender and it works correctly

Code: Select all

 return {
	on = {
		timer = {
			'every minute',			
			function(domoticz)
			    
				return true
			end,
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, timer)
	    
	    package.cpath ="/usr/lib/arm-linux-gnueabihf/lua/5.3/?.so;" .. package.cpath
	    package.path = "/usr/share/lua/5.3/?.lua;" .. package.path
	    
	    local socket = require("socket")
	    local udp = assert(socket.udp())
        local data
        udp:settimeout(2)
        assert(udp:setsockname("*",0))
        assert(udp:setpeername("192.168.1.10",8899))
        
        for i = 0, 2, 1 do
            assert(udp:send("\xAA\x55\xc0\x7f\x01\x06\x00\x02\x45"))
            data = udp:receive()
                if data then
                    break
                end
        end
        if data == nil then
            print("timeout")
            else
            print("Datos Recibidos: "..data)
            end
        
        
        local energiadisponible=-200
	    print("Energia disponible="..energiadisponible)
	    domoticz.devices('Energia Disponible').updateEnergy(energiadisponible)
	    
	    
		domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
	end
} 
reply: Datos Recibidos: ʕ�ƌw
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Script Send UDP Problem

Post by boum »

Not sure if this is the root cause, but you probably shouldn't wait for an answer in a dzVents (or Lua) script. The scripting engine runs in only one thread and if a current iteration of dzVents takes more than 10s to run, it is interrupted.
You should look at https://www.domoticz.com/wiki/DzVents:_ ... _execution
There you can run your Lua script and return some information to your script using a shellCommandResponses trigger. The response will be parsed in another iteration of the scripts.

Of course, the issue might be somewhere else. I don't know the socket package so I cannot help you there. (maybe you have to loop on udp:receive until you get all the response?)
tonymer
Posts: 16
Joined: Wednesday 15 June 2022 16:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script Send UDP Problem

Post by tonymer »

Thanks, I'll look into it. If I figure something out I'll post it here.
tonymer
Posts: 16
Joined: Wednesday 15 June 2022 16:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script Send UDP Problem

Post by tonymer »

Hi, that wasn't the problem, I tried to put it in an infinite loop and after 10 seconds domoticz took you out. The problem was that there was no problem, you just had to convert the output to hexadecimal. Once this was done, all the bytes of the inverter's information were output. When I have it ready I'll post it here in case it's worth it to someone. I check all the data of the inverter locally every minute. the inverter is a Goodwe Hybrid (although without batteries) of the EM series.
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Script Send UDP Problem

Post by boum »

good for you
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest