GruppeC/frontend/SmartLectureRoom/Tools/fake_arduino.py

55 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
Simuliert einen Arduino/ESP32 per UDP – zum Testen ohne Hardware.
1. In Unity: Dashboard → Verbindung → "UDP / WLAN" → Verbinden (Port 5005)
2. python3 Tools/fake_arduino.py (normaler Verlauf)
python3 Tools/fake_arduino.py --heat (Temperatur steigt auf 30 °C -> Fenster öffnet)
python3 Tools/fake_arduino.py --co2 (CO2 steigt über 1000 ppm)
Empfangene Steuerbefehle von Unity werden ausgegeben.
"""
import argparse
import json
import random
import socket
import time
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=5005)
parser.add_argument("--reply-port", type=int, default=5006)
parser.add_argument("--heat", action="store_true", help="Temperatur langsam auf 30 °C steigen lassen")
parser.add_argument("--co2", action="store_true", help="CO2 langsam über den Grenzwert steigen lassen")
args = parser.parse_args()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", args.reply_port))
sock.settimeout(0.1)
temp, co2 = 22.0, 650.0
print(f"Sende an {args.host}:{args.port}, empfange Befehle auf Port {args.reply_port} (Strg+C beendet)")
try:
while True:
if args.heat:
temp = min(30.5, temp + 0.4)
if args.co2:
co2 = min(1800.0, co2 + 40)
packet = {
"t": round(temp + random.uniform(-0.1, 0.1), 1),
"co2": round(co2 + random.uniform(-15, 15)),
"lux": round(random.uniform(330, 370)),
"noise": round(random.uniform(42, 48)),
}
sock.sendto((json.dumps(packet) + "\n").encode(), (args.host, args.port))
print("→", packet)
end = time.time() + 2
while time.time() < end:
try:
data, _ = sock.recvfrom(1024)
print(" ← Unity:", data.decode().strip())
except socket.timeout:
pass
except KeyboardInterrupt:
pass