Difference between revisions of "MQTT: install di Ubuntu 22.04"

From OnnoWiki
Jump to navigation Jump to search
 
Line 110: Line 110:
 
  persistence_location /mosquitto/data/
 
  persistence_location /mosquitto/data/
 
   
 
   
 +
allow_anonymous true
 +
listener 1883 0.0.0.0
 
  persistence_file mosquitto.db
 
  persistence_file mosquitto.db
 
  log_dest syslog
 
  log_dest syslog

Latest revision as of 17:23, 3 July 2023

sumber: https://linux.how2shout.com/how-to-install-mosquitto-in-ubuntu-22-04-or-20-04-lts/

Mosquitto MQTT Server adalah message broker yang bekerja di atas protokol MQTT. MQTT adalah protokol messaging ringan yang standar ISO untuk digunakan di atas protokol TCP/IP. Hal ini banyak digunakan untuk berkomunikasi dengan perangkat Internet of Things. Kami akan menginstal Mosquitto di server Ubuntu 22.04 dan kami akan mengirim pesan dari perangkat lunak MQTT-spy. Mosquitto adalah proyek Eclipse dan didistribusikan dengan lisensi EDL. Jadi mari kita mulai.

Install server Mosquitto MQTT

Instalasi aplikasi pendukung,

sudo su
apt update
locale-gen id_ID.UTF-8
sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt -y install mosquitto mosquitto-clients
mosquitto -version

Install Web Manajemen UI

Install docker sesuai petunjuk untuk Ubuntu 22.04 - Docker: Instalasi di Ubuntu 22.04

Selanjutnya, install free dan open-source Management UI untuk MQTT Cedalo

sudo docker run -it -v ~/cedalo_platform:/cedalo cedalo/installer:2-linux

Pastikan mosquitto tidak di install dari Docker Cedalo

cd ~/cedalo_platform
sudo sh start.sh


Jika berhasil

Starting cedalo_platform_mosquitto_1 ... done
Creating cedalo_platform_streamsheets_1      ... done
Creating cedalo_platform_management-center_1 ... done
Attaching to cedalo_platform_mosquitto_1, cedalo_platform_streamsheets_1, cedalo_platform_management-center_1
streamsheets_1       | Password for default Mosquitto does not exist, will create one.
mosquitto_1          | 1688368702: mosquitto version 2.0.15 starting
mosquitto_1          | 1688368702: Config loaded from /mosquitto/config/mosquitto.conf.
mosquitto_1          | 1688368702: Loading plugin: /usr/lib/mosquitto_dynamic_security.so
mosquitto_1          | 1688368702: Opening ipv4 listen socket on port 1883.
mosquitto_1          | 1688368702: Opening ipv6 listen socket on port 1883.
mosquitto_1          | 1688368702: mosquitto version 2.0.15 running
management-center_1  | Starting Management Center for Eclipse Mosquitto
streamsheets_1       | sed: cannot rename streamsheets/packages/gateway/config/sedPC5i8a: Device or resource busy
streamsheets_1       | -e Password for default Mosquitto created successfully.
streamsheets_1       | -e Hashed password for Mosquitto broker is located inside the installation directory in the file settings/mosquitto/pw.txt.
streamsheets_1       | -e Clear text password for Mosquitto broker is located inside the installation directory in the file settings/mosquitto/pw_clear.txt.
streamsheets_1       | -e Warning: for security reasons please copy the password from settings/mosquitto/pw_clear.txt and delete that file afterwards.
streamsheets_1       | 2023-07-03 07:18:36,850 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
streamsheets_1       | 2023-07-03 07:18:36,935 INFO supervisord started with pid 14
management-center_1  | [Error: ENOENT: no such file or directory, access '/management-center/src/license/utils/config/license.lic'] {
management-center_1  |   errno: -2,
management-center_1  |   code: 'ENOENT',
management-center_1  |   syscall: 'access',
management-center_1  |   path: '/management-center/src/license/utils/config/license.lic'
management-center_1  | }
management-center_1  | /management-center/src/license/utils
management-center_1  | undefined
streamsheets_1       | 2023-07-03 07:18:37,943 INFO spawned: 'mongod' with pid 17
streamsheets_1       | 2023-07-03 07:18:37,952 INFO spawned: 'redis' with pid 18
streamsheets_1       | 2023-07-03 07:18:37,969 INFO spawned: 'mosquitto-default' with pid 19
streamsheets_1       | 2023-07-03 07:18:37,973 INFO spawned: 'nginx' with pid 20
streamsheets_1       | 2023-07-03 07:18:37,977 INFO spawned: 'mosquitto' with pid 21
streamsheets_1       | 2023-07-03 07:18:37,980 INFO spawned: 'gateway' with pid 22
streamsheets_1       | 2023-07-03 07:18:37,983 INFO spawned: 'service-graphs' with pid 23
streamsheets_1       | 2023-07-03 07:18:37,986 INFO spawned: 'service-machines' with pid 24
streamsheets_1       | 2023-07-03 07:18:37,991 INFO spawned: 'service-streams' with pid 25
streamsheets_1       | 2023-07-03 07:18:38,989 INFO success: mongod entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,989 INFO success: redis entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,990 INFO success: mosquitto-default entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,990 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,990 INFO success: mosquitto entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,991 INFO success: gateway entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,993 INFO success: service-graphs entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,994 INFO success: service-machines entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
streamsheets_1       | 2023-07-03 07:18:38,994 INFO success: service-streams entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
management-center_1  | Mon, 03 Jul 2023 07:18:39 GMT express-session deprecated undefined resave option; provide resave option at start.js:179:10
management-center_1  | Mon, 03 Jul 2023 07:18:39 GMT express-session deprecated undefined saveUninitialized option; provide saveUninitialized option at start.js:179:10
management-center_1  | Connecting to "Mosquitto 2.0" on mqtt://mosquitto:1883
management-center_1  | Started Mosquitto proxy at http://localhost:8088
management-center_1  | "CEDALO_MC_PLUGIN_DIR" is not set. Skipping loading of plugins
management-center_1  | Mosquitto proxy server started on port 8088
mosquitto_1          | 1688368719: New connection from 172.18.0.4:53494 on port 1883.
mosquitto_1          | 1688368719: New client connected from 172.18.0.4:53494 as mqttjs_97f98186 (p2, c1, k60, u'cedalo').
management-center_1  | Connected to 'Mosquitto 2.0' at mqtt://mosquitto:1883


