TechnikGadgets

Raspberry Pi – VNC Mirror

What is a VNC?

VNC, or Virtual Network Computing, is a way to allow a user to control a computer remotely. VNC software makes it possible for someone to view and interact with a desktop environment from another computer. In order to use VNC, both the remote user and the computer being controlled must have VNC software installed. Additionally, the remote user will need to know the IP address or hostname of the computer they wish to control.

There are a few different VNC software options available, but TightVNC is one of the most popular. Once TightVNC is installed on both the remote computer and the computer you wish to control, you can launch the TightVNC Viewer application on your local machine. From there, you will need to enter the IP address or hostname of the remote computer. Once connected, you will be able to see and interact with the remote desktop as if you were sitting in front of it.

While VNC is a great way to remotely control a computer, it does have some limitations. One major limitation is that VNC only provides access to the graphical user interface (GUI) of a machine. This means that if you need to access the command line of a remote machine, you will need to use another method. Additionally, VNC is not designed for high-bandwidth applications such as video streaming. For these types of tasks, you will likely want to use a more robust solution such as Remote Desktop Protocol (RDP).

Creating a VNC mirror with Raspberry Pi

The idea is to show any machine (virtual or real) to public. So you can setup a computer with VNC anywhere and can put a big TV screen at another place. We need a Raspberry Pi Model B, a network connection and a micro USB power plug (either over USB <-> micro USB plugged into your RPi or TV or seperate power device)

I have downloaded the Image of the Raspbian Image wheezy. Follow the basic instructions of the RPi config.

Important: if you don’t have 1920×1080 resolution (Full HD) it might not work for you. I have experienced that if your remote computer has a resolution of 1280×768 the ssvncviewer exits immediately. But if you changed it to a common resoltion of 1280×720 it will work with one little dissadvantage: The screen stays black with a circle shaped cursor. You have to login from another computer on the remote machine first, so it will work. These 1280xYYY resolutions make somehow troubles.

Paste these lines in your commandline (or over putty) and ommit the lines with proxy if you have none.

Programms and settings

