CONTEXT

DESCRIPTION

I'm using a RaspberryPi 3 as a Youtube server in my living room : output sound is plugged into my amp input, it works fine !!! :-)

It works without any screen (headless), power comes from its usual USB adapter. The USB adapter is plugged into an electrical power strip where other devices are plugged too (a tv, an amp, and so on).

PROBLEM

If powering a RaspberryPi is as simple as plug in a PSU, switch it off may become a real problem because system needs to be halted correctly before (mainly in headless mode).

Unfortunately, when I have to switch off all my devices, I'm facing to that major problem... risk is too important, it may corrupt the SD card quickly and raspberry pi won't boot anymore.

So, I searched for a couple of time a solution to make my raspberry pi work alone and be able to stop correctly as soon as there's no power anymore.

A cheap solution, of course :-)

SOLUTION : UPS

Solution is an UPS : Uninterruptible Power Supply. Not exactly, in fact, because I need to stop my server when I do not hear music anymore in my case...

I found out an HAT from geekworm (can be bought here).

This product does not match exactly my needs, but makes the main job for that price : maintain power while server is shutting down, controlling charge and discharge of a Lipo battery !!!

UPS PART : Geekworm Power Pack Pro V1.1

This product is introduced here.

As you can see there's a huge contrainst : a Push Button has to be used to change the board states !!! So, I'm unable to plug this module on my RaspberryPi and make it run alone as I was expected...

So, I added a processor to control this Push Button and a few components to power it and detect logical inputs : that is the UPS Controller, simple and very accessible.

To make it easily and as fast as possible I used an arduino Nano (you can buy one here).

Cost

Without RaspberryPi, cost is about only 20$ !!!


PROJECT - UPS Controller

UPS Controller has been programmed to match my needs (described above). Provided source codes are easily understandable, and can be modified by yourself to match your needs.

HARDWARE

Schematic

UPS controller uses 4 wires to connect on the Geekworm Power Pack Pro :

  • USB in : plug from USB adapter
  • USB out : 5v that Power Pack generates, plug out where is connected raspberry pi
  • GND : commun voltage
  • BP : Push button of the Power Pack board


USB-in and USB-out will bring power to Nano board.

Push Button will be short circuit by our MOSFET, it will behave as it had been pressed.

Schematic

Hacking board

Here are the 4 wires soldered on the Power Pack Pro board.

The 3 first wires are soldered on :

  • R1 (white wire) : is for BP
  • C1 (red wire) : is for USB in
  • Cout (red wire) : is for USB out


Hack1

The last one is for the ground (GND) :

GND

Prototype

Fast prototype was designed on a board : this diagram is closely the same as above. Board

Final board

Here are a few pictures of the final working project.

IMG_20180212_121709.jpg
IMG_20180212_122035.jpg
IMG_20180212_122107.jpg

SOFTWARE

Arduino Nano Side

Arduino behaves as a new i2c slave that Raspberry Pi will request.

Default address is 0x64 (be careful if you want to change address, don't use 0x62 which is used by one of the component of the Geekworm board).

Slave understands two frames :

  • STOP Frame : made of one byte 0xFF, and optionaly a 2nd one that indicates delay (in 10s of seconds) before STOP procedure really starts.

When there's no 2nd byte, default value is 40s (software constant).

  • USB-In request frame : answers 0 when USB-In is unplugged, 1 when plugged.



RaspberryPi Side

Raspberry Pi will regularly verify if USB-In was unplugged (or main power was turned off) : every 10s. Program must be executed as a daemon at the end booting step.

i2c commands and bus are enabled using sudo raspi-config.

Here below is a script called UPS.sh that scans USB-in. As soon as Nano says it was unplugged, RaspberryPi launches delayed STOP procedure, and halts itself. Delayed time let RaspberryPi halt cleanly. At this step, power is coming from the LiPo battery.

Script must have eXecution rights : use sudo chmod +X UPS.sh if script name is UPS.sh

Finally, add this command at the bottom of that file /etc/rc.local, just before exit 0 :
su -c "/home/pi/UPS/UPS.sh &" - pi if your script is at /home/pi/UPS/

Note : if power is back before RaspberryPi is entirely turned off, Nano will detect it. Nano will switch off RaspberryPi and switch it on to make it boot again.

PROGRAMS

Here are the two programs I wrote I'm using for my needs.

Arduino Nano

This program is well documented if you need to change its behaviour to match your needs. Default values are :

  • slave i2c address : 0x64
  • delay before switch off when received RaspberryPi STOP order : 40s
  • delay before switch off when USB-In is unplugged : 300s (5 minutes)


UPS.ino

RaspberryPi Daemon

This script is very simple and uses those values :

  • slave i2c address : 0x64
  • reading every 10s
  • delay before UPS turn off : 40s


UPS.sh

VIDEO

Introduction to UPS Controller