The Purpose of the app is to save medical info for the users and get it linked with NFC tag…there should be medical id of each user.
I have created a streamlit application and it is opening but as I don’t have nfc tag currently, so have no idea like how it will work when nfc tag is detected nearby.
The code is:
import streamlit as st
import sqlite3
import nfc
from datetime import datetime
# Initialize SQLite database
conn = sqlite3.connect('********.db', check_same_thread=False)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nfc_tag_id TEXT UNIQUE,
name TEXT,
age INTEGER,
blood_type TEXT,
allergies TEXT,
medications TEXT,
medical_conditions TEXT,
emergency_contact TEXT,
insurance_info TEXT,
last_checkup DATE
)
''')
def save_medical_info(nfc_tag_id, name, age, blood_type, allergies, medications, conditions, contact, insurance, checkup):
try:
c.execute('''
INSERT INTO users (nfc_tag_id, name, age, blood_type, allergies, medications, medical_conditions, emergency_contact, insurance_info, last_checkup)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (nfc_tag_id, name, age, blood_type, allergies, medications, conditions, contact, insurance, checkup))
conn.commit()
st.success("Information saved successfully!")
except sqlite3.IntegrityError:
st.error("Error: NFC Tag ID already exists or invalid data")
def read_nfc_tag():
try:
# You may need to adjust 'usb', 'ttyACM0', or 'serial' based on your NFC reader connection
connection_type = 'usb' # Default to 'usb', adjust as needed
if connection_type == 'usb':
clf = nfc.ContactlessFrontend('usb')
elif connection_type == 'ttyACM0':
clf = nfc.ContactlessFrontend('ttyACM0')
elif connection_type == 'serial':
clf = nfc.ContactlessFrontend('serial')
else:
raise ValueError(f"Unsupported connection type: {connection_type}")
tag = clf.connect(rdwr={'on-connect': lambda tag: False})
tag_id = tag.identifier.decode('utf-8')
st.write(f"Raw Tag ID: {tag.identifier}")
return tag_id
except Exception as e:
st.error(f"Error reading NFC tag: {e}")
return None
def main():
st.title("Holistica World")
nfc_tag_detected = False
user_id = None
if st.button("Search for NFC Tag"):
user_id = read_nfc_tag() # Try to read NFC tag
if user_id:
nfc_tag_detected = True
st.write(f"NFC Tag ID: {user_id}")
else:
st.write("NFC tag not detected. Please enter information manually.")
nfc_tag_detected = False
with st.form(key='medical_info_form'):
if not nfc_tag_detected:
user_id = st.text_input("Enter User ID", value='')
name = st.text_input("Name")
age = st.number_input("Age", min_value=0)
blood_type = st.text_input("Blood Type")
allergies = st.text_input("Allergies")
medications = st.text_input("Medications")
conditions = st.text_input("Medical Conditions")
contact = st.text_input("Emergency Contact")
insurance = st.text_input("Insurance Info")
checkup = st.date_input("Last Checkup Date")
submit_button = st.form_submit_button("Save Information")
if submit_button:
# Save the new information
save_medical_info(user_id if nfc_tag_detected else None, name, age, blood_type, allergies, medications, conditions, contact, insurance, checkup)
if __name__ == '__main__':
main()
# Close the database connection when the app ends
conn.close()
When I am clicking on Search For NFC Tag button, I am getting error which is
Error reading NFC tag: [Errno 19] No such device.
Rest, If I am entering user information manually, then it is being stored in sqlite3 db correctly.
What correction do I need to make in my code so that it will get linked to nfc tag. Any idea?