Raspberry Pi TV server using TVheadend

I have a projector out in the theatre room, however, this same room does not have any coax run for TV reception. Nor does the projector have a tuner for Digital TV. Thus the idea for the RPi TV streamer! I could have easily gone to the local tech shop and purchased a tuner and ran some coax, but I already have an android device hooked up to the projector, why not try and stream TV to it locally in the house. My own IPTV service of sorts.

I do want to give a bit of a shout out to CWNE88, (Go follow him if you get a chance!) This is the guy that got me started streaming TV via the Raspberry Pi, however he concentrates a lot on multicast streaming and to be honest, most of us do not have the infrastructure to handle multicast traffic on our LAN. So I had to find an alternative option that actually worked. (dvblast works great…if you have suitable switches/routers to handle multicast) Anyhow, during my research, I stumbled across TVheadend. I learned pretty quickly that it could be installed on a Raspberry Pi and be used to stream tv via HTSP. (Home TV Streaming Protocol) HTSP is a TCP based protocol and works in a unicast type fashion without killing your home network.


This is the hardware you will require:

  • Raspberry Pi 3 B+ (This is what we tested with)
  • USB Extension Cables
  • Good quality 5v power supply for RPi
  • USB TV Tuner (We used Avermedia Volar Green HD @ $29ea)
  • Splitter (We used a 3-way splitter to really push the RPi3B+)
  • F-type adapters
  • USB to Ethernet Adapter (Optional)


Prerequisites

I am going to assume you know how to get Raspbian “Stretch” Lite onto your Raspberry Pi. If not check out the “Prerequisites” of this post here. It will walk you through, downloading the image, using Etcher to put the image onto an SD card, setting up the RPi via Raspi-config, and enabling SSH. Once you have completed those steps, come back here and continue.

The end state of the “Prerequisites” is to have access to your Raspberry Pi via SSH and have the command line up and in front of you.


For those confident on the RPi

For those of you who want to skip straight to the chase, the code below outlines all the commands issued to the RPi to install TVheadend. I will then take you step by step through these commands below:


Step 1: Update/Upgrade the Raspberry Pi

First things first, you need to ensure your Raspbian repositories are up to date. Run the Following commands:

sudo apt-get update

sudo apt-get upgrade
Select ‘Y’ and go get a coffee. This process can take 5-10 minutes.


Step 2: Install TVheadend server

Now that your Raspberry Pi is fully up to date, you need to run the following commands:

sudo apt-get install dirmngr

This command may not need to be run, however, for me, TVheadend would not install without it.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61

More info about installing the GPG keys can be found here.

echo "deb https://dl.bintray.com/mpmc/deb raspbianstretch stable-4.2" | sudo tee /etc/apt/sources.list.d/tvheadend.list

Again, more info on where to get the correct repo for the RPi ishere.
Check and make sure the correct one is used. This can update at times.

sudo apt-get update

In a nut-shell, we are telling ‘apt-get’ to grab any updates from the repository we added above. This will then allow us to install TVheadend because it now knows where to ‘grab’ it from.

sudo apt-get update

In a nut-shell, we are telling ‘apt-get’ to grab any updates from the repository we added above. This will then allow us to install TVheadend because it now knows where to ‘grab’ it from.

sudo apt-get install tvheadend

Select ‘Y’ and allow TVheadend to install. If you tried to run this command without doing the steps above, you would run into errors.

During the install, you will be asked to set an administrator username and password. Be sure to set this correctly and remember. You do not get two chances to enter the password. I learned this the hard way!

If for some reason, you did stuff up the username and password. Uninstall tvheadend with sudo apt-get purge tvheadend and sudo apt-get autoremove Then reinstall tvheadend again.


Step 3: Log into TVheadend

Now that your Raspberry Pi is fully up to date, you need to run the following commands:

Open up a web browser and enter the IP address of your Raspberry Pi. Also, use the port number ‘9981’. For example http://192.168.1.168:9981 (Your IP address for the RPi will be different.) Type ifconfig in the terminal screen to confirm.

