So to summarize we have collected all the RF codes for my Energenie Power Strip, created a Sketch on an Arduino connected to a an RF transmitter the only thing left it to use PowerShell to send Serial commands.
Now for the PowerShell code:
function Send-ArduinoSerial { param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [int[]] $byte ) #Find Arduino COM Port $PortName = (Get-WmiObject Win32_SerialPort | Where-Object { $_.Name -match "Arduino"}).DeviceID if ( $PortName -eq $null ) { throw "Arduino Not Found"} #Create SerialPort and Configure $port = New-Object System.IO.Ports.SerialPort $port.PortName = $PortName $port.BaudRate = "9600" $port.Parity = "None" $port.DataBits = 8 $port.StopBits = 1 $port.ReadTimeout = 2000 #Milliseconds $port.open() #open serial connection Start-Sleep -Milliseconds 100 #wait 0.1 seconds $port.Write($byte) #write $byte parameter content to the serial connection try { #Check for response if (($response = $port.ReadLine()) -gt 0) { $response } } catch [TimeoutException] { "Time Out" } finally { $port.Close() #close serial connection } }
Once the function has been loaded into your PowerShell console we can turn on and off each of the sockets, for example
This is one of those moments when an image fails to express the sheer excitement of a lamp going on and off!
If you forget to plug in your Arduino an exception will be thrown for example
What’s Next/Further thoughts
- Program some keyboard shortcuts to power the sockets on and off.
- Create a web front end and on that note I should point out that Energenie have another solution the LAN Power Management System available, this would be an easier solution for the none techie folks.
- Try this with a Raspberry Pi
Anyways, thanks for reading my posts and I hope you enjoyed Powering Power Sockets with PowerShell as much as I did!
Regards,
jfrmilner