MQTT: install di Ubuntu 22.04
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
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
Kita perlu menambahkan ijin ke mosquitto user pada directory yang relevan.
sudo mkdir /var/lib/mosquitto/ sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/
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 05:58:02 UTC; 1min 11s ago Main PID: 4320 (mosquitto) Tasks: 1 (limit: 2219) Memory: 1.1M CPU: 114ms CGroup: /system.slice/mosquitto.service └─4320 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: mosquitto version 2.0.15 starting Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: Config loaded from /etc/mosquitto/mosquitto.conf. Jul 03 05:58:02 server2204 systemd[1]: Started Mosquitto MQTT Broker. Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: Starting in local only mode. Connections will only be possible from clients running on this mac> Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: Create a configuration file which defines a listener to allow remote access. Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: For more details see https://mosquitto.org/documentation/authentication-methods/ Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: Opening ipv4 listen socket on port 1883. Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: Opening ipv6 listen socket on port 1883. Jul 03 05:58:02 server2204 mosquitto[4320]: 1688363882: mosquitto version 2.0.15 running Jul 03 05:59:00 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