DIY Raspberry Pi based Media Box

This was posted on 2023-07-08

This project is focused on building a system for my Raspberry Pi so that it can do the following things.

Setting up the Raspberry Pi

I initially got started with a new Raspbian lite OS and connect through SSH

sudo apt-get update
sudo apt-get upgrade

Mounting USB Drives

Check the drives currently mounted

sudo cat /proc/mounts


sudo fdisk -l

and look for the partition starting with /dev/ that has the expected file size, e.g. /dev/sda/.

sudo mkdir /mnt/MediaBox
sudo mount /dev/sda1 /mnt/MediaBox

Gety UUID to Set up Automatic Mounting

sudo blkid
sudo nano /etc/fstab

Add following line with UUID, mount location and file system type 

UUID=27705CE4072C15B2 /mnt/MediaBox ntfs defaults,auto,users,rw,nofail 0 0

Setting up a location to maintain server files

mkdir server
cd server

mkdir static
mkdir templates

Setting static IP

hostname -I
ip r
grep 'namesever' /etc/resolv.conf

nano /etc/dhcpcd.conf

Then appended the following lines to the end of the file

interface wlan0
static ip_address=
static routers=
static domain_name_servers=

interface eth0
static ip_address=
static routers=
static domain_name_servers=
sudo reboot

Setting up NAS (Samba server)

sudo apt-get install samba samba-common-bin
sudo nano /etc/samba/smb.conf
path = /mnt/MediaBox
create mask=0777
directory mask=0777

path = /home/pi
create mask=0777
directory mask=0777

Creating user account with username 'pi' and password 'raspberry'

sudo smbpasswd -a pi
sudo systemctl restart smbd

Setting up Transmission

sudo apt-get install transmission-daemon

Setting up transmission

sudo systemctl stop transmission-daemon
sudo nano /etc/transmission-daemon/settings.json

How to Setup Transmission on the Raspberry Pi - Pi My Life Up

Install PyEnv

sudo apt install git
curl | bash
sudo apt-get install --yes libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libgdbm-dev lzma lzma-dev tcl-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev wget curl make build-essential openssl

pyenv install 3.7.12
cd /mnt/MediaBox/MediaBox
pyenv local 3.7.12

Setting up Flask Server

sudo apt-get install python3-flask

pip install schedule -y
pip install logger -y
pip install Flask -y
pip install telepot -y
pip install Pillow -y
pip install feedparser -y
pip install numpy -y
pip install opencv-python -y
pip install urllib3 -y
pip install imaplib2 -y
pip install transmission-rpc -y
pip install openai -y
pip install MySQL-python -y
pip install dirsync -y

Setting up TensorFlow Lite (May not be needed)

sudo apt update
sudo apt upgrade -y

curl | sudo apt-key add -
echo 'deb coral-edgetpu-stable main' | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
sudo apt-get update

sudo apt install python3-tflite-runtime libatlas-base-dev

Setting up TensorFlow 2

Guide: Sam Westby

sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran libgfortran5 libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev liblapack-dev cython3 libatlas-base-dev openmpi-bin libopenmpi-dev python3-dev

sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran libgfortran5 libatlas3-base libatlas-base-dev libopenblas-dev libblas-dev liblapack-dev cython3 libatlas-base-dev openmpi-bin libopenmpi-dev python3-dev

pip install -U wheel mock six
uname -m
cat /etc/os-release
python -V


PRETTY_NAME='Raspbian GNU/Linux 11 (bullseye)'
NAME='Raspbian GNU/Linux'
VERSION='11 (bullseye)'
Python 3.9.2

Wheels: Tensorflow-bin/previous_versions at main · PINTO0309/Tensorflow-bin · GitHub

sudo chmod +x
pip install tensorflow-2.5.0-cp37-none-linux_armv7l.whl

Depreciation Errors

pip install protobuf==3.20.*

pip uninstall h5py
pip install h5py

sudo apt-get install libopenjp2-7

SQL Database

Guide: Setup a Raspberry Pi MYSQL Database - Pi My Life Up

sudo apt install mariadb-server

Set Authorization

sudo mysql_secure_installation


The program was written in Python. The momst up to date file can be seen on my Github: iamJohnnySam/MediaBox (

Below are some highlights

Checking Email

class CCTVChecker:
    folderSize = 0
    output = 0.000001
    outlook = imaplib.IMAP4_SSL('', 993)

    def __int__(self):
        for f in os.listdir(settings.cctv_download):
            self.folderSize = self.folderSize + os.path.getsize(os.path.join(settings.cctv_download, f))
        print('Current Folder size -', '{:,}'.format(self.folderSize))

    def log_in(self, mb):
            pass, readonly=False)

    def get_attachment(self, data):
        msg = email.message_from_bytes(data[0][1])
        date = msg['Date']
        date = date.replace(' +0530', '')

        att_path = 'No attachment found.'
        for part in msg.walk():
            if part.get_content_maintype() == 'multipart':
            if part.get('Content-Disposition') is None:

            filename = part.get_filename()
            save_as = date + ' ' + filename
            att_path = os.path.join(settings.cctv_download, save_as)

            image = part.get_payload(decode=True)
            fp = open(att_path, 'wb')

  , photo=open(att_path, 'rb'))
  , 'Sus lvl ' + str(self.output))

            if sus:
                self.folderSize = self.folderSize + os.path.getsize(att_path)
                print(filename, self.output, '{:,}'.format(self.folderSize))

    def scan_mail(self, scan_type, get_attach, delete):
        (result, messages) =, scan_type)

        if result == 'OK':
            for message in messages[0].split():
                    ret, data = self.outlook.fetch(message, '(RFC822)')
                    print('No new emails to read.')

                if get_attach:

                if delete:
          , '+FLAGS', 'Deleted')

    def send_email(self, send_to, body):
        msg = MIMEMultipart()
        msg['From'] = settings.em
        msg['To'] = ', '.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = 'Suspicious Activity'

        msg.attach(MIMEText(body, 'html'))

        for f in os.listdir(settings.cctv_download):
            with open(os.path.join(settings.cctv_download, f), 'rb') as fil:
                part = MIMEApplication(, Name=basename(f))
            part['Content-Disposition'] = 'attachment; filename='%s'' % basename(f)

        context = ssl.create_default_context()
        server = smtplib.SMTP('', 587)
        server.sendmail(settings.em, send_to, msg.as_string())

    def run_code(self):
        self.scan_mail('UnSeen', True, True)

        if self.folderSize > 0:
        send_to = [settings.email1, settings.email2]
        body = '''Hi,

               We detected some activity on the CCTV in the last 24 hours


        self.send_email(send_to, body)

Flask Boilerplate code

from flask import Flask, render_template

app = Flask(__name__)

def index():
    return render_template('index.html')

def movies():
    return render_template('movies.html')

ChatGPT for writing starter code:


Similar Posts

See other projects and posts in the same category as this post

blog item Creating a Personal Assistant to automate my lifestyle
Designing, Building and Programming a personal assistant to automate my lifestyle.
python, machine learning, programming, smart home 2024-01-13 | Read More....
blog item DIY Raspberry Pi based Media Box
My effort at creating a system to unify the media around my house
raspberry pi, ssh, media 2023-07-08 | Read More....
blog item Home-made Raspberry Pi based Home Security System
A home made security solution that can be customized and expanded as needed
Raspberry Pi, Project, Home Security 2022-02-25 | Read More....
blog item Home-made Raspberry Pi based Media Center
A homemade solution for a networked media storage and madia playback
Raspberry Pi, Kodi, LibreELEC, Samba, Network Storage 2020-10-30 | Read More....
Comment Box is loading comments...