Personal micro server

Useful Companion

© Lead Image © Andrea Danti, 123RF.com

© Lead Image © Andrea Danti, 123RF.com

Article from Issue 242/2021
Author(s):

Turn a Raspberry Pi into a useful personal micro server for streaming Internet radio, reading RSS feeds, jotting down notes, sharing files, and more.

Using a Raspberry Pi for streaming Internet radio is probably one of the easiest projects you can do with the little machine. Install a player application that supports streaming, connect an external speaker or headphones to the Raspberry Pi, and you are pretty much done. But why stop there? Why not improve on radio streaming and add other features that transform your Raspberry Pi into a simple yet useful personal server? Enter Nyttig [1], simple PHP software (Figure 1) I developed that can be used as it is or as a starting point for building your very own Raspberry Pi-based micro server (see the "Why Nyttig?" box).

Figure 1: Nyttig offers a handful of simple applications.

Why Nyttig?

With so many excellent open source applications out there, why would you choose Nyttig instead of just installing existing tools or deploying something like Nextcloud? There are several reasons why Nyttig may be a better solution. First, Nyttig is lightweight. It consumes very little resources, and it runs happily even on something like Raspberry Pi Zero. Simplicity is another important Nyttig trait. All PHP scripts in Nyttig are intentionally made as simple as possible, which has two benefits: You can figure out how to use Nyttig's features in a matter of minutes, and you can easily tweak, improve, and extend Nyttig even with modest PHP skills.

Installing Nyttig

Nyttig includes a shell script that automates the entire installation procedure on a Raspberry Pi. Before you install Nyttig, make sure that the Raspberry Pi is running the latest version of Raspberry Pi OS Lite. You also need to confirm that the machine has an Internet connection and is accessible via SSH. Establish an SSH connection to the Raspberry Pi, and run the following command:

curl -sSL https://gitlab.com/dmpop/nyttig/-/raw/master/install-nyttig.sh | bash

This command fetches and runs the installation script that does all the work for you. During the installation, the script prompts you to install the puvmeter utility that turns a Blinkt! [2] LED stick into a colorful VU meter.

Once Nyttig has been installed, open the nyttig/config.php file for editing and modify the example settings. By default, access to Nyttig is protected by a password, and it's a good idea to change the default password by modifying the value of the $PASSWORD variable. You can also disable password protection by setting the value of the $PROTECT variable to false. Set the $CITY variable to your actual city to allow Nyttig to fetch weather conditions. You can leave the rest of the variables at their default values for now.

While you can deploy Nyttig with a proper web server like Apache or NGINX, it runs perfectly well using the server that comes with PHP. Better still, the installer script automatically adds a cron job that starts the PHP server on boot. To enable Nyttig, restart the Raspberry Pi and point your browser to the Raspberry Pi's IP address (see the "Handy Little Helper" box for locating the IP address.)

Handy Little Helper

If the Raspberry Pi running Nyttig sits permanently on your local network, then finding its IP address is not that difficult. But what if you carry your little micro server with you and want to use it on different wireless networks? How can you connect it to the desired WiFi network without access to the Raspberry Pi? Comitup [3] helps to solve this catch-22. Install the utility, and it automatically connects the Raspberry Pi to the default WiFi network. If the network is not found, Comitup enables the Access Point mode, so you can connect directly to the Raspberry Pi and select the desired WiFi network.

Using Nyttig

While it's possible to control the player running on the Raspberry Pi via an SSH connection, it's hardly practical in everyday use. And Nyttig provides a no-frills web interface that allows you to switch between stations, control volume, and manage radio stations (Figure 2). The list of radio stations contains a couple of examples. Pick the one you like, plug headphones or an external speaker into the Raspberry Pi's audio jack, and hit the Play button. Of course, you can add your own stations to the list. First, you need to obtain a playlist link for the desired radio station. One of the best places to do that is the radio-browser website [4], which maintains a large database of radio stations around the world. Find the station you like, download its .pls file, and add it to the list of radio stations in Nyttig using the upload form.

Figure 2: The Nyttig Radio interface lets you manage radio stations and control playback.

