Friday, September 28, 2012

Arduino Controlled Computer Startup / Shutdown


Update 10/1/2012
After being unsatisfied with not having the RTC included in the build I dug through the both the  Adafruit & Arduino forums looking for someone who may have had the same issues. Sure enough I wasn't alone when dealing with the issues I was having and found the solution looking me right in thee face. Below is the corrected code minus the code for the LCD display which I still have to order, but after consideration will probably be skipping and leaving in just a project box with some light modification such as LED status lights. I have also removed the directly soldered wires going from the PC to the shield and replaced them with pinheaders as seen above (see pictures below for comparison) to make disconnecting the wires from the shield easier then leaving them soldered in.

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

int led = 2; //5v signal to relay
int led2 = 8; //5v signal to reed relay

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(57600);
  Wire.begin();
    RTC.begin();
   
     if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
   DateTime now = RTC.now();
  setTime(hour(), minute(), second(), day(), month(), year());
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); 
  
  // Alarms
  Alarm.alarmRepeat(6,15,0, WakeAlarm);  // 6:15am Computers On
  Alarm.alarmRepeat(9,15,0, WorkAlarm);  // 9:15am Computers Off
  Alarm.alarmRepeat(15,20,0, HomeAlarm); // 3:20pm Computers On
  Alarm.alarmRepeat(2,0,0, NightAlarm);  // 2:00am Computers Off
   
}

void  loop(){ 
  DateTime now = RTC.now();
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
  Alarm.delay(1000); //clock display delay
 
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
}

// Alarm Functions
void WakeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");   
}

void WorkAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
 
}
void HomeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");
}

void NightAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
}




Originally Posted 9/28/12





Like many folks all over the world my household's entertainment runs on XBMC. For those of you reading this who may have never heard of XBMC or what it can do in short XBMC is "One Media Player To Rule Them All". Our movie & TV show library is on our Network Attached Storage PC's located in our basement streaming to our XBMC compatible devices in our home. While this approach is nothing new for many already using XBMC or just those who use a at home general file servers, the electric cost for keeping things running 24hrs a day can be costly and wasteful. Looking at software solutions to turn off the PC's at a certain time of day there is plenty, but none to turn the PC back on.

Through out the life the Arduino platform and other microcontrollers we have seen countless DIY clocks, timers, alarm, & even garden watering cycles .... well why not an Arduino controlled computer? Anyone who has built a PC in the past 15yrs knows that the power button on a case does nothing more then complete a circuit. The situation screamed two things to me when looking into a Auto PC timer,  an Arduino & a reed relay.



Hooking up the Adafruit RTC DS1307 breakout board, two 5VDC reed relays, and a Seed Studio protoboard from Radio Shack to the Arduino I connected the #2 & #8 pin to the reed relays & the use of the LED comment to send 5v to the relay closing the N.O. (normally open) connection joining the two wires together just like the power button does on the PC case & simulating a button press.

 Inside the PC I added a simple PCB add-on to allow the power switch terminals to connect to the pin headers on the PCB, then from the PCB to the two pins on the motherboard. The power switch on the case still functions as normal and required no case modification.

I originally intended to use 1 reed relay to operate both machines but after it showed signs of failure I added a second relay for the 2nd PC  (see F.A.Q) to allow the systems to turn on and off without any issues in separate relays.

 As the schedule now stands the 2 NAS PC's turn on at 6AM at the start of everyone's day, off at 9:00AM back on at 3:00PM and off again at 2AM cutting 10 hours off of wasteful non use.

While many could say that it's just as easy to turn them on when I need them , this kind of is a problem to my 5yr old who I don't want walking down the stairs at 6:30AM on a Sunday cause she wants to be able to watch a movie on her XBMC device in her room.



As this was only a test run or more of a proof of concept so to speak the LCD screen hooked up was not running cause I feel the screen is just to small for the size of the project box and overall feel the display needs to be much larger in size for it to be effective as a display. Also I am redoing the overall protoboard to make a full shield add-on specifically designed for handling more then one machine possibly with a wireless Xbee solution to remove the wire aspect. 


 
Code Used

#include <Time.h>
#include <TimeAlarms.h>

int led = 2; //5v signal to relay
int led2 = 8; //5v signal to reed relay

void setup()
{
  Serial.begin(57600);
  setTime(12,0,0,9,29,12); // Set Time
  // create the alarms
  Alarm.alarmRepeat(6,15,0, WakeAlarm);  // 6:15am Computers On
  Alarm.alarmRepeat(9,15,0, WorkAlarm);  // 9:15am Computers Off
  Alarm.alarmRepeat(15,20,0, HomeAlarm); // 3:20pm Computers On
  Alarm.alarmRepeat(2,0,0, NightAlarm);  // 2:00am Computers Off
   
}

void  loop(){ 
  digitalClockDisplay();
  Alarm.delay(1000); //clock display delay
 
 
}

// Alarm Functions
void WakeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");   
}

void WorkAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
 
}
void HomeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");
}

void NightAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


Arduino Controlled Computer Startup / Shutdown in Action





F.A.Q
(Based on questions asked by friends during build)

Q. Why 2 relays?
A. When hooking up a single PC with both sets of wires hooked up to one relay it worked fine , but hooking up both PC's to the single reed relay immediately turned off both PC's and would not allow them to turn back on without first removing the one of the hook ups from the other machine. The solution to this was to have a single relay for each machine, however a double pole single throw relay I am assuming would work seeing as both connections would continue to remain separate.





 

1 comment: