This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Blinds Control - Multiple Socket Connections Per Script
#1
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;
Reply
#2
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
Reply
#3
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
Reply
#4
'\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
Reply
#5
Hi That seems to work. Many thanks!

Kind Regards
James
Reply


Forum Jump: