from machine import Pin
import time
# ---------------------------------------------------------
# 1. HARDWARE SETUP
# ---------------------------------------------------------
# Button: Connected to GP22 with Internal Pull-Up
button = Pin(22, Pin.IN, Pin.PULL_UP)
# LEDs: Red for 'Waiting', Green for 'Success/Registered'
red_led = Pin(27, Pin.OUT)
green_led = Pin(17, Pin.OUT)
file_name = "attendance.csv" # To store guest data
# ---------------------------------------------------------
# 2. DRINKS DATABASE (Knowledge Base)
# ---------------------------------------------------------
# Includes health-focused options aligned with UAE hospitality
drinks = [
{"Name": "Orange Juice", "Type": "Fruit", "Sugar": 20, "Cals": 120},
{"Name": "Lemon Water", "Type": "Fruit", "Sugar": 2, "Cals": 5},
{"Name": "Green Tea", "Type": "Tea", "Sugar": 0, "Cals": 5},
{"Name": "Arabic Coffee", "Type": "Coffee", "Sugar": 0, "Cals": 2},
{"Name": "Chocolate Milk", "Type": "Milk", "Sugar": 22, "Cals": 150},
{"Name": "Water", "Type": "None", "Sugar": 0, "Cals": 0}
]
# ---------------------------------------------------------
# 3. AI & ANALYSIS LOGIC
# ---------------------------------------------------------
# Feature: Analyze popularity from previous logs (Social Proof)
def get_popular_drinks():
popularity = {}
try:
with open(file_name, "r") as f:
for line in f:
parts = line.strip().split(",")
if len(parts) >= 5:
chosen = parts[4] # Index 4 is the recommended drink
popularity[chosen] = popularity.get(chosen, 0) + 1
except:
pass
return popularity
# Feature: Multi-factor Recommendation Engine
def smart_recommend(pref_type, health_pref):
pop_data = get_popular_drinks()
recommendations = []
p_type = pref_type.lower().strip()
h_pref = health_pref.lower().strip()
for d in drinks:
score = 0
reasons = []
# Logic A: Preference Match (+3 pts)
if p_type in d["Type"].lower():
score += 3
reasons.append("Matches your style")
# Logic B: Health Analysis (Priority) (+5 pts)
if h_pref == "low sugar" and d["Sugar"] < 5:
score += 5
reasons.append("Healthy low-sugar option")
elif h_pref == "low calories" and d["Cals"] < 50:
score += 5
reasons.append("Low-calorie choice")
# Logic C: Social Proof (Popularity) (+1 pt)
if d["Name"] in pop_data:
score += 1
reasons.append("Popular among guests")
recommendations.append({
"Name": d["Name"],
"Score": score,
"Reason": " & ".join(reasons) if reasons else "Safe choice"
})
# Sort: Highest score first
recommendations.sort(key=lambda x: x["Score"], reverse=True)
return recommendations[:3]
# ---------------------------------------------------------
# 4. MAIN OPERATIONAL LOOP
# ---------------------------------------------------------
print("="*45)
print(" SMART AI GUEST SYSTEM - FULL VERSION")
print("="*45)
while True:
# --- STANDBY MODE ---
green_led.value(0)
red_led.value(1) # Red light indicates waiting for guest
print("\n[READY] System Waiting... Press the button to start.")
# Wait for button press (Value 0 means pressed in PULL_UP)
while button.value() == 1:
time.sleep(0.05)
# --- ACTIVE MODE ---
red_led.value(0)
green_led.value(1) # Green light indicates processing/success
print("\n[EVENT] Guest detected! Initializing AI Analysis...")
time.sleep(1) # Simulated processing time
# Data Input (User Interface Simulation)
name = input(" > Guest Name: ").strip()
p_drink = input(" > Drink Preference (Fruit/Tea/Milk/Coffee): ")
p_health = input(" > Health Needs (Low Sugar/Low Calories/None): ")
print("\n[AI] Analyzing health data and generating recommendation...")
time.sleep(1.5)
# Run AI Logic
results = smart_recommend(p_drink, p_health)
top_pick = results[0]["Name"]
# Save to CSV (Registration)
timestamp = time.localtime()
time_str = "{}:{}:{}".format(timestamp[3], timestamp[4], timestamp[5])
try:
with open(file_name, "a") as f:
# Format: Name, Time, Preference, Health, Recommendation
f.write("{},{},{},{},{}\n".format(name, time_str, p_drink, p_health, top_pick))
print("✅ Attendance recorded & saved to attendance.csv")
except Exception as e:
print("❌ Data Error:", e)
# Display Smart Recommendations
print("\n" + "-"*40)
print("AI TOP RECOMMENDATIONS FOR {}:".format(name.upper()))
for i, res in enumerate(results, 1):
print("{}. {} (Score: {})".format(i, res["Name"], res["Score"]))
print(" Reason: {}".format(res["Reason"]))
time.sleep(4) # Pause to allow reading results before resetting