We're technologically informed, and we would love to take you along.

Friday, 11 December 2015

Updated: How to stream files from Raspberry Pi to a smartphone

Updated: How to stream files from Raspberry Pi to a smartphone

Introduction and getting started

Note: Our Raspberry Pi streaming tutorial has been fully updated. This feature was first published in May 2013.

The best thing about the Raspberry Pi (RPi) is its versatility – originally launched as an educational device, it's transcended these limitations to become the go-to computer for just about anything you can think of. For example, it can double up as a very capable headless server.

Contrary to popular belief, not every server task requires lots of processing power. For example, if you wish to download files and share them with users on the network irrespective of their operating system or device, you don't need a machine with a multi-core processor. In fact, using an old Linux computer as such a seedbox is a popular way of rescuing discarded hardware.

The only downside of this approach is that it consumes a lot of electricity. Most modern routers have a USB port, and will share the plugged-in device with all computers connected to this router. But what if you have an older router and want to do more than just share files?

With the RPi, you can do all this and a lot more without the costs associated with a traditional computer, and without the restrictions of dedicated devices such as routers.

DHCP

Gather the ingredients

We've used a Raspberry Pi 2 with 1GB OF RAM for this tutorial, but it'll work fine on other Pi models too, including the brand new Raspberry Pi Zero. It'll be powered by the latest version of Raspbian at the time of writing (version 2015-11-21-raspbian-jessie).

The best thing about the newer releases is that they can use all of the increased memory without any tweaks. They also ship with SSH enabled, which allows users to access and set up the RPi without ever connecting it to a monitor.

Download the latest version of the Raspbian distro and flash it to an SD card, either with dd on Linux or Win32DiskImager if you are on Windows. While you can use a USB wireless adapter to connect the RPi to the internet, it's best to use the Ethernet port and pull up a cable to the router.

After connecting and powering up the RPi, head to your router's admin page (check the router or its documentation for the address). In all probability, your router is using DHCP to hand out IP addresses to connected devices.

Explore the admin interface, look for a list of all connected devices and note down the IP address the router has assigned to the RPi. Later, you'll have to assign it a static IP address to ensure the RPi is always accessible at the same address.

For now, let's assume the dynamic IP address assigned to the RPi is 192.168.3.100. With this bit of information, we're all set to connect to the RPi and prepare Raspbian via SSH.

Every Linux distro ships with an SSH client (it's built into the Mac's Terminal too). Windows users can use the PuTTY tool. To connect from a remote Linux machine, launch a terminal and enter the command:

$ sudo ssh pi@192.168.3.100

After agreeing to add this address to the list of trusted hosts, you'll be asked for the login credentials. On a fresh, unconfigured Raspbian install, the password for the default 'pi' user is 'raspberry'.

From this point on, the commands in this tutorial will be the same irrespective of whether you are connected to the RPi from a Linux or a Windows machine. That's because the commands are run on the Raspbian Linux distro on the RPi.

Since this is a fresh install, you'll be asked to configure it before going any further. Enter the command:

$ sudo raspi-config

This will bring up a screen with lots of options. Scroll down and select the Advanced option, then selectA0 Update, which will grab the latest version of this configuration utility.

Once it's done, the RPi may restart and you'll have to reconnect via the ssh command or PuTTY like earlier. You'll have to do this every time you tweak any setting and restart the RPi.

Setting up users

When you're back in the utility, select the Expand Filesystem option to allow the Raspbian partition to take over all the space on the SD card. We'd also recommend changing the user password at this point too.

Next, select Boot Options and choose B1 Console to stop booting to the desktop GUI. Finally select the Memory Split option under Advanced, which lets you split the RAM on the RPi between the GPU and the CPU. Since we'll be accessing the Pi only via a remote connection, make sure you assign the bare minimum memory to the GPU, which is 16MB.

Close raspi-config and reboot. Next, you should add other users to the RPi. Later on, we'll restrict access to certain directories on an attached USB device to certain users as well as to a group of users, while still having public areas on the device. But first:

$ sudo adduser <username>