During playback, you can adjust the volume by specifying the desired value in the appropriate field and pressing Volume. By default, the radio interface controls the volume of the headphones device (on Raspberry Pi, it's the 3.5mm audio jack). But if you are using a different sound device, you need to specify it in the config.php file. To find what sound devices are available on the Raspberry Pi, run the aplay -L command.

Speaking of sound devices, since Nyttig can easily run on Raspberry Pi Zero, you can use the latter to build a diminutive personal server that can fit in a shirt pocket and can be powered by a battery. Raspberry Pi lacks an audio jack, but this problem can be solved by adding a cheap generic external USB sound card. If you opt for this solution, you have to set the value of the $SOUND variable in the config.php file to Master. To enable the USB sound card on Raspberry Pi, use the command

sudo nano /usr/share/alsa/alsa.conf

to open the alsa.conf file for editing and locate the following lines:

defaults.ctl.card 0
defaults.pcm.card 0

Modify the lines, so that they look as follows:

defaults.ctl.card 1
defaults.pcm.card 1

Then, reboot the Raspberry Pi Zero to enable the sound card.

In addition to the basic radio controls, the radio interface provides a couple of other features. The # button plays white noise instead of radio. This can come in useful when you work in a noisy environment, but you find listening to a radio too distracting.

Instead of streaming a radio station to the external speaker or headphones connected directly to the Raspberry Pi, you can listen to it in your browser. Enter the streaming link in the Radio station URL field, press Submit, and use additional controls to listen to the radio.

With so many excellent RSS aggregators at your disposal, Nyttig's RSS reader may seem superfluous (Figure 3). However, it's not designed to replace a proper RSS aggregator, but rather offers you a quick and easy way to check your favorite RSS feeds when you have a spare minute. Since the RSS reader is not exactly overloaded with the creature comforts of a proper RSS aggregator, it's supremely easy to use. Select a feed from the list of feeds and press Show Feed to view articles in the feed. To add and remove the feeds, press Edit. That's all there is to it.

Figure 3: Nyttig's no-frills RSS reader is perfect for quickly checking your favorite feeds.

If you often find yourself using random text files for jotting down notes or drafting articles, then you might appreciate the editor that comes with Nyttig (Figure 4). While it's decidedly bare-bones, it does have one clever trick up its sleeve. Whenever you press the Save button, the editor saves the previous version as a separate file with a time stamp. So the editor keeps track of all previous versions, and none of your edits are lost.

Figure 4: You can use the Nyttig Editor for jotting down notes and drafting texts.

Need to share a file or an image with someone on the same local network? Use Nyttig's Files tool (Figure 5). Upload a file, and give the recipient the generated link.

Figure 5: Need to quickly share a file or a photo? Use Files to do that.

Finally, the dashboard interface provides a basic overview of the server's vitals, including processor temperature, CPU load, and RAM usage (Figure 6). You can use the appropriate buttons to restart and shut down the Raspberry Pi.

Figure 6: Dashboard gives a quick overview of resource usage.

Tweaking and Extending Nyttig

Since Nyttig consists of just a handful of simple PHP scripts, you can tweak and extend it to your heart's content. What functionality you can add depends entirely on your imagination and PHP skills. Here are a couple of examples to get you started.

The simplest improvement that requires no coding is to enable an existing module in Nyttig. In the nyttig directory, you'll find a folder named did. It contains a small application that can be used to record what you've accomplished during the day. DID supports Markdown, so you can format your entries as lists. The tool creates a separate file for each day when you add the first entry and then appends subsequent entries to the file. DID is not included in Nyttig by default, but there is an empty tile for it on Nyttig's main page. To enable DID, open the index.php file for editing and locate the empty tile with the Empty tile comment in it. Replace the comment with the following HTML code:

<a href="did/"><img data-src="did/favicon.png" width="" height="" alt="" uk-img></a>
<p>DID</p>

Save the changes and you are done. Refresh Nyttig's main page, and you should see the DID tile.

The next tweak involves a bit of PHP coding. Using just a few lines of PHP code, you can display a random line from a text file on Nyttig's main page. This can be useful if you want to show random quotes, new words and expressions, or something else. First prepare a list and save it in Nyttig's directory under the list.txt name. Open the index.php file for editing and locate the following line:

<h1 class="uk-heading-line uk-text-center">N Y T T I G</h1>

Add the following code snippet right after the line:

<p class="uk-text-center">
<?php
$lines = file('list.txt');
echo $lines[array_rand($lines)];
?>
</p>

Save the changes, refresh Nyttig's main page in the browser, and you should see a random line from the text file displayed right under the header.

To try something more advanced, you can add a module that pulls a photo from NASA's Astronomy Picture of the Day service and displays it in a lightbox. For this tweak to work, you need to install a PHP JSON library on the Raspberry Pi using the command:

sudo apt install php-json

Assuming you want to add the new module to the empty tile, replace the comment in it with the code block shown in Listing 1.

Listing 1

Adding a Module to an Empty Tile

<?php
  $request = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY";
  $response = file_get_contents($request);
  $data = json_decode($response, true);
  $apod = $data['hdurl'];
?>
<div uk-lightbox>
<a href="<?php echo $apod; ?>"><img data-src="dummy/favicon.png" width="" height="" alt="" uk-img></a> <p>Astronomy Picture of the Day</p>
</div>

The code in Listing 1 performs several tasks. First, it sends an HTTP request to NASA's API. It then reads the received JSON-formatted response and extracts the value of the hdurl key (this value is the URL to the actual image). The obtained URL is then used as an image source in the HTML code that displays it in a lightbox.

By the way, Nyttig is based on the UIkit framework [5], and it's worth perusing UIKit's documentation to understand how you can use it to tweak and improve Nyttig.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News