# IVYLight # # Simple Tool to send UDP-Ports to control LED Bulbs # # Pending: Disco, flashing, # Pending: program 0-20 # Pending: brightness # .\ivylight.1.0.ps1 -channel 1 -brightness 27 -color white param ( [Validatepattern("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")] [string]$remoteip = "192.168.178.22", # IP to send to [ValidateRange(1,65535)] [int]$remoteudpport=8899, # port to send to [ValidateRange(0,65535)] [int]$sourceudpport = 0, # SourcePort, 0 uses an available port [ValidateRange(0,4)] [int]$Group = 0, # SourcePort, 0 is all 1-4 is group 1-4 [ValidateRange(1,27)] [int]$brightness = 1, # valid is 2-27 1= "not change" [string]$color = "" #use a color or value ) #region Global preferences set-psdebug -strict $error.clear() $color = $color.tolower() write-host "IVYLight:RemoteIPAddr :"$remoteip write-host "IVYLight:RemoteUDPort :"$remoteudpport write-host "IVYLight:SourceUDPPort:"$sourceudpport write-host "IVYLight:Group :"$Group write-host "IVYLight:Color :"$color #endregion try { #region initialization #write-host "IVYLight:Create startcommandarray1 with 3 bytes" $startcommandarray1 = New-Object Byte[] 3 write-host "IVYLight:Initialize UDP-Socket" $udpClient = new-Object system.Net.Sockets.Udpclient($sourceudpport) $startcommandarray1[1]=0x00 $startcommandarray1[2]=0x55 switch ($Group) { 0 {$startcommandarray1[0]=0x41} 1 {$startcommandarray1[0]=0x45} 2 {$startcommandarray1[0]=0x47} 3 {$startcommandarray1[0]=0x49} 4 {$startcommandarray1[0]=0x4B} } if ($color -ne "off") { write-host "IVYLight:Color is off" $startcommandarray1[0]+=1 #add 1 to switch light on } # write-host ("IVYLight:Send first command:"+ [string]$startcommandarray1) #$sentbytes = $udpClient.Send($startcommandarray1, $startcommandarray1.length, $remoteip, $remoteudpport) #if ($sentbytes -ne $startcommandarray1.length) { # write-host "IVYLight:Send Bytes Mismatch" #} #endregion #region color write-host "IVYLight:Color-------------------" $startcommandarray2 = New-Object Byte[] 3 $startcommandarray2[0]=0x40 $startcommandarray2[2]=0x55 if ($color -eq "off") { write-host "IVYLight:Color: no additional OFF paket" } elseif ($color -eq "on") { write-host "IVYLight:Color: no additional ON paket" } elseif ($color -eq "white") { write-host "IVYLight:Color: Set to White" $startcommandarray2[1]=0x00 switch ($Group) { 0 {$startcommandarray2[0]=0xc2} 1 {$startcommandarray2[0]=0xc5} 2 {$startcommandarray2[0]=0xc7} 3 {$startcommandarray2[0]=0xc9} 4 {$startcommandarray2[0]=0xcB} } } else { switch -regex ($color) { "0x[a-f0-9]{2}" {write-host "IVYLight:Color HEX: $color"; $startcommandarray2[1]=[byte]($color.Substring(2,2))} "blue" {write-host "IVYLight:Color : $color"; $startcommandarray2[1]=0xFF} "green" {write-host "IVYLight:Color : $color"; $startcommandarray2[1]=0x55} "yellow" {write-host "IVYLight:Color : $color"; $startcommandarray2[1]=0x80} "red" {write-host "IVYLight:Color : $color"; $startcommandarray2[1]=0xA0} "pink" {write-host "IVYLight:Color : $color"; $startcommandarray2[1]=0xc0} "/d{1,3}" {write-host "IVYLight:Color Byte: $color"; $startcommandarray2[1]=([byte]$color)} default {write-host "IVYLight:Color Invalid: $color"; $startcommandarray2[2]=0x00} } } if ($startcommandarray2[2] -eq 0x55) { write-host ("IVYLight:Send command 1:"+ (($startcommandarray1 | foreach {$_.ToString("X2")}) -join " " )) $sentbytes = $udpClient.Send($startcommandarray1, $startcommandarray1.length, $remoteip, $remoteudpport) if ($sentbytes -ne $startcommandarray1.length) { write-host "IVYLight:Send Start Bytes Mismatch" } start-sleep -milliseconds 200 # sleep 100ms reqired write-host ("IVYLight:Send command 2:"+ (($startcommandarray2 | foreach {$_.ToString("X2")}) -join " " )) $sentbytes = $udpClient.Send($startcommandarray2, $startcommandarray2.length, $remoteip, $remoteudpport) if ($sentbytes -ne $startcommandarray2.length) { write-host "IVYLight:Send Bytes Mismatch" } } #endregion #region brightness write-host "IVYLight:Brightness-------------------" if ($brightness -ne 1) { write-host "IVYLight:Brightness set to $brightness" $startcommandarray2 = New-Object Byte[] 3 $startcommandarray2[0]=0x4e $startcommandarray2[1]=$brightness $startcommandarray2[2]=0x55 write-host "IVYLight:Send Bridghtness" write-host ("IVYLight:Send command 1:"+ (($startcommandarray1 | foreach {$_.ToString("X2")}) -join " " )) $sentbytes = $udpClient.Send($startcommandarray1, $startcommandarray1.length, $remoteip, $remoteudpport) if ($sentbytes -ne $startcommandarray1.length) { write-host "IVYLight:Send Start Bytes Mismatch" } start-sleep -milliseconds 200 # sleep 100ms reqired write-host ("IVYLight:Send command 2:"+ (($startcommandarray2 | foreach {$_.ToString("X2")}) -join " " )) $sentbytes = $udpClient.Send($startcommandarray2, $startcommandarray2.length, $remoteip, $remoteudpport) if ($sentbytes -ne $startcommandarray2.length) { write-host "IVYLight:Send Bytes Mismatch" } } else { write-host "IVYLight:Brightness not changed" } #endregion } catch { write-host "IVYLight:Error found "$error } finally { write-host "IVYLight:Closing UDPSocket" $udpclient.close() write-host "IVYLight:End" }