Files
mosquitto-mqtt-broker/test-mqtt.sh
2025-11-10 10:12:03 +00:00

74 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
COMPOSE_CMD=${COMPOSE_CMD:-"docker compose"}
CONTAINER_NAME="mosquitto"
if [ ! -f .env ]; then
echo "❌ Fehler: .env nicht gefunden. Bitte kopiere .env.example und setze die Passwörter."
exit 1
fi
set -a
source .env
set +a
ADMIN_USER=${MQTT_ADMIN_USERNAME:-}
ADMIN_PASS=${MQTT_ADMIN_PASSWORD:-}
if [ -z "$ADMIN_USER" ] || [ -z "$ADMIN_PASS" ]; then
echo "❌ Fehler: MQTT_ADMIN_USERNAME und MQTT_ADMIN_PASSWORD müssen in .env gesetzt sein."
exit 1
fi
if ! docker ps --format '{{.Names}}' | grep -qx "$CONTAINER_NAME"; then
echo "❌ Fehler: Mosquitto Container läuft nicht!"
echo " Starte mit: docker compose up -d"
exit 1
fi
publish() {
local topic=$1
local payload=$2
echo "$topic : $payload"
$COMPOSE_CMD exec -T "$CONTAINER_NAME" mosquitto_pub -h localhost -t "$topic" -m "$payload" -u "$ADMIN_USER" -P "$ADMIN_PASS"
}
echo "================================================"
echo "MQTT Server Test"
echo "================================================"
echo ""
echo "📡 Sende Test-Nachrichten..."
echo ""
echo "1⃣ Temperature Sensor: sensors/temperature"
publish "sensors/temperature" "22.5"
echo "2⃣ Humidity Sensor: sensors/humidity"
publish "sensors/humidity" "65"
echo "3⃣ CPU Usage: system/cpu"
publish "system/cpu" "45"
echo "4⃣ Device Power: devices/device1/power"
publish "devices/device1/power" "ON"
echo "5⃣ Device Brightness: devices/device1/brightness"
publish "devices/device1/brightness" "75"
echo "================================================"
echo "✅ Test abgeschlossen!"
echo "================================================"
echo ""
echo "🌐 Öffne das Dashboard: http://localhost:5000"
echo " Die Test-Daten sollten jetzt in den Widgets sichtbar sein."
echo ""
echo "📊 Subscribe auf alle Topics:"
echo " $COMPOSE_CMD exec -T mosquitto mosquitto_sub -h localhost -t '#' -v -u $ADMIN_USER -P <ADMIN_PASSWORD>"
echo ""
echo "📡 Subscribe auf öffentliche Topics (ohne Auth):"
echo " $COMPOSE_CMD exec -T mosquitto mosquitto_sub -h localhost -t 'public/#' -v"
echo ""