Category: Bash
May 14, 2015

Sometimes you really want to play with a Raspberry Pi, but don’t have a display, keyboard, or mouse handy, and the wifi isn’t configured correctly to just be able to SSH in. Invariably you spend an hour digging around for a keyboard or refreshing a wireless clients list, but this doesn’t have to be the case. After a quick one-time setup, everything you need to use a Raspberry Pi will already be in your pocket.

Android Raspberry Pi display over USBThe idea

Once configured, if you have an Android phone with USB tethering and a cable, you should be well-equipped to use your Pi. Bonus points if you have a bluetooth mouse and keyboard. We’ll be setting up a USB network interface on the Pi and installing a VNC server to pass a session over that interface, thus making your phone a Raspberry Pi display. By the end you should be able to just power up your Pi, plug in your phone, turn on USB tethering, and open up a full desktop.

Setting up the network

Log into your Pi via SSH or open up a terminal in its GUI and pull up your network interfaces.

sudo nano /etc/network/interfaces

Paste the following onto the bottom of the file, then save and exit (ctrl-X, Y):

On your next restart, you should have a new interface when you type ifconfig. We’ve set this interface to have a static IP address, always 192.168.42.42, which you will later use to start your VNC session or connect via SSH on your phone.



Configuring VNC

VNC, or Virtual Network Computing, is a way of sharing a graphical desktop environment over a network, which in this case happens to be your phone’s USB cable. First, we need to install a VNC server onto the Raspberry Pi. We’ll be using TightVNC since raspberrypi.org has a tutorial for it and it’s easy to find help on forums.

sudo apt-get install tightvncserver

Next, use the command tightvncserver to configure VNC for your Pi. It should ask you for a password–be aware that TightVNC will truncate your password to eight characters. It does tell you this in the terminal, but it can be easy to miss and lead to many failed login attempts.

Lastly, we need the VNC server to start up every time the Pi starts so that you really do only need your phone. First, change into your /etc/init.d directory.

cd /etc/init.d

Create a new file called vncboot. You’ll need root privileges to change anything in this directory.

sudo nano vncboot

Paste the following into the file (change export USER=’pi’ to your username if not pi, and edit the screen resolution in the start) block if necessary):

Save this file, then update its permissions:

sudo chmod 755 vncboot

Finally, run the following command to add it to your startup:

sudo update-rc.d vncboot defaults

Reboot your Pi and it should be ready to rock.



Getting connected

Now that one side of your setup is complete, you’ll need a VNC client on your phone. VNC Viewer seems plenty quick for this purpose and you can’t argue with the price. Optionally, you may also download an SSH client like JuiceSSH for those times when a GUI just isn’t necessary.

With your app downloaded, power up your Pi and connect your phone via a data USB cable. As your Pi boots up, you should get a notification that the phone is now connected as a media device. Go into your phone’s settings and turn on USB tethering.

Assuming you’ve given the Pi enough time to boot, you should now be ready to pull up your desktop. Open your VNC viewer app, connect to 192.168.42.42:1 (the :1 is important here), and provide your password. If you just need to SSH, open up your SSH app and connect to 192.168.42.42.

Congratulations! You now have a Raspberry Pi display, keyboard, and mouse even when you don’t physically have those items available.

Dec 23, 2013

One of the most frequent requests I receive for help in the Linux command line is actually for a little setup I generally use for pranks.  Using the program Motion, you can run a bash script any time a webcam detects movement (among many, many other useful things).  The uses for this are, as you can imagine, endless–especially if you get your setup running so that shell scripts can activate pins on an Arduino, but more on that in a later post.

I thought I’d share the setup and even put a useful twist on it.  Let’s assume you have something cool you’d like to show information about, like a 3D printer.  You make a neat looping slideshow about how it works and what sorts of things it can make.  That’s not good enough, though.  Say you’d like to give people a better view of what’s happening right at the print head whenever it starts moving.  Let’s turn this prank setup into something that can launch a full-screen view onto that monitor whenever motion is detected, and then go back to your slideshow when the motion ends!

The Setup

First, ensure that you have two USB webcams.  There are ways to do this with just one (there’s a way to do everything), but since the camera will already be opened directly by Motion, you won’t be able to open it directly from any other program.  You’ll need to think of a workaround like streaming out a port, then opening the network stream into a video window.

Next, install motion and xawtv (I’m assuming a Debian-based distribution here. These first two commands will be slightly different on RPM-based distributions like Red Hat and OpenSUSE):

01

02

 

Navigate to home if you’re not already there:

03

 

Create a shell script called startCam.sh (whatever editor you prefer—use nano if you don’t know vi):

04

 

Fill it with this code (ignore the tilde at the bottom—that just signifies a blank line):

05

 

Save the file, then make it executable like so:

06

 

Now create a shell script called stopCam.sh and fill it with this code:

07

 

Make it executable as well.

Now open motion.conf. We need to make a few changes in here, all of which can be edited later to give us abilities like streaming the motion camera on a certain port.

08

 

Set your gap to however many seconds after the print heads stop moving you’d like to turn off the display camera and return to whatever is on the screen (like a slideshow):

09

 

Turn off output_normal. We don’t want to clutter up the memory with random security footage:

10

 

Disable ffmpeg_cap_new for the same reason:

11

 

Disable the webcam server:

12

 

Disable HTTP control of motion:

13

 

Now we’re ready to link to the scripts. Find this block in the config:

14

 

Remove the “;”s before each line so that they’re activated, then replace their values with the path to the scripts we made earlier:

15

 

Now, to start it up, ensure that two cameras are connected over USB and then type this command:

16
Dec 20, 2013

Dogecoin, an alternative cryptocurrency similar to bitcoin, has recently become available with clients for Windows, Mac, and Linux, but I’ve found that building on Ubuntu and its derivatives can run into a snag sometimes. When attempting to run make, the build process stops and reports “src/main.cpp:17:53: error: boost/random/uniform_int_distribution.hpp: No such file or directory”.  Luckily, it’s […]

Continue reading...
Fork me on GitHub