GruppeC/backend/simulator.py

28 lines
1.0 KiB
Python

# Simuliert den ESP8266: schickt alle 2 s Fantasiewerte an den Server (Entwicklung ohne Hardware).
# Start: python simulator.py [Server-URL] z. B. http://localhost:3000
import json
import math
import sys
import time
import urllib.request
basis = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:3000"
schritt = 0
while True:
schritt += 1
messwert = {
"temperatur": round(22 + 2 * math.sin(schritt / 20), 1),
"luftfeuchte": round(45 + 5 * math.sin(schritt / 15), 1),
"eco2": round(800 + 400 * math.sin(schritt / 25)), # ueberschreitet zeitweise 1000 ppm
"tvoc": round(70 + 30 * math.sin(schritt / 10)),
}
anfrage = urllib.request.Request(basis + "/api/messwerte", data=json.dumps(messwert).encode(),
headers={"Content-Type": "application/json"})
try:
urllib.request.urlopen(anfrage, timeout=3).read()
print("gesendet", json.dumps(messwert))
except OSError as fehler:
print("Server nicht erreichbar:", fehler)
time.sleep(2)