Google+ Followers

Thursday, July 11, 2013

27. TightVNC - a Lightweight Server for a Remote Pi

I remembered that some time ago, Raspberry π IV Beginners told us how to set the Raspberry Pi up as a VNC (Virtual Network Computing) server.  VNC is a platform-independent "graphical desktop sharing system".  It uses the Remote Frame Buffer (RFB) protocol, in this case, to transmit my Windows PC's keyboard and mouse events to the Raspberry Pi, and transmits the Linux machine's (RasPi) desktop and its responses back to the PC, thus allowing the Pi to be controlled remotely and 'headless', meaning, without a mouse, keyboard or monitor.  'Network' includes the internet, so in theory, this should be possible over the web, but we'll see later how we get on with that.....  TightVNC uses a special type of encoding, tight encoding, which is useful for low bandwidth connections.

So I looked this up and found it on the new website and this is really a reproduction of the excellent Raspberry π IV Beginners' instructions:

As the Pi is currently headless, and I have been using Xming and PuTTY (see previous post No 21 at to communicate with it, there is no reason why I wouldn't be able to do all the following stuff through PuTTY :

You can see the Pi Cam on the top left, mounted in a Pimoroni Ltd Raspberry Pi Camera Mount,, which is very useful.  

The camera is now without a scope, but it's also upside-down, so the video images have to be flipped not only horizontally, but also vertically.  Incidentally, as my Pi has recently been having difficulty picking up WiFi in the area where it currently is, I thought that a short (0.25 m) USB 2.0 A Male to A Female cable would make it easier to orientate the Edimax Wireless Nano USB Adapter:
and it works beautifully, with enough length to twist the Edimax to face the WiFi router.  See the Edimax at the bottom of the picture, connected to the short USB cable.

The next picture also shows the 25 cm cable going down to the Edimax at the bottom.  The Edimax is Duct-Taped, so you can't actually see it, but its broad face, the one which shows the flashing blue LED, is facing the router, which is downstairs.

On the Pi, after doing a 

sudo apt-get update

to ensure the Pi's software is up to date, I did a

sudo apt-get install tightvncserver

to install this version of VNC Server and any dependencies it may have, like Java etc.

Then to run this, I did a 


to get it running.  You are then asked for passwords, and to verify them.

I then entered

vncserver :1 -geometry 800x600 -depth 24

to set up a socket - in this case, number 1 (you can set up socket 2, 3 etc to start up more VNC Server sessions at the same time).

Now, back on the PC, you need VNC installed, so the instructions said to go to (clicking this link will immediately download the Windows installer package to your computer) to download the appropriate TightVNC files, (which are free, and open-source, as usual) including the tvnserver and tvnviewer programs.  When these were fully installed, I opened tvnviewer and the following came up:

As you can see above, I entered the Pi's IP address, appended with the socket number (1), and after clicking "Connect" I was asked for the password I had given above.

Then up pops the Pi's desktop:

You will see from the cursor label above, (at the top left, in tiny print) that you can save this session to a .vnc file, (which I named VNCSession.vnc) so that when you want to run again, clicking the icon on the PC will display the Pi's desktop, providing you are already logged on to the Pi with  PuTTY .  Now use:

vncserver :1 -geometry 800x600 -depth 24

followed by starting 


allowing you now to open LXTerminal as follows:

Here I have shown that I have double-clicked on the Pi's LXTerminal, which opens the 'pi@raspberrypi' window.  Then I opened the Command Prompt program on the PC, changed to the mplayer and netcat directory, and entered the command

nc -l -p 5001 | mplayer -fps 31 -cache 1024 -

This of course, runs the netcat program (nc) and pipes its output from port 5001 to the mplayer program as before.  When I run the raspivid program in the pi@raspberrypi window, 

raspivid -vf -hf -t 86400000 -o - | nc 5001

raspivid pipes its video output to netcat and mplayer magically opens up and wmplayer shows the vertically flipped (-vf), horizontally flipped (-hf) (because my PiCam is currently upside-down) raspivid video on port 5001 on the PC's IP address, for a time of 86400000 milliseconds (24 hours).

And the result:
You can see that the video image has opened up to fill the screen (automagically, as Lady Ada would say), and the windows can be minimised.

From now on, provided you are logged in with PuTTY, simply clicking the .vnc icon on the PC, mentioned above, which I called VNCSession.vnc, brings up the RasPi desktop immediately.  
If you need to reboot the Pi, you may need to go back and run tvnviewer to set up the socket again.  

So the whole procedure for running raspivid on a remote headless Pi, is:

Log in with PuTTY

Run the VNCSession.vnc config file mentioned above.  This opens the TightVNC Viewer with my pre-defined parameters, to give the Pi's desktop on the PC's monitor.

Back to the PC, run the Windows Command Prompt program.  Then

cd mplayer and netcat 

to change directory.  Then run the command:

nc -l -p 5001 | mplayer -fps 31 -cache 1024 -

Finally, on the Pi desktop, open the LXTerminal, and enter the command

raspivid -vf -hf -t 86400000 -o - | nc 5001

MPlayer should, after a minute or so, open up and show full-screen real-time video from the remote PiCam.

The verdict?  Well, since I made the above changes (putting the Edimax dongle on a 25 cm cable, and using VNC) the RasPi stays on the WiFi network much better - in fact - it hasn't dropped out yet, after several days of continuous use, except when the heat of the sun caused the Duct Tape to slip, allowing the Edimax to point in a different direction.  Once again - magic!

No comments:

Post a Comment