As mentioned in the previous post, one use case is to load a picture of the real engine from my online database when the model goes over the RFID reader. Here's the big plan:
In this post, I am focusing on the link between the RFID reader and the Airtable link to display a picture. I'm therefore not looking at the ampoule tag and I'm not yet using the Raspberry Pi, but rather my desktop.
Reading from the Arduino
It's actually very easy to read the RFID tag from PyCharm on my desktop. Here's the code:
importserial
prefix ='Card UID: '
ser = serial.Serial('COM3', baudrate=9600, timeout=0.1)
while1:
data = ser.readline().decode('ascii')
if data.startswith(prefix):
tag_id = data.replace(prefix, '').strip()
print(tag_id)
And the output is:
C:\Tools\python37env\Scripts\python.exe C:/Users/Olivier/dev/workspace/airtable/test/test_arduino.py
8378 FF 20
Process finished withexit code -1
Airtable
Airtable is basically a smart spreadsheet in the cloud. For this test, I just want a simple table with a name, a picture, and a RFID tag. It looks like this:
importosimportrequestsimportserialfromairtableimport Airtable
frompathlibimport Path
frompprintimport pprint
airtable_base_key ='appC0WNPsCWPblK9H'
airtable_table_name ='Test'
arduino_tag_prefix ='Card UID: '
path_local_cache ='images/'
use_local_cache =Falsedefget_local_filename_from_tagid(tag):
return path_local_cache + tag.replace(' ', '_') +'.jpg'defget_from_airtable(tag_id, file_name):
pprint('Calling AirTable for {}'.format(tag_id))
airtable = Airtable(airtable_base_key, airtable_table_name, api_key=os.environ['AT_API_KEY'])
results = airtable.search('RFID', tag_id)
iflen(results) ==0:
pprint('No results found for RFID Tag {}'.format(tag_id))
else:
iflen(results) >1:
pprint('More than 1 result found for RFID Tag {}'.format(tag_id))
url = results[0]['fields']['Image'][0]['url']
f =open(file_name, 'wb')
f.write(requests.get(url).content)
f.close()
defdisplay(tag):
# get local filename and check local cache if required
filename = get_local_filename_from_tagid(tag)
if use_local_cache:
pprint('Looking in local cache for {}'.format(tag_id))
local_file = Path(filename)
ifnot local_file.is_file():
get_from_airtable(tag, filename)
else:
get_from_airtable(tag, filename)
os.system('start {}'.format(filename))
ser = serial.Serial('COM3', baudrate=9600, timeout=0.1)
while1:
data = ser.readline().decode('ascii')
if data.startswith(arduino_tag_prefix):
tag_id = data.replace(arduino_tag_prefix, '').strip()
display(tag_id)