When you first open Tvheadend, you will get the Wizard which will step you through setting up the tv tuners. Hopefully, you left them plugged in from the start. If not, plug them in and restart the Raspberry Pi. Go back into the web browser and log back into TVheadend.


Step 4: Configure TVheadend for streaming

The configuration for TVheadend settings post is here.

I have no doubt, however, if you got this far, you will be able to bumble your way through the configuration. This was the easiest part. I then used VLC to watch the streams. You can get these links from inside Tvheadend.

If you want to consume some TV from your iOS device, I can confirm that the tvhclient app works a treat. No nonsense setup, easy channel browsing. It can be used in conjunction with the VLC app to stream video easily.



Video: Example Hardware Configuration


Streaming and graphing real time data with the Raspberry Pi

So you have a fancy little sensor/contraption and its working great, the system is portable maybe even in a waterproof case or is mobile with no room for a display. However, how do you know the quality of your data or if the system is working without carrying a display or connecting up an SSH session? A black and white terminal impresses no one and is a terrible way to view trends in data.

There is an answer and it is fantastically flexible. You turn your Raspberry PI into a wireless access point and web server. Then with a little HTML, JavaScript, JSON and the chart.js library, you have a clean and dynamic interface for your project.


The particular project in this example uses the following hardware:


When assembled and strapped to a buoy anchored in the swan river it is capable of logging and analysing the motion and frequency of waves. This data is collected as part of the research for Woodside’s River Lab run in partnership with UWA’s Oceans Institute. Here you can see the real-time data stream from the buoy in a lab environment.


The Sensor

The BNO055 sensor performs all of the internal calculation and compensation to output clean acceleration and heading information, however as a demonstration to students and visitors the data is hard to visualise when it must be copied and charted from a CSV file. To improve the accessibility of the data and increase its value this tutorial will utilise several scripting & markup languages and several key libraries:

  • Python
  • HTML

  • CSS
  • JavaScript


Configure the Raspberry Pi

To configure the Raspberry Pi and the website there are several steps. Keep in mind any existing configuration or modifications to a vanilla Raspbian Jessie install may have unforeseen ramifications to this tutorial. Thankfully for my fingers many of these steps are already well documented, the links below will take you to the tutorials I used for some of these steps.

  1. Install Apache Web Server
  2. Configure Raspberry Pi as an Access Point
  3. Modifying your python script
  4. Configuring your script to run on boot
  5. Writing a webpage with chart.js

Having configured your Raspberry Pi as a hot spot web server as per tutorials 1 & 2 above, you should be able to load your web page on a laptop or mobile phone connected to the access point according to the IP address you configured in the second tutorial. If you followed the tutorial verbatim this is 172.24.1.1 although in my application I configured my /etc/network/interfaces AND /etc/dnsmasq.conf files with the address 192.168.0.1 as that is common across other hardware we develop.


Modifying your python script

To improve the portability of our data and leverage existing data handling tools built into python and JavaScript we need to output our sensor data as a JSON file. It is a simple and human readable file format for structuring data reminiscent of a dictionary/list within many languages. Python has some built-in functions that make it easy to output a dictionary as a JSON file. In this example, we want to create one file that details the current sensor reading as well as the calibration state of the sensor.

Most importantly, our JSON data is written to the directory for our web server /var/www/html/whatever.JSON  This means our web server can share the file when it is requested by a client. Any JSON file can be placed and requested natively via http://192.168.0.1/whateverTheFileNameIs.JSON this is great for viewing configuration information and system states without the additional effort of writing and formatting a full web page.

JSON can be viewed directly in the browser with no html required. Great for checking system states

Running the script on boot allows for truly headless operation. Although some form of visual feedback that the code is running, such as flashing an LED, will save any head scratching should the Pi fail to boot or script fail. To run the script on boot we will be modifying the /etc/rc.local file again with another line.

 (sleep:10;python /whatever/the/file/path/is/script.py)& 

As all operations within the rc.local file are already executed with privileges you will not need to include sudo in the command. Full copies of the source code used in this example can be found here.

