Announcement

Collapse
No announcement yet.

Using HomeSeer & FreeStyler to control DMX Lighting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using HomeSeer & FreeStyler to control DMX Lighting

    http://users.telenet.be/freestylerdmx/
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    
    Dim DAddS As String = "FreeStylerDMX"
    Dim ConnAddress As String = "192.168.1.190"
    Dim maxScenes As Integer = 5 'If you want more scenes then change this to how many
    
    Sub Main(ByVal Parms As Object)
    
        Try
            If Parms(1).Contains(DAddS) Then
    
                Log("Device Of Interest: " & Parms(1))
                Log("FreeStyler DMX CallBack Script Starting")
    
                Dim SequenceNumber As Integer = Convert.ToInt32(Parms(1).Replace(DAddS & "-Sequence", ""))
                Dim dValue As Integer = Convert.ToDouble(Parms(2))
    
                Log("Sequence Number: " & SequenceNumber & " Value: " & dValue)
    
                Select Case dValue
                    Case 0 : SendCommand(SequenceNumber + 524, 255)
                    Case 100 : SendCommand(SequenceNumber + 504, 255)
                End Select
    
            End If
    
        Catch ex As Exception : Log("Exception: " & ex.Message.ToString)
        End Try
    
    End Sub
    
    Sub StartScript(ByVal Parms As Object) 
    
        Try
            Log("FreeStyler DMX Script Starting")
    
            If hs.DeviceExistsAddress(DAddS & "-Root", False) = -1 Then 'check if the route device is there
                CreateDevices()
            Else
                Log("Registering For Value/Change CallBack")
                hs.RegisterStatusChangeCB("HS3FreeStyler.vb", "Main") 
            End If
    
        Catch ex As Exception : Log("Exception: " & ex.Message.ToString)
        End Try
    
    End Sub
    
    Sub SendCommand(ByVal FSCode As Integer, ByVal TCPArgument As Integer)
    
        Try
            Dim ByteArr(2) As Byte
    
            Log("SendCommand FreeStyler Code: " & FSCode & " TCP Argument: " & TCPArgument)
    
            Dim bAr() As Byte
            bAr = BitConverter.GetBytes(FSCode)
    
            ByteArr(0) = bAr(0)
            ByteArr(1) = bAr(1)
            ByteArr(2) = TCPArgument
    
            NetSend(ByteArr)
    
        Catch ex As Exception : Log("Exception: " & ex.Message.ToString)
        End Try
    
    End Sub
    
    Sub NetSend(ByVal ByteArr As Byte())
    
        Log("NetSend Called - Address: " & ConnAddress)
    
        For i As Integer = 0 To ByteArr.GetUpperBound(0)
            Log("NetSend ByteArr(" & i & ") - " & ByteArr(i))
        Next
    
        Try
            Dim tClient As New TcpClient
            Dim sEP As New IPEndPoint(IPAddress.Parse(ConnAddress), 3332)
    
            tClient.SendTimeout() = 1000
            tClient.NoDelay = True        'Short burst of info
            tClient.Connect(sEP)
    
            Dim nStream As NetworkStream = tClient.GetStream()
    
            Log("Attempting Connection")
            Log("Sending Data...")
    
            Dim buffer As Byte() = New Byte(8) {70, 83, 79, 68, ByteArr(0), ByteArr(1), ByteArr(2), 0, 0}
    
            nStream.Write(buffer, 0, buffer.Length)
            nStream.Flush()
    
            Log("Command Sent : " & Text.Encoding.ASCII.GetString(buffer))
    
            Log("Command Sent To FreeStyler")
            Log("Closing Connection")
    
            nStream.Close() 'close the stream
            tClient.Close() 'get rid of the client
    
        Catch Ex As Exception : Log("NetSend Error: " & Ex.ToString)
        End Try
    
    End Sub
    
    Sub Log(ByVal ParStr As String)
        hs.writelog(DAddS, ParStr)
    End Sub
    
    Sub CreateDevices()
    
        Dim CurrRef As Integer = 0
        Dim BaseRef As Integer = 0
        Dim dv As Scheduler.Classes.DeviceClass = Nothing
        Dim root_dv As Scheduler.Classes.DeviceClass = Nothing
    
        Try
            Log("Devices Do Not Exist - Creating")
            For i As Byte = 0 To maxScenes 'needs to be four new devices
                Select Case i
                    Case 0
                        dv = hs.GetDeviceByRef(hs.NewDeviceRef("Root"))
                        dv.Address(hs) = DAddS & "-Root"
                        BaseRef = dv.Ref(hs)
                    Case Else
                        dv = hs.GetDeviceByRef(hs.NewDeviceRef("Sequence " & i))
                        dv.Address(hs) = DAddS & "-Sequence" & i
                End Select
    
                dv.Location(hs) = "FreeStyler"
                dv.Last_Change(hs) = Now
                dv.Device_Type_String(hs) = DAddS & " Device"
                'dv.Interface(hs) = DAddS
    
                Dim DT As New DeviceTypeInfo
    
                DT.Device_API = DeviceTypeInfo.eDeviceAPI.Script
                DT.Device_Type = DeviceTypeInfo.eDeviceType_Script.Run_On_Value_Change
                dv.DeviceType_Set(hs) = DT
    
                dv.ScriptName(hs) = "HS3FreeStyler.vb" '<--- This is broken
    
                If i = 0 Then 'on the base device do this, set up the relationships between the devices
                    root_dv = dv
                    dv.Relationship(hs) = Enums.eRelationship.Parent_Root
                Else
                    If root_dv IsNot Nothing Then root_dv.AssociatedDevice_Add(hs, dv.Ref(hs))
                    dv.Relationship(hs) = Enums.eRelationship.Child
                    dv.AssociatedDevice_Add(hs, BaseRef)
                End If
    
                Select Case i
                    Case 0 : hs.setdevicestring(dv.Ref(hs), "Root Device", True)
                    Case Else
                        Dim Pair As VSPair
    
                        Pair = New VSPair(HomeSeerAPI.ePairStatusControl.Both)
                        Pair.PairType = VSVGPairType.SingleValue
                        Pair.Status = "Stop"
                        Pair.Value = 0
                        Pair.Render = Enums.CAPIControlType.Button
                        Pair.ControlUse = ePairControlUse._Off 'HSTouch?
                        hs.DeviceVSP_AddPair(dv.Ref(hs), Pair)
    
                        Pair = New VSPair(HomeSeerAPI.ePairStatusControl.Both)
                        Pair.PairType = VSVGPairType.SingleValue
                        Pair.Status = "Start"
                        Pair.Value = 100
                        Pair.Render = Enums.CAPIControlType.Button
                        Pair.ControlUse = ePairControlUse._On 'HSTouch?
    
                        hs.DeviceVSP_AddPair(dv.Ref(hs), Pair)
    
                        dv.MISC_Set(hs, Enums.dvMISC.SHOW_VALUES)
    
                        hs.setdevicevaluebyref(dv.Ref(hs), 0, True)
                 
                End Select
    
                hs.saveeventsdevices()
    
            Next
    
            Log("End Of Create Devices Routine - Calling StartScript Again")
            StartScript(Nothing)
    
        Catch ex As Exception : Log("Create Devices Exception: " & ex.message.tostring)
        End Try
    
    End Sub
    Last edited by mrhappy; December 28, 2016, 03:17 PM.

    #2
    mrhappy,

    I'm very happy to see your posts on here. I've been a Freestyler DMX user for many years, using the software to control various zones of LED strip lighting throughout my home. I've mostly had scenes triggered by time of day through Freestyler or starting/stopping them with the Freestyler mobile app on Android.

    I've just recently purchased Homeseer, and am very excited about the possibility of controlling my scenes through Homeseer. I'm trying to replicate what you've shared in your example above. I'm pretty much a newb when it comes to running scripts through Homeseer, and using this as my first attempt. Once this is successfully executed, should I expect to see new devices created for my scenes in Homeseer? So far I'm just getting "FreeStylerDMX Exception: Index was outside the bounds of the array." in my event log when I manually trigger the event that runs the script.

    Basically, I'm stuck on step #2 of your instructions for what needs to be done in order to control the scenes. (I already have my scenes created in Freestyler.) Is there any other thread you could point me to that would go into a little further detail (for a scripting newb) of what you are instructing in your step #2 of the process above? Any advice would be appreciated. Thanks. - Jeff

    Comment


      #3
      I'll have another try and see if it still works, I don't use it (and I later went to DMXControl which was free) and probably have not tried it since this thread so either Freestyler may have changed or the script has. I'll post back here after my testing to see what the issue is.

      Comment


        #4
        I have made the script a little simpler, I've removed the devices so you may be able to get it working a little easier to start with to verify it works.

        Copy the following into notepad, then save it as HS3FreeStyler.vb in your HS scripts directory;

        Code:
        Imports System.Net
        Imports System.Net.Sockets
        
        Dim ConnAddress As String = "192.168.1.190"
        
        Sub Main(ByVal Parms As Object)
        
            Try
                    Log("FreeStyler DMX Script Starting")
        
                    Dim SequenceNumber As Integer = 1 'change this to your sequence number
        	        Dim RunSequence As Boolean = True 'true to run, false to stop
        
                    Select Case RunSequence
                        Case False : SendCommand(SequenceNumber + 524, 255)
                        Case True : SendCommand(SequenceNumber + 504, 255)
                    End Select
        
            Catch ex As Exception : Log("Exception: " & ex.Message.ToString)
            End Try
        
        End Sub
        
        Sub SendCommand(ByVal FSCode As Integer, ByVal TCPArgument As Integer)
        
            Try
                Dim ByteArr(2) As Byte
        
                Log("SendCommand FreeStyler Code: " & FSCode & " TCP Argument: " & TCPArgument)
        
                Dim bAr() As Byte
                bAr = BitConverter.GetBytes(FSCode)
        
                ByteArr(0) = bAr(0)
                ByteArr(1) = bAr(1)
                ByteArr(2) = TCPArgument
        
                NetSend(ByteArr)
        
            Catch ex As Exception : Log("Exception: " & ex.Message.ToString)
            End Try
        
        End Sub
        
        Sub NetSend(ByVal ByteArr As Byte())
        
            Log("NetSend Called - Address: " & ConnAddress)
        
            For i As Integer = 0 To ByteArr.GetUpperBound(0)
                Log("NetSend ByteArr(" & i & ") - " & ByteArr(i))
            Next
        
            Try
                Dim tClient As New TcpClient
                Dim sEP As New IPEndPoint(IPAddress.Parse(ConnAddress), 3332)
        
                tClient.SendTimeout() = 1000
                tClient.NoDelay = True        'Short burst of info
                tClient.Connect(sEP)
        
                Dim nStream As NetworkStream = tClient.GetStream()
        
                Log("Attempting Connection")
                Log("Sending Data...")
        
                Dim buffer As Byte() = New Byte(8) {70, 83, 79, 68, ByteArr(0), ByteArr(1), ByteArr(2), 0, 0}
        
                nStream.Write(buffer, 0, buffer.Length)
                nStream.Flush()
        
                Log("Command Sent : " & Text.Encoding.ASCII.GetString(buffer))
        
                Log("Command Sent To FreeStyler")
                Log("Closing Connection")
        
                nStream.Close() 'close the stream
                tClient.Close() 'get rid of the client
        
            Catch Ex As Exception : Log("NetSend Error: " & Ex.ToString)
            End Try
        
        End Sub
        
        Sub Log(ByVal ParStr As String)
            hs.writelog("HS3FreeStyler", ParStr)
        End Sub
        When you have saved it open it back up and change the IP address to that of the PC running FreeStyler.

        By default it is set up to start/stop sequence one, the first two lines under 'FreeStyler DMX Script Starting' in the script will determine which sequence to run and whether to start/stop it.

        Then run the script, I would suggest from an event is the easiest way and you do not need to send it anything else.

        Check the HS log if FreeStyler does not respond, you should be looking for some entries to say that it has been able to send the data. If it hangs for 20 seconds check your firewall settings.

        The network interface is pretty complex and as such I only ever supported starting and stopping of sequences, really the sky is the limit with the available options but they might start getting complex.

        Comment


          #5
          mrhappy, thank you so much for your reply, explanation, and sample scripts!

          Just had a chance today to get back on here and start playing around with it again..

          It works beautifully now, and I'm very excited! I truly appreciate all the work you are willing to share on here, and I apologize for the delayed "thank you"!

          Comment

          Working...
          X