more G-Labs products

Author Topic: Wake on Lan Function  (Read 2383 times)

December 07, 2014, 07:20:17 PM
Read 2383 times

tasioX

  • *
  • Information
  • Newbie
  • Posts: 19
I would love to see some Wake on Lan functionality in HG for scheduled power on of different equipment.  NAS, PC, Servers, etc.  I will be digging in on this a bit by adding a wake on lan server to my RPI and see if I can get HG to send it the proper commands, but I am a cut and paste coder so I may not get very far.  Having this function built in would be great.

December 18, 2014, 02:17:59 AM
Reply #1

tasioX

  • *
  • Information
  • Newbie
  • Posts: 19
I did manage a wake on lan function using my slave HG Raspberry Pi.  I did it as follows:

SSH in to the raspberry pi and install apache2 and wakeonlan

Code: [Select]
sudo apt-get install apache2 wakeonlan
When finished create a new file in the default cgi script directory with vi. Rename the file to whatever you wish just remember to update the code where needed to point at the correct file.

Code: [Select]
sudo vi /usr/lib/cgi-bin/MCPC.cgi
Press the "i" key to invoke the insert command and paste the following inside vi replacing macaddresshere with the MAC address of the machine you want to wake up.

Code: [Select]
#!/usr/bin/bash

wakeonlan macaddresshere

Now create a C# script in HG to call the script like so.  Remember to update the names and IPs to your equipment.  The 192.168.0.91 is my PI with the CGI script.  MCPC.cgi is the name of the file created above.  MediaClient-PC is the name I gave to the machine I am waking up and will show on the HG Dash when executed and in the logs

Code: [Select]
var requeston = Net.WebService("http://192.168.0.91/cgi-bin/MCPC.cgi");
   
var resulton = requeston.GetData();
Program.Notify("MediaClient-PC Starting Up", resulton);

Hit Compile and save and you should be all set.

Hope this can be used by others.

December 19, 2014, 10:29:09 AM
Reply #2

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Hi Tasio,

Could you test the following code for me, I haven't got access to a machine that responds to WOL at the moment.

According to the Wake on LAN monitor, the magic packet sent is OK, but I'd like to know that it actually works before making a HomeGenie APP for this.

Here is the code in C#. (or you can import the .hgx file)

Code: [Select]
// CSharp Automation Program Plugin
// Example for using Helper Classes:
// Modules.WithName("Light 1").On();

string MACAddress = "A4-F7-32-A4-79-87";
string BroadcastAddress = "192.168.0.255";

// Remove '-'
if (MACAddress.Contains("-")) { MACAddress = MACAddress.Replace("-", String.Empty); }
// Remove ':'
if (MACAddress.Contains(":")) { MACAddress = MACAddress.Replace(":", String.Empty); }

// Build Magic packet as string, begin with 6 x 0xFF
string MagicPacket = "FFFFFFFFFFFF";

// Add 16 times the MAC address.
for (int i = 1; i <= 16; i++)
{
  MagicPacket += MACAddress;
}

byte[] packet = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary.Parse(MagicPacket).Value;

System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();
client.Connect(BroadcastAddress, 4343);
// Send the Magic packet packet.
client.Send(packet, packet.Length);

Thanks for your help.

December 19, 2014, 04:59:29 PM
Reply #3

tasioX

  • *
  • Information
  • Newbie
  • Posts: 19
Tested and working.  Nice work.  If you put in the menu to enter the variables easily I think Gene should consider adding this to the releases.  Much easier than my solution.

December 19, 2014, 05:52:10 PM
Reply #4

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks for testing it, I've put some code here to begin with an APP, any feedback is appreciated:

http://www.homegenie.it/forum/index.php?topic=558.0