A word of caution. As you are writing & reading the same file concurrently your code must be tolerant of not being able to access the file on occasion.

Now we have everything configured on the raspberry pi and our data is streaming we can start to build our chart.js data interface.


Andrew Van Dam

An electronics expert based in Australia dedicated to creating great tools for the maker community.


A Brief History of the Raspberry Pi

Who would have thought a US$35 computer would change our lives so much. The Raspberry Pi foundation have changed the way we learn, teach and innovate by bringing a hardware platform which is cheap enough for the ‘average joe’ yet powerful enough to drive some of our most ambitious ideas. I have no doubt in my mind that I have left some info out here. Please feel free to leave a comment below to educate me a little bit! happy for input. I’ll keep updating this timeline as new releases come out. Thanks.



Some additional credits for the pics and info: Adafruit.com, Raspberry Pi Foundation, elinux.org

Who would have thought a US$35 computer would change our lives so much. The Raspberry Pi foundation have changed the way we learn, teach and innovate by bringing a hardware platform which is cheap enough for the ‘average joe’ yet powerful enough to drive some of our most ambitious ideas. I have no doubt in my mind that I have left some info out here. Please feel free to leave a comment below to educate me a little bit! happy for input. I’ll keep updating this timeline as new releases come out. Thanks.


The Alpha Boards

Announced in August 2011, the ‘Alpha’ boards were among the first revisions. The Board itself was of a large format for debugging, was populated with headers for GPIO, JTAG, DSI, CSI and switches for LED and I/O testing.


Raspberry Pi First Production Board

Announced in 29th February 2012, The first of the “credit-card” sized computers was released. The initial batch of 10,000 (All Model-B’s) which were manufactured in China.


Raspberry Pi Model B

The RPi full production board – Model B Rev 2.0 was announced in April 2012, first orders were not sent out until June 2012. Manufacturing and orders were made via RS and Farnell. This meant that higher volumes of units were produced and made available. September 2012 saw the announcement of Model B rev 2.0 also being manufactured by Sony.


Raspberry Pi Model A

As of February 2013, the Model A was made available from distributors in Europe. The board had 256MB of RAM rather than the originally planned 128MB. The RPI foundation produced a small Beta batch of boards before handing over manufacturing to RS and Farnell. The Model A boards use an identical Rev 2.0 PCB but come with a different selection of components fitted.


Raspberry Pi Model B+

Announced on the 14th July 2014 The Model B+ was the first of the new layout. The B+ featured a new and faster processor, new VideoCore, 512MB RAM, Micro SD, 4 x USB slots, HDMI and many other upgrades.


Raspberry Pi Model A+

A few months after the release of the B+, the RPi foundation released a lower-cost variant labeled ‘A+’, At a new low price point of US$20 it was a smaller form and only a few components had been left out. But from an IoT perspective, it was still a very powerful unit for embedded hardware projects.


Raspberry Pi 2 Model B

Announced n 2nd February 2015. The RPi 2 Model B+ retained the same US$35 which was key to the continued success of the platform. The RPi 2 featured another upgrade to the CPU and an upgrade to 1GB of RAM. Other changes were also made.


Raspberry Pi Zero

The Raspberry Pi Zero was released in November 2015. The idea behind the Zero was a cheaper/smaller form factor that could be used to embed inside smaller projects. On 28th February 2017, the foundation then released the RPi Zer ‘W’ which had built-in wireless and Bluetooth.


Raspberry Pi 3 Model B

Another upgrade to the CPU (1.2Ghz 64-bit quad-core ARM Cortex-A53) was announced with a new release of the RPi 3 Model B on the 29th February 2016. Again the Raspberry Pi foundation achieved the same US$35 price point.


Raspberry Pi 3 Model B+

Announced in “Pi Day”, 14th March 2018. The Raspberry Pi 3 Model B+ is an incremental improvement over the previous RPi 3 for the same US$35 price.


Some additional credits for the pics and info: Adafruit.com, Raspberry Pi Foundation, elinux.org