The purpose of the application will be to save medical information of the users and get it linked with NFC tag. I have taken medical id’s and there should be different medical id for each user.
The code that I have made is able to open streamlit application using VS Code.
import streamlit as st
import sqlite3
import nfc
from datetime import datetime
# Initialize SQLite database
conn = sqlite3.connect('medical_info.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()
I have a question that when I am clicking on Search for NFC tag, it is immediately display error of Error reading NFC tag: [Errno 19] No such device. So, I am entering manually. Can it be modified in such a way that when streamlit application opens, it will detect if any NFC tag or nfc is nearby, it will display else manually user will enter information and save it.
Please help.