Screensaver Power Strip


I have always been interested in saving electricity in my home especially in these hot summer months. While the air conditioner is by far the greatest consumer of electricity in my house, especially in the summer, the most constant user is my computer or I should say my computer setup.



I've tweaked Windows power settings to be fairly aggressive but there is only so far that goes and no matter how far you tweak Windows power settings it will only have a limited effect on the peripherals plugged into it. Monitors are one of the few computer peripherals that have energy saving modes and I have three of them. My main monitor is relatively new and has a pretty deep sleep mode but the two older task monitors in my setup have more of a snooze than a sleep. Most people tend to think if their monitors are black they aren't using electricity but they are, and worse than that are the many other items we don't use when we are away from our computers like small printers, USB devices, and for me an old audio amplifier that I have hooked up to my external speakers.

So whats a fella to do? Previously for me, whenever I locked my computer I would have to go around and push several power buttons to turn everything off...when I remembered to. My solution? A computer controlled power strip.

My first designs started with regular size power strips and trying to come up with a design small enough to fit in the very limited space available, then lightning struck...literally. After a thunderstorm one of my old floor model UPSs died, and replacing the battery had no effect the patient had become spare parts. There aren't usually much in the way of spare parts in a dead UPS but I took it apart hoping to see some cool catastrophic failure that didn't exist. As I was in the process of disemboweling the UPS I started to notice how much room was available inside once I took out all the non-functioning parts. What was I left with? A roomy power strip with plenty of space to put stuff.

The first, most obvious and most utilitarian area cleared out was the battery storage. By removing the battery this made a great area to house my low power parts and add cable access for outside controls. This would be the roomiest Arduino project box ever. The only modifications I made here was a small opening to run the external control cables and a small hole for some signal wires to control an electrical relay. The great part about this is after the project would be completed, the battery cover would allow for easy access to the Arduino brains inside without having to get a screwdriver out.


The second part of the UPS is the part that houses all of the high voltage parts. If you don't understand high voltage understand this; electricity that comes from the mains in your walls is sufficient to seriously injure or kill you. If you don't thoroughly understand how to work on mains electricity, do not do this project. Here is a diagram of the wiring on the high voltage side of the switch.
Notice in the diagram the switch and the relay are wired in serial so that they both need to be in the ON position for the switch to turn on, this is important. Safety is important and if there is a failure with electricity, that failure should be OFF not ON. By wiring the relay and switch in serial it provides both a logical ON/OFF and a physical ON/OFF where a physical OFF will render the switch harmless.

By this point you may have picked up on the fact that I said "cables" plural when mentioning external control. The first and most necessary is the USB cable which not only provides power for the Arduino but also the communications path for the computer to send ON and OFF commands. The second external control cable is simply a remote ON button since my monitors are wired up to the switch. If for some reason my switch didn't turn on when I was typing on the keyboard I could push the button and see that I had the CAPS LOCK on and my password wasn't being accepted. The button only switches ON not OFF, a simple press turns the power strip ON and I didn't want an accidental bump to turn everything OFF. The button is a fail-safe.


PART 1: Making the Physical Build

The remote ON button is a simple plastic box with a momentary switch mounted in it. I filled up the box with pennies to give it some weight. The cable to the button is fashioned from a spare network cable, soldered and sealed to the button and providing for 6ft of distance between the button and the power strip. The two leads from the button are wired to the 5V and D2 connections of the Arduino with an additional 10K Ohm resistor connected to the D2 and ground on the Arduino to avoid errant "bouncing" on the button.

The relay is a Fotek SSR-25 DA which is an excellent high voltage relay for Arduino as it is a solid state relay and requires very little voltage to switch on and it has an embedded ON light which is great for testing without a high voltage load hooked up. The relay is mounted on the high voltage side of the project and it's exact mounting will vary from project to project based on the physical characteristics of the UPS utilized.


Once the relay was physically mounted and the connections have been made and tested I added the extra safety of a liberal amount of hot glue to insure there was no way it could break free and slide around in the case. The AC connections of the relay are wired in to interrupt the voltage on the high voltage side of the circuit the same way a regular light or power switch would be while the positive and negative DC side of the relay are wired respectively to the D8 and ground connections of the Arduino.

PART 2: Program the Arduino

The code used on the Arduino is pretty straight forward and relatively simple. The USB connection on the Arduino will be used for serial communication utilizing two simple text commands; "POWERON" to turn the strip on and "KILLWATTS" to turn the powerstrip off. Why such long command words? The serial communication can be a bit sketchy and making sure the two command words are long and very different helps insure that one is not mistaken for the other. End effect of the code; push button strip comes ON, send "POWERON" via the COM port strip comes on, send "KILLWATTS" via the COM port strip goes off.

