MQTT Visualisierte Steuerung Aktor: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(Die Seite wurde neu angelegt: „=== Voraussetzungen === *Voraussetzungen für Node.js-Projekt ==== Neues Verzeichnis für das Projekt erstellen ==== *'''mkdir /usr/local/control''' *''…“) |
|||
| Zeile 14: | Zeile 14: | ||
==== Konfiguration des Node.js-Servers ==== | ==== Konfiguration des Node.js-Servers ==== | ||
Erstelle die Datei '''server.js''' im Verzeichnis '''/usr/local/control''' mit folgendem Inhalt: | Erstelle die Datei '''server.js''' im Verzeichnis '''/usr/local/control''' mit folgendem Inhalt: | ||
| − | |||
<pre> | <pre> | ||
const mqtt = require('mqtt'); | const mqtt = require('mqtt'); | ||
| Zeile 23: | Zeile 22: | ||
let status = { | let status = { | ||
| − | + | livingRoom: 'off', | |
| − | + | bedroom: 'off', | |
| − | + | kitchen: 'off', | |
| − | + | frontDoor: 'closed' | |
}; | }; | ||
// MQTT-Verbindung herstellen | // MQTT-Verbindung herstellen | ||
| − | const client = mqtt.connect('mqtt:// | + | const client = mqtt.connect('mqtt://mqtt.dkbi.int:1883', { |
| − | username: 'xinux', | + | // username: 'xinux', |
| − | password: ' | + | // password: 'secret' |
}); | }); | ||
client.on('connect', () => { | client.on('connect', () => { | ||
| − | console.log(' | + | console.log('Connected to the broker'); |
client.subscribe('home/+/status', (err) => { | client.subscribe('home/+/status', (err) => { | ||
if (!err) { | if (!err) { | ||
| − | console.log(' | + | console.log('Subscribed to all topics'); |
} | } | ||
}); | }); | ||
| Zeile 47: | Zeile 46: | ||
const room = topic.split('/')[1]; | const room = topic.split('/')[1]; | ||
status[room] = message.toString(); | status[room] = message.toString(); | ||
| − | console.log(`Status | + | console.log(`Status of ${room}: ${status[room]}`); |
}); | }); | ||
| − | // API | + | // API route to get the status of the lights and front door |
app.get('/status', (req, res) => { | app.get('/status', (req, res) => { | ||
res.send(status); | res.send(status); | ||
}); | }); | ||
| − | // | + | // Serve static directory for the web interface |
app.use(express.static(path.join(__dirname))); | app.use(express.static(path.join(__dirname))); | ||
| − | // | + | // Start the web server |
app.listen(port, () => { | app.listen(port, () => { | ||
| − | console.log(` | + | console.log(`Control Center is running at http://0.0.0.0:${port}`); |
}); | }); | ||
| + | |||
</pre> | </pre> | ||
Version vom 30. Oktober 2024, 09:48 Uhr
Voraussetzungen
Neues Verzeichnis für das Projekt erstellen
- mkdir /usr/local/control
- cd /usr/local/control
Initialisiere das Node.js-Projekt
- npm init -y
Installiere die benötigten Pakete
- npm install mqtt express
Konfiguration des Node.js-Servers
Erstelle die Datei server.js im Verzeichnis /usr/local/control mit folgendem Inhalt:
const mqtt = require('mqtt');
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
let status = {
livingRoom: 'off',
bedroom: 'off',
kitchen: 'off',
frontDoor: 'closed'
};
// MQTT-Verbindung herstellen
const client = mqtt.connect('mqtt://mqtt.dkbi.int:1883', {
// username: 'xinux',
// password: 'secret'
});
client.on('connect', () => {
console.log('Connected to the broker');
client.subscribe('home/+/status', (err) => {
if (!err) {
console.log('Subscribed to all topics');
}
});
});
client.on('message', (topic, message) => {
const room = topic.split('/')[1];
status[room] = message.toString();
console.log(`Status of ${room}: ${status[room]}`);
});
// API route to get the status of the lights and front door
app.get('/status', (req, res) => {
res.send(status);
});
// Serve static directory for the web interface
app.use(express.static(path.join(__dirname)));
// Start the web server
app.listen(port, () => {
console.log(`Control Center is running at http://0.0.0.0:${port}`);
});
HTML-Datei erstellen
Erstelle eine Datei index.html im selben Verzeichnis, die den Status der Lichter und des Haustüröffners anzeigt und eine visuelle Darstellung mit Farben bietet:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Haussteuerung</title>
<style>
.device {
margin: 20px;
padding: 10px;
border-radius: 10px;
text-align: center;
font-size: 1.5em;
}
.on {
background-color: green;
color: white;
}
.off {
background-color: red;
color: white;
}
.open {
background-color: blue;
color: white;
}
.closed {
background-color: gray;
color: white;
}
</style>
<script>
function fetchStatus() {
fetch('/status')
.then(response => response.json())
.then(data => {
document.getElementById('wohnzimmer').className = 'device ' + (data.wohnzimmer === 'on' ? 'on' : 'off');
document.getElementById('schlafzimmer').className = 'device ' + (data.schlafzimmer === 'on' ? 'on' : 'off');
document.getElementById('kueche').className = 'device ' + (data.kueche === 'on' ? 'on' : 'off');
document.getElementById('haustuer').className = 'device ' + (data.haustuer === 'offen' ? 'open' : 'closed');
});
}
setInterval(fetchStatus, 1000); // Alle 1 Sekunde den Status abrufen
</script>
</head>
<body>
<h1>Haussteuerung</h1>
<div id="wohnzimmer" class="device off">Wohnzimmer Licht: Aus</div>
<div id="schlafzimmer" class="device off">Schlafzimmer Licht: Aus</div>
<div id="kueche" class="device off">Küchen Licht: Aus</div>
<div id="haustuer" class="device closed">Haustür: Geschlossen</div>
</body>
</html>
systemd-Unit erstellen
Erstelle eine Datei /etc/systemd/system/control.service mit folgendem Inhalt:
[Unit] Description=Home Control Center [Service] Type=simple WorkingDirectory=/usr/local/control ExecStart=/usr/bin/node server.js ExecStartPost=/bin/echo "Home Control Center Start" [Install] WantedBy=multi-user.target
Server starten
systemd-Service starten
- systemctl start control.service
systemd-Service beim Booten starten lassen
- systemctl enable control.service
Öffne deinen Browser und gehe zu
MQTT-Nachrichten senden
Zuerst definieren wir die Variable für den MQTT-Server:
Definiere die Variable für den MQTT-Server
- export MQTTSERVER=mqtt.dkbi.int
Licht im Wohnzimmer einschalten
- mosquitto_pub -h $MQTTSERVER -t home/wohnzimmer/status -m "on" -u xinux -P geheim
Licht im Wohnzimmer ausschalten
- mosquitto_pub -h $MQTTSERVER -t home/wohnzimmer/status -m "off" -u xinux -P geheim
Haustür öffnen
- mosquitto_pub -h $MQTTSERVER -t home/haustuer/status -m "offen" -u xinux -P geheim
Haustür schließen
- mosquitto_pub -h $MQTTSERVER -t home/haustuer/status -m "geschlossen" -u xinux -P geheim
Zusammenfassung
Dieses Projekt ermöglicht die Steuerung von Wohnzimmer, Schlafzimmer, Küchen Lichtern und des Haustüröffners, mit einer visuellen Darstellung des Status über eine farbcodierte Webseite. Die MQTT-Nachrichten steuern die Geräte und zeigen deren aktuellen Zustand an.