sudo apt update
sudo apt upgrade

select-editor

# Einstellungen:
# Passwort 560612
# Deutsches Tastaturlayout
# Deutsche WLAN-Kanäle
# Hostname pizero01
# SSH aktiviert
sudo raspi-config

dmesg
lsusb
sudo apt install iw

# WLAN installieren (Quelle: https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md)
# WLANs scannen
sudo iwlist wlan0 scan
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
#	# Eintrag:
#	network={
#		ssid="The_ESSID_from_earlier"
#		psk="Your_wifi_password"
#	}

sudo ifdown wlan0
sudo ifup wlan0

sudo reboot

# disable HDMI (http://www.jeffgeerling.com/blogs/jeff-geerling/raspberry-pi-zero-conserve-energy)
sudo nano /etc/rc.local
#	/usr/bin/tvservice -o

# Disable the ACT LED.
sudo nano /boot/config.txt
#	dtparam=act_led_trigger=none
#	dtparam=act_led_activelow=off

# configure NTP
sudo apt install ntp
sudo /etc/init.d/ntp restart
sudo nano /etc/ntp.conf
#	# in /etc/ntp.conf Zeitserver ergänzen
#	server de.pool.ntp.org
sudo /etc/init.d/ntp restart
date

sudo apt install lirc zbar-tools mc moc fswebcam python3 python-rpi.gpio python3-rpi.gpio

# PHAT DAC (https://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install)
curl -sS get.pimoroni.com/phatdac | bash

# check for errors
dmesg

# MP3-Test
mocp
sudo nano /etc/modprobe.d/alsa-base.conf

# Webcam-Auflösung bestimmen (http://askubuntu.com/questions/214977/how-can-i-find-out-the-supported-webcam-resolutions)
lsusb
lsusb -s 001:006 -v | egrep "Width|Height"

# Webcam-Test
python3 lighton.py
sudo fswebcam -r 320x240 -d /dev/video0 -v ~/test.jpg
python3 lightoff.py

# QR-Test
python3 lighton.py
zbarcam --quiet --nodisplay --raw -Sdisable -Sqrcode.enable --prescale=320x240 /dev/video0
python3 lightoff.py


# enable lirc in /boot/config.txt and add parameters
sudo nano /boot/config.txt
#	# Uncomment this to enable the lirc-rpi module
#	dtoverlay=lirc-rpi,gpio_in_pin=23,gpio_out_pin=22

##### DIESER SCHRITT HILFT NICHTS. Man muss immer noch lirc manuell laden mit "sudo lircd -d /dev/lirc0", bevor man einen Befehl absetzen kann (s.u.).
		# add lirc configuration to /etc/modules (gpio_in_pin wird mit ungenutztem PIN belegt, weil der default PIN 18 schon vom PHAT DAC verwendet wird) 
		sudo nano /etc/modules
		#	lirc_dev
		#	lirc_rpi gpio_in_pin=23 gpio_out_pin=22
#####

# add lirc configuration to /etc/lirc/hardware.conf
sudo nano /etc/lirc/hardware.conf
#	LIRCD_ARGS="--uinput"
#	LOAD_MODULES=true
#	DRIVER="default"
#	DEVICE="/dev/lirc0"
#	MODULES="lirc_rpi"
#	LIRCD_CONF=""
#	LIRCMD_CONF=""

sudo reboot

# copy the JBL remote configuration in /etc/lirc/lircd.conf
# --> lirc_jbl_iiip.conf

sudo /etc/init.d/lirc restart

# turn on music, list available commands and test remote control
mocp -S && mocp -l ~/adele.mp3
sudo lircd -d /dev/lirc0
irsend LIST JBLIIIP ""
## Der Befehl muss 2x schnell hintereinander gesendet werden. Offensichtlich muss die Konfiguration noch verbessert werden.
irsend SEND_ONCE JBLIIIP KEY_MUTE

# autostart for piplayer with log output redirection
# (siehe http://raspberrypi.stackexchange.com/questions/8169/run-startup-application-as-a-specific-user)
sudo nano /etc/rc.local
#	...
#	su pi -c 'python3 /home/pi/piplayer2/piplayer2.py >> /home/pi/piplayer2/piplayer2.log 2>&1 &'
#	exit 0

sudo reboot

# check if running after restart
ps -ef | grep python

# Tests mit QR-Code-Karten
tail -f /home/pi/piplayer2/piplayer2.log


# SD-Karte read-only nach https://www.heise.de/ct/ausgabe/2016-21-Den-Raspi-als-Bluetooth-Empfaenger-einsetzen-3330683.html

sudo nano /etc/fstab
#	proc            /proc            proc    defaults                      0       0
#	/dev/mmcblk0p1  /boot            vfat    defaults,ro                   0       2
#	/dev/mmcblk0p2  /                ext4    defaults,noatime,ro           0       1
#	tmpfs           /var/log         tmpfs   nodev,nosuid                  0       0
#	tmpfs           /var/tmp         tmpfs   nodev,nosuid,mode=1777        0       0
#	tmpfs           /tmp             tmpfs   nodev,nosuid,mode=1777        0       0
#	tmpfs           /home/pi/.config tmpfs   nodev,nosuid,mode=1777,uid=pi 0       0
#	tmpfs           /home/pi/.moc    tmpfs   nodev,nosuid,mode=1777,uid=pi 0       0
#	# a swapfile is not a swap partition, no line here
#	#   use  dphys-swapfile swap[on|off]  for that

sudo rm -rf /var/lib/dhcp/ /var/spool /var/lock /etc/resolv.conf
sudo ln -s /tmp /var/lib/dhcp
sudo ln -s /tmp /var/spool
sudo ln -s /tmp /var/lock
sudo ln -s /tmp/resolv.conf /etc/resolv.conf

sudo nano /etc/rc.local (hier nochmal mit allen Ergänzungen aus den vorherigen Schritten)
#	...
#	
#	# make /tmp on RAM disk writable
#	chmod 777 /tmp
#	
#	# disable hdmi
#	/usr/bin/tvservice -o
#	
#	# start piplayer
#	su pi -c 'python3 /home/pi/piplayer2/piplayer2.py >> /tmp/piplayer2.log 2>&1 &'

sudo reboot
exit 0



# Dateisystem temporär beschreibbar machen
sudo mount -o rw,remount /
sudo mount -o rw,remount /boot










# ------ HIER WEITER



# install python bottle web framework - http://bottlepy.org
sudo apt-get install python-pip
sudo pip install bottle








-- HERE BE DRAGONS ------------------------------------------------------------



# GEHT LEIDER NICHT WEGEN KNISTERN (BRUMMSCHLEIFE)
# USB Soundkarte verbinden, dann
lsusb
alsamixer   # -> zeigt oben das aktive Sounddevice an. Mit F6 kann man die Liste aller Devices anzeigen

# Konfiguration anlegen (https://www.youtube.com/watch?v=GQDQ_Z-NmHQ)
sudo nano /etc/asound.conf
#	defaults.ctl.card 1
#	defaults.pcm.card 1
#	defaults.timer.card 1

alsamixer # -> sollte jetzt auf das USB-Device umgeschaltet haben