# start a root session
sudo bash
echo "exec openbox-session">/home/pi/.config/openbox/autostart.sh
# proxies
export http_proxy=http://proxy1.sysstem.at:8080
export https_proxy=https://proxy1.sysstem.at:8080
echo "Acquire::http::Proxy \"http://proxy1.sysstem.at:8080\";">>/etc/apt/apt.conf
# updates
apt-get update
apt-get upgrade -y
# install programs
apt-get install x11vnc vim ssvnc unp htop -y
# autostart x11vnc
mkdir .config/autostart/
echo [Desktop Entry]>/home/pi/.config/autostart/x11vnc.desktop
echo Encoding=UTF-8>>/home/pi/.config/autostart/x11vnc.desktop
echo Type=Application>>/home/pi/.config/autostart/x11vnc.desktop
echo Name=X11VNC>>/home/pi/.config/autostart/x11vnc.desktop
echo Comment=>>/home/pi/.config/autostart/x11vnc.desktop
echo Exec=x11vnc -forever -shared -usepw -ultrafilexfer -display :0>>/home/pi/.config/autostart/x11vnc.desktop
echo StartupNotify=false>>/home/pi/.config/autostart/x11vnc.desktop
echo Terminal=false>>/home/pi/.config/autostart/x11vnc.desktop
echo Hidden=false>>/home/pi/.config/autostart/x11vnc.desktop
# remove desktop links
rm /home/pi/Desktop/*.desktop

Info X11 Settings

If the X11VNC does not start correctly after boot try to change parameters in the x11vnc.desktop file.

sudo vim /home/pi/.config/autostart/x11vnc.desktop

Time zone & geo settings

# set timezone and select your city
sudo dpkg-reconfigure tzdata

#reconfigure ntp
sudo dpkg-reconfigure ntp

# set timeserver
sudo vim /etc/ntp.conf
server ntp.sysstem.at iburst

#stop ntp daemon
sudo /etc/init.d/ntp stop

#set time
sudo ntpdate ntp.sysstem.at

#start ntp
sudo /etc/init.d/ntp start

Monitor preferences

Open up vim and search / replace settings to your needs.

sudo vim /boot/config.txt

# remove black borders on screen. set to 0 if problem occour
disable_overscan=1

# pretend hdmi is plugged in and hdmi resolution will work
hdmi_force_hotplug=1

#Force console size

framebuffer_width=1920
framebuffer_height=1080

# set resolution to 1920x1080@60Hz
# hdmi_mode=4 for 1280x720@60Hz
#check website for more modes
#http://elinux.org/RPi_config.txt
hdmi_group=1
hdmi_mode=16

# remove 1 of 2 desktops (we only need 1)

vim /home/pi/.config/openbox/lxde-rc.xml
# at desktops:
<number>1</number>

X11VNC settings

# define a password for your x11vnc
x11vnc -storepasswd /home/pi/.vnc/passwd

Upload a new desktop wallpaper, most likely the logo of your company. Change background by rightclicking on your desktop and choose a new picture.

VNC client – autostart script

sudo vim /home/pi/.config/autostart/vncview.sh

Paste script for autostart VNC

# Process check script: The script simply checks if a process is running and if it is not found to be running it will execute it.
# The script loops in preset intervals, hence it is possible to monitor a process continuously.

# Variables
Running=1
SleepInterval=60
ProcessInstances=`sudo ps aux | grep [s]svncviewer | wc -l`

#VNC Variables
vnc=ssvncviewer
host=WSTERMNL01
display=0
resolution=1920x1080
passfile=/home/pi/.vnc/passwd2
para="-display :$display -viewonly -fullscreen -shared -passwd $passfile -scale $resolution -encodings zywrle"

# Logic
while [ $Running -gt 0 ]
do

if [ `sudo ps aux | grep [s]svncviewer | wc -l` -gt 0 ]; then
echo Process already running! Nothing to do.
else
echo Process not running! Starting process
# This is the command that should start the process in question
$vnc $host $para &
fi

# How often shall we repeat the check?
echo Sleeping for $SleepInterval seconds
sleep $SleepInterval

done

exit 0

Enable Service for VNC Client script

sudo vim /etc/init.d/vncviewd

Paste script for VNC service autostart

### BEGIN INIT INFO
# Provides: vncviewd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Viewer Daemon.
# Description: Start VNC Viewer at boot time.
### END INIT INFO

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

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting vncviewd"
    # run application you want to start
    su pi /home/pi/.config/autostart/vncview.sh
    exit 0
    ;;
  stop)
    echo "Stopping vncviewd"
    # kill application you want to stop
    pkill -f vncview.sh
    exit 0
    ;;
  *)
    echo "Usage: /etc/init.d/vncviewd {start|stop}"
    exit 1
    ;;
esac

exit 0

register vncviewd for autostart

sudo update-rc.d vncviewd

Monitor script

sudo vim /etc/init.d/monitor

Paste monitor script

### BEGIN INIT INFO

# Provides: monitor
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop Monitor
# Description: Start or stop Monitor
### END INIT INFO
#! /bin/sh
# /etc/init.d/monitor
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting monitor"
# Enable HDMI with preferred mode
/opt/vc/bin/tvservice -p
# Change console to enable monitor output
# not very nice
sudo chvt 6
sudo chvt 7
;;
stop)
echo "Stopping monitor"
/opt/vc/bin/tvservice -o
;;
*)
echo "Usage: /etc/init.d/monitor {start|stop}"
exit 1
;;
esac
exit 0

register for monitor script for autostart

sudo update-rc.d monitor

Disable sleep mode for monitor / display

sudo vim /etc/kbd/config
# DO NOT changes this line. VNC needs to blank the console for fullscreen mode
BLANK_TIME=30 (Was 30)

# Powerdown time. The console will go to DPMS Off mode POWERDOWN_TIME
# minutes _after_ blanking. (POWERDOWN_TIME + BLANK_TIME after the last input)
# change this line (original was 30)
POWERDOWN_TIME=0

restart KBD

sudo /etc/init.d/kbd restart

Set a time schedule for your monitor. This reduces energy consumption. Monitor only runs form 6 am to 6 pm (06:00-18:00)

sudo vim /etc/crontab

paste the following to create a cronjob

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
0  6 * * 1-5    root    /etc/init.d/monitor start
0 18 * * 1-5    root    /etc/init.d/monitor stop
#

Test your configuration and make a backup of your SD card. The only thing left what i wanted to do was to add a custom loading screen while booting. Maybe you can get it working by using plymouth or other programs.