powerSwitch | How to turn on/off PC by just switching on/off the button on power extension cord with switch
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
arduino
doc/image
server
README.md

README.md

power-switch

How to turn on/off PC by just switching on/off the button on power extension cord with switch

Introduction

I would like to be able to turn on/off PC by just switching on/off the button on power extension cord with switch: plug Turning on is simple - just switch on the power extension switch ! ... the issue is with turning off - all PCs needs some time to shut down properly This idea is similar to the way how UPS communicates with PC in case of power failure.

General concept

  • arduino will be used as central unit
  • arduino will control the relay module which will power PC
  • arduino will be connected to mains by power adapter and to PC with USB
  • power cord switch will switch on/off the arduino power adapter and not the PC
  • arduino will auto-select the power source between power adapter and USB
  • arduino will be responsible for showing current boot sequence status of the PC (turning on, turning off, switched off).

How does it work ?

Turning PC on:

  • switch on arduino's power adapter
  • arduino starts and by default sets relay in ON mode and PC boots up
  • arduino sets RGB LED to "red blink" mode
  • PC boots up and when fully started sends message to arduino to set RGB LED in "solid green" mode

Turning PC off (this is tricky part):

  • switch off arduino's adapter using button on extension cord
  • arduino switches itself to USB power source using built into board auto-select functionality
  • arduino "senses" that power adaptor is switched off
  • arduino sets RGB LED to "red blink" mode
  • arduino sends message over USB to PC ordering PC to enter shut down sequence
  • PC receives the message and enters shut down sequence, in the process of running shut down scripts PC sends following command to arduino:
    • set RGB LED to "red blink" mode
    • set RGB LED to "off" mode after 55 seconds
    • set relay in "off" mode after 60 seconds
  • arduino receives message and sets RGB LED TO "red blink" mode
  • arduino after 55 seconds sets RGB LED to "off" mode
  • arduino after another 5 seconds switch relay to "off" mode - at this point whole system (PC and arduino) is completely disconnected from mains

Modules

There are 3 elements which had to be implemented:

  • software
    • listener on PC to intercept the shut down message from arduino
    • software running on micro controller to monitor current power source
  • hardware
    • device to monitor power source for arduino
    • relay to drive power for PC
    • led to show current status of the power switch and PC

Software implementation details

PC listener

This is code running on PC which is responsible for listening on the shut down message from arduino. This code is written in Python and is based on infinitive loop which listens on data from serial. Code was tested on Debian Squeezy.

Linux init scripts

This script is responsible for setting up, initiating and controlling arduino from PC at star up and shut down of the system. Code was tested on Debian Squeezy.

Arduino code

This code is responsible for controlling LED and relay.

Hardware implementation details:

Hardware is based on Arudino Uno: arduino

Power source monitor

2 resistors connected between VCC/GND/A0 to intercept the switching off of power adaptor. Arduino has built in functionalit to switching between power adaptor and USB.

Relay

Relay module.

LED

RGB LED connected to the board over 3 few resistors.

Communicatio between arduino and PC

In this project 2 way communication between arduino and PC was implemented. Each message starts from command id - integers number, this number might be in most cases followed by delay parameter which means that command should be executed in N milliseconds after the command was sent, and in some special cases message can also contain some additional load (TODO for LCD). Each command must end with semicolon ; character and after that ENTER character.

  • PC (server) to Arduino command list
command id description parameters example
5 turns off LED delay 5,1000; - turn off LED after 1 seconds
6 turns red LED on delay 6,2000; - turn red LED on after 2 seconds
7 turns green LED on delay 7,2000; - turn green LED on after 2 seconds
8 starts red LED blinking delay 8,2000; - starts red LED blinking after 2 seconds
9 starts green LED blinking delay 9,3000; - starts green LED blinking after 3 seconds
10 resets all characters from LCD TODO TODO
11 prints characters on LCD TODO TODO
12 turns relay ON delay 12; - turns relay to ON mode, in this example delay is not set = immediate
13 turns relay OFF delay 13,50000; - turns relay to OFF mode after 50 seconds

Example of sending message from PC (linux) to Arduino:

Arduino connected over USB is available as serial device, example for dmesg output:

[   15.124508] udev[180]: starting version 164
[   16.318056] cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device

In my case device name is /dev/ttyACM0

To send message use following command:

echo 12,60000; > /dev/ttyACM0

above command will turn relay OFF after 60 seconds.

  • Arduino to PC (server) command list
command id description parameters example
4 starting shut down process none 4,0; - arduino sensed that its power adaptor is switche off so sends messege to PC (server) to start shut down sequence as it will switch relay OFF in next 120 seconds (120 seconds is hardcoded)

Example of listening on the message from Arduino on PC (linux):

Assuming that Arduino registered as serial device /dev/ttyACM0 the python script to listen on messages:

 #!/usr/bin/python
 

# usage: send_and_receive_arduino <DEVICE> <BAUDRATE> # <TEXT> # where <DEVICE> is typically some /dev/ttyfoobar
 # and where <BAUDRATE> is the baudrate
 ## and where <TEXT> is a text, e.g. "Hello"
 

import sys
 import serial
 import time
 import subprocess
 port = sys.argv[1] baudrate = sys.argv[2] print "Initializeing serial port: " + port + " " + baudrate
 ser = serial.Serial()
 ser.port = port
 ser.baudrate = baudrate
 ser.open()
 while 1:
        output = ser.readline()
        print output
        if output.startswith("4,"):
                powerOffCommand = "/root/scripts/mpd_v2/commands/powerOff/powerOffSingleRunWrapper.sh" 
                print powerOffCommand
                subprocess.call([powerOffCommand])
                # relayOffCommand = "echo" 
                # relayOffParameters = "\"12,120000;\" > " + port
                # print relayOffCommand
                # subprocess.call([relayOffCommand, relayOffParameters])

Images

Arduino - central unit - box closed - front:

arduino-box_closed-front

Arduino - central unit - box closed - back:

arduino-box_closed-back

Arduino - central unit - box open - front:

arduino-box_open-front

Arduino - central unit - box open - back:

arduino-box_open-back

Arduino - central unit - box open - main board:

arduino-box_open-mainboard

Arduino - central unit - box open - interfaces board:

arduino-box_open-interfaces_board

Arduino - relay unit - box closed:

arduino-box_closed-relay_unit

Arduino - relay unit - box open (close-up):

arduino-box_open-relay_unit

Arduino - relay unit - box open (from distance - with connectors):

arduino-box_closed-relay_unit-from_distance

Schematics:

schmatics

Elements list:

  • Arduino1 - Arduino - processor ATmega; variant Arduino UNO R3
  • C1 - Electrolytic Capacitor - package 100 mil [THT, electrolytic]; capacitance 10µF; voltage 6.3V
  • J1 - Screw terminal - 3 pins - package THT; hole size 1.0mm,0.508mm; pins 3; pin spacing 0.137in (3.5mm)
  • K1 - Relay - package THT; contact rating 125VAC / 30VDC @ 1 AMP; voltage 5V; switching circuit SPDT; part # FRS1B-S
  • LED1 - RGB LED (com. cathode, rgb) - package 5 mm THT?; pin order rgb; polarity common cathode; rgb RGB
  • R3 - 220 Ω Resistor - package THT; tolerance ±5%; bands 4; resistance 220Ω; pin spacing 400 mil
  • R6 - 220 Ω Resistor - package THT; tolerance ±5%; bands 4; resistance 220Ω; pin spacing 400 mil
  • R7 - 220 Ω Resistor - package THT; tolerance ±5%; bands 4; resistance 220Ω; pin spacing 400 mil
  • R8 - 20k Ω Resistor - package THT; tolerance ±5%; bands 4; resistance 20kΩ; pin spacing 400 mil
  • R9 - 10k Ω Resistor - package THT; tolerance ±5%; bands 4; resistance 10kΩ; pin spacing 400 mil

Libraries used in the project

Links