Close
0%
0%

Link an old UPS to a QNAP NAS

I wanted to have my ancient UPS (uninterruptible power supply) to be able to shut down my NAS when the power goes out.

Similar projects worth following
A couple of years ago I purchased a QNAP TS-453 NAS and decided to power it from my ancient APC UPS that only has a pseudo-RS232 port. The NAS only has USB ports so something special would be required to allow them to communicate with each other.

I didn't wish to replace the UPS since there's nothing wrong with it.

I thought it’d be fun to design an Arduino interface between the two machines so that the Arduino can:

1. Tell the NAS when to initiate shutdown if the power goes out
2. Tell the UPS to shut itself off after the NAS has completed its shutdown

*This project should only be undertaken at your own risk.* This project includes making some permanent changes to your QNAP NAS.

It was not a trivial task to make the necessary permanent changes to the QNAP NAS.

Some parts of this are at/beyond my usual skillset:  Linux, setting up a Linux toolchain.

My intention was to make a useful and reliable tool.  Much more would have been possible if the UPS's interface had offered more information.  In my case, the simplicity of the UPS’ interface limits how much we can do.  

Terminology

cron      The "task scheduler" build into all Unix-like systems.

NAS      Network Attached Storage  (a network device for storing files)

UPS      uninterruptible power supply

stty        a command-line application commonly provided on *nix operating system installations for serial communication from the command line.

Theory of Operation

For those who aren't familiar with a sequence diagram shown below (figure 1), at one minute intervals, the NAS's operating system will run a scheduled task (we will configure this in the setup file for "cron").  I had chosen for it to run this task every minute but you could choose any polling interval you like.

When our cron task runs our application, the application will open the USB port and initiate a conversation with the Arduino asking about the power situation (literally "HowAreThings?").  The Arduino will be aware of the power status because it's polling the UPS status signals.  The Arduino will tell our NAS application whether the NAS must initiate shutdown or not.  If the NAS application has been instructed to initiate shutdown, it will run the QNAP/Linux application /sbin/poweroff.  When the NAS shutdown is complete, the USB power will go off.  The Arduino is powered from the UPS so it continues running even when the NAS shutdown is complete.  When the Arduino sees that the USB power went off and that the UPS is running on batteries, it will signal the UPS to also shut down.

Figure 1:  sequence of operation

Required skills

Soldering with a good soldering iron (3 wire plug)

Basic Arduino IDE skills

Basic Linux skills

Skills with Linux text editors

Basic C 

Software you’ll need

Arduino IDE

A Linux installation having gcc (or an ability to cross-compile to Linux)

Libusbp  (C USB library)

upsmon.c

the source for the application to run on the QNAP NAS.

plain - 3.26 kB - 09/24/2017 at 20:09

Download

NASUPSMonitor.sch

System schematic (Eagle format)

sch - 69.17 kB - 09/24/2017 at 20:08

Download

NASUPSMonitor.ino

Arduino sketch for the completed project.

ino - 3.25 kB - 09/24/2017 at 20:05

Download

BasicSerialTest.ino

Arduino sketch to test serial communication with the NAS.

ino - 159.00 bytes - 09/24/2017 at 20:05

Download

  • 1 × Arduino Nano
  • 1 × Assorted connectors & bits of wire
  • 1 × USB cable to connect Arduino to NAS
  • 1 × A separate power supply for the Arduino

  • 1
    Finding the Arduino's port number in the NAS

    1. Use SSH to get a command prompt in your NAS.

    2. type the command:    

    ls /dev/tty*

    3. Connect the Arduino

    4. type the command:    

    ls /dev/tty*

    5. compare the results from step 2 and 4.  The extra device that was added in step 4 is your Arduino.  Make a note of the port name...we'll use that throughout the rest of the project.  It'll probably be ttyACM0 or ttyUSB0

    If you don't see your device, you'll need to install a device driver for your device in Linux.  That's beyond the scope of this article.

  • 2
    Communication testing

    This is a good time to test the linkage between your NAS and the Arduino.

    1. download attached BasicSerialTest.ino into the Arduino

    2. connect the Arduino to the NAS

    3. type the command (use your device port name from the previous step):  

    screen /dev/ttyUSB0 9600

    4. Review the results for a few minutes.  If the results are regular and predictable ("1234567890" every second), you can skip ahead

  • 3
    Fixing corrupted communication

    In my case, I got what you see in figure 2 below.

    Figure 2:  corrupted communications from the Arduino as seen by the NAS's "screen" command.

    We'll cover the causes individually.

    The first cause can be that serial ports can be opened by more than one application simultaneously.  In my case, I used the following command to see what applications had the Arduino's USB port open:   

    lsof  /dev/ttyUSB0

    The lsof command showed that an application called "ups_yec" also had the port open.  From what I’ve read, ups_yec is a driver for a brand of UPS and it will attempt to use any USB device with the default VendorID/ProductID provided in the OSEPP Arduino Nano.  It will open the port and remove characters from the USB port’s buffer and it may also be changing the baud rate. 

View all 16 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates