目录

介绍

环境

先决条件 - 设置静态 IP 地址

安装 Mosquitto

启动/停止 Mosquitto

配置先决条件 - 安装 mqtt_spy

配置 Mosquitto

配置 Mosquitto - 无安全性

测试 Mosquitto 配置 - 无安全性

配置 Mosquitto - 使用密码身份验证

Mosquitto 测试 - 带密码验证

概括

介绍

在本文中,这是致力于令人惊叹的 Raspberry Pi 的系列文章的第二篇文章,我们将使用 Mosquitto 将 Pi 配置为 MQTT 消息代理。Mosquitto 是一个轻量级但功能强大的发布/订阅模型消息传递系统,可以安装在各种平台上。本教程的目标是在 Rasspberry Pi 2 上安装、配置和测试软件,并且不仅能够在 Pi 上发布和订阅消息,还能够在其他计算机上发布和订阅消息。

​ 如果您还没有阅读描述如何加载和配置操作系统的第一篇文章,您可以在Raspberry Pi 2, 1 of n - 初始设置或代码项目中找到它。虽然不是必需阅读,但如果您是 Linux 或 Pi 的新手,我鼓励您查看一下。

本文或多或少是我使用 Mosquitto 将 Raspberry Pi 设置为 MQTT 消息代理的步骤的记录。我使用 SwitchDoc Labs [3]的教程作为参考,但对其进行了修改以满足我的特定需求。在以后的文章中,我计划悬挂各种设备,例如;ESP8266-12E WiFi 模块、嵌入式处理器板(例如 Arduino UNO 以及可能的其他板)有望在今年夏天趁远足和露营天气炎热时抽出时间来做这件事。

环境

我为此项目设置的开发环境是;

Windows 桌面运行 64 位 Windows 7 操作系统,配备 AMD-FX-8350 8 核处理器和 32GB 内存。Raspberry Pi 2,BCM2709 ARMv7 4 核,具有 1GB RAM,运行 Raspian Jesse 操作系统。下载站点上有一个新的 Raspbian Jessie Lite 版本,我最初为该项目加载和配置了该版本,但它没有 GUI,我计划在以后的文章中使用 NginX 和 WebSockets 来提供用于控制各种设备的前端所以我恢复使用完整版本并删除其中的垃圾软件。Pi 上的 WiFi 由edimax 适配器 提供。运行 Ubuntu 12.04 LTS 的 VirtualBox VM,我的配置几乎与 Pi 相同,但修改了一些针对 Ubuntu 的项目。虽然我已经将其设置为自己使用,但我不会详细介绍我是如何做到这一点的,我会将其留给您作为额外的学分练习。祝蚱蜢好运!

先决条件 - 设置静态 IP 地址

尽管此步骤是可选的,但我强烈建议您在 Pi 上设置静态 IP 地址[8,11]。如果不这样做,Pi 可能会 在启动时选择不同的 IP 地址,并且每次启动 Pi 时都必须编辑配置文件,这将是一个 PITA。对我来说,使用廉价的edimax 加密狗, 这是一个无痛的过程。如果您愿意等待,可以通过 eBay 购买 802.11 加密狗,每件价格约为 2 美元。我订购了其中一些,它们工作得相当好,尽管我还没有确定是否会在生产环境中使用它们。

此处描述的过程使用 edimax 加密狗,但应该适用于 Pi 支持的任何兼容 WiFi 加密狗。在编辑配置网络所需的系统文件之前,我们需要收集一些信息。

#To display current network info type;

ifconfig

输出应该与此类似;

wlan0 Link encap:Ethernet HWaddr b8:27:eb:a8:cf:a4

inet addr:192.168.254.20 Bcast:192.168.254.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:161 errors:0 dropped:0 overruns:0 frame:0

TX packets:227 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:26075 (25.4 KiB) TX bytes:46356 (45.2 KiB)

inet addr 和 Mask 值是我们要寻找的值,在我的例子中,addr是 192.168.254.20,Mask 是 255.255.255.0,在大多数情况下都是如此。接下来我们需要找到网关地址。

#To display network gateway address type;

netstat -n -r

输出应该看起来像这样;

Kernel IP routing table

Destination Gateway Genmask Flags MSS Window irtt Iface

0.0.0.0 192.168.254.254 0.0.0.0 UG 0 0 0 eth0

192.168.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

由此我们得到目标或网络地址是 192.168.254.0,网关地址是 192.168.254.254,现在我们准备配置我们的网络。

