tinymqttbroker-Adapter für ioBroker
Dies ist ein sehr kleiner MQTT-Broker, der keine Objekte/Zustände in iobroker verwaltet, sondern eine zentrale MQTT-Broker-Instanz zum Veröffentlichen und Abonnieren von Themen als MQTT-Client bereitstellt. Er ist sehr hilfreich, um mehreren Geräten die Kommunikation mit einem Broker und die Interaktion über iobroker mit einem MQTT-Client-JavaScript zu ermöglichen.
Dieser Adapter verwendet Sentry-Bibliotheken, um Ausnahmen und Codefehler automatisch an die Entwickler zu melden. Weitere Details und Informationen zum Deaktivieren der Fehlerberichterstattung finden Sie in der Sentry-Plugin-Dokumentation !
Erfordert
- Node.js 22 oder höher
- ioBroker-Host (JS-Controller) Version 6.0.11 oder höher
Wie man es benutzt
Ein MQTT-Client könnte wie folgt aussehen:
const mqtt = require('mqtt');
const protocol = 'mqtt';
const host = 'localhost';
const portClient = 1884;
const clientId = `iobroker_mqtt_client_` + Math.floor(Math.random() * 100000 + 100000);
const connectUrl = `${protocol}://${host}:${portClient}`;
const client = mqtt.connect(connectUrl, {
clientId,
clean: true,
connectTimeout: 4000,
reconnectPeriod: 10000
})
const topics = ['topic1', 'topic2', 'topic3];
client.on('connect', () => {
client.subscribe(topics, () => {
console.log(`MQTT client says: Connected and subscribe to topics '${topics}'`)
})
})
client.on('message', (topic: string, payload) => {
payload = payload.toString();
// deal as you need with topics and payload here
// 'switch' could be helpful
switch (topic) {
case 'topic1':
//your code
break;
case 'topic2':
//your code
break;
})
Für die Veröffentlichung von Nachrichten wird ein dedizierter ioBroker-Zustand verwendet, der auf Änderungen lauscht und diese an den MQTT-Broker weiterleitet.
Der Zustand erwartet eine JSON-Nutzlast, die Thema und Nachricht enthält.
on({ id: stateMqttIn, change: 'any' }, function (obj) {
const input: any = obj.state.val;
const topic: string = input?.topic ?? null;
const message: string = String(input?.message) ?? null;
if (topic && message) client.publish(topic, message);
else log(`MQTT publish not possible for topic '${topic}' and message '${message}'`, 'warn');
});
WICHTIG! Wenn Sie Ihren eigenen MQTT-Client in einem ioBroker-JavaScript-Skript implementieren, stellen Sie sicher, dass Sie den Client am Ende des Skripts ordnungsgemäß schließen, indem Sie Folgendes aufrufen:
onStop(function (callback) {
log('MQTT Client will be closed...');
client.end(() => {
if (callback) {
callback();
log('MQTT Client closed');
}
})
}, 2000 /*ms*/);
Changelog
0.1.7 (2026-06-16)
- (HGlab01) add logs for clientError, connectionError and keepaliveTimeout
- (HGlab01) Bump aedes to 1.0.2
- (HGlab01) code quality improvements
- (HGlab01) dependecy updates
0.1.6 (2026-03-19)
- (HGlab01) Bump aedes to 1.0.1
- (HGlab01) Rename config items
0.1.5 (2026-03-04)
- (HGlab01) Update libs
- (HGlab01) (c) 2026
- (HGlab01) Bump aedes to 1.0.0
0.1.4 (2025-04-22)
- (HGlab01) Improve port scan
0.1.3 (2024-10-19)
- (HGlab01) Improve port scan for available ports
- (HGlab01) Improve UI config
- (HGlab01) Bump json-explorer to 0.1.16
- (HGlab01) Bump aedes to 0.51.3
License
MIT License
Copyright (c) 2026 HGlab01 myiobrokeradapters@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.