ユーザ用ツール

サイト用ツール


mae3xx_tips:setup_mosquitto:start

Mosquittoの導入 (MQTT)

MQTTとは

ここのページに非常にわかりやすいまとめがあります。
そこはかとなく書くよん。 - MQTTについてのまとめ

ここでは、Mosquitto をセットアップする方法を紹介します。


セットアップ

PPAの追加

Mosquitto PPA を、add-apt-repository コマンドで追加します。

user1@plum:~$ sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
[sudo] password for user1: 
 
 More info: https://launchpad.net/~mosquitto-dev/+archive/mosquitto-ppa
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpfmypnq/secring.gpg' created
gpg: keyring `/tmp/tmpfmypnq/pubring.gpg' created
gpg: requesting key 262C4500 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpfmypnq/trustdb.gpg: trustdb created
gpg: key 262C4500: public key "Launchpad mosquitto" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK
user1@plum:~$


パッケージリストの更新

“apt-get update” により、パッケージリストの更新を行います。

user1@plum:~$ sudo apt-get update
Ign http://ppa.launchpad.net saucy InRelease  
Ign http://ports.ubuntu.com saucy InRelease   
Get:1 http://ppa.launchpad.net saucy Release.gpg [316 B]
Ign http://ports.ubuntu.com saucy-updates InRelease  
Get:2 http://ppa.launchpad.net saucy Release [11.9 kB]
Ign http://ports.ubuntu.com saucy-security InRelease       
Hit http://ports.ubuntu.com saucy Release.gpg      
Get:3 http://ports.ubuntu.com saucy-updates Release.gpg [933 B]
Get:4 http://ppa.launchpad.net saucy/main armhf Packages [1328 B]
Get:5 http://ports.ubuntu.com saucy-security Release.gpg [933 B]
Hit http://ports.ubuntu.com saucy Release                              
Get:6 http://ports.ubuntu.com saucy-updates Release [49.6 kB]          
Get:7 http://ports.ubuntu.com saucy-security Release [49.6 kB]           
Ign http://ppa.launchpad.net saucy/main Translation-en   
Hit http://ports.ubuntu.com saucy/main armhf Packages    
Hit http://ports.ubuntu.com saucy/restricted armhf Packages
Hit http://ports.ubuntu.com saucy/universe armhf Packages
Hit http://ports.ubuntu.com saucy/main Translation-en
Hit http://ports.ubuntu.com saucy/restricted Translation-en
Hit http://ports.ubuntu.com saucy/universe Translation-en
Get:8 http://ports.ubuntu.com saucy-updates/main armhf Packages [219 kB]
Get:9 http://ports.ubuntu.com saucy-updates/restricted armhf Packages [14 B]                                 
Get:10 http://ports.ubuntu.com saucy-updates/universe armhf Packages [149 kB]                                
Get:11 http://ports.ubuntu.com saucy-updates/main Translation-en [106 kB]                                    
Hit http://ports.ubuntu.com saucy-updates/restricted Translation-en                                          
Hit http://ports.ubuntu.com saucy-updates/universe Translation-en                                            
Get:12 http://ports.ubuntu.com saucy-security/main armhf Packages [110 kB]                                   
Get:13 http://ports.ubuntu.com saucy-security/restricted armhf Packages [14 B]                               
Get:14 http://ports.ubuntu.com saucy-security/universe armhf Packages [34.8 kB]                              
Hit http://ports.ubuntu.com saucy-security/main Translation-en                                               
Hit http://ports.ubuntu.com saucy-security/restricted Translation-en                                         
Hit http://ports.ubuntu.com saucy-security/universe Translation-en                                           
Fetched 734 kB in 12s (61.1 kB/s)                                                                            
Reading package lists... Done
user1@plum:~$


Mosquitto関係のパッケージを検索してみます。

user1@plum:~$ sudo apt-cache search mosquitto
libmosquitto0 - MQTT version 3.1 client library
libmosquitto0-dev - MQTT version 3.1 client library, development files
libmosquittopp0 - MQTT version 3.1 client C++ library
libmosquittopp0-dev - MQTT version 3.1 client C++ library, development files
mosquitto - MQTT version 3.1 compatible message broker
mosquitto-clients - Mosquitto command line MQTT clients
python-mosquitto - MQTT version 3.1 Python client library
python3-mosquitto - MQTT version 3.1 Python 3 client library
libmosquitto-dev - MQTT version 3.1 client library, development files
libmosquittopp-dev - MQTT version 3.1 client C++ library, development files
user1@plum:~$

クライアント(ライブラリ/アプリケーション)、ブローカーサーバーなどが導入できることがわかります。


クライアントの導入

mosquitto-clients を導入します。

user1@plum:~$ sudo apt-get install mosquitto-clients
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libmosquitto0
The following NEW packages will be installed:
  libmosquitto0 mosquitto-clients
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 30.6 kB of archives.
After this operation, 147 kB of additional disk space will be used.
Do you want to continue [Y/n]? 
Get:1 http://ports.ubuntu.com/ubuntu-ports/ saucy/universe libmosquitto0 armhf 0.15-2ubuntu1 [13.1 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports/ saucy/universe mosquitto-clients armhf 0.15-2ubuntu1 [17.5 kB]
Fetched 30.6 kB in 1s (25.6 kB/s)       
Selecting previously unselected package libmosquitto0.
(Reading database ... 18538 files and directories currently installed.)
Unpacking libmosquitto0 (from .../libmosquitto0_0.15-2ubuntu1_armhf.deb) ...
Selecting previously unselected package mosquitto-clients.
Unpacking mosquitto-clients (from .../mosquitto-clients_0.15-2ubuntu1_armhf.deb) ...
Setting up libmosquitto0 (0.15-2ubuntu1) ...
Setting up mosquitto-clients (0.15-2ubuntu1) ...
Processing triggers for libc-bin ...
localepurge: Disk space freed in /usr/share/locale: 0 KiB
localepurge: Disk space freed in /usr/share/man: 0 KiB

Total disk space freed by localepurge: 0 KiB

user1@plum:~$


導入されたアプリケーションのファイルを確認してみます。

user1@plum:~$ sudo dpkg -L mosquitto-clients
/.
/usr
/usr/bin
/usr/bin/mosquitto_sub
/usr/bin/mosquitto_pub
/usr/share
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/mosquitto_pub.1.gz
/usr/share/man/man1/mosquitto_sub.1.gz
/usr/share/doc
/usr/share/doc/mosquitto-clients
/usr/share/doc/mosquitto-clients/copyright
/usr/share/doc/mosquitto-clients/changelog.Debian.gz
user1@plum:~$


サーバーブローカーの導入

サーバーブローカーを導入します。

user1@plum:~$ sudo apt-get install mosquitto
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  apparmor libapparmor-perl libapparmor1
Suggested packages:
  apparmor-profiles apparmor-docs apparmor-utils
The following NEW packages will be installed:
  apparmor libapparmor-perl libapparmor1 mosquitto
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 438 kB of archives.
After this operation, 1269 kB of additional disk space will be used.
Do you want to continue [Y/n]? 
Get:1 http://ports.ubuntu.com/ubuntu-ports/ saucy-updates/main libapparmor1 armhf 2.8.0-0ubuntu31.1 [38.1 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports/ saucy-updates/main libapparmor-perl armhf 2.8.0-0ubuntu31.1 [32.1 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports/ saucy-updates/main apparmor armhf 2.8.0-0ubuntu31.1 [314 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports/ saucy/universe mosquitto armhf 0.15-2ubuntu1 [54.1 kB]
Fetched 438 kB in 3s (122 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package libapparmor1.
(Reading database ... 18549 files and directories currently installed.)
Unpacking libapparmor1 (from .../libapparmor1_2.8.0-0ubuntu31.1_armhf.deb) ...
Selecting previously unselected package libapparmor-perl.
Unpacking libapparmor-perl (from .../libapparmor-perl_2.8.0-0ubuntu31.1_armhf.deb) ...
Selecting previously unselected package apparmor.
Unpacking apparmor (from .../apparmor_2.8.0-0ubuntu31.1_armhf.deb) ...
Selecting previously unselected package mosquitto.
Unpacking mosquitto (from .../mosquitto_0.15-2ubuntu1_armhf.deb) ...
Processing triggers for ureadahead ...
Setting up libapparmor1 (2.8.0-0ubuntu31.1) ...
Setting up libapparmor-perl (2.8.0-0ubuntu31.1) ...
Setting up apparmor (2.8.0-0ubuntu31.1) ...
Processing triggers for ureadahead ...
Setting up mosquitto (0.15-2ubuntu1) ...
mosquitto start/running, process 1774
Processing triggers for libc-bin ...
Processing triggers for ureadahead ...
localepurge: Disk space freed in /usr/share/locale: 0 KiB
localepurge: Disk space freed in /usr/share/man: 0 KiB

Total disk space freed by localepurge: 0 KiB

user1@plum:~$


サーバーブローカーを導入すると、自動でサービスとして起動します。

 1774 ?        Ss     0:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf


実行例

早速、導入したホスト上で、PUB/SUB の実験をしてみます。

ターミナルで、Subscribe するクライアントを立ち上げておきます。
トピックは “hello” としておきます。

user1@plum:~$ mosquitto_sub -d -t hello
Received CONNACK
Received SUBACK
Subscribed (mid: 1): 0


別のターミナルから、Subscribeされているトピック “hello” に対して、メッセージを Publish してみます。

user1@plum:~$ mosquitto_pub -d -t hello -m "Hello World"
Received CONNACK
Sending PUBLISH (d0, q0, r0, m1, 'hello', ... (11 bytes))
user1@plum:~$

Subscribe している側にメッセージが送信されたのが確認できます。

user1@plum:~$ mosquitto_sub -d -t hello
Received CONNACK
Received SUBACK
Subscribed (mid: 1): 0
Received PUBLISH (d0, q0, r0, m0, 'hello', ... (11 bytes))
Hello World
mae3xx_tips/setup_mosquitto/start.txt · 最終更新: 2014/04/09 18:04 by admin