commit d9fc362e34fd9c5ee1028b109704de16b5913866 Author: Lucca Pirovano Date: Sat Nov 15 18:01:18 2025 -0500 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..d950eac --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# arduino-fan-vehicle diff --git a/app.py b/app.py new file mode 100644 index 0000000..10041ce --- /dev/null +++ b/app.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +from flask import Flask, render_template, jsonify +import serial +import serial.tools.list_ports +import threading +import time + +app = Flask(__name__) + +# --- Auto-find Arduino --- +def find_arduino(): + for p in serial.tools.list_ports.comports(): + if 'Arduino' in p.description or 'ACM' in p.device or 'USB' in p.device: + return p.device + return '/dev/ttyACM0' + +PORT = find_arduino() +BAUD = 115200 + +# Global serial object +ser = None +serial_lock = threading.Lock() + +def init_serial(): + global ser + try: + ser = serial.Serial(PORT, BAUD, timeout=0) + time.sleep(2) # wait for Arduino reset + print(f"Connected to {PORT}") + except Exception as e: + print(f"Serial error: {e}") + ser = None + +init_serial() + +# --- Send command safely --- +def send_command(cmd): + with serial_lock: + if ser and ser.is_open: + try: + ser.write(cmd.encode()) + ser.flush() + except: + print("Serial write failed") + else: + print("Serial not open") + +# --- Background reader (optional: show live feedback) --- +def serial_reader(): + while True: + if ser and ser.in_waiting: + line = ser.readline().decode(errors='ignore').strip() + if line: + print(f"Arduino: {line}") + time.sleep(0.01) + +threading.Thread(target=serial_reader, daemon=True).start() + +# --- Routes --- +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/cmd/') +def command(key): + if len(key) == 1 and key in '0123456789fFsS': + send_command(key) + return jsonify(status="ok", key=key) + return jsonify(status="invalid"), 400 + +if __name__ == '__main__': + # Allow access from your phone/tablet on same WiFi + app.run(host='0.0.0.0', port=5000, debug=False) diff --git a/fan_control.py b/fan_control.py new file mode 100644 index 0000000..fc7a4a5 --- /dev/null +++ b/fan_control.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +""" +Ultra-responsive fan control – every key press is sent instantly. +Uses raw stdin + select() with zero timeout. +""" +import serial +import sys +import select +import termios +import tty +import serial.tools.list_ports + +# ------------------------------------------------- +# Auto-detect Arduino port +# ------------------------------------------------- +def find_arduino(): + for p in serial.tools.list_ports.comports(): + if 'Arduino' in p.description or 'ACM' in p.device or 'USB' in p.device: + return p.device + return None + +PORT = find_arduino() or '/dev/ttyACM0' +BAUD = 115200 + +# ------------------------------------------------- +def raw_stdin(): + """Put stdin into raw mode and return old settings.""" + fd = sys.stdin.fileno() + old = termios.tcgetattr(fd) + tty.setraw(fd) + return old + +# ------------------------------------------------- +def main(): + # --- open serial ------------------------------------------------- + try: + ser = serial.Serial(PORT, BAUD, timeout=0) # non-blocking + except Exception as e: + sys.exit(f"Cannot open {PORT}: {e}") + + print(f"Connected to {PORT}") + print("Keys: 0-9, f=full, s=stop, q=quit (every press is sent immediately)") + + old_term = raw_stdin() + try: + while True: + # ---- non-blocking read from keyboard (0-second timeout) ---- + rlist, _, _ = select.select([sys.stdin], [], [], 0) + if rlist: + key = sys.stdin.read(1) # exactly one char + if key.lower() == 'q': + print("\nBye!") + break + ser.write(key.encode()) # send raw byte + print(f" → {key}", end='', flush=True) + + # ---- echo any reply from Arduino (non-blocking) ---- + if ser.in_waiting: + reply = ser.read(ser.in_waiting).decode(errors='ignore') + print(reply, end='', flush=True) + + finally: + termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_term) + ser.close() + +if __name__ == '__main__': + main() diff --git a/sketch_oct23a/sketch_oct23a.ino b/sketch_oct23a/sketch_oct23a.ino new file mode 100644 index 0000000..a2379f6 --- /dev/null +++ b/sketch_oct23a/sketch_oct23a.ino @@ -0,0 +1,14 @@ +const int FAN_PIN = 9; // Pin connected to the FAN + +void setup() { + pinMode(FAN_PIN, OUTPUT); // Set the FAN pin as an output +} + +void loop() { + digitalWrite(FAN_PIN, HIGH); // Turn the FAN on + Serial.print("Fan-High"); + delay(2000); // Wait for 1 second + digitalWrite(FAN_PIN, LOW); // Turn the FAN off + Serial.print("Fan-Low"); + delay(2000); // Wait for 1 second +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6391539 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,68 @@ + + + + + + Fan Speed Control + + + +

Fan Speed Control

+

Click buttons or press keys: 0–9, f, s

+ +
+ + + + + + + + + + + +
+ +
Ready. Press keys or click buttons.
+
Keyboard: 0–9, f = full, s = stop
+ + + +