GruppeC/backend/simulator.js

28 lines
976 B
JavaScript

// Simuliert den ESP8266: schickt alle 2 s Fantasiewerte an den Server (Entwicklung ohne Hardware).
// Start: node backend/simulator.js [Server-URL] z. B. http://localhost:3000
const basis = process.argv[2] || "http://localhost:3000";
let schritt = 0;
async function sende() {
schritt++;
const messwert = {
temperatur: Math.round((22 + 2 * Math.sin(schritt / 20)) * 10) / 10,
luftfeuchte: Math.round((45 + 5 * Math.sin(schritt / 15)) * 10) / 10,
eco2: Math.round(800 + 400 * Math.sin(schritt / 25)), // überschreitet zeitweise 1000 ppm
tvoc: Math.round(40 + 30 * Math.sin(schritt / 10) + 30),
};
try {
await fetch(basis + "/api/messwerte", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(messwert),
});
console.log("gesendet", JSON.stringify(messwert));
} catch (fehler) {
console.log("Server nicht erreichbar:", fehler.message);
}
}
setInterval(sende, 2000);
sende();