Logic Machine Forum
Blinds Control - Multiple Socket Connections Per Script - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Blinds Control - Multiple Socket Connections Per Script (/showthread.php?tid=2459)



Blinds Control - Multiple Socket Connections Per Script - jamesng - 09.02.2020

Hi

I'm trying to control Somfy blinds by issuing an UP or DOWN command followed by a STOP command a few seconds later to achieve a partially open blind.  ie: to automatically open the blind 30%.


I'm making a socket connection to the Somfy controller - this works fine - however I don't seem be able to send two separate commands over two socket connections from the one event-based script.  The second sendCommand never gets sent.  The controller is able to receive multiple commands ok when triggered from separate buttons / scripts within the same timeframe.

Is there a different way to code this so that the LUA sends the second command?   

Thanks in advance
 
Kind Regards
James



FUNCTION

Code:
function sendCommand(command)
  local socket = require("socket").tcp()
  data, err = socket:connect('192.168.1.200', 4999)
  data, err = socket:send(command .. '\x0D\x0A') 
end


SCRIPT


Code:
BlindsTravel = 22; -- Time in seconds to fully open/close
BlindsStep = math.round(100/BlindsTravel);  -- Number of percentage steps to move per second
BlindsNewPosition = GetUserParam('Wired', 'BlindsNewPosition');
BlindsCurrentPosition = GetUserParam('Wired', 'BlindsCurrentPosition');

-- Move Blind Down / Open
if (BlindsNewPosition > BlindsCurrentPosition) then
 
  sendCommand('0113D\x0D\x0A'); -- Send down command
   
  while (BlindsNewPosition >= BlindsCurrentPosition) do
   
    -- Update current position of blind during movement 
    os.sleep(1)
    BlindsCurrentPosition = BlindsCurrentPosition + BlindsStep;
    SetUserParam('Wired', 'BlindsCurrentPosition-Study', BlindsCurrentPosition);
     
    -- Stop blind when blind has moved to new position
    if (BlindsCurrentPosition >= BlindsNewPosition) then
      sendCommand('0113S\x0D\x0A');  -- Send stop command (never gets sent)
    end;     
   
  end;

end;



RE: Blinds Control - Multiple Socket Connections Per Script - Erwin van der Zwart - 09.02.2020

Hi,

To close a command you probably need to send \r  to be able to send the next, try to a add this into your sendcommand function  '\x0D\x0A' .. '\r'

BR,

Erwin


RE: Blinds Control - Multiple Socket Connections Per Script - jamesng - 09.02.2020

Hi Erwin,

That still didn't work either .. the first command executes however the second one does not.

Even in a really simple example, the second command doesn't execute with the added \r.  For example


Code:
function sendCommand(command)
  local socket = require("socket").tcp()
  data, err = socket:connect('192.168.1.200', 4999)
  data, err = socket:send(command .. '\x0D\x0A' .. '\r') 
end

sendCommand('0113U'); 
os.sleep(1)
log('Sleep 1')
sendCommand( '0113S'); 
log('Sleep 2')



If I split this into two separate scripts and run the first one, then the second one a second later the commands execute as expected - however if I call sendCommand twice from the same script, the second calls fails (and I never see 'Sleep 2' in the logs.

Any idea why this would be happening?

Kind Regards
James


RE: Blinds Control - Multiple Socket Connections Per Script - admin - 09.02.2020

'\x0D\x0A' is the same as '\r\n' so extra '\r' is not needed
I think the problem might be with multiple connections which the gateway might not handle properly. Try sending two commands in the same connection:
Code:
sock = require("socket").tcp()
sock:settimeout(5)
res, err = sock:connect('192.168.1.200', 4999)

if res then
  sock:send('0113U\r\n')
  os.sleep(1)
  sock:send('0113S\r\n')
  sock:close()
end



RE: Blinds Control - Multiple Socket Connections Per Script - jamesng - 09.02.2020

Hi That seems to work. Many thanks!

Kind Regards
James