Use Arduino to Shutdown Windows like a UPS

Good afternoon,

I use an inverter system as a backup power source for my desktop computer, however since I built it myself, it lacks a program which shuts it down before the battery reserve runs out.

I know how to detect a power cut using the Arduino, but I need some help for my next step. How can I create an interaction between the Arduino and my windows functions; such as shutting down?

Your reply is greatly appreciated, thank you

Alex

(deleted)

Yes exactly, that is the answer I am looking for! I would like to try both alternatives, and require your guidance as I am somewhat under-experienced in this field.

If I were to use a relay, it would be powered by 5v, can this be taken from the Arduino or do I need a separate power supply? I have seen Arduino relays powered by a physical switch, but how will I close the relay with the Arduino, is it through a sketch?

For the second alternative, could you please elaborate on what a "small service" is and how it can monitor a USB port? My final question is if I can 'force shutdown' to avoid programs preventing it from shutting down.

I would greatly appreciate a detailed response for all my questions,

Thank you for you time,

Alex

alexanderstroborg:
If I were to use a relay, it would be powered by 5v, can this be taken from the Arduino or do I need a separate power supply? I have seen Arduino relays powered by a physical switch, but how will I close the relay with the Arduino, is it through a sketch?

I normally power my relays from external power source. Some people says relays sometimes draw more power and can cause damage to GPIO pins.
In normal scenario a relay can be controlled by setting HIGH/LOW to INP pin, see one of my blog post

alexanderstroborg:
For the second alternative, could you please elaborate on what a "small service" is and how it can monitor a USB port?

I assume @spycatcher's idea is like

  1. Arduino is connected to the computer via a USB port.
  2. In case of a power failure, the Arduino detetects it.
  3. Arduino sends a serial message to the USB COM port.
  4. A background service should listen to this COM port, if any time it receives a message like 'SHUTDOWN'. The service will invoke the shutdown command and shutdown the system.

This can be done extremely simply in windows. No service on the PC is required.

Using an Arduino which can emulate a keyboard (leonardo for example). Simply send the key combinations required.

You could either automate navigation to the shutdown button (What keys depends on which Windows, but all have key combinations).

Or you could first open the run command (Win + R) then type "shutdown -f -t 0".

In the shutdown command -f is 'force'.

pYro_65:
This can be done extremely simply in windows. No service on the PC is required.

Using an Arduino which can emulate a keyboard (leonardo for example). Simply send the key combinations required.

You could either automate navigation to the shutdown button (What keys depends on which Windows, but all have key combinations).

Or you could first open the run command (Win + R) then type "shutdown -f -t 0".

In the shutdown command -f is 'force'.

Stupid Windows uses slashes for some reason.

shutdown /s /f /t 0

It's rather disturbing how easy that is.

Thank you for the replies, I only have a UNO R3 board at my disposal which does not support keyboard or mouse libraries like a Leonardo board would do. I know that there are ways to upgrade the firmware but that is not what I want to do.

However I do think that using the command prompt within windows 7 is the most effective way. How can I create an interaction between my Arduino and Windows? I’ve thought a lot about the second alternative that Sarouje suggested. Is there a way I can send a serial message from my Arduino straight into the command prompt? Instead of printing into my serial monitor, I would effectively print into the command prompt. If possible, the simpler alternative would revolve around executing a .bat file, but once again how can I open a file on my desktop through the Arduino or serial monitor?

I really appreciate your time, and it is helping me a lot! :slight_smile:

You need a program on your computer that listens to the Serial port, and when it gets the message from your UPS initiates a shutdown.

This topic has been dealt with quite a few times here already: UPS Windows Threads

pYro_65:
This can be done extremely simply in windows.

leonardo for example

Once that word fell my mind was like, uhm, simple...

Easiest way is a power button press. And you don't even need a relay. The power switch just pulls to GND (aka, one wire to the button is GND). All you need is a cheap optocoupler (PC817 for example) and a resistor. Done!

(deleted)

I would like to simply use a power button press, however it needs to be 'transferable' between computers, meaning that I cannot do physical modifications, rather just plug it in via USB.

The UPS service program created by spycatcher2k seems very promising and it recognizes my COM port, however the problem is that the hibernation feature had been disabled to save storage on my SSD. Also how can I send a capital 'Z' from the Arduino when the UNO does not support keyboard inputs; is there something I am missing?

Considering I do not know much about coding within windows 7, is there anyone that can lead me into a direction where I can code a program, similarly to spycatcher2k's UPS service to listen to a COM port, and then execute a shutdown or .bat file?

I am sorry for my repetition, your patience is appreciated,

Alex

(deleted)

(deleted)

Thank you for reprogramming a shutdown version,

However I have a problem with the program; basically it listens to a COM port until the Arduino sends a capital 'Z' through the serial monitor. I've created a sketch as spycatcher2k described, however when the UPS program 'listens' to the COM port, I get an error message when uploading my sketch saying "Port busy". What am I doing wrong?

It seems that I cannot use a COM port for two things simultaneously, how can I listen to a COM port while still being able to send serial data through it?

Alex

I fixed it! I have been tearing my hair out trying to figure out the problem.

I just need to close the serial monitor, I cannot have it open while running the UPS Handler. Now I understand while the port was busy.

Thanks once again, I will have to move on to some C# now ;D

spycatcher2k:
Shutdown version : UK-PCB part of the BADnetwork Group.

Mind sharing the code for that so we can all see what you're doing?

(deleted)

I'm not familiar with the file formats, am I correct in thinking it was made with C#?

(deleted)