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:
#!/bin/bash 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 done
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.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) #GPIO.setup(24, GPIO.IN) inputcount = 0 while True: if (GPIO.input(23)): inputcount = inputcount + 1 #print("Trigger") if (inputcount > 3): inputcount = 0 print("Start Scan") os.system("/root/scripts/scan.sh &") else: inputcount=0 #print("reset- no input") sleep(1)
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":
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