import os, serial, time, datetime import google.generativeai as genai from dotenv import load_dotenv # Add the .env file and put your GOOGLE_API_KEY load_dotenv() genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) model = genai.GenerativeModel("gemini-2.0-flash") # Open serial # This is for MAC. Adjust the serial port if needed! ser = serial.Serial('/dev/cu.usbmodem2101', 9600, timeout=1) time.sleep(2) print("✅ Connected to Arduino") while True: if ser.in_waiting > 0: line = ser.readline().decode(errors="ignore").strip() if not line: continue print("👉 Arduino says:", line) if line == "request:initial": # Send today's date today = datetime.date.today().strftime("%Y-%m-%d") ser.write((f"date::{today}\n").encode()) # Ask Gemini for initial fortune prompt = "Write one short fortune for today. Limit to 1-2 short sentences." response = model.generate_content(prompt, generation_config={"max_output_tokens": 50}) fortune = response.text.strip().replace("\n", " ") ser.write((f"fortune::{fortune}\n").encode()) elif line.startswith("request:tone:"): parts = line.split("::", 1) header = parts[0] fortune_text = parts[1] if len(parts) > 1 else "" tone = header.replace("request:tone:", "").strip() prompt = f"Rewrite this fortune in a {tone} tone, keeping meaning the same: {fortune_text}" response = model.generate_content(prompt, generation_config={"max_output_tokens": 40}) fortune = response.text.strip().replace("\n", " ") ser.write((f"fortune::{fortune}\n").encode())