我们需要修改的第一个文件是 wpa_supplicant [12]文件。wpa_supplicant [13]实现无线网络的安全协议。[13]

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

network={

ssid="Your SSID Here"

psk="YourPresharedKeyHere"

}

请注意,“network={”中没有空格,这是故意的。将 ssid 和 psk 值替换为您的无线网络值,保存并关闭文件。我使用nano来编辑这些文件,当要求保存文件时,保存和退出的过程是ctrl+X和“Y” 。

接下来,我们将继续讨论网络接口文件,我们将在其中配置 wlan0 无线网络适配器。使用您在前面的步骤中收集的值并将它们插入文件中的适当区域,如下所示。

allow-hotplug wlan0

iface default inet static

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

address 192.168.254.20

netmask 255.255.255.0

network 192.168.254.0

gateway 192.168.254.254

重新启动 Pi 并尝试从另一台计算机执行 ping 操作,或尝试使用您选择的浏览器访问网站。Jessie 预装了 IceWeasel,即 Firefox。如果一切按计划进行,那么您应该在线并准备好继续,如果没有重新访问前面的步骤或谷歌搜索适合您特定硬件配置的解决方案。

安装 Mosquitto

安装 Mosquitto 相当简单,我们将配置代理侦听端口 1883 进行测试,它默认为端口 80。

在 Pi 上打开一个终端并一次输入以下命令。

sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

sudo apt-key add mosquitto-repo.gpg.key

cd /etc/apt/sources.list.d/

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

sudo apt-get update

sudo apt-get install mosquitto mosquitto-clients

wget [4]代表“web get”,是一个从网站递归下载文件和目录结构的实用程序。在这种情况下,我们将获得一个 GPG(Gnu Privacy Guard Key),它允许提供商使用通用密钥系统对您的数据和通信进行加密和签名。apt-key [3]用于管理 apt(高级打包工具)用于验证包的密钥列表。更改目录并使存储库可供 apt 使用。获取存储库文件,其中包含指向下载包的 URL 的链接。使用 apt-get 更新 apt 信息[5]最后安装 Mosquitto 和客户端。客户端提供 mosquitto_pub 和 mosquitto_sub 实用程序。我们将在稍后的测试阶段使用它们!

如果一切按计划进行,Mosquitto 应该已安装,并且默认情况下将启动,由于我们需要配置它,因此我们需要停止它。请参阅下一节了解如何阻止 Mosquitto。

启动/停止 Mosquitto

当 Pi 启动时,它会自动启动 Mosquitto,因此我们想了解如何启动、停止和重新启动该应用程序。

#To start the Mosquitto service

sudo /etc/init.d/mosquitto start

#To stop the Mosquitto service

sudo /etc/init.d/mosquitto stop

#To restart the Mosquitto service

sudo /etc/init.d/mosquitto restart

配置先决条件 - 安装 mqtt_spy

这也是一个可选步骤,但我发现 mqtt_spy [7]实用程序在配置/测试阶段非常有价值。mqtt-spy 是一个 JavaFX 应用程序,因此理论上应该可以在安装了适当版本的 Java 8 的任何操作系统上运行。

当 mqtt_spy 首次启动时,用户会看到以下窗口,敏锐的观察者会注意到应用程序认为我拥有的版本不是最新的,但它是吗?无论如何,显示已配置连接的部分是我们主要关心的。该应用程序预装了几种常见的配置,例如本地主机等。

要为我们的目的配置连接,请从主菜单中选择“连接”=>“新建连接”菜单项。这将显示所示的窗口并输入与您的配置相关的信息并使用适当的名称保存。

连接建立后我们就可以开始工作了。如本教程前面所述,MQTT 协议是一种发布订阅模型,因此下面显示的窗口分为 2 个部分;顶部是发布部分,底部是订阅部分。

首先,我们通过单击“新建”按钮创建一个新订阅,并在弹出的对话框中输入主题信息,输入主题后单击“订阅”按钮。

接下来,我们将向刚刚配置的主题发布一条消息,因此在发布部分中输入主题、一条消息,然后单击“发布”按钮。该消息应显示在“订阅”部分的“数据”文本框中。瞧,这就是全部了,不是很酷吗?

在本教程的其余部分中,我将参考 mqtt_spy 实用程序,如果您没有安装它,请忽略它,因为我还将提供 Mosquitto 客户端命令行命令来从 Linux 终端测试配置。配置 mqtt_spy 时,单击主窗口中的连接按钮断开连接,这将导致按钮变为红色,进行适当的更改,然后再次单击连接按钮,如果连接成功,按钮将变为绿色。

配置 Mosquitto

在下面的部分中,我们将讨论 Mosquitto 可以配置运行的各种方式以及我们将用来测试配置的技术。当 Mosquitto 启动时,它会在 /etc/mosquitto/conf.d 目录中查找任何带有 .conf 扩展名的文件[10] ,如果找到,则使用它们来配置 Mosquitto。我们将创建一个文件 /etc/mosquitto/conf.d/mosquitto.conf 并输入后面代码部分中概述的数据。

配置 Mosquitto - 无安全性

这是没有安全性的基本配置,在端口 80 上侦听 IP 地址 localhost。使用您选择的编辑器编辑 /etc/mosquitto/conf.d/mosquitto.conf 文件,保存并重新启动 Mosquitto。

# Boolean value that determines whether clients that connect without providing a username are

# allowed to connect. If set to false then another means of connection should be created to

# control authenticated client access. Defaults to true.

allow_anonymous true

#

# Listen for incoming network connection on the specified port. A second optional argument

# allows the listener to be bound to a specific ip address/hostname. If this variable is used

# and neither the global bind_address nor port options are used then the default listener will

# not be started.

# listener port [bind address/host]

listener 1883 192.168.254.20

测试 Mosquitto 配置 - 无安全性

使用 mqtt_spy 实用程序,只需在服务器 URI 文本框中输入 IP 地址,应用并连接。然后转到发布/订阅窗口并输入适当的信息。

从命令行测试时,您需要打开 2 个窗口;一个用于发布,另一个用于订阅。在端口 1883 上输入以下内容,其中 topic = 'hello/world'。

#In the publish terminal window

sudo mosquitto_pub -d -t hello/world -m 'The message'

#In the subscribeterminal window

sudo mosquitto_sub -d -t hello/world

配置 Mosquitto - 使用密码身份验证

为了使用密码身份验证,我们需要编辑 mosquitto.conf 文件并添加 password_file 指令以及 pwfile 的路径。

# Boolean value that determines whether clients that connect without providing a username are

# allowed to connect. If set to false then another means of connection should be created to

# control authenticated client access. Defaults to true.

allow_anonymous false

#

# Listen for incoming network connection on the specified port. A second optional argument

# allows the listener to be bound to a specific ip address/hostname. If this variable is used

# and neither the global bind_address nor port options are used then the default listener will

# not be started.

#listener port [bind address/host]

listener 1883 192.168.254.20

#

# Set the path to a password file. If defined, the contents of the file are used to control

# client access to the broker. The file can be created using the mosquitto_passwd(1) utility.

# If mosquitto is compiled without TLS support (it is recommended that TLS support is

# included), then the password file should be a text file with each line in the format

# "username:password", where the colon and password are optional but recommended.

# If allow_anonymous is set to false, only users defined in this file will be able to connect.

# Setting allow_anonymous to true when password_fileis defined is valid and could be used with

# acl_file to have e.g. read only guest/anonymous accounts and defined users that can publish.

#password_file file path

password_file /etc/mosquitto/pwfile

接下来,我们需要使用 mosquitto_passwd 实用程序创建 pwfile,方法是在终端窗口中输入以下内容。

sudo mosquitto_passwd -c /etc/mosquitto/pwfile username

系统将提示您输入密码并输入另一个密码进行确认。

Mosquitto 测试 - 带密码验证

使用 mqtt_spy 选择菜单选项 Connections=>Manage Connections,然后选择 Security 选项卡、User auth。标签。选中启用用户身份验证复选框并输入配置 pwfile 时使用的用户名密码。然后连接并配置发布/订阅信息。

使用 pub/sub 终端输入以下命令;

#In the publish terminal window

sudo mosquitto_pub -d -t hello/world -p 1883 -u 'username' -P 'password' -m 'The message'

#In the subscribeterminal window

sudo mosquitto_sub -d -t hello/world -p 1883 -u 'username' -P 'password'

概括

我发现 Mosquitto 易于使用,功能强大,有足够的文档和教程来帮助我入门,而且足够直观,我可以在短时间内安装和配置它。本教程已经做了好几次了,而且很简单,因此我的目的是在以后的文章和家庭物联网项目中使用它,这将是本系列的最终目标。

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: