Using Plex Media Server on a Raspberry PI 3

I am currently experimenting to create a cheap Plex Media Server running on a Raspberry Pi 3!

I am aiming to share my media to my chromecast and my smartphone. So that I can play my music everywhere or turn off my PC when watching a TV Show.

I followed this guide:  https://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/blog/2016/03/11/a-more-powerful-plex-media-server-using-raspberry-pi-3.

Install Raspbian

Raspbian is easily installed! Download the latest lite version: https://www.raspberrypi.org/downloads/raspbian/.

On windows, I prefer Etcher to burn the image on an SD card This may help you if you don’t know how to do it.

After loading the image, connect your usb keyboard and hdmi monitor so you can get started with your Pi 3! Now connect the power and see it boot up!

SSH not working?

Already twice I’ve experienced SSH not working with a Connection refused exception in putty.

The problems lies with the host certificate used for ssh and the solution is to remove them and generate them again:

sudo rm /etc/ssh/ssh_host_* && sudo dpkg-reconfigure openssh-server

Ntfs

To use ntfs usb stick I had to install ntfs-3g:

sudo apt-get install ntfs-3g

Then I could use ntfs-3g as a type:

Spin down the hard disk

I tried this guide but unfortunately my old external hard drive doesn’t really support spinning down. I am also not certain if it really ads value.

Let’s install Plex!

Best to begin with updating your Raspbian installation!

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get update && sudo apt-get dist-upgrade

Install Plex:

sudo apt-get install apt-transport-https -y --force-yes
wget -O - https://dev2day.de/pms/dev2day-pms.gpg.key | sudo apt-key add -
echo "deb https://dev2day.de/pms/ jessie main" | sudo tee /etc/apt/sources.list.d/pms.list
sudo apt-get update
sudo apt-get install -t jessie plexmediaserver -y

Now restart your PI:

sudo reboot

After figuring out your ip, you should be also to surf to Plex: 192.168.0.3:32400/web and register your new server!

Sources

Create a connected temperature sensor with a Raspberry PI

Since the Raspberry PI was introduced in 2012, it’s so much easier to build the Internet of Things! Connecting sensors and exposing the values they read, so much fun!

Time to get our hands dirty! In the coming months, I will stuff my house with sensors to gather data. Motion, temperature, barometric pressure and any other sensors I may come across?

In this post I’ll explain how to connect a digital temperature to a Raspberry PI (1B) and expose this via a NodeJS api.

Prerequisites

Raspbian

Raspbian is the preferred operating system for experimenting with your PI. It’s forked from Debian and is optimized for all the Raspberry PI models. For this project you should download the Lite version, as a GUI is not required at all.

On Windows, I usually create the bootable SD card via Win32 Disk Imager.

SSH

After installing the SD card you can boot the PI. Make sure yours is connected with a screen and keyboard so you can enable SSH (so that you can access it via terminal from another computer).

Login with the default Raspbian user account (pi – raspberry) and enable SSH via raspi-config:

sudo raspi-config

Select Interfacing Options, then P2 SSH, then enable and reboot.

Remote access

Now that SSH is enabled you can access it via Putty. FilleZilla also supports SSH to move files from and on it!

Enabling services

We will also need to enable the I2C interface (to communicate with our temperature chip). You can easily do this with raspi config:

sudo raspi-config

Select Interfacing Options, then P5 I2C, then enable.

Install the sensor

Now to fun part! Connecting the Sparkfun TMP102 with your PI!

Raspberry Pi TMP102
3v3 Power VCC
Ground GND
SDA SDA
SCL SCL
Ground ADD0

Make sure to connect the correct pins on your Raspberry PI, by googling the pin layout of your Raspberry PI! This is how mine looks:

Read the sensor data

The following Python script will read the temperature (in Celcius) and print the value to the terminal.

tmp102.py

#!/usr/bin/python
import smbus

I2C_BUS = 1
bus = smbus.SMBus(I2C_BUS)

# 7 bit address (will be left shifted to add the read write bit)
DEVICE_ADDRESS = 0x48

# Read the temp register
temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS, 0)
temp_low = (temp_reg_12bit & 0xff00) >> 8
temp_high = (temp_reg_12bit & 0x00ff)

# Convert to temp from page 6 of datasheet
temp = (((temp_high * 256) + temp_low)>>4)

# Handle negative temps
if temp > 0x7FF:
 temp = temp-4096;
temp_C = float(temp) * 0.0625
print "%3.2f" % (temp_C)

If you want to execute it, first mark it executable:

chmod +x tmp102.py

And now:

./tmp102.py

Create the api

Install Node.js

We’ll be using Node.js to display the temperature over an api!

Install Node.js:

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt install nodejs

See if it’s installed and the version:

node -v

Add the Node.js files

package.json

#package.json
{
 "main": "index.js",
 "author": "Elias Lecomte <elias.lecomte@gmail.com>",
 "dependencies": {
  "express": "^4.14.1"
 }
}

And an Express app:

server.js

"use strict";

const http = require('http');
const express = require('express');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;

// Express app
const app = express();
app.get('/', (req, res) => {

// Get raw temp data
 let tempRaw = execSync(__dirname + '/tmp102.py').toString();

// Parse raw temp data
 let temp = parseFloat(tempRaw.split('\n')[0]);

// Response
 res.json({
 temperature: {
 value: temp,
 description: 'Temperature in Celcius'
 }
 });
});

app.listen(80);

Now let’s download the required dependencies:

npm install

We can now start our Node.js app:

node index.js

You can now see the results by surfing to the ip (when you are on the same network):

Make it start at boot

The last bit. We want to start our Node.js api when the Raspberry PI boots. I found the easiest way to add the startup command to /etc/rc.local.  This script is run at the end of the boot cycle.

su pi -c 'sudo node /home/pi/server/server.js < /dev/null &'

Now just reboot your pi (sudo reboot -n) and your Node.js app should start when the boot is finished! Enjoy!!

Sources

Raspbian cheat sheet

My own cheat sheet for commands I often use. It’s a work in progress.

See CPU Temperature
/opt/vc/bin/vcgencmd measure_temp
List hard disks
sudo blkid
Reboot
sudo reboot -n
Shutdown
sudo shutdown -h now
Upgrade your installed packages
sudo apt-get update
sudo apt-get upgrade
See available disk space
df -h

Detect motion with a Raspberry PI

It’s relatively easy to detect motion with a PIR sensor connected to your Raspberry pi’s GPIO.

This is merely an overview of how to write the python script. For more details on how to connect the sensor, read this.

When you have Python V2 you can follow this, check with python -V.

sudo apt-get update
sudo apt-get install python-gpiozero python-pkg-resources python-dev

Now that you have all dependencies you can create a python script like:

#!/usr/bin/python
from gpiozero import MotionSensor

pir = MotionSensor(4)
while True:
    if pir.motion_detected:
        print("Motion detected!")

Or you could just print if there is motion once:

#!/usr/bin/python
from gpiozero import MotionSensor

pir = MotionSensor(4)
print pir.motion_detected

Now make the script executable:

chmod +x script.py

Now you can execute the script:

./script.py

Mount a usb drive or hard disk on Raspbian

What’s the best way to auto mount a usb drive or hard disk? What if the filesystem is ntfs?

To get a list of connected usb disks you can:

sudo blkid

In the screenshot above you can see there are two usb devices:

  • /dev/sda1: UUID=”E89484EA9484BC96″ TYPE=”ntfs” PARTUUID=”008ffb75-01″
  • /dev/sdb1: LABEL=”Extern station” UUID=”92FA278AFA2769A5″ TYPE=”ntfs” PARTUUID=” 0009a8db-01″

To be able to use these devices, you have to mount them in a folder. Let’s create two folders:

sudo mkdir /mnt/usb1
sudo mkdir /mnt/hd1

The usb1 folder I’ve created for my usb stick. The hd1 folder for my external hard drive.

We need to take ownership of these folders:

sudo chown -R pi:pi /mnt/usb1
sudo chown -R pi:pi /mnt/hd1

If any of the usb devices is formatted in ntfs, you best install ntfs-3g:

sudo apt-get install ntfs-3g

Now let’s edit our file system table, so that the usb stick & external hard drive are mounted every time the system boots:

sudo nano /etc/fstab

As you can see in the screenshot above I’ve used the disk UUID as name to find it. This way, if you unplug your usb flash drive and put it in another usb port, it still works. As type, make sure to use ntfs-3g.

Now you just need to reboot and the disks are both mounted!

sudo reboot

For completeness, you could also mount the usb stick just once with this:

sudo mount -o uid=pi -o gid=pi -t ntfs-3g /dev/sda1 /mnt/usb1

Sources

 

Raspbmc doesn’t update

For some reason, xbmc and raspbmc didn’t auto update anymore on my raspberry pi (running Raspbmc) since april this year.

Usually when you reboot your device, auto update makes sure your raspbmc is using the latest version.

By deleting some files, you can trigger your pi to update again on the next restart. This will probably solve your problem if you have the same issue:

  1. sudo -s
  2. cd /scripts/upd_sys
  3. rm *.sh
  4. wget http://svn.stmlabs.com/svn/raspbmc/release/update-system/getfile.sh
  5. wget http://svn.stmlabs.com/svn/raspbmc/release/update-system/cdn_env_prep.sh
  6. cd /scripts/upd_hist
  7. rm xbmcver
  8. reboot

Source

Update CouchPotato on Raspbmc

If you want to update CouchPotato just execute the following commands in the bash shell:

  1. sudo /etc/init.d/couchpotato stop
  2. cd CouchPotatoServer (or cd CouchPotato, whatever folder you installed CouchPotato in).
  3. git pull
  4. sudo /etc/init.d/couchpotato start

Even after starting, it might take 60 seconds before your site responds to requests.

 

BitTorrent Sync on raspbmc

BitTorrent Sync is an open source tool to create a Dropbox-like file-syncing service. You can share a folder with anyone. Files are stored on the server (which can be your raspberry pi) and on all the other computers that you gave a secret key.

Install

You can install it by following this guide: http://jack.minardi.org/raspberry_pi/replace-dropbox-with-bittorrent-sync-and-a-raspberry-pi/

Auto start

You want BitTorrent Sync to start automatically when your raspberry pi starts. To do so we will create a script to help us. It will contain the logic and have a start and stop parameter.

Create it with:

sudo nano /etc/init.d/btsync

Paste this:

#! /bin/sh
# /etc/init.d/btsync
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
    /home/pi/.btsync/btsync
    ;;
stop)
    pkill btsync
    ;;
*)
    echo "Usage: /etc/init.d/btsync {start|stop}"
    exit 1
    ;;
esac

exit 0

You need to change the permissions:

sudo chmod 755 /etc/init.d/btsync

You can test the script by stopping and starting it, just for fun:

sudo /etc/init.d/btsync stop
sudo /etc/init.d/btsync start

And at last add it to the boot process:

sudo update-rc.d btsync defaults

Raspbmc: iptables

By default, all requests arriving at the pi, coming from the internet (WAN), are blocked. Only requests coming from your local LAN are allowed.

To do this, Raspbmc uses iptables. It’s like a firewall, and uses a set of rules to determine if a request has to be blocked or not.

Interested to see your current iptables rules? Use this command:

iptables -L -n

If you want to allow a port, to be not blocked, you have to add this to your iptables. The following bash command is an example that allows all tcp requests to the port 5050:

iptables -A INPUT -p tcp --dport 5050 -j ACCEPT

However, every time you restart your pi, this rules are flushed or emptied. That’s why we need to add them to a script, that gets executed on every boot. That script is: /etc/network/if-up.d/secure-rmc.

Use:

sudo nano /etc/network/if-up.d/secure-rmc

Change/add the ports you want open, to the script. I’ll add 5050, 9091 & 8888. That’s for couchpotato, transmission & BitTorrent Sync. You’ll find the block at the end of the script.

if [ "$IFACE" != "lo" ]; then
    NETMASK=$(get_subnet $IFACE)
    if [ ${#NETMASK} -eq 0 ]; then
        logger -t secure-rmc "netmask not found"
        exit 1
    fi
    iptables -A INPUT -s $NETMASK -i $IFACE -j ACCEPT
    iptables -A INPUT -p tcp --dport 5050 -j ACCEPT
    iptables -A INPUT -p tcp --dport 9091 -j ACCEPT
    iptables -A INPUT -p tcp --dport 8888 -j ACCEPT
    iptables -A INPUT -i $IFACE -j DROP
fi

Resources

My raspberry pi

The raspberry pi is an awesome device. It’s affordable, it’s a media center, a web server, very slow and doesn’t use much electricity. It’s also a very good looking piece of hardware. Fits your living room!

pi-1

pi-2

This post is about how mine is configured!

The services

  • Xbmc (installed as raspbmc): a powerful media center, running on Debian.
  • Transmission: A minimalistic torrent client.
  • CouchPotato: Software that searches the movies you want and starts to download them as soon as they come available.
  • BitTorrent Sync: Dropbox like service, easy way to backup folders on multiple computers.
  • Samba: Share a hard disk to the local network.

The place

My pi lives next to my TV. It’s powered by a 5V 2A power supply. It has a 8 gb sd card and their is a 2 TB external usb hard drive attached.

Connected

I use Putty to modify raspbmc via ssh, Yatse android app to control Xbmc. When needed I can connect an external keyboard because I still have 1 usb port over.

Security

As you could possibly open up some ports to the internet, you will want to secure your raspberry pi.

Install

I won’t list the complete process but refer to guides I followed.

0. Static IP

http://choorucode.com/2012/08/14/how-to-assign-a-static-ip-address-to-raspbmc/

1. Raspbmc

First we need to install raspbmc on the pi: http://www.raspbmc.com/wiki/user/windows-installation/.

2. CouchPotato, Transmission

3. BitTorrent Sync

4. Enable samba

You can do this in the xbmc interface.

5. Dynamic Dns

http://www.techjawab.com/2013/06/setup-dynamic-dns-dyndns-for-free-on.html

XBMC tweaks

  1. By default, if there is no hdmi connected on boot, it won’t enable hdmi. That’s ridiculous!