Ir al contenido

← Broker MQTT Mosquitto central para una flota de seguidores

Seguidores solares de dos ejesServidor centralMQTTInfraestructura

Broker MQTT Mosquitto central para una flota de seguidores — ejemplo completo

Monta un broker MQTT Mosquitto central con autenticación y CSVs de mapeo de IDs por planta para supervisar una flota de seguidores solares de dos ejes.

Programa completo y ejecutable para el Servidor central (central-mosquitto-broker.sh): incluye cabecera de conexionado, requisitos y notas de integración.

Descarga el pack completo del proyecto — gratisEste ejemplo + los relacionados + lista de materiales

Vista de solo lectura.

#!/bin/bash
# -----------------------------------------------------------------------------
# COMPLETE EXAMPLE — Central Mosquitto broker with per-plant ID map
#
# Device:    Central plant server (Linux)
# Based on:  dual-axis solar tracker project
#
# What this script sets up:
#   1. Installs Mosquitto and creates the /etc/mosquitto structure on the
#      central server.
#   2. Generates mosquitto.conf with an authenticated listener (no anonymous
#      access).
#   3. Creates the password file (passwd) with one user per role.
#   4. Deploys the ID mapping CSVs (id_map_101, id_map_102 and
#      id_map_general) that link each MQTT ID to its physical tracker.
#
# Plant topic convention:
#   trackers///status      tracker positions and status
#   trackers///command     commands sent to the tracker
# -----------------------------------------------------------------------------
set -e

# === 1. Broker installation ===================================================
apt-get update
apt-get install -y mosquitto mosquitto-clients

# === 2. Main configuration: /etc/mosquitto/conf.d/central.conf ===============
# - Port 1883 on the plant's internal network only.
# - allow_anonymous false: every tracker authenticates.
# - persistence: keeps the last state of each tracker across restarts.
cat > /etc/mosquitto/conf.d/central.conf << 'EOF'
# --- Main listener (plant internal network) ---
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd

# --- Persistence of retained messages and sessions ---
persistence true
persistence_location /var/lib/mosquitto/

# --- Logging ---
log_dest file /var/log/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
connection_messages true

# --- Conservative limits for a fleet of trackers ---
max_connections 256
max_keepalive 120
EOF

# === 3. Users and passwords ===================================================
# One user for the trackers and another for the dashboard/analytics.
# MQTT_PASS_* are placeholders: set them in the real deployment.
touch /etc/mosquitto/passwd
mosquitto_passwd -b /etc/mosquitto/passwd tracker    "MQTT_PASS_TRACKER"
mosquitto_passwd -b /etc/mosquitto/passwd dashboard  "MQTT_PASS_DASHBOARD"
chown mosquitto:mosquitto /etc/mosquitto/passwd
chmod 600 /etc/mosquitto/passwd

# === 4. Per-plant ID map ======================================================
# Each CSV links the logical MQTT ID to the physical row/position of the
# tracker in the plant. The suffix (101, 102...) identifies the plant;
# id_map_general consolidates all plants for the central dashboard.
mkdir -p /etc/mosquitto/map

cat > /etc/mosquitto/map/id_map_101.csv << 'EOF'
id,plant,row,position
1,101,1,1
2,101,1,2
3,101,2,1
EOF

cat > /etc/mosquitto/map/id_map_102.csv << 'EOF'
id,plant,row,position
1,102,1,1
2,102,1,2
EOF

# Consolidated map of all plants (consumed by the central dashboard)
cat > /etc/mosquitto/map/id_map_general.csv << 'EOF'
global_id,plant,local_id
101-1,101,1
101-2,101,2
101-3,101,3
102-1,102,1
102-2,102,2
EOF

# === 5. Startup and verification ==============================================
systemctl enable mosquitto
systemctl restart mosquitto
sleep 2
systemctl is-active mosquitto && echo "Central broker active"

# Quick authenticated publish/subscribe test:
#   mosquitto_sub -h localhost -u dashboard -P MQTT_PASS_DASHBOARD \
#                 -t 'trackers/#' -v &
#   mosquitto_pub -h localhost -u tracker -P MQTT_PASS_TRACKER \
#                 -t 'trackers/101/1/status' -m '{"elev":34.2,"az":181.5}'
Descarga el pack completo del proyecto — gratisEste ejemplo + los relacionados + lista de materiales