const int relayPin = 8;
const int buttonPin = 2;

int relayState = 0;
int buttonState = 0;
String incomingStr = "";

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
 
  pinMode(relayPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}


void loop() {
  //Emergency ON button
  buttonState = digitalRead(buttonPin);
  if (buttonState == 1) {
    relayState = 1;
    digitalWrite(relayPin, HIGH);
  }

  //Serial port input
  while (Serial.available()) {
      delay(10);  //delay to allow serial buffer to fill
      if (Serial.available() >0) {
        char c = Serial.read();  //get byte from serial buffer
        incomingStr += c;
      }
  }

  //Commands received from serial port
  if(incomingStr.indexOf("POWERON")>=0){
    incomingStr = "";
    digitalWrite(relayPin, HIGH);
  }
  if(incomingStr.indexOf('KILLWATTS')>=0){
    incomingStr = "";
    digitalWrite(relayPin, LOW);
  }
}

PART 3: Setup Windows Auditing

The original plan to trigger communication from Windows to the power strip was to use a custom screensaver which may come in the future but currently we will be looking for specific EventIDs in the Windows Event Logs. To enable some of those events we will need to change a windows policy by running gpedit.msc. Once the group Policy Editor is running go to the following path and enable both success and failures as pictured.

Path;
Computer Configuration / Windows Settings / Security Settings / Advanced Audit Policy Configuration / System Audit Policies - Local Group Policy Object / Logon/Logoff / Audit Other Logon/Logoff Events

PART 4: Create the CMD files

For Windows to send the commands necessary, we will need to use two simple CMD scripts. I run all of my scripts from a common "CMD" directory on the C: drive. If you plan to put the scripts in another location you will need to alter the instructions accordingly. In both scripts the variable PSCOM is set as COM3 which is the COM port my Arduino detected as in Windows Device Manager, you will need to change this to the appropriate COM port on your computer. Once edited, running the CMD files will turn the power strip on and off. The sending of the POWERON and KILLWATTS commands twice is again due to the sketchy serial communications.

ScreenSaverPowerStripON.CMD

@ECHO OFF

SET PSCOM=COM3

mode %PSCOM% BAUD=9600 PARITY=n DATA=8
set /p x="POWERON" <nul >\\.\%PSCOM%
TIMEOUT /t 2
set /p x="POWERON" <nul >\\.\%PSCOM%


ScreenSaverPowerStripOFF.CMD

@ECHO OFF

SET PSCOM=COM3

mode %PSCOM% BAUD=9600 PARITY=n DATA=8
set /p x="KILLWATTS" <nul >\\.\%PSCOM%
TIMEOUT /t 2
set /p x="KILLWATTS" <nul >\\.\%PSCOM%


PART 5: Create Tasks to Run the CMDs

Now that we have CMD files and auditing enabled its time to take advantage of both by creating tasks that look for certain events in Windows to run the CMD files and turn the power strip on and off. To create a task, run the Task Scheduler, right-click on "Task Scheduler Library" and select "Create Task". The settings for the two tasks we will create are almost identical.

  • On the "General" tab select "SYSTEM" as the user to run the task.
  • On the "Triggers" task we will create two triggers, one for the System log and one for the Security log. Create a new trigger, select "On an event" from the dropdown, select "Custom" then click "New Event Filter". On the New Event Filter dialog select all of the "Event level" options, select either "Security" or "System" as appropriate for the trigger being created, in the text box that says "<All Event IDs>" type over it with a comma separated list of the event IDs you wish to include in the trigger (I suggest using all of the EventIDs listed in the table below). Click "OK" a few times until you are back on the task window.

EVENTIDS: On
EventLogEventID
Screensaver DismissedSecurity4803
Console unlockedSecurity4801
Power ResumeSystem107
Power Standby EndSystem507
System StartupSystem6005


EVENTIDS: Off
EventLogEventID
Screensaver InvokedSecurity4802
Console lockedSecurity4800
Power SleepSystem42
Power Standby StartSystem506
System ShutdownSystem6006

  • On the "Actions" tab add a new action and point it at the appropriate CMD file.

If that's a few too many steps for you the ScreenSaverPowerStrip.ZIP file contains the Arduino sketch, both CMD files and the two XML files can be imported as new tasks in the Task Scheduler.


-Fini




No comments:

Post a Comment