Home // Electronics // Arduino Control PC Powerstate

Arduino Control PC Powerstate

IMG_0049
 

I found myself in a situation where I needed to be able to ping a computer and based on the response power cycle the machine. The Arduino pings the target system and determines if the machine is responsive. If the machine is not responsive it will simply power cycle the machine and wait 3 minutes to check again.

 

I used a Arduino along with an Arduino Ethernet shield. Here is the code that was loaded onto the Arduino. This is link to the source code below PcPowerUp. The white wire is connected PC power switch 5V side. The orange wire is connected to the ground.

This is great for those mining rigs that are just slightly unstable. Couple this with CGWatcher and your mining troubles should subside!

*****************************************************************************************************************

/*
  Ping Example
 
 This example sends an ICMP pings every 500 milliseconds, sends the human-readable
 result over the serial port. 

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 30 Sep 2010
 by Blake Foster
 
 //Modified by William Fecke
 //25 Jan 2014
 added a logic statement to trip power on pc if ping failed.
 
 */

#include <SPI.h>         
#include <Ethernet.h>
#include <ICMPPing.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
byte ip[] = {192,168,4,177}; // ip address for ethernet shield
IPAddress pingAddr(192,168,4,114); // ip address to ping
int failogic = 0;
int alternatePress = 0;
int powerPin = 4;
int powerPinStatus = 5;

SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() 
{
  // start Ethernet
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(powerPin,OUTPUT);
  pinMode(powerPinStatus,OUTPUT);
  digitalWrite(powerPin,HIGH);
  digitalWrite(powerPinStatus,LOW);
}

void loop()
{
  ICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
            failogic = 0; // resets the logic count if machine starts to respond to the pings
            alternatePress = 0;
  }
  else
  {
    failogic++; //this will increment the counter for every failed ping
    sprintf(buffer, "Echo request failed; %d", echoReply.status); 
  }
  Serial.println(buffer);
  if (failogic > 5) // 5 failed pings will trip the if statement and cause the arduino to hit the power button
   {
     digitalWrite(powerPin,LOW); //The positive side of the power button is connected to PIN 4 on my Arduino so moving it to the LOW state simulates pushing the power button
     digitalWrite(powerPinStatus,HIGH); //LED turns on while button is pressed
     if(alternatePress == 0)
    { 
     delay(5500); //This delay kills the PC power all together
     alternatePress = 1;
    }
    else
    {
      delay(500); //This delay quickly presses the button;
    }
     digitalWrite(powerPin,HIGH); //This is the release of the power button
     digitalWrite(powerPinStatus,LOW);//LED turns off when button is released
     delay(180000); 
   }
  delay(500);
}

**********************************************************************************************************

 

Posted in Electronics, Fun Hacking, Programming

Leave a Reply