Sunday, February 9, 2014

UPS for Raspberry Pi

As described in the previous post, I'm using the Raspberry Pi as a server for my calender and addressbook data. It is directly connected to my WLan station, and I want to be able to simply shutdown both devices by simply switching off power. Simply switching off power is not the best idea for a Linux system. That is, the preferred way is to neatly shutdown the system before. Since I do not want to start a terminal application before in order to shutdown the Pi properly, I found a different solution: CW2's Pi USV (USV is german for UPS). It is a battery powered UPS, which comes with a nice software package for Pi's Debian Linux. The UPS and the software shutdown the system when the USB power supply is switched off. This is exactly what I needed.

Installation is rather easy. On piusv.de you find a short movie demoing the device, along with some PDFs of how to connect the hardware and set up the software (there are english manuals, even though the web page is in german language only).

I have a case for my Pi, and in order to be able to use that case with the UPS, I had to cut three holes as shown in the picture. The installation of the hardware is nicely documented in CW2's manuals.

Software

CW2 UPS comes with nice software, which monitors the power and automatically shutdown the system if the USB power is switched off. The software comes as a Debian package, and the installation sets up everything -- usually you do not need to manually adjust things.

This is how I installed the software on command line (as I have no monitor attached to my Pi, I always connect via ssh):

Download piusvmonitor software on command line via wget:

wget http://www.piusv.de/support/piusvmonitor.deb

Install piusvmonitor as super user:

sudo dpkg --install piusvmonitor.deb

After installation, you will find a shell script

piusvautostart
in /etc/init.d for starting the piusmonitor automatically. After installation, the software is automatically started. The monitor actually is a service, running in the background.

Configuration and log files are found in

/usr/share/piusvmonitor

Check functionality:

tail -f /usr/share/piusvmonitor/log.piusv
Initially:
Sun Feb  9 12:21:38 2014
PRI_POW 
Now, remove one battery as shown on the demo on pisv.de: This will be shown in the log:
Sun Feb  9 12:21:39 2014
PRI_POW BAT_LOW 
Sun Feb  9 12:21:39 2014
PRI_POW BAT_LOW
Now, put battery back:
Sun Feb  9 12:21:53 2014
PRI_POW 
Sun Feb  9 12:21:53 2014
PRI_POW 
Eventually, try the shutdown by removing the USB power supply:
Sun Feb  9 12:22:02 2014
SEC_POW BAT_LOW 
Sun Feb  9 12:22:02 2014
SEC_POW BAT_LOW 
Sun Feb  9 12:22:12 2014
 PIUSV shutdown
Sun Feb  9 12:22:12 2014
 PIUSV shutdown
Sun Feb  9 12:22:16 2014
SEC_POW BAT_LOW 
Connection to xxx.xxx.xxx.xxx closed by remote host.
Connection to xxx.xxx.xxx.xxx closed.    
The last two lines a due to the ssh connection, of course. After rebooting and reconnecting to the PI, the piusv software is started automatically:
Sun Feb  9 12:22:28 2014
PRI_POW 
Sun Feb  9 12:28:20 2014
PRI_POW BAT_LOW 
Sun Feb  9 12:28:21 2014
PRI_POW 

Summary

The CW2 UPS is a nice little device, and the software is working great. Unfortunately the common Rasperry Pi case does not really fit. Most of all, it would be nice to have a real connector for the battery package instead of the press-stud connection. The CW2 UPS costs about 30€ and can be ordered irectly from CW2. Additionally, you need batteries, of course. Note that the UPS does not automatically charge the batteries, so there is still some "manual" maintenance necessary.

Sunday, January 12, 2014

Raspberry Pi and ownCloud: Calendar and contact synchronization without Apple or Google

The probably badest news about Mac OS X Maverick is that the synchronization services were removed, thus disabling tools like Missing Sync to work. I used Missing Sync to synchronize calendar and contacts between my MacBook and Android phone. Since I do not want to use Google, I had to find another solution.
My new solution is a combination of the following things:
  • Raspberry Pi
  • ownCloud
  • CalDav- and CardDav-Sync for Android
I have a Raspberry Pi running ownCloud connected to my Airport Extreme via Ethernet cable (as it is directly placed near to the Airport). It has a static IP, so that it is easy to find in my local network. For security reasons it is only available in my local network: I don't need global access to my few contact and calendar data, as it is sufficient to sync the data when I (and my Android and MacBook) are at home. ownCloud is a little cloud solution, providing WebDav, CalDav and CardDav services. These services are used to sync the data similar to Google or Apple.
Why a Raspberry Pi?
For security reasons, I didn't want to use a (virtual) server publicly available on the internet. I simply do not have the time to constantly update a server for fixing security issues. Also, setting up a server in the cloud is not that much different from using Google, is it? Since the server needs to be accessible to the Android device, a static IP address is very convenient. I could have installed ownCloud on my MacBook directly, and assigning the book a static IP address (or reserve a specific DSCP IP address to the MacBook), however with the Pi I can use the server not only for me but also for roommates ;-) And then, for a geek a real local server is simply a must, isn't it?

Setup Raspberry Pi and ownCloud

Installation if this set up was pretty easy:
  1. Buy a Raspberry Pi along with an USB power supply, an SDHC card, and a little case and maybe little cooling elements
  2. Format SDHC card (luckily my MacBook has an SD card slot) via SD Formatter for Mac (https://www.sdcard.org)
  3. Install Rasbian via Noobs (http://www.raspberrypi.org)
    1. follow readme instruction foud in Noobs zip
    2. install Rasbian via Noobs, enable SSH via menu
    3. set up static IP address (e.g., see https://thepihut.com/blogs/raspberry-pi-tutorials/)
    4. while 3.1-3.3 needed the Pi to be connected to a monitor, once the static IP address is configured, the following steps can be applied via ssh.
  4. Install ownCloud (https://owncloud.com), following the tutorial at http://www.connect.de (german only).
    Minor adjustment: The SSL configuration file has to have newlines, that is, use this template:
    <virtualhost>
    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/apache.pem
    DocumentRoot /var/www
    </virtualhost>

Set up clients

Now, configure ownCloud, that is, set up initial admin user and create a new user via the web interface.
There is not much magic to configuring CalDav and CardDav to the OS X apps (in the apps, select preferences and add your own server using the static IP address and the configured user).
There may be some trouble setting up CardDav. For El Capitan, add a new CardDav account, manual setup, and use the following server address:
https://SERVER/owncloud/remote.php/dav/principles/users/USERNAME



For initial import of the calendar entries, export your calendar to a file. This file is then uploaded via drag'n drop to ownCloud and imported there via click (only click once, this might take a while).
The last point has been a little bit tricky, as I had to create a temporary folder manually in order make it work. This is how I did it:
cd /var/www/apps/calendar/
sudo mkdir import_tmp
sudo chown www-data:www-data import_tmp
Android Apps
I'm using the following Android apps now (instead of the Missing Sync software with Fliq Calendar, which I never really liked):
(Thank you Marcus for pointing me to these apps!)