This project initially started out as a Raspberry Pi printer server. Looking for a new printer I didn’t feel like paying an additional 60 euros for a network enabled printer. I knew our beloved Raspberry Pi could do that job just fine. While waiting for my new printer to arrive I decided to also hook up my old scanner and make it a print and scan server. The scanner part is now finished and can scan straight to Dropbox with the push of a button. Not bad for a 35 euro print and scan server.
I’ll admit right away inspiration for the scanning part this project came from a few other great projects out there (references at the bottom of this post). But non of those projects did exactly what I had in mind and also weren’t described well enough to start building straight away.
The primary goal of this first part of the project was to scan without turning on a computer and pick up the result somewhere. As a bonus I figured it would be nice to see the progress of your scan jobs. So I created my own hardware with a Scan button and progress indication LEDs, selected the right programs and wrote scripts to glue everything together. Let’s see how it’s done.
NOTE: Before reading any further (and attempting this yourself) you should know the GPIO pins of the Raspberry Pi are not protected against high voltages. Incorrect wiring can result in a framboise flambée (yep, a Fried Pi). The hardware isn’t complex but only attempt this if you’re sure you know what you’re doing. I’m in no way responsible for any damage inflicted on your Raspberry Pi.
The first thing to do is check if you’ve got all required ingredients. This project uses the following hardware:
- A Raspberry Pi model B (revision / memory is not important)
- An SD card (2 GB or more)
- A Sane compatible scanner
- Components for GPIO interface board (breadboard, push button, 6 LEDs (5 green, 1 red), 10K Ohm resistor, 300 Ohm resistor, jumper cables)
If you’ve got all the required hardware you should start of with a clean Raspbian “Wheezy” installation. Make sure you re-size the partition (more space for programs and storing scans) and enable SSH. Also give your Raspberry a fixed IP address because it is a server after all.
Once Raspbian is up and running you can start installing the required programs. For scanning images we’ll use Sane. Login to your Pi (directly or over SSH) and type: sudo apt-get update followed by sudo apt-get install sane . After that you need to fix permissions so your user is allowed to scan. To do so enter: usermod -a pi -G scanner . Now it’s time to check if Sane is setup correctly. Type scanimage –L and Sane should list your scanner. Great.
The number of image formats Sane can output is limited. We’ll install ImageMagick so we can convert our images to almost any format. The current version of my setup only supports jpegs but future versions should also allow you to export as (multi page) PDF. To install ImageMagick simply type: sudo apt-get install imagemagick .
While other projects use email or network shares to distribute scanned images I thought it would be really cool if you could upload them directly to Dropbox. Not much information could be found about this and there is also no official Raspberry Dropbox client.
Fortunately Andrea Fabrizi created a great Bash script for uploading files to Dropbox which we will use. Download the script with the following command:
After that make the script executable ( sudo chmod +x dropbox_uploader.sh ). We’ll call this script later from our own script. For now you can leave the script alone.
Since we’ll be using Python to glue everything together and control the LEDs you need to install the RPi.GPIO library. You can do this, surprise surprise, using apt-get:
- sudo apt-get update
- sudo apt-get install python-dev
- sudo apt-get install python-rpi.gpio
The last step can probably be skipped since it is already part of the python-dev package.
The last piece of software needed is the python script to make all the magic happen. I’ve made this script, named scan.py, available on GitHub (raspberry-pi-scan-print-server). It should be placed in your home directory (/home/pi). To do so login to your Pi if you haven’t already and go to your home directory (cd ~) and run the command wget https://raw.github.com/gijsbertpeijs/raspberry-pi-scan-print-server/master/scan.py .
With the software all in place it is time to focus on the hardware. Like said before: the scheme isn’t too difficult but if you don’t feel comfortable doing this just don’t. It’s not worth frying your Pi over this and in the future I’ll write a post about controlling the scanner with your phone anyway.
The circuit we’re trying to make looks like this:
Basically it’s a switch hooked up to GPIO pin 8 using a 10K Ω pull up resistor for a clear On/Off signal. The 6 LEDs are connected to GPIO pins 10, 11, 12, 13, 15 and 16 with a 300Ω resistor to keep voltages at an acceptable level. The resistor for the LEDs should be roughly 300 Ω but a bit more or less doesn’t hurt. Also make sure you connect the LEDs in the right way (minus towards the GPIO pins).
Once you circuit board is finished you can hook it up to your Raspberry Pi (make sure you hook up all pins correctly before powering your Pi. Especially pin 1 (right next to the P1 marking on your Raspberry Pi) is very important.). Once the Raspberry Pi is booted login and type sudo python scan.py (root is required because we’re fiddling with the GPIO connector). The first LED should turn on (System Ready) and after you push the switch your scanner should start scanning.
When the scanner is finished the next LED will go green indicating the tiff file is being converted to a (much smaller) JPEG. After that the Dropbox upload script will kick in. The first time you run it you need to follow the simple on-screen instructions to connect the Raspberry Pi to your Dropbox account. Since you need you copy/paste some links to your browser it helps enormously to perform this part on a PC while connecting to your Raspberry Pi over SSH.
After you finished your Dropbox setup the 5th LED should blink three times telling you everything went well. Push the switch again to test if the script will now run without any user input. If it does go to your Dropbox folder and under Apps/<YourAppName> there should be 2 scans. Well done!
For the finishing touch go to /etc/ on your Raspberry Pi and edit the rc.local file (sudo nano rc.local). Just before the end (exit 0) add a line containing: sudo python scan.py & . This makes the scripts start up automatically on boot. Reboot your Pi and see if the System Ready LED comes up. If it does your in business and you’ve got your Raspberry Pi powered scan server with one-click-to-Dropbox capabilities.
That’s it for part 1 of this series. In part 2 I’ll be setting up the printer bit of the server. But I also got plenty of nice ideas I want to add to the scanner server. What would you think about:
- web interface for scanning (advanced settings)
- control scanner from your smartphone
- scanning to PDF (with multi page support)
- one button copy to printer
So watch this space. There’s lots more to come.
Inspiration for this project came from the following sources: