Tutorial „upnp Server mit Wireless Access Point“ für Raspi 3

Ein am Raspi angeschlossener USB Stick ist die Quelle einer MP3 Sammlung
welche über WLAN an verschiedene upnp/dlna fähige Geräte weitergegeben wird.

Installation und konfigurieren von MiniDLNA (upnp Server)

automatisches einbinden (mounten) von USB Sticks

Installation und konfigurieren von Samba (Zugang von Windows auf den USB Stick)

Installation und konfigurieren von rdp (Remotedesktopverbindung von Windows herstellen)

Installation und konfigurieren von dnsmasq hostapd (ACCESS Point Modus (Router)

Voraussetzung:
Raspberry Pi 3 Model B
Image with PIXEL desktop based on Debian Jessie
hier wurde verwendet „2017-04-10-raspbian-jessie.zip”.
Quelle: https://www.raspberrypi.org/downloads/raspbian/
Mit dem Tool „USB Image Tool.exe“ das Images auf die SD Karte schreiben.
Quelle: http://www.alexpage.de/usb-image-tool/download/

Die SD Karte in den „Raspberry Pi 3 Model B“ stecken, HDMI, USB-Tastatur, USB-Maus
sowie LAN-Kabel (mit Internetzugang) anbringen. Power ON -> … eindeutschen …

### Raspi aktualisieren ###

sudo apt-get update


### MiniDLNA ###

MiniDLNA will allow streaming of video, TV, and pictures from the external hard drive
to other devices in your home. Perfect for NZBGet and SickBeard

sudo apt-get install minidlna

sudo chown minidlna:minidlna /var/lib/minidlna

Create log and cache files for MiniDLNA, edit the conf file and restart.

sudo mkdir /var/{cache,log}/minidlna

sudo chown minidlna:minidlna /var/{cache,log}/minidlna

Edit the config file

sudo nano /etc/minidlna.conf

# Pfad zur MP3 Sammlung
media_dir=A,/media/usbstick
# Pfad zur Datenbank
db_dir=/var/cache/minidlna
# Pfad zu den log Dateien
log_dir=/var/log
# automatisch nach neuen MP3 scannen
inotify=yes
# Scanninterval definieren
notify_interval=300
# IP Adresse des Raspberry eintragen
listening_ip=172.24.1.1
# Root Ordner im Client definieren, M=Musik ...
root_container=M
# Anzeigename deines upnp/dlna Servers vergeben
friendly_name=Mein_MP3_Server


Add this to the end of the /etc/sysctl.conf file to fix some issues

sudo nano /etc/sysctl.conf

# minidlna server tweaks
fs.inotify.max_user_watches = 10000
# end of minidlna server tweaks



sudo nano /proc/sys/fs/inotify/max_user_watches

hier nur den Wert von

10000

eingeben (wie schon in /etc/sysctl.conf)


sudo service minidlna force-reload
sudo service minidlna start



sudo nano /etc/init.d/minidlna

die Zeile USER=minidlna suchen und ändern zu

USER=root


In einen Browser den Status von MiniDLNA abfragen 127.0.0.1:8200



### USB Stick automatisch einbinden ###

Step 1
Zunächst installieren wir Treiber, damit NTFS und HFS+ Speichermedien eingebunden
werden können.

sudo apt-get -y install ntfs-3g hfsutils hfsprogs exfat-fuse

Step 2
Wir legen nun einen Ordner im Verzeichnis /media an,
in den das USB-Speichermedium später eingebunden wird (Mountpoint genannt).
In meinem Fall nenne ich den Ordner usbstick.
Der Name kann frei gewählt werden, sollte aber keine Sonder- und Leerzeichen enthalten.

sudo mkdir /media/usbstick

Step 3
Nun führen wir folgenden Befehl in der Konsole aus und stecken anschließend das USB-
Medium an.
Daraufhin sollte in der Konsole eine Ausgabe aller angeschlossenen Speichermedien
erscheinen.
USB-Sticks heißen meist sda und externe Festplatten hd, ggf. mit anhängender Nummer.
Uns interessiert die erste und letzte Spalte des richtigen Gerätes mit dessen device Pfad
und UUID.

sudo blkid -o list -w /dev/null

hier am Beispiel : Frank USB 128 GB USBstick

/dev/sda1 vfat 128GB /media/pi/128GB 0C00-0B44

Step 4
Nachdem wir nun den device Pfad des USB-Speichermediums kennen und einen
Mountpoint erstellt haben,
können wir den USB-Stick oder die USB-Festplatte mit folgendem Kommando einbinden.
Dabei muss man je nach Dateisystem ein anderes Kommando verwenden,
sda durch den Namen des USB-Speichermediums und /media/usbstick/ durch den
Mountpoint ersetzt werden.
Die zwei pi Angaben müssen, sofern ein anderer Benutzer als pi auf das USB-
Speichermedium zugreifen soll,
durch dessen Namen ersetzt werden.

FAT32
sudo mount -t vfat -o utf8,uid=pi,gid=pi,noatime /dev/sda /media/usbstick

NTFS
sudo mount -t ntfs-3g -o utf8,uid=pi,gid=pi,noatime /dev/sda /media/usbstick

HFS+
sudo mount -t hfsplus -o utf8,uid=pi,gid=pi,noatime /dev/sda /media/usbstick

exFAT
sudo mount -t exfat -o utf8,uid=pi,gid=pi,noatime /dev/sda /media/usbstick

ext4
sudo mount -t ext4 -o defaults /dev/sda /media/usbstick

Bedeutung der Parameter nach -o für Option:

utf8: Die Dateinamen verwenden den UTF-8-Zeichensatz und dürfen somit auch Umlaute
enthalten
uid=pi,gid=pi: Das Speichermedium wird dem Benutzer (uid) und der Gruppe (gpi) pi
zugewiesen
noatime: Die Inode Access Time (letzter Lesezugriff) wird nicht aktualisiert und damit
werden unnötige Schreiboperatoren vermieden
defaults: Dem Gerät werden die Optionen rw, suid, dev, exec, auto, nouser und async
(siehe auch ubuntuusers Wiki) zugewiesen

Step 5 (optional)
Um das Speichermedium wieder zu dismounten/auszuwerfen, wenden wir folgenden Befehl
an, wobei wir wieder den Mountpoint anpassen müssen.

sudo umount /media/usbstick

Step 6
Soll der USB-Stick oder die USB-Festplatte nun bei jedem Start des Raspberry Pis
automatisch eingebunden werden,
können wir dies für einen Eintrag in fstab erlauben, wozu wir die zuvor ausgelesene UUID
benötigen.
Je nach Dateisystem muss man eine der folgenden Zeilen am Ende der Datei /etc/fstab
eintragen.
Zum Bearbeiten der Datei nutzen wir den Editor nano
in dem wir zum Speichern STRG + O, und anschließend Enter drücken müssen. STRG+X beendet Nano

sudo nano -w /etc/fstab

FAT32
UUID=0C00-0B44 /media/usbstick/ vfat utf8,uid=pi,gid=pi,noatime 0

NTFS
UUID=3241-40CE /media/usbstick/ ntfs-3g utf8,uid=pi,gid=pi,noatime 0

HFS+
UUID=3241-40CE /media/usbstick/ hfsplus utf8,uid=pi,gid=pi,noatime 0

exFAT
UUID=3241-40CE /media/usbstick/ exfat utf8,uid=pi,gid=pi,noatime 0

ext4
UUID=3241-40CE /media/usbstick/ ext4 defaults 0

Fertig! Wir haben nun ein USB-Medium mit dem Dateisystem FAT32, NTFS, HFS+, exFAT
oder ext4
in unseren Raspberry Pi eingebunden und können dieses automatisch beim Start des Mini-
Computers mounten.



### Samba installieren ###

sudo apt-get install samba samba-common-bin

Dann prüfen wir, ob Samba läuft.

sudo service smbd status
sudo service nmbd status

alles ok ?

testparm

sudo service smbd restart
sudo service nmbd restart


## Pfade und Rechte definieren ##
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf_alt
sudo nano /etc/samba/smb.conf

Allgemeines Verzeichnis für alle zum Testen:

sudo mkdir /media/usbstick/alle
sudo chown root:root /media/usbstick/alle


Verzeichnis nur für die Benutzergruppe "users":

sudo mkdir /media/usbstick/users
sudo chown root:users /media/usbstick/users
sudo chmod 770 /media/usbstick/users

Verzeichnis nur für den Benutzer "pi":

sudo mkdir /media/usbstick/pi
sudo chown pi:pi /media/usbstick/pi
sudo chmod 700 /media/usbstick/pi


sudo nano /etc/samba/smb.conf

[SambaTest]
comment = Samba-Test-Freigabe
path = /media/usbstick/alle
read only = no
[SambaUsers]
comment = Samba-Users-Freigabe
path = /media/usbstick/users
read only = no
[SambaPi]
comment = Samba-Pi-Freigabe
path = /media/usbstick/pi
read only = no

testparm alles ok?


sudo service smbd restart
sudo service nmbd restart

sudo smbpasswd -a pi

jetzt 2x das Passwort eingeben
(? das gleiche wie für user pi ?)


### RemoteDesktopverknüpfung zu Windows ###

Step:01 zuerst den vnc Server deinstallieren

sudo apt-get remove realvnc-vnc-server

Step:02 danach den RDP-Server "xrdp" installieren

sudo apt-get install xrdp

sudo reboot



## Nun am Windows-PC im Startmenü nach Remotedesktopverbindung suchen ##

Im Dialog zum Verbindungsaufbau müssen Sie den Hostnamen des Raspberry Pi

rassberrypi

sowie den Login-Namen

pi

angeben.

Step:03 einen ordentlichen Mauscursor für xrdp einstellen

cd
echo "xsetroot -cursor_name left_ptr&" > .xsessionrc
chmod a+x .xsessionrc
sudo systemctl restart xrdp



### ACCESS Point Modus (Router) ###

There's a new Raspberry Pi. This is exciting.
It also has on-board WiFi. This makes it doubly exciting!

One of my first thoughts was, can I use it as a SoftAP for some ESP8266 sensor nodes?
As it turns out, you can, and it's not that difficult,
as the BCM43438 chip is supported by the open-source brcmfmac driver!

The first step is to install the required packages:

sudo apt-get install dnsmasq hostapd

I'll go into a little detail about the two:
hostapd - This is the package that allows you to use the built in WiFi as an access point
dnsmasq - This is a combined DHCP and DNS server that's very easy to configure

If you want something a little more 'heavyweight', you can use the isc-dhcp-server
and bind9 packages for DHCP and DNS respectively, but for our purposes, dnsmasq works
just fine.
Configure your interfaces

The first thing you'll need to do is to configure your wlan0 interface with a static IP.

If you're connected to the Pi via WiFi, connect via ethernet/serial/keyboard first.

In newer Raspian versions, interface configuration is handled by dhcpcd by default.
We need to tell it to ignore wlan0, as we will be configuring it with a static IP address
elsewhere.
So open up the dhcpcd configuration file with

sudo nano /etc/dhcpcd.conf

and add the following line to the bottom of the file:

denyinterfaces wlan0

Note: This must be ABOVE any interface lines you may have added!

Now we need to configure our static IP.
To do this open up the interface configuration file with

sudo nano /etc/network/interfaces

and edit the wlan0 section so that it looks like this:

allow-hotplug wlan0
iface wlan0 inet static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


Restart dhcpcd with

sudo service dhcpcd restart

and then reload the configuration for wlan0 with

sudo ifdown wlan0; sudo ifup wlan0


Configure hostapd

Next, we need to configure hostapd. Create a new configuration file with

sudo nano /etc/hostapd/hostapd.conf

with the following contents:

# This is the name of the WiFi interface we configured above
interface=wlan0
# Use the nl80211 driver with the brcmfmac driver
driver=nl80211
# This is the name of the network, das hier anpassen !!!
ssid=Pi3-AP
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
# The network passphrase, das hier anpassen !!! euer Passwort !!!
wpa_passphrase=raspberry
# Use AES, instead of TKIP
rsn_pairwise=CCMP


We can check if it's working at this stage by running

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

If it's all gone well thus far, you should be able to see to the network Pi3-AP!
If you try connecting to it, you will see some output from the Pi,
but you won't receive and IP address until we set up dnsmasq in the next step.
Use Ctrl+C to stop it.

We aren't quite done yet, because we also need to tell hostapd where to look for the config
file
when it starts up on boot. Open up the default configuration file with

sudo nano /etc/default/hostapd

and find the line #DAEMON_CONF=""

and replace it with

DAEMON_CONF="/etc/hostapd/hostapd.conf"


Configure dnsmasq

The shipped dnsmasq config file contains a wealth of information on how to use it,
but the majority of it is largely redundant for our purposes.
I'd advise moving it (rather than deleting it), and creating a new one with

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

sudo nano /etc/dnsmasq.conf

Paste the following into the new file:

interface=wlan0 # Use interface wlan0
listen-address=172.24.1.1 # Explicitly specify the address to listen on
bind-interfaces # Bind to the interface to make sure we aren't sending things elsewhere
server=8.8.8.8 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150
with a 12 hour lease time Set up IPv4 forwarding


One of the last things that we need to do before we send traffic anywhere is to enable
packet forwarding.
To do this, open up the sysctl.conf file with

sudo nano /etc/sysctl.conf

and remove the # from the beginning of the line containing net.ipv4.ip_forward=1.


This will enable it on the next reboot, but because we are impatient, activate it immediately
with :

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

We also need to share our Pi's internet connection to our devices connected over WiFi
by the configuring a NAT between our wlan0 interface and our eth0 interface.
We can do this using the following commands:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

However, we need these rules to be applied every time we reboot the Pi,so run

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

to save the rules to the file /etc/iptables.ipv4.nat
Now we need to run this after each reboot,
so open the rc.local file with

sudo nano /etc/rc.local

and just above the line exit 0, add the following line:

iptables-restore < /etc/iptables.ipv4.nat

We're almost there!
Now we just need to start our services:

sudo service hostapd start
sudo service dnsmasq start

Wenn an LAN Buchse des Raspberry kein Internet anliegt ist es reines sichers Heimnetzwerk. :-)



### WLAN ON OFF ###
Ziel ist es das WLAN Zeitgesteuert aus und ein zuschalten!(Nachtschaltung)
benötigt wird rfkill
sowie gnome-schedule
installation bzw. aktualisierung von gnome-schedule
sudo apt-get install gnome-schedule
installation bzw. aktualisierung von rfkill
sudo apt-get install rfkill
Auflisten der Zustände von WLAN Bluetooth ...
rfkill list
0: phy0: Wireless LAN

Soft blocked: no
Hard blocked: no

1: hci0: Bluetooth

Soft blocked: yes
Hard blocked: no

### 3 geplante Aufgaben sind zu definieren ###
Systemwerkzeuge->geplante Aufgaben (GNOME-Schedule)
## 1. ##
täglich 22:00 Uhr
wlan ausschalten
sudo rfkill block wlan
## 2. ##
täglich 07:59 Uhr
wlan einschalten
sudo rfkill unblock wlan
## 3. ##
täglich 08:00 Uhr
danach ist ein reboot sinvoll
damit alle Dienste wieder gestartet werden
sudo reboot




##################################################################################################

Update 2019.10.03

UPNP-Server mit "miniDLNA_1.2.1
*******************************

Grundlage ist ein images vom Projekt "mini_dlna_1.2.1;RDP;samba;WLAN Accespoint"

1:## Umstellung auf VGA Monitor Betrieb mit einen ###
"UGREEN HDMI auf VGA kabel Adapter 1080P HDTV Aktiv" nun möglich.

Dazu muss die config.txt angepasst werden.
Dies ist von einen Windows PC möglich.

sudo nano /boot/config.txt

Jetzt sucht Ihr in der Datei die folgenden Zeilen,

hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=16

und ändert die Werte in

#hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=16

Der Eintrag hdmi_group=2 setzt den Raspberry in den VGA Modus
und mit dem zweiten Eintrag hdmi_mode=16 wird die Auflösung auf 1024×768,
60 Hz eingestellt. Der Eintrag hdmi_force_hotplug=1 schaltet den Hotplug modus ein.

Weitere Auflösungen:

hdmi_mode=9 800×600 60 Hz
hdmi_mode=16 1024×768 60 Hz
hdmi_mode=23 1280×768 60 Hz
hdmi_mode=28 1280×800 60 Hz
hdmi_mode=35 1280×1024 60 Hz
hdmi_mode=82 1080p 60 Hz
hdmi_mode=85 720p 60 Hz

*******************************
2:## alte Version miniDLNA deinstallieren ###

sudo apt-get purge minidlna -y
sudo apt-get remove minidlna
sudo apt-get autoremove -y

3:## Raspi aktualisieren ###

Stellen Sie sicher, dass Sie über ein Quell-Repository verfügen.
Standardmäßig enthält Raspbian dies nicht, ändern Sie wheezy in jessie,
wenn Sie wissen, dass Sie auf jessie sind

echo "deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free" | sudo tee -a /etc/apt/sources.list

Aktualisieren Sie die Repositorys, damit Ihr neues Quell-Repo erkannt wird

sudo apt-get update

Holen Sie sich Abhängigkeiten für die Erstellung aus der Quelle heraus.

sudo apt-get build-dep minidlna -y
Wenn Sie Fehler erhalten, können Sie die Abhängigkeiten manuell installieren.

sudo apt-get install autopoint debhelper dh-autoreconf gcc libavutil-dev libavcodec-dev libavformat-dev libjpeg-dev libsqlite3-dev libexif-dev libid3tag0-dev libogg-dev libvorbis-dev libflac-dev -y


4:## miniDLNA_1.2.1 downloaden und entpacken ###

https://sourceforge.net/projects/minidlna/files/minidlna/

Download Latest Version hier die Version 1.2.1 minidlna-1.2.1.tar.gz

Unpack miniDLNA_1.2.1.tar.gz mit

tar -xvf minidlna-1.2.1.tar.gz


