I have written a script to control Somfy motors using a MyLink. To set it up do the following:


You will use the IP address, System ID, and Target Ids as parameters to the script.
The parameters are: IP address of the MyLink, System ID, the Target ID of the motor, and the operation. The operation can be either Up, down, or STOP. The operations are not case sensitive.
There is a Boolean variable named verbose that is set to False in the script. You may want to set it to True to receive additional logging messages for troubleshooting.
The script connects to the MyLink, sends the JSON, reads the response and disconnects. A connection is not maintained.
- Setup the MyLink using the Somfy smart phone app (iPhone version shown). Make sure you have paired your devices and everything is working as it should.
- Go to the App’s menu and choose Integration. The following screen will appear:
- Choose Control4 and create a System ID. I chose “shutters”. Once you have created your System ID, select Get Integration Report.
- A screen similar to the following will appear:
You will use the IP address, System ID, and Target Ids as parameters to the script.
- Create a HomeSeer counter named “Somfy MyLink”. This counter is used to assure that the JSON transaction ids are unique. It will automatically increment and reset once it reaches 99.
- Copy the script to the HomeSeer scripts directory and name it somfy.vb or a name of your choosing.
- Create an event. Similar to the following:
The parameters are: IP address of the MyLink, System ID, the Target ID of the motor, and the operation. The operation can be either Up, down, or STOP. The operations are not case sensitive.
Code:
Public Sub Main(parms as object) 'parameters are IP Address, System Name, Target ID, Operation(up,down,or stop) Dim parmArray(4) As String Dim tcpC as New System.Net.Sockets.TcpClient() Dim networkStream As System.Net.Sockets.NetworkStream Dim strResponse As String = "No response" 'Change verbose to False to eliminate most of the logging Dim verbose As Boolean = False Dim jsonString As String Dim port As Integer = 44100 Dim ipAddress as String Dim method As String Dim targetID As String Dim auth As String Dim action as String Dim cv as Integer = hs.CounterValue("Somfy MyLink") 'Reset the counter once it reaches 99 so that it doesn't keep growing If (cv >= 99) Then hs.CounterReset("Somfy MyLink") End If hs.CounterIncrement("Somfy MyLink") Dim id as String = CStr(cv) 'This is required because Events pass parameters as a single string 'but HSTouch passes parameters as an array of strings If parms.GetType().ToString = "System.String" Then 'Event parmArray = parms.ToString.Split(Convert.ToChar(",")) ipAddress = parmArray(0) auth = parmArray(1) targetID = parmArray(2) action = parmArray(3).ToUpper() If (verbose) Then hs.WriteLog("MyLink", "Event: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth) End If Else 'HSTouch ipAddress = parms(0).ToString() auth = parms(1).ToString() targetID = parms(2).ToString() action = parms(3).ToString().ToUpper() If (verbose) Then hs.WriteLog("MyLink", "HSTouch: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth) End If End If If action = "UP" Then method = "mylink.move.up" ElseIf action = "DOWN" Then method = "mylink.move.down" Else method = "mylink.move.stop" End If jsonString = "{""id"":"+ id + ",""method"":""" + method + """," jsonString = jsonString + """params"":{""targetID"":"""+targetID + """," jsonString = jsonString + """auth"":""" + auth + """}}" jsonString = jsonString +"{""jsonrpc"":""2.0"",""result"":true,""id"":" + id + "}" Try tcpC.Connect(ipAddress, port) networkStream = tcpC.GetStream() Catch e As Exception hs.WriteLog("MyLink", "Network Error: " + e.ToString()) End Try If networkStream.CanWrite Then Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(jsonString) networkStream.Write(sendBytes, 0, sendBytes.Length) Dim bytes(tcpC.ReceiveBufferSize) As Byte networkStream.Read(bytes, 0, CInt(tcpC.ReceiveBufferSize)) Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes) strResponse = "Device responded: " & CStr(returndata) Else If Not networkStream.CanWrite Then strResponse = "Error: Cannot write data to the device." End If End If If (verbose) Then hs.WriteLog("MyLink", "JSON=" + jsonString) End If hs.WriteLog("MyLink",strResponse) tcpC.Close() End Sub
There is a Boolean variable named verbose that is set to False in the script. You may want to set it to True to receive additional logging messages for troubleshooting.
The script connects to the MyLink, sends the JSON, reads the response and disconnects. A connection is not maintained.
Comment