Home scan station based on Raspberry Pi

Aim

Scan documents, photos and everything with one button press. Copy scanned images to common file server (network attached server, NAS).

  • Use simple USB scanner as network attached independent device.
  • No need to use PC for scan.
  • No need to install scanner drivers on all computers in home network.
  • Easy to use:
    • Turn on the system
    • Insert paper to scan
    • Press “Scan” button on scanner
    • Wait until scan is done
    • Find image file on file server later on

Hardware

I have used following gadgets to create a scan station

0. Home LAN: Router, swithches, LAN wires, etc.

1. Raspberry Pi RBCA000 mainboard (ARM 1176JZF-S, 512MB RAM, HDMI, 2x USB 2.0, 3,5 Watt)

Raspberry Pi RBCA000 Mainboard wired

1.2 Connection cables: HDMI-DVI, USB (diverse, see below), LAN

1.3 Raspberry Pi case (optional)

Raspberry Pi Case Black Empty

2. Canon CanoScan LIDE 110 scanner

Canon CanoScan LIDE 110 Scanner3. NAS (Network Attached Storage) server

4. Display with HDMI or DVI input and an embedded USB-hub (or, optionally, USB-hub with power adapter)

5. USB keyboard and mouse (optional)

Software

1. Raspbian Wheezy operating system from NOOBS Linux distribution for Raspberry Pi

2. SANE (Scanner Access Now Easy) distribution for Debian

3. Canon LiDE 110 scanner drivers for SANE

4. Scanner Button Daemon for Linux

5. Imagemagick package for Debian

Wiring

1. Connect USB hub (embedded into display) to power input of Raspberry Pi using micro-USB cable:

MicroUSB

2. Connect USB port of Raspberry Pi back to input of USB hub with normal USB cable (may vary depending on hub input type):

USB Old

3. Connect scanner to USB hub (not Raspberry Pi directly!) using Mini USB cable:

MiniUSB

4. Connect Raspberry Pi to your LAN:

Patch Cord

Installation

Download and install software

See instructions from the links at software section above.

Checklist:

  • Raspbian OS is installed and updated to latest version
  • SANE package is installed: scanimage command is available
  • Scanner driver is installed and correctly configured: scanimage -L command finds the scanner
  • Scan buttons daemon is installed: scanbd command is available
  • Imagemagick is installed: convert command is available

Configure output for image files

I’m going to copy all scanned images to my already installed NAS. See NAS documentation about it’s configuration and connection to the network. Assume, that NAS drive is accessible over SMB (CIFS) interface, like an usual Windows file share.

Checklist:

  • NAS is accessible from Raspberry Pi
  • NAS IP address is known. ping command to this address doesn’t show connection problems
  • Using Raspbian file browser, it is possible to view content of the NAS
  • NAS target folder (where to copy imabe files) is writable. Using file browser it is possible to copy any file to this folder
  • NAS IP address is fixed, or DHCP name is accessible and correctly resolves to NAS: Check in the router administration interface

Configure scan buttons

In order to avoid any manual interaction with Raspberry Pi but pressing buttons on the scanner, I use scan button daemon. See scanbd above.

All configuration files are placed in folder /usr/local/etc/scanbd.

My scanner (CanoScan LIDE 110) has several buttons, but I really use only one of them: “Scan”. Example configuration below does everything only with this button.

File /usr/local/etc/scanbd/scanbd.conf configures actions for each button. Here is a part of my configuration for “Scan”:

...
        action scan {
                filter = "^scan.*"
                numerical-trigger {
                        from-value = 1
                        to-value   = 0
                }
                desc   = "Scan to file"
                # script must be an relative path starting from scriptdir (see above), 
                # or an absolute pathname. 
                # It must contain the path to the action script without arguments
                # Absolute path example: script = "/some/path/foo.script 
                script = "action.script"
        }
...

This part of configuration defines a name of a separate script file, which will be started as soon as daemon detects a button press.

You can also configure logging level for daemon (in debugging purposes), and set user and group to define privileges for running scripts.

Script below (/usr/local/etc/scanbd/action.script) is actually standard (provided with scanbd) and can hanlde multiple commands. I use only one command scan. See highlighted lines below.

#!/bin/bash

scan_dir=/home/pi/scan

datetime=`date +%F_%H%M%S`
filename=scan-$datetime
...

 scan)
  logger -t "scanbd: $0" "$SCANBD_ACTION - scanning --resolution 300 --mode Color --depth 8 --format=tiff to $scan_dir/$filename.jpg"
  echo heartbeat >/sys/class/leds/led0/trigger
  scanimage -d $SCANBD_DEVICE --resolution 300 --mode Color --depth 8 --format=tiff | convert tiff:- $scan_dir/$filename.jpg
  echo mmc0 >/sys/class/leds/led0/trigger
  logger -t "scanbd: $0" "Finished scanning"
  ;;

...

What the script actually does:

  • Writes start message to system log
  • Starts blinking (heartbeat) with Raspberry “ACT” LED
  • Starts scanning of image in tiff format into output stream
  • Converts tiff stream to jpeg using imagemagick and saves the image directly to NAS. Note the target directory definition scan_dir and file name format, which contains date and time of scan
  • Stops blinking with “ACT” LED
  • Writes finish message to system log

Configure autostart

Last step: Make scan button daemon start automatically when Raspberry Pi reboots or being switched on.

1. Copy (as root) scanbd.debian to /etc/init.d

2. Check correct path in following lines in scanbd.debian:

NAME=scanbd
DAEMON=/usr/local/sbin/$NAME

2.1 Remove or comment following line out:

export SANE_CONFIG_DIR=/usr/local/etc/scanbd

3. Configure auto-start

http://www.stuffaboutcode.com/2012/06/raspberry-pi-run-program-at-start-up.html

Register script to be run at start-up
To register your script to be run at start-up and shutdown, run the following command:

sudo update-rc.d scandb.debian defaults

Note – The header at the start is to make the script LSB compliant and provides details about the start up script and you should only need to change the name. If you want to know more about creating LSB scripts for managing services, see http://wiki.debian.org/LSBInitScripts

If you ever want to remove the script from start-up, run the following command:

sudo update-rc.d -f scandb.debian remove

Useful commands

1. Run scanbd

sudo scanbd

1.1 Mount file server (if not mounted yet)

sudo mount.cifs -v //nas-address/share /home/pi/nas -o guest,sec=ntlm

1.1.1 If mount is getting lost after reboot – open /etc/fstab as root and add following line:

#...
//nas-address/share /home/pi/nas cifs sec=ntlm,password=        0       0

2. Check scanbd is running

ps aux | grep scanbd

3. Monitor system messages

tail -f /var/log/messages

3.1 Monitor all debug messages

tail -f /var/log/syslog

4. Update settings (do not forget to restart if updating scanbd.conf)

sudo leafpad /usr/local/etc/scanbd/scanbd.conf

and

sudo leafpad /usr/local/etc/scanbd/action.script

5. Stop scanbd

ps aux | grep scanbd
sudo kill #####

6. Restart scanbd

  • See stop above
  • See start above

7. Find where scanbd is really placed

which scanbd

8. Controlling onboard LED “ACT” (only root)
Raspberry Pi mainboard

“ACT” LED can be controlled programmatically.
See http://www.raspberrypi.org/forum/viewtopic.php?f=31&t=12530 and http://www.netzmafia.de/skripten/hardware/RasPi/RasPI_OnboardLED.html

Raspberry Pi onboard LEDs

8.1 Info about trigger

cat /sys/class/leds/led0/trigger

8.2 Switch trigger off

echo none >/sys/class/leds/led0/trigger

8.3 Blink (hearbeat or timer)

echo heartbeat >/sys/class/leds/led0/trigger

8.4 Direct control (on/off)

echo 1 >/sys/class/leds/led0/brightness
echo 0 >/sys/class/leds/led0/brightness

8.5 Restore trigger

echo mmc0 >/sys/class/leds/led0/trigger

Troubleshooting

Autostart permission problem

(see solution below)

sudo service scanbd.debian start
tail -n 100 -f /var/log/syslog
 Apr 25 19:30:18 raspberrypi scanbd: /usr/local/sbin/scanbd: Not Primary Owner (-1)
 Apr 25 19:30:18 raspberrypi scanbd: /usr/local/sbin/scanbd: Name Error (Connection ":1.12" is not allowed to own the service "de.kmux.scanbd.server" due to security policies in the configuration file)
 Apr 25 19:30:18 raspberrypi scanbd: /usr/local/sbin/scanbd: udev fd is non-blocking, now setting to blocking mode
 Apr 25 19:31:21 raspberrypi scanbd: /usr/local/sbin/scanbd: exiting scanbd
 scanbd: reading config file /usr/local/etc/scanbd/scanbd.conf
 scanbd: debug on: level: 4
 scanbd: dropping privs to uid root
 scanbd: dropping privs to gid root
 scanbd: drop privileges to gid: 0
 scanbd: Running as effective gid 0
 scanbd: drop privileges to uid: 0
 scanbd: Running as effective uid 0
 scanbd: dbus_init
 scanbd: dbus match type='signal',interface='org.freedesktop.Hal.Manager'
 scanbd: sane version 1.0
 scanbd: Scanning for local-only devices
 scanbd: found device: genesys:libusb:001:005 Canon LiDE 110 flatbed scanner
 scanbd: start_sane_threads
 scanbd: Starting poll thread for genesys:libusb:001:005
 scanbd: Thread started for device genesys:libusb:001:005
 scanbd: start dbus thread
 scanbd: Not Primary Owner (-1)
 scanbd: Name Error (Connection ":1.18" is not allowed to own the service "de.kmux.scanbd.server" due to security policies in the configuration file)
 scanbd: udev init
 scanbd: get udev monitor
 scanbd: sane_poll
 scanbd: udev fd is non-blocking, now setting to blocking mode
 scanbd: start udev thread
 scanbd: udev thread started
 scanbd: found 41 options for device genesys:libusb:001:005
 scanbd: sane_find_matching_options
 scanbd: found 5 actions in section (null)
 scanbd: checking action scan with filter: 

 genesys:libusb:001:005
 scanbd: checking action preview with filter: genesys:libusb:001:005
 scanbd: sane_thread_cleanup_mutex
 scanbd: closing device genesys:libusb:001:005
 scanbd: freeing opt ressources for device genesys:libusb:001:005 thread
 scanbd: freeing funtion ressources for device genesys:libusb:001:005 thread
 scanbd: stop dbus thread
 scanbd: stop udev thread
 scanbd: cleanup device handler
 scanbd: join udev thread
 scanbd: close udev monitor
 scanbd: close udev
 scanbd: exiting scanbd
sudo service scanbd.debian start

 

 /usr/local/sbin/scanbd: config-file: /usr/local/etc/scanbd/scanbd.conf
 /usr/local/sbin/scanbd: reading config file /usr/local/etc/scanbd/scanbd.conf
 /usr/local/sbin/scanbd: debug on: level: 4
 ...
 /usr/local/sbin/scanbd: start_sane_threads
 /usr/local/sbin/scanbd: start dbus thread
 /usr/local/sbin/scanbd: Not Primary Owner (-1)
 /usr/local/sbin/scanbd: Name Error (Connection ":1.20" is not allowed to own the service "de.kmux.scanbd.server" due to security policies in the configuration file)
 /usr/local/sbin/scanbd: udev init
 /usr/local/sbin/scanbd: get udev monitor
 /usr/local/sbin/scanbd: udev fd is non-blocking, now setting to blocking mode
 /usr/local/sbin/scanbd: start udev thread
 /usr/local/sbin/scanbd: udev thread started
 /usr/local/sbin/scanbd: sig_term/int_handler called with signal 2
 /usr/local/sbin/scanbd: stop_sane_threads
 /usr/local/sbin/scanbd: stop dbus thread
 /usr/local/sbin/scanbd: stop udev thread
 /usr/local/sbin/scanbd: join udev thread
 /usr/local/sbin/scanbd: cleanup device handler
 /usr/local/sbin/scanbd: close udev monitor
 /usr/local/sbin/scanbd: close udev
 /usr/local/sbin/scanbd: exiting scanbd

Solution for autotstart daemon

(see problem above)

leafpad /etc/init.d/scanbd.debian

Remove or comment following line:

export SANE_CONFIG_DIR=/usr/local/etc/scanbd

Scanner detection problem

To chect the scanner avalilability run the command:

scanimage -L

Problem: Scanner not found.

No scanners were identified. If you were expecting something different,
check that the scanner is plugged in, turned on and detected by the
sane-find-scanner tool (if appropriate). Please read the documentation
which came with this software (README, FAQ, manpages).

USB devices list shows the scanner device:

lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 05e3:1205 Genesys Logic, Inc. Afilias Optical Mouse H3003 / Trust Optical USB MultiColour Mouse MI-2330
Bus 001 Device 005: ID 04cc:1520 ST-Ericsson USB 2.0 Hub (Avocent KVM)
Bus 001 Device 006: ID 04a9:1909 Canon, Inc. CanoScan LiDE 110
Bus 001 Device 007: ID 045e:07b9 Microsoft Corp.

Possible problem reason

Scanner may be locked by some running program. For example if your xsane (scanner front end) is running at the moment, or if scanbd is active.
Only one program can access the scanner at the time. Multiple programms cannot use the scanner simultaneously.

If scanbd is running – shut down the service and try to identify the scanner again.

sudo service scanbd stop
scanimage -L
device `genesys:libusb:001:006' is a Canon LiDE 110 flatbed scanner

Date or time in generated file names is incorrect

Timezone correction

See http://elinux.org/R-Pi_Troubleshooting#The_time_is_incorrect

sudo dpkg-reconfigure tzdata