5:## miniDLNA_1.2.1 installieren und vorbereiten ###

Öffne eine Shell im entstandenen Ornder "miniDLNA-1.2.1"

cd minidlna*

und installiere mit

./configure && sudo make && sudo make install

Kopieren Sie die Standardkonfigurationsdatei

sudo cp minidlna.conf /etc/

Kopieren Sie das Startup-Daemon-Skript,
um ReadyMedia miniDLNA beim Start automatisch zu starten

sudo cp linux/minidlna.init.d.script /etc/init.d/minidlna

Machen Sie das Startskript ausführbar

sudo chmod +x /etc/init.d/minidlna

Aktualisieren Sie rc, um die miniDLNA-Standardeinstellungen zu verwenden

sudo update-rc.d minidlna defaults

Bearbeiten Sie die Konfiguration

sudo nano /etc/minidlna.conf


Bearbeiten Sie die folgenden Einstellungen, um auf Ihre Medienordner zu verweisen.

Diese Version von minidlna enthält mehrere Ordner unter Video.
Bevor Filme und Fernsehen in getrennten Kategorien gewesen wären,
Filme und Fernsehen werden jetzt unter der Kategorie Video angezeigt.
Es wird auch die Ordnerstruktur von ihnen angezeigt, anstatt sie anzuzeigen
nur die Videodateien. Avi-Dateien werden ebenfalls angezeigt und können gestreamt werden.

inotify verwendet Ressourcen, da es Ihre Bibliothek automatisch sammelt,
Wenn Sie Inotify nicht verwenden, müssen Sie dies tun
Starten Sie den miniDLNA-Dienst manuell neu und laden Sie ihn erneut

Der Anzeigename ist, wie Ihr miniDLNA-Server bei seinen Streaming-Clients angezeigt wird.

Anpassung der Pfade ...

media_dir=V,/mnt/usbstorage/Movies
media_dir=V,/mnt/usbstorage/TV
media_dir=A,/mnt/usbstorage/Music
media_dir=P,/mnt/usbstorage/Pictures
# Names the DLNA server
friendly_name=RasPi Media Server
# Weist den DLNA an, die Bibliothek bei Änderungen zu aktualisieren
inotify=yes

Ctrl+X, Y and Enter to save and exit


Start den minidlna service

sudo service minidlna start

Stellen Sie jetzt sicher, dass miniDLNA beim Booten gestartet wird
Es wird auf Port 8200 ausgeführt,
so dass Sie überprüfen können, wie viele Mediendateien dort indiziert wurden

sudo reboot

### Feintuning und Verschlimm-Besserungen ###

## Schreibzugriff auf SD-Karte reduzieren ##

Werden bestimmte Dateien regelmäßig erstellt oder geändert,
z.B. Bilddateien einer Webcam, sollten diese möglichst ausgelagert werden (RAM, NAS,
USB-Speicher).
Aber auch bereits im normalen Betrieb gibt es unter Linux Verzeichnisse (z.B. /var/log),
die systembedingt ständiger Zugriffe ausgesetzt sind.
Für einige dieser Systemverzeichnisse kann es daher sinnvoll sein,
diese ins RAM auslagern.

Um z.B. /var/log in den Arbeitsspeicher auszulagern,
müssen folgende Einträge in der /etc/fstab hinzugefügt werden:


sudo nano /etc/fstab

none /var/log tmpfs size=5M,noatime 00



Hinweis: Verlagert man Dateien und Ordner RAM, so sollte man sich darüber im Klaren
sein,
dass diese nach einem Neustart nicht mehr verfügbar sind.


Swapping deaktivieren

Der Raspberry Pi verfügt von Haus aus über recht begrenzten Speicher.
Für Speicherhungrige Anwendungsfälle kann Swap-Speicher deshalb sinnvoll sein.

In meinen Fall ist der zur Verfügung stehende Speicher jedoch mehr als ausreichend
und eine Vollauslastung ist nicht zu erwarten.
Um die Zugriffe auf die SD-Karte weiter zu reduzieren,
kann daher Swapping mit folgenden Befehlen deaktiviert werden:


sudo dphys-swapfile swapoff
sudo systemctl disable dphys-swapfile
sudo apt-get purge dphys-swapfile