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

About these ads
Comments
  1. matt2005 says:

    Reblogged this on matt2005.

  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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s