Raspberry PI USB network scanner

Eduardo Luis came up with the nice idea of using the Raspberry PI to drive a scanner that sends the scanned images via mail by just pressing a button. I think this is a quite useful way to recycle USB-Scanners. However, I found some "glitches" when following his documentation, you can find it here. This is the reason why I added some changes to the original project, and documented these changes here.

This "tutorial" is based on the following assumptions:

  • You have your Raspberry PI up and running, connected to the Internet via LAN or WLAN, I am using the "2013-02-09-wheezy-raspbian" image
  • You have a Mailserver like Exim up and running, and can send mail when using a console client like mutt
  • The Scanner is supported by SANE, when you run "sudo scanimage" there are no errors shown
  • Python is installed
  • You did not make changes on your Raspberry PI based on Eduardo's project, if so, replace the files you have created already, please

First of all we need to adapt the "/etc/rc.local". Add the following lines above the "exit" statement, and do not remove the "exit" statement:

echo 14 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio14/direction
echo 18 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio18/direction
echo 0 > /sys/class/gpio/gpio14/value
echo 0 > /sys/class/gpio/gpio18/value
/root/scripts/blinking.sh &
/root/scripts/script_listen_for_scan.py &
/bin/rm -f /root/scripts/scan.lock

/etc/init.d/ntp stop
/usr/sbin/ntpdate ptbtime1.ptb.de
/etc/init.d/ntp start

The last 3 lines just update the systems time on each reboot - I think this is useful. But these lines are not part of the current Scanner project, so it is completely on you.

I did not touch the "/root/scripts/blinking.sh", so it looks like this:

while true; do
echo 1 > /sys/class/gpio/gpio14/value
sleep 0.05
echo 0 > /sys/class/gpio/gpio14/value
sleep 0.09
echo 1 > /sys/class/gpio/gpio14/value
sleep 0.05
echo 0 > /sys/class/gpio/gpio14/value
sleep 5

Make that script executable by doing a "sudo chmod 755 /root/scripts/blinking.sh".

The next script we need to take care for is the "/root/scripts/script_listen_for_scan.py", which controls the press of the button to start the scan job. I found some glitches here I did not like to live with, so I changed it to this:

#!/usr/bin/env python
#coding: utf-8
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setup(23, GPIO.IN)
#GPIO.setup(24, GPIO.IN)
inputcount = 0
while True:
    if (GPIO.input(23)):
        inputcount = inputcount  + 1
        if (inputcount > 3):
            inputcount = 0
            print("Start Scan")
            os.system("/root/scripts/scan.sh &")
        #print("reset- no input")

Please note the comments ("#"), useful for debugging - just uncomment them.

In Eduardo's script he is referring to GPIO24, but he never uses this IO during his whole project. Therefore I deactivated that part. Another change is about blocking the scanner from running several scan-jobs at the same time, maybe by pressing the button several times. My script is waiting for 3 seconds, which means you have to press the button for 3 seconds to start the scan-job. Make the script executable by "sudo chmod 755 /root/scripts/script_listen_for_scan.py".

The last script is the script that does the scan-part itself, the "/root/scripts/scan.sh":

cd /root
if [ -f /root/scripts/scan.lock ]
    exit 1

/usr/bin/touch /root/scripts/scan.lock
/bin/rm -rf /root/sent.lock # to ensure that if something goes wrong, next time system will not hang up because of this
echo 1 > /sys/class/gpio/gpio18/value # process initiated – let’s turn led RED on
scanimage --resolution 200dpi -x 215 -y 285 > /root/scan_job.pnm # scan and save the file
/usr/bin/convert /root/scan_job.pnm /root/scan_job.jpg
/usr/bin/convert /root/scan_job.jpg -compress jpeg /root/scan_job.pdf # convert to pdf
cd /root # ensure to run as root to avoid the mutt to not send the file
echo "Scan" | /usr/bin/mutt -F /etc/Muttrc -a /root/scan_job.pdf -s "Scan" -- Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!
echo 0 > /sys/class/gpio/gpio18/value # job completed – Turn RED led off.
/bin/rm /root/scripts/scan.lock

To prevent the scan job from running twice or more often at the the same time I added a lock-functionality. Furthermore you need to add some vars you get from the user environment when being logged it. Keep in mind, later there is no user environment running, since the scanner-"daemon" will start on booting the system. Another change by me is just to create a PDF out of the scan, at least for the recipients of my scans PDFs are easier to handle than Jpegs. And finally, don't forget to change "Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!" to something useful. Make the script executable by "sudo chmod 755 /root/scripts/scan.sh".

We are done now with the software part. Time for a small test: put something to scan into your scanner, and run "sudo /root/scripts/scan.sh". Satisfied?

It is time now to do the hardware part. I am not an electronic guru, and therefore I was quite confused when reading advice like "just use a pullup resistor"...so I will go into detail a bit - without being to technical. However, power off your Raspberry PI to avoid damage.

If you just have just connected one of the GPIOs to ground or 3.3V+, you might see kind of fast switching of that GPIO. This is a well known behavior of the Raspberry PI. You have to provide a clear Ground in case the button is not pressed, and a clear V+ when the button is pressed. To achieve this, you have to use 1 additional part, a 10 kOhm resistor - the gurus have fantastic names for that like "pull up" or "pull down" resistor, and expect everybody to know the details (LOL). Let's assume you connect the switch to V+3.3 (Pin 1). The other side of this switch will be connected with two wires: one wire going directly to GPIO23 (Pin 16), and the other wire you have to connect to the 10 kOhm resistor. The other side of the resistor will be connected to GND (Pin 14 as example).

Now we need to connect a LED (yellow), just to show the Raspberry is ready to use. Connect the short pin of the LED (-) to a resistor of 270 Ohm, and the other side of the resistor to GND (Pin 9 as example). Now connect the longer pin of the LED (+) with GPIO14 (Pin 8).

The last LED is a red one. Connect the shorter pin of the LED (-) to a 270 Ohm resistor, and connect the other side of that resistor to GND (Pin 3 as example). Now connect the longer pin of the LED (+) with GPIO18 (Pin 12).

Check the wiring, especially for shortcuts etc. Looks good? Power on the Raspberry PI. After booting the yellow LED should flash from time to time, and shows you the Raspberry is up and running. Now press the button, hold it for 3 seconds until the red LED shines bright. The scanner will start its task, finish, the Raspberry will convert the image into PDF, and send it to you via mail. After handing over the mail to Exim (or your MTA), the red LED goes off. If nothing is happen, check on the console via "ps fax" if both the "blinking.sh" and "script_listen_for_scan.py" are in the process list.

Any comments? Join the Raspberry PI communities on Google+ as example:

...I will be there


  • Keine Kommentare gefunden

Einen Kommentar verfassen

0 / 500 Zeichen Beschränkung
Dein Text sollte zwischen 2-500 Zeichen lang sein

Diese Seite teilen