PowerShell Power Sockets (Arduino/RF) – Part 3 – Use PowerShell to send Serial commands to your Arduino

Posted: 30/12/2012 in Arduino, PowerShell

First Post 1 of 3

Previous Post 2 of 3

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

Energenie-PSFunctionExample

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

Energenie-PSFunctionExampleError

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

Comments
  1. matt2005 says:

    Reblogged this on matt2005.

    • chedzshed says:

      hi, an interesting project, thanks for sharing.
      I noticed your next steps were to try with a pi, did you ever? if so, any info?

      thanks

  2. Scott says:

    This looks exactly right for a project I’m working on… Will it work with most RF devices or just the Energenie?

  3. Jonny says:

    Thx for your project,very good job.Bye from Italy

Leave a comment