Akses web

http://192.168.0.62:8088
http://192.168.0.62:8088
username cedalo
password mmcisawesome
Cedalo-web-ui.png


ubah konfigurasi

cd /home/onno/cedalo_platform/mosquitto/config
vi mosquitto.conf

Tambahkan

listener 1883

persistence true
persistence_location /mosquitto/data/

allow_anonymous true
listener 1883 0.0.0.0
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous true
password_file /etc/mosquitto/pwfile
 
plugin /usr/lib/mosquitto_dynamic_security.so
plugin_opt_config_file /mosquitto/data/dynamic-security.json



Restart

sudo docker restart cedalo_platform_mosquitto_1
sudo docker restart cedalo_platform_management-center_1
sudo docker restart cedalo_platform_streamsheets_1

Konfigurasi Mosquitto MQTT Server

Pada dasarnya mosquitto memungkinkan anonymous access.

Jika dibutuhkan, kita bisa membuat password untuk user, misalnya, mqtt-spy,

sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy

masukan password untuk mqtt-spy user yang akan digunakan untuk connect dari client. Jika masih belajar ada baiknya password 123456. Untuk percobaan bisa membuat

Buat file konfigurasi Mosquitto MQTT Server,

sudo nano /etc/mosquitto/conf.d/local.conf

Isi dengan

persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous true
password_file /etc/mosquitto/pwfile


Setelah config di simpan, jalankan ldconfig

sudo ldconfig

Tambahkan systemd unit file

sudo nano /etc/systemd/system/mosquitto.service

Isi dengan

[Unit]
Description=Insite MQTT Broker

[Service]
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

Jalankan

Jalankan

sudo systemctl start mosquitto.service

Set supaya mosquitto jalan saat boot,

sudo systemctl enable mosquitto.service

Cek status

systemctl status mosquitto.service

Hasilnya kira-kira,

● mosquitto.service - Insite MQTT Broker
     Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-03 06:37:08 UTC; 2min 39s ago
   Main PID: 2147 (mosquitto)
      Tasks: 1 (limit: 2219)
     Memory: 1.3M
        CPU: 181ms
     CGroup: /system.slice/mosquitto.service
             └─2147 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Jul 03 06:37:08 server2204 systemd[1]: Starting Mosquitto MQTT Broker...
Jul 03 06:37:08 server2204 systemd[1]: Started Mosquitto MQTT Broker.
Jul 03 06:39:38 server2204 systemd[1]: mosquitto.service: Current command vanished from the unit file, 
execution of the command list won't be resumed.

Quick Test

Monitoring

Buka terminal di server, ketik misalnya,

mosquitto_sub -v -t 'esp/test' -u mqtt-spy -P 123456

dimana

-v  verbosity
-t  topik yang di ikuti

coba ketik dari aplikasi java misalnya on, akan keluar

mosquitto_sub -v -t 'esp/est' -u mqtt-spy -P 123456
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON


Posting

Kirim Perintah

mosquitto_pub -t 'esp/test' -u mqtt-spy -P 123456 -m "lampu ON"

Referensi


Pranala Menarik