Replace <username> with your target username (don't include the <> marks). This will add the user and create the appropriate directories. The command will ask for the user's password and other details. Then add this user to the users group with:

$ sudo usermod -a -G users <username>

Raspi config

To the dance floor

When you're done, it's time to make the RPi accessible to users on the internet. For this, we'll use Samba, which allows us to share files via the Common Internet File System (CIFS) protocol. To install Samba on the RPi, type:

$ sudo apt-get install samba samba-common-bin

When it's done, you'll have to add users to Samba. To do this for the default Pi user:

$ sudo smbpasswd -a pi

You'll then be prompted for a password. It's usually safe to use the same password as the user's account password. Repeat this step for every user on the system.

Samba is controlled via a configuration file that you need to tweak before you can use it. It's always a good idea to back up existing configuration files before making changes:

$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Now use the nano command-line text editor to edit the configuration file:

$ sudo nano /etc/samba/smb.conf

In the file, search for the section marked Global. Under this section, remove the hash sign at the start of the line that reads #security = user so that it now reads security = user (if it's missing, add it below the Workgroup line).

This will ensure that Samba only allows users to log in who have home directories on RPi. To let users access their own home directories, look for the [homes] section under Share Definitions, and make sure that browseable = yes and read only = no.

To exit nano, press [Ctrl] + [X], then type y and hit [Enter] when it shows you the name of the file. Every time you make any changes to any service, you need to restart it before the changes are enabled. To restart Samba:

$ sudo service smbd restart

$ sudo service nmbd restart

It's now time to plug in the USB device to the RPi. Even if your Pi only has two or even four USB ports, it's a good idea to plug in any additional USB devices via a powered USB hub, particularly if you're connecting large USB storage devices that consume a lot of power.

Also, pay close attention to how the drive is formatted. By default, many USB flash drives are formatted as FAT32. It might be the best format in terms of operating system compatibility, but it is by far the worst for sharing files over the network.

Then there's NTFS, which is used by many large external USB drives. This isn't the format for you if you want to stream media off the remote drive, which is best served via ext4. However, use ext4 only if the drive will be used over the network or on Linux machines and you know what you're doing – it can be a nightmare configuring permissions.

After plugging in the drive, find out its location with sudo fdisk -l. This will list the devices attached to the RPi and the partitions in them. Scan the output and look for the disk which size matches the USB drive that you have plugged in. The device will probably be at sda and the partition we want to mount at sda1.

Create the mount point and mount the device:

$ sudo mkdir /mnt/usb

$ sudo mount /dev/sda1 /mnt/usb

The USB drive will remain mounted until you reboot the RPi. To avoid having to remount the device, first find its UUID:

$ sudo blkid

/dev/sda1: LABEL="USB" UUID="3B5C053D35CAD865" TYPE="ntfs"

Now add it to the list of devices that are mounted at boot:

$ sudo nano /etc/fstab

UUID=3B5C053D35CAD865 /mnt/usb ntfs-3g uid=1000,gid=100,umask=0002 0 0

Replace ntfs-3g with vfat if your drive is FAT32. Save the file, and type the following to ensure the drive will mount correctly at boot:

$ sudo mount -a

Installing torrent server

Samba is designed to share files and folders that are defined in its configuration file. To share folders on the USB device, let's assume it has a bunch of folders:

$ ls /mnt/usb

documents downloads music videos

To share the downloads folder, open the /etc/samba/smb.conf file with nano, scroll down to the bottom and enter:

[Downloads]
comment = Place all your downloads here
Path = /mnt/usb/downloads
browseable = yes
writable = yes
read only = no
valid users = @users

This block of text will share the /mnt/usb/downloads directory with all users in the users group. Later, we'll mount this directory on both Linux and Windows computers. Users will then be able to configure their download managers to save the files directly to this folder on the USB device attached to the RPi, from any computer on the network.

You can also restrict access to some folders to certain users only:

[Documents]
comment = Important eyes-only PDF files
path = /mnt/usb/documents
browseable = no
writable = yes
read only = no
valid users = pi, bodhi

This will let only the users pi and bodhi mount and modify the contents of folders.

Transmission

Install the torrent server

Torrents are the preferred mediums for sharing open source content. Most Linux distros are distributed this way either via their own trackers or via linuxtracker.org.

There's no dearth of torrent clients for the Linux desktop. What sets Transmission apart from others is its easy-to-use web interface which resembles the desktop one. We'll install Transmission on Raspbian and then access it from any browser on any computer to add, monitor and control torrents.

To install Transmission, SSH into the RPi and type:

$ sudo apt-get install transmission-daemon

This will install and start the Transmission daemon. But before you can use it to download torrents, you need to configure it. Before making any changes to Transmission's configuration file, ensure that its daemon isn't running:

$ sudo service transmission-daemon stop

Also, before going any further, add the transmission user (debian-transmission), which is created automatically when the daemon is installed, to our group of users:

$ sudo usermod -a -G users debian-transmission

Now create a public share on the USB device, where we'll download the torrents. First, create the public share on the USB and assign the transmission user as its owner:

$ sudo mkdir /mnt/usb/public
$ sudo chown debian-tranmission /mnt/usb/public

Now add this share to Samba's configuration file:

[Public]
comment = Public share for torrents
browseable = yes
path = /mnt/usb/public
public = yes
writeable = yes
guest ok = yes

Share

Restart the smbd and nmbd services to make the share available to everyone. With the public share in place, it's time to configure the Transmission daemon. Its settings are defined in:

/etc/transmission-daemon/settings.json

Open the file in nano and first change the "rpc-whitelist-enabled": true parameter, to "rpc-whitelist-enabled": false to allow users to connect from all computers.

Next, specify the download directory with:

"download-dir": "/mnt/usb/public/downloads/Complete"

You can also separate incomplete files by keeping them in a different folder. First, enable the option by changing the "incomplete-dir-enabled": false parameter to "incomplete-dir-enabled": true and then specify the directory that'll house the incomplete downloads with "incomplete-dir": "/mnt/usb/public/downloads/Incomplete"

User authentication and accessing shares

User authentication

Since we've given the ownership of the /mnt/usb/public directory to the transmission user, it'll automatically create any new directories. While the downloaded torrents are public, you can ensure that not everyone can queue torrents for downloads.

Pi connect

One way of doing this is by asking users to authenticate themselves before allowing them access to Transmission. For this, first change the "rpc-authentication-required": false parameter to "rpc-authentication-required": true if necessary and then, define a password in the "rpc-password": "<password>" parameter, which Transmission will automatically encrypt.

Now, save the file and start the Transmission daemon with:

$ sudo service transmission-daemon start

By default, Transmission runs at port 9091. In our example, the complete URL for Transmission's web user interface would be 192.168.3.100:9091. Launch a browser and head to that address.

If you've enabled the password parameter, you'll be asked for login credentials. The username is transmission and the password is the one you specified in the configuration file.

Before you can download a torrent, you need the location of the .torrent file. Go to Transmission's web interface and click the Open Torrent button. Paste the URL in the window that pops up and click Upload to start.

The interface is easy to navigate. By default, it shows all added torrents, but you can use the pull-down menus to view torrents as per their download state or trackers. When you right-click on a listed torrent, it displays a context menu.

Once a torrent has been downloaded, as per our configuration, it'll automatically be moved to the publicly accessible /mnt/usb/public/downloads/complete directory. The easiest way to access a share in Linux is to enter its address in the default file manager. Most modern Linux file managers, such as Gnome's Nautilus, support Samba. Launch Nautilus and press [Ctrl] + [L] to access the address bar.

Now enter smb:// followed by the IP address of the RPi running Samba. In our case, this would besmb://192.168.3.100. To access a particular share, you can also append its name to the end of the address, such as smb://192.168.3.100/documents.

Or you can mount the share from the command line:

# mount -t cifs -o username=pi,password=raspberry //192.168.3.100/usb/downloads/mnt/downloads

To mount this share automatically, you can add it to the /etc/fstab file:

//192.168.3.100/usb/downloads /mnt/downloads cifs username=pi,password=raspberry 0 0

You can map the remote USB device on a Windows box. On a Windows 10 PC open File Explorer and navigate to This PC. Click the Map Network Drive button. In the window that pops up, select a drive letter, and enter the location of the network folder. Here, this would be something like192.168.3.100\usb\videos. You'll be asked for your authentication information, and then the drive will show up in File Explorer.

Accessing shares on an Android device

Search and install

1. Search and install

The Android Play store is full of file managers that can work with Samba shares. We use the popular ES File Explorer File Manager.

Configure

2. Configure

In the app, change View from Local to Network > LAN, and then tap the + button to enter your Pi's IP address and folder share into the Server box, plus your user connection and authentication details.

Browse and stream

3. Browse and stream

Depending on your permissions, you'll be able to download, upload and stream files and access public and private areas on the share.










No comments:

Post a Comment