USB Barcode Scanner (Raspberry Pi)

Introduction: USB Barcode Scanner (Raspberry Pi)

About: the Raspberry Pi is Awesome!

Basic tutorial of how to setup a USB barcode scanner with the Raspberry Pi.

Step 1: Parts

Step 2: Setup

1. Plug in USB Barcode Scanner to Raspberry Pi

2. Boot Pi and open terminal

Type "sudo raspi-config"

3. Go to interfacing options and enable "Serial"

4. Install dependencies

Type "pip install requests"

5. Navigate to https://upcdatabase.org/ and create a free account and take note of api key

6. Edit line 6 of barcode.py and enter your api key

7. Type "sudo python barcode.py" to run script (press ctrl+c to exit script)

Step 3: Code

Dependencies:

"pip install requests"

run:

sudo python barcode.py

Step 4: Additional Info

1 Person Made This Project!

Recommendations

  • DIY Summer Camp Contest

    DIY Summer Camp Contest
  • Backyard Contest

    Backyard Contest
  • Metal Contest

    Metal Contest

18 Comments

0
freek herregods
freek herregods

2 months ago

I get the error "line 32, in barcode_reader
if ord(c) > 0:
TypeError: ord() expected string of length 1, but int found" in your code, anyone that knows how to fix this issue?

0
negrotico19
negrotico19

Tip 2 months ago

Hello, I was able to follow the guide and make it work in the RPI3 by using buster:
Distributor ID:Raspbian
Description:Raspbian GNU/Linux 10 (buster)
Release:10
Codename:buster

One tip. It is not mentioned how to install pip in case of required:
```sudo apt-get install python-pip```

0
thone
thone

1 year ago

I just wanted to add a tip. I found that if I had anything else plugged in, like a mouse or keyboard, the scanner didn't always default to hidraw0 which broke the program. So to avoid having to manually change this every time, I did the following.

1. Install pyudev (https://pyudev.readthedocs.io/en/latest/guide.html):
sudo pip install pyudev
Note: This needs to be installed for the root since the script has to be run with root permissions.
2. Determine which hidraw is your scanner.
- start with your scanner unplugged
- open a terminal on your raspberry pi and type: udevadm monitor
- plug in your scanner and it will print a lot of lines
- go through the text that printed and determine which hidraw# the scanner was assigned to
Note: for this example my scanner is hidraw0
3. Find a unique identifier for your scanner
type python3
>>>import pyudev as pd # This imports the function
>>>context = pd.Context() # pyudev requires a context to be used
>>> for device in context.list_devices(subsystem='hidraw'):

... print(device)

(make sure you have 4 spaces before print(device) and hit enter twice)
Copy the path right before /hidraw/, mine printed like this:
Device('/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/0003:046A:B091.0014/hidraw/hidraw1')
Device('/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.4/1-1.4:1.0/0003:0416:C141.0013/hidraw/hidraw0')

The information after the period will change depending on which usb port it's plugged into. So you will want to copy the part up to the period. For me this is 0003:0416:C141
4. Add the pyudev code to provide dynamic support of your scanner.

To do this open the python script in your preferred code editor or text editor and add the following code after the last import line

import pyudev as pd
context = pd.Context()
for device in context.list_devices(subsystem='hidraw'):
if ('{0}'.format(device.find_parent('hid'))).find('0003:0416:C141') != -1:
location = device.device_node
Make sure you replace the 0003:0416:C141 with your device information

change def barcode_reader(): to def barcode_reader(device_local):
change fp = open('/dev/hidraw0', 'rb') to fp = open(device_local, 'rb')

now when you call the function you need to add the location i.e. UPC_lookup(api_key,barcode_reader(location)) instead of UPC_lookup(api_key,barcode_reader())

I hope this helps!
0
stevedyas
stevedyas

Reply 7 months ago

I'm a noob here and to Rpi and Python and this comment just helped me SOOO much! Thank you!!!

0
thone
thone

Reply 7 months ago

I'm glad I could help. Often times I go back and use my own comments as notes if it's been a while.

0
AkiraR2
AkiraR2

Question 3 years ago on Step 4

It shows an error called
No such file/directory dev/hidraw0 exists
how should I fix it?

0
piddlerintheroot
piddlerintheroot

Answer 3 years ago

What version RPI are you running it on? Are you running it in sudo? Try running the updated the code.

0
yusufgmi97
yusufgmi97

Reply 1 year ago

i think because you did not plug in your barcode scanner,if you plug in the barcode scanner,the hidraw0 file will appear in dev folder.

0
yusufgmi97
yusufgmi97

1 year ago

1)i did not find directory of /dev/hidraw0,'rb',any file that i can used to connect with my raspberry?
(pi3 b+ and raspbian buster)

2)i try used /dev/ttyAMA0,the barcode.py is didnt have any error,but the terminal didnt show any result

0
thone
thone

Reply 1 year ago

I just posted a comment that might help you with this.

0
yusufgmi97
yusufgmi97

Reply 1 year ago

ok tq thone

0
skishn
skishn

2 years ago

I get this error message when running on my pi B+ using sudo:

{
"status": 400,
"message": "Bad Request. The code you are trying to enter does not contain all digits.",
"error": true
}

The barcode that gets reported on the terminal does include all digits. When I try to check the code on the website using my api key, I get an item not found, but that's different from this error. Any ideas?

Sample UPC (bottle of tylenol):
050428377512

0
piddlerintheroot
piddlerintheroot

Reply 2 years ago

Looks like the request is malformed. I wrote the script a while ago, so most likely the UPC_lookup function is deprecated as it was written for the old API. https://www.upcdatabase.com/xmlrpc.asp provides documentation for the new API.

0
JohnphilC
JohnphilC

Question 3 years ago on Step 3

where do i find the api key? what does it look like?

0
tssparky
tssparky

Question 3 years ago

Is a pi3 mandatory? I have a pi2, and I'm running into difficulties getting python script to respond with python2, and python3 can't install request.

0
Guus_e
Guus_e

Question 3 years ago

Hello, i'm completely new with the raspberrypi. I've used arduino alot but was wondering what u use the micro SD and jumper cables for. I can't find any use for them in your project, do you have a cicuit I can follow?

0
bivav
bivav

3 years ago

Hi there... I just wanted to know if it is possible to buy any bar code scanner to work with this code.. can we?

Thank you in advance!!