MQTT Visualisierte Schaltzentrale von Lichtern und Haustüröffner: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
| Zeile 16: | Zeile 16: | ||
<pre> | <pre> | ||
| + | root@sensor:/usr/local/home-control# cat server.js | ||
const mqtt = require('mqtt'); | const mqtt = require('mqtt'); | ||
| + | const fs = require('fs'); | ||
const express = require('express'); | const express = require('express'); | ||
| + | const path = require('path'); | ||
const app = express(); | const app = express(); | ||
| − | const port = | + | const port = 3000; |
| − | // MQTT- | + | let status = { |
| − | const | + | wohnzimmer: 'off', |
| − | + | schlafzimmer: 'off', | |
| − | + | kueche: 'off', | |
| − | }); | + | haustuer: 'geschlossen' |
| + | }; | ||
| + | |||
| + | // MQTT-Verbindung herstellen über TLS | ||
| + | const options = { | ||
| + | port: 8883, | ||
| + | host: 'mqtt.lab.int', | ||
| + | protocol: 'mqtts', | ||
| + | username: 'xinux', | ||
| + | password: 'geheim', | ||
| + | ca: fs.readFileSync('./ca.crt') // Pfad zur ca.crt | ||
| + | }; | ||
| + | |||
| + | const client = mqtt.connect(options); | ||
client.on('connect', () => { | client.on('connect', () => { | ||
console.log('Verbunden mit dem Broker'); | console.log('Verbunden mit dem Broker'); | ||
| + | client.subscribe('home/+/status', (err) => { | ||
| + | if (!err) { | ||
| + | console.log('Alle Topics abonniert'); | ||
| + | } | ||
| + | }); | ||
}); | }); | ||
| − | + | client.on('message', (topic, message) => { | |
| − | + | const room = topic.split('/')[1]; | |
| − | + | status[room] = message.toString(); | |
| − | + | console.log(`Status von ${room}: ${status[room]}`); | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
}); | }); | ||
| − | app.get('/ | + | // API-Route für den Status der Lichter und Haustür |
| − | + | app.get('/status', (req, res) => { | |
| − | res.send( | + | res.send(status); |
}); | }); | ||
| − | app. | + | // Statisches Verzeichnis für die Webseite |
| − | + | app.use(express.static(path.join(__dirname))); | |
| − | |||
| − | |||
| − | // | + | // Starte den Webserver |
app.listen(port, () => { | app.listen(port, () => { | ||
| − | console.log(` | + | console.log(`Webserver läuft auf http://localhost:${port}`); |
}); | }); | ||
| + | |||
</pre> | </pre> | ||
Version vom 29. Oktober 2024, 10:55 Uhr
Voraussetzungen
Neues Verzeichnis für den Steuerungsserver erstellen
- mkdir /usr/local/control-switch
- cd /usr/local/control-switch
Initialisiere das Node.js-Projekt
- npm init -y
Installiere die benötigten Pakete
- npm install mqtt express
Konfiguration des Steuerungsservers
Erstelle die Datei switch-server.js im Verzeichnis /usr/local/control-switch mit folgendem Inhalt:
root@sensor:/usr/local/home-control# cat server.js
const mqtt = require('mqtt');
const fs = require('fs');
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
let status = {
wohnzimmer: 'off',
schlafzimmer: 'off',
kueche: 'off',
haustuer: 'geschlossen'
};
// MQTT-Verbindung herstellen über TLS
const options = {
port: 8883,
host: 'mqtt.lab.int',
protocol: 'mqtts',
username: 'xinux',
password: 'geheim',
ca: fs.readFileSync('./ca.crt') // Pfad zur ca.crt
};
const client = mqtt.connect(options);
client.on('connect', () => {
console.log('Verbunden mit dem Broker');
client.subscribe('home/+/status', (err) => {
if (!err) {
console.log('Alle Topics abonniert');
}
});
});
client.on('message', (topic, message) => {
const room = topic.split('/')[1];
status[room] = message.toString();
console.log(`Status von ${room}: ${status[room]}`);
});
// API-Route für den Status der Lichter und Haustür
app.get('/status', (req, res) => {
res.send(status);
});
// Statisches Verzeichnis für die Webseite
app.use(express.static(path.join(__dirname)));
// Starte den Webserver
app.listen(port, () => {
console.log(`Webserver läuft auf http://localhost:${port}`);
});
systemd-Unit für den Steuerungsserver erstellen
Erstelle die Datei /etc/systemd/system/home-control-switch.service mit folgendem Inhalt:
[Unit] Description=Home Control Switch Server [Service] Type=simple WorkingDirectory=/usr/local/home-control-switch ExecStart=/usr/bin/node switch-server.js ExecStartPost=/bin/echo "Schalt-Server gestartet" [Install] WantedBy=multi-user.target
Steuerungsserver starten und beim Booten aktivieren
- systemctl start home-control-switch.service
- systemctl enable home-control-switch.service
Steuerung der Geräte über HTTP
Nun können die Geräte über den separaten Server gesteuert werden:
- Wohnzimmerlicht einschalten: `http://localhost:3001/toggle/wohnzimmer` - Schlafzimmerlicht einschalten: `http://localhost:3001/toggle/schlafzimmer` - Küchenlicht einschalten: `http://localhost:3001/toggle/kueche` - Haustür öffnen: `http://localhost:3001/toggle/haustuer`
Der Server ist für die Steuerung der Geräte per HTTP zuständig und sendet die entsprechenden MQTT-Nachrichten.