0% found this document useful (0 votes)
39 views2 pages

Message

This Python code reads barcodes from a USB barcode scanner connected to a Raspberry Pi. It uses the barcode number to look up product information from an API. It then lights up an LED connected to the Pi to indicate the first digit of the barcode number by turning on the LED pin corresponding to that number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Message

This Python code reads barcodes from a USB barcode scanner connected to a Raspberry Pi. It uses the barcode number to look up product information from an API. It then lights up an LED connected to the Pi to indicate the first digit of the barcode number by turning on the LED pin corresponding to that number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

```

import sys
import requests
import json
import RPi.GPIO as GPIO
import time

api_key = "a94b49b6992dc798428669d3dd99914ce60aca82"

def barcode_reader():
hid = {4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12:
'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm',
17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u',
25: 'v', 26: 'w', 27: 'x', 28: 'y',
29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7',
37: '8', 38: '9', 39: '0', 44: ' ',
45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';', 52: '\'', 53:
'~', 54: ',', 55: '.', 56: '/'}
hid2 = {4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12:
'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M',
17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U',
25: 'V', 26: 'W', 27: 'X', 28: 'Y',
29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&',
37: '*', 38: '(', 39: ')', 44: ' ',
45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':', 52: '"', 53: '~',
54: '<', 55: '>', 56: '?'}
fp = open('/dev/hidraw0', 'rb')
ss = ""
shift = False
done = False
while not done:
buffer = fp.read(8)
for c in buffer:
if ord(c) > 0:
if int(ord(c)) == 40:
done = True
break
if shift:
if int(ord(c)) == 2:
shift = True
else:
ss += hid2[int(ord(c))]
shift = False
else:
if int(ord(c)) == 2:
shift = True
else:
ss += hid[int(ord(c))]
return ss

def UPC_lookup(api_key, upc):


url = f"https://api.upcdatabase.org/product/{upc}/{api_key}"
headers = {'Authorization': 'Bearer THISISALIVEDEMOAPIKEY19651D54X47'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
return None
def main():
LedPins = [12, 16, 18, 22]

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPins, GPIO.OUT)
GPIO.output(LedPins, GPIO.LOW)

def update_led(upc):
product_data = UPC_lookup(api_key, upc)
if product_data:
barcode = product_data.get('barcode')
if barcode:
first_digit = int(barcode[0])
led_index = first_digit % len(LedPins)
GPIO.output(LedPins[led_index], GPIO.HIGH)
time.sleep(5)
GPIO.output(LedPins[led_index], GPIO.LOW)
else:
GPIO.output(LedPins, GPIO.LOW)

try:
while True:
upc_code = barcode_reader()
update_led(upc_code)
time.sleep(5)
except KeyboardInterrupt:
pass

GPIO.output(LedPins, GPIO.LOW)
GPIO.cleanup()

if __name__ == '__main__':
main()
```

You might also like