Page 1 of 1

Script Send UDP Problem

Posted: Wednesday 22 June 2022 15:09
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

Re: Script Send UDP Problem

Posted: Thursday 23 June 2022 15:27
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?)

Re: Script Send UDP Problem

Posted: Friday 24 June 2022 9:27
by tonymer
Thanks, I'll look into it. If I figure something out I'll post it here.

Re: Script Send UDP Problem

Posted: Monday 27 June 2022 17:28
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.

Re: Script Send UDP Problem

Posted: Monday 27 June 2022 22:35
by boum
good for you