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

From OnnoWiki
Jump to navigation Jump to search
 
(15 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
  apt update
 
  apt update
 
  locale-gen id_ID.UTF-8
 
  locale-gen id_ID.UTF-8
  apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
+
  apt install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
  
 
Tambahkan mosquitto user, karena secara default berjalan sebagai non-root.
 
Tambahkan mosquitto user, karena secara default berjalan sebagai non-root.
Line 27: Line 27:
 
Download, extraxt, compile & install,
 
Download, extraxt, compile & install,
  
  wget https://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz
+
  wget https://mosquitto.org/files/source/mosquitto-1.5.gz
  tar xvzf mosquitto-1.4.9.tar.gz
+
  tar xvzf mosquitto-1.5.tar.gz
  cd mosquitto-1.4.9/
+
  cd mosquitto-1.5/
 
  make && sudo make install
 
  make && sudo make install
  
 
==Konfigurasi Mosquitto MQTT Server==
 
==Konfigurasi Mosquitto MQTT Server==
  
buat password untuk mqtt-spy,
+
Pada dasarnya mosquitto memungkinkan anonymous access.
 +
 
 +
Jika dibutuhkan, kita bisa membuat password untuk user mqtt-spy,
  
 
  sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
 
  sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
  
masukan password untuk mqtt-spy user yang akan digunakan untuk connect dari client.
+
masukan password untuk mqtt-spy user yang akan digunakan untuk connect dari client. JIka masih belajar ada baiknya password 123456.
 
Kita perlu menambahkan ijin ke mosquitto user pada directory yang relevan.
 
Kita perlu menambahkan ijin ke mosquitto user pada directory yang relevan.
  
Line 62: Line 64:
 
  connection_messages true
 
  connection_messages true
 
  log_timestamp true
 
  log_timestamp true
  allow_anonymous false
+
  allow_anonymous true
 
  password_file /etc/mosquitto/pwfile
 
  password_file /etc/mosquitto/pwfile
 +
 +
Atau kalau mau aman
 +
 +
cd /etc/mosquitto
 +
cp mosquitto.conf.example mosquitto.conf
  
  
Line 91: Line 98:
  
 
  sudo systemctl start mosquitto.service
 
  sudo systemctl start mosquitto.service
 +
 +
Set supaya mosquitto jalan saat boot,
 +
 +
sudo systemctl enable mosquitto.service
  
 
Cek status
 
Cek status
Line 96: Line 107:
 
  systemctl status mosquitto.service
 
  systemctl status mosquitto.service
  
Set supaya mosquitto jalan saat boot,
+
Hasilnya kira-kira,
  
  sudo systemctl enable mosquitto.service
+
  mosquitto.service - Insite MQTT Broker
 +
    Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
 +
    Active: active (running) since Sab 2017-05-20 08:18:28 WIB; 1h 5min ago
 +
  Main PID: 751 (mosquitto)
 +
    Tasks: 1 (limit: 512)
 +
    Memory: 2.8M
 +
      CPU: 6.170s
 +
    CGroup: /system.slice/mosquitto.service
 +
            └─751 /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
 +
 +
Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Config loaded from /etc/mosquitto/mosquitto.conf.
 +
Mei 20 08:18:30 refserver mosquitto[751]: Config loaded from /etc/mosquitto/mosquitto.conf.
 +
Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Opening ipv4 listen socket on port 1883.
 +
Mei 20 08:18:30 refserver mosquitto[751]: Opening ipv4 listen socket on port 1883.
 +
Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Opening ipv6 listen socket on port 1883.
 +
Mei 20 08:18:30 refserver mosquitto[751]: Opening ipv6 listen socket on port 1883.
 +
Mei 20 08:48:30 refserver mosquitto[751]: 1495244910: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
 +
Mei 20 08:48:30 refserver mosquitto[751]: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
 +
Mei 20 09:18:31 refserver mosquitto[751]: 1495246711: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
 +
Mei 20 09:18:31 refserver mosquitto[751]: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
  
 
==Setup MQTT-Spy dan connnecting==
 
==Setup MQTT-Spy dan connnecting==
  
MQTT-Spy adalah Java based client untuk menyambungkan diri ke MQTT server.
+
Instalasi apps pendukung
MQTT-Spy bisa di ambil di https://github.com/eclipse/paho.mqtt-spy
 
  
  cd /usr/local/src
+
  apt-get install openjfx
git clone git://github.com/eclipse/paho.mqtt-spy mqtt-spy
 
cd /usr/local/src/mqtt-spy/
 
  
  
MQTT-Spy membutuhkan Oracle JDK.
+
MQTT-Spy adalah Java based client untuk menyambungkan diri ke MQTT server.  
 +
MQTT-Spy bisa di ambil di
  
You can get it from github but note that you must have Oracle JDK installed. It does NOT work with OpenJDK. First thing to do after staring MQTT-spy is to create new connection. You add your server IP and default port 1883.
+
https://github.com/eclipse/paho.mqtt-spy/wiki/Downloads
 +
https://github.com/kamilfb/mqtt-spy/releases/download/mqtt-spy_v0.5.4/mqtt-spy-0.5.4-jar-with-dependencies.jar
  
mqtt spy
+
download
  
Then you add password and user name we made earlier, in my case it is mqtt-spy and password is password.
+
cd /usr/local/src
 +
wget https://github.com/kamilfb/mqtt-spy/releases/download/mqtt-spy_v0.5.4/mqtt-spy-0.5.4-jar-with-dependencies.jar
  
mqtt password
+
Menjalankan dengan
  
Then we can open terminal window on server and type following command
+
java -jar mqtt-spy-0.5.4-jar-with-dependencies.jar
  
mosquitto_sub -v -t 'linoxide/topic' -u mqtt-spy -P password
+
Langkah yang perlu dilakukan
  
To explain flags, -v is for verbosity, -t is for topic followed by topic inside quotes, -u is for user and -P is password. After typing this command it will seeming hang, but then we need to use MQTT-spy to create new topic and send a message to same topic, like on picture bellow.
+
* Klik new connection, masukan IP server (misalnya 192.168.0.100)
 +
* Kalau anonymous tidak di ijinkan, masukan username & password, untuk percobaan mqtt-spt 123456
 +
* masuk ke salah satu topik misalnya "lampu"
  
MQTT-spy
+
Buka terminal di server, ketik misalnya,
  
==Conclusion==
+
mosquitto_sub -v -t 'lampu' -u mqtt-spy -P 123456
 
 
We have successfully installed Mosquitto MQTT server that enables you to have network of connected IoT devices over MQTT 3.1 protocol. We installed it on Ubuntu on classic x86 PC which is what most people use for development and learning purposes. For real IoT you would want an ARM device. This is all for this article thank you for reading.
 
  
 +
dimana
  
 +
-v  verbosity
 +
-t  topik yang di ikuti
  
 +
coba ketik dari aplikasi java misalnya on, akan keluar
  
 +
mosquitto_sub -v -t 'lampu' -u mqtt-spy -P 123456
 +
lampu ON
 +
lampu ON
 +
lampu ON
 +
lampu ON
 +
lampu ON
 +
lampu ON
 +
lampu ON
  
 
==Referensi==
 
==Referensi==
  
 
* http://linoxide.com/tools/setup-mosquitto-mqtt-server-ubuntu-16-04/
 
* http://linoxide.com/tools/setup-mosquitto-mqtt-server-ubuntu-16-04/
 +
 +
 +
==Pranala Menarik==
 +
 +
* [[MQTT]]

Latest revision as of 15:41, 11 May 2018

sumber: http://linoxide.com/tools/setup-mosquitto-mqtt-server-ubuntu-16-04/

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 16.04 dan kami akan mengirim pesan dari perangkat lunak MQTT-spy. Mosquitto adalah proyek Eclipse dan didistribusikan dengan lisensi EDL. Jadi mari kita mulai.

Kompilasi server Mosquitto MQTT dari sumber

Instalasi aplikasi pendukung,

sudo su
apt update
locale-gen id_ID.UTF-8
apt install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc

Tambahkan mosquitto user, karena secara default berjalan sebagai non-root.

adduser mosquitto

Untuk memudahkan beri sudo rights ke user ini, karena kita akan melakukan proses instalasi dengan user ini.

usermod -aG sudo mosquitto

Login sebagai mosquitto

su mosquitto
cd

Download, extraxt, compile & install,

wget https://mosquitto.org/files/source/mosquitto-1.5.gz
tar xvzf mosquitto-1.5.tar.gz
cd mosquitto-1.5/
make && sudo make install

Konfigurasi Mosquitto MQTT Server

Pada dasarnya mosquitto memungkinkan anonymous access.

Jika dibutuhkan, kita bisa membuat password untuk user 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. 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/mosquitto.conf

Isi dengan

persistence true
persistence_location /var/lib/mosquitto/
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

Atau kalau mau aman

cd /etc/mosquitto
cp mosquitto.conf.example mosquitto.conf


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 Sab 2017-05-20 08:18:28 WIB; 1h 5min ago
 Main PID: 751 (mosquitto)
    Tasks: 1 (limit: 512)
   Memory: 2.8M
      CPU: 6.170s
   CGroup: /system.slice/mosquitto.service
           └─751 /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Config loaded from /etc/mosquitto/mosquitto.conf.
Mei 20 08:18:30 refserver mosquitto[751]: Config loaded from /etc/mosquitto/mosquitto.conf.
Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Opening ipv4 listen socket on port 1883.
Mei 20 08:18:30 refserver mosquitto[751]: Opening ipv4 listen socket on port 1883.
Mei 20 08:18:30 refserver mosquitto[751]: 1495243110: Opening ipv6 listen socket on port 1883.
Mei 20 08:18:30 refserver mosquitto[751]: Opening ipv6 listen socket on port 1883.
Mei 20 08:48:30 refserver mosquitto[751]: 1495244910: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
Mei 20 08:48:30 refserver mosquitto[751]: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
Mei 20 09:18:31 refserver mosquitto[751]: 1495246711: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
Mei 20 09:18:31 refserver mosquitto[751]: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

Setup MQTT-Spy dan connnecting

Instalasi apps pendukung

apt-get install openjfx


MQTT-Spy adalah Java based client untuk menyambungkan diri ke MQTT server. MQTT-Spy bisa di ambil di

https://github.com/eclipse/paho.mqtt-spy/wiki/Downloads
https://github.com/kamilfb/mqtt-spy/releases/download/mqtt-spy_v0.5.4/mqtt-spy-0.5.4-jar-with-dependencies.jar

download

cd /usr/local/src
wget https://github.com/kamilfb/mqtt-spy/releases/download/mqtt-spy_v0.5.4/mqtt-spy-0.5.4-jar-with-dependencies.jar

Menjalankan dengan

java -jar mqtt-spy-0.5.4-jar-with-dependencies.jar

Langkah yang perlu dilakukan

  • Klik new connection, masukan IP server (misalnya 192.168.0.100)
  • Kalau anonymous tidak di ijinkan, masukan username & password, untuk percobaan mqtt-spt 123456
  • masuk ke salah satu topik misalnya "lampu"

Buka terminal di server, ketik misalnya,

mosquitto_sub -v -t 'lampu' -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 'lampu' -u mqtt-spy -P 123456
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON
lampu ON

Referensi


Pranala Menarik