Raspberry Pi 2,第2个,共n个-作为物联网消息代理的Pi(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/pi/raspberry-pi-of-n-the-pi-as-an-iot-message-broker-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 14 分钟阅读 - 6793 个词 阅读量 0Raspberry Pi 2,第2个,共n个-作为物联网消息代理的Pi(译文)
原文地址:https://www.codeproject.com/Articles/1107378/Raspberry-Pi-of-n-The-Pi-as-an-IoT-message-broker
原文作者:Mike Hankey
译文由本站 robot-v1.0 翻译
前言
Configuring the Raspberry Pi as an MQTT message broker using Mosquitto.
使用Mosquitto将Raspberry Pi配置为MQTT消息代理.
介绍(Introduction)
在本文中,这是有关Raspberry Pi惊人系列的第二篇文章,我们将使用Mosquitto将Pi配置为MQTT消息代理. Mosquitto是一个轻量级但功能强大的发布/订阅模型消息传递系统,可以安装在各种平台上.我们在本教程中的目标是在Rasspberry Pi 2上安装,配置和测试软件,并能够不仅在Pi上而且在其他计算机上发布和订阅消息.(In this, the second article in an ongoing series dedicated to the amazing Raspberry Pi we will be configuring the Pi as an MQTT message broker using Mosquitto. Mosquitto is a lightweight, but powerful publish/subscribe model messaging system that can be installed on a wide varieity of platforms. Our goal in this tutorial is to install, configure and test the software on a Rasspberry Pi 2 and to be able to publish and subscribe to messages on not only the Pi but other copmputers as well.) 如果您尚未阅读介绍如何加载和配置操作系统的第一篇文章,可以在以下位置找到它(If you haven’t read the first article describing how to load and configure the Operating System you can find it at) Raspberry Pi 2,第1个,共n个-初始设置(Raspberry Pi 2, 1 of n - Initial setup) 或上(or on) 代码项目(Code Project) .虽然不需要阅读,但如果您是Linux或Pi的新手,我还是鼓励您检查一下.(. While not required reading I would encourage you to check it out if you are new to Linux or the Pi.) 本文或多或少是我使用Mosquitto将Raspberry Pi设置为MQTT消息代理的步骤的笔录.我在SwitchDoc Labs使用了该教程(This article is more or less a transcript of the steps I used to set up the Raspberry Pi as an MQTT message broker using Mosquitto. I used the tutorial at SwitchDoc Labs)3作为参考,但对其进行了修改以满足我的特殊需求.在以后的文章中,我计划悬挂各种设备,例如; ESP8266-12E WiFi模块,诸如Arduino UNO之类的嵌入式处理器板以及可能的其他产品,并有望在今年夏天趁热徒步和扎营时解决这个问题.(as a reference but modified it to suit my particular needs. In future articles I plan on hanging various devices such as; the ESP8266-12E WiFi module, embedded processor boards such as the Arduino UNO and possibly others, and will hopefully get around to doing it this summer while it’s to hot to hike and camp.)
环境(Environment)
我为此项目设置的开发环境是:(The development environment that I have set up for this project is;)
- 运行64位Windows 7操作系统,AMD-FX-8350 8核心处理器和32GB内存的Windows桌面.(Windows Desktop runnng a 64-bit Windows 7 OS with an AMD-FX-8350 8 core processor and 32GB of memory.)
- Raspberry Pi 2,BCM2709 ARMv7 4核,具有1GB RAM,运行Raspian Jesse操作系统.我最初为该项目加载和配置的下载站点上有一个新的Raspbian Jessie Lite版本,但没有GUI,我计划在以后的文章中使用NginX和WebSockets提供用于控制各种设备的前端所以我恢复使用完整版本,只是将垃圾软件删除了. Pi上的WiFi由(Raspberry Pi 2, BCM2709 ARMv7 4-core with 1GB RAM running the Raspian Jesse operating system. There is a new Raspbian Jessie Lite version available at the download site that I originally loaded and configured for this project but it doesn’t have a GUI and I plan on using NginX and WebSockets in future articles to provide a front end for controlling various devices so I reverted to using the full version and just stripped the junkware out of it. WiFi on the Pi is provided by a) edimax加密狗(edimax dongle) .(.)
- 运行Ubuntu 12.04 LTS的VirtualBox VM,我与Pi进行了几乎相同的配置,但修改了一些特定于Ubuntu的项目.尽管我将其设置为自己使用,但我不会介绍它的工作原理,但我会把它留给您作为额外的功劳.祝你好运蚱grass!(VirtualBox VM running Ubuntu 12.04 LTS that I configured nearly identically to the Pi with but a few items modified that were specific to Ubuntu. Although I have set this up for my own use I will not go into the mechanics of how I did it, I will leave that to you as an extra credit exercise. Good luck grasshopper!)
先决条件-设置静态IP地址(Prerequisite - Setting static IP address)
尽管此步骤是可选步骤,但我强烈建议您在Pi上设置静态IP地址(Although this step is optional I highly encourage you to set a static IP address on the Pi)8,11.如果您不这样做,Pi可能会在启动时选择其他IP地址,并且每次启动Pi时都必须编辑配置文件是PITA.对我来说,使用廉价的设备可轻松完成(. If you don’t the Pi may select a different IP address when booted and it would be a PITA to have to edit the configuration files every time you boot your Pi. For me it was a painless procedure using an inexpensive) edimax加密狗(edimax dongle) .如果您愿意等待,可以通过ebay购买802.11加密狗,每单位大约2美元.我订购了其中的一些,它们工作得很好,尽管我还不确定是否可以在生产环境中使用它们.(. If you’re willing to wait an 802.11 dongle may be purchased through ebay for about $2 per unit. I ordered a few of them and they worked fairly well, although I haven’t determined if I would use them in a production environment yet.) 此处描述的过程使用edimax加密狗,但应适用于Pi支持的任何兼容的WiFi加密狗.在我们可以编辑配置网络所需的系统文件之前,我们需要收集一些信息.(The procedure described here uses the edimax dongle but should work for any compatible WiFi dongle supported by the Pi. Before we can edit the system files required to configure our network we need to gather some information.) 输出应与此类似;(The output should look similar to this;) 我们正在寻找inet addr和Mask值,在我的情况下,(The inet addr and Mask values are what we’re looking for and in my case the)**地址是192.168.254.20(addres is 192.168.254.20)**和(and the)**遮罩为255.255.255.0(Mask is 255.255.255.0)**在大多数情况下都是如此.接下来,我们需要找到网关地址.(which it will be in most cases. Next we need to find the gateway address.) 输出应如下所示;(The output should look something like this;) 从中我们得到(and from this we get that the)**目标或网络地址为192.168.254.0(Destination or network address is 192.168.254.0)**和(and the)网关地址是192.168.254.254(gateway address is 192.168.254.254),现在我们可以配置网络了.(, now we’re ready to configure our network.) 我们需要修改的第一个文件是wpa_supplicant(The first file we need to modify is the wpa_supplicant)12文件. wpa_supplicant(file. wpa_supplicant)13实现无线网络的安全协议.(implements security protocols for wireless networks.)13 请注意," network ={“中没有空格,这是有意的.用无线网络值替换ssid和psk值,保存并关闭文件.我用(Notice that there are no spaces in “network={”, this is intentional. Replace the ssid and psk values with your wireless network values, save and close the file. I use)**纳米(nano)**编辑这些文件,保存和退出的过程是(to edit these files and the procedure to save and exit is)**Ctrl + X(ctrl+X)和(and)” Y"(“Y”)**当要求保存文件时.(when asked to save the file.) 接下来,我们将转到网络接口文件,在其中配置wlan0无线网络适配器.使用您在前面的步骤中收集的值,并将它们插入文件中的相应区域,如下所示.(Next we’ll move on to the network interfaces file where we’ll configure the wlan0 wireless network adapter. Use the values you collected in the preceding steps and plug them in the appropriate areas in the file as shown below.) 重新启动树莓派并尝试从另一台计算机ping或尝试使用您选择的浏览器访问网站. IceWeasel是Firefox,它是Jessie预先安装的.如果一切都按计划进行,那么您应该在线上并可以继续进行下去,如果不重新访问上述步骤,或者谷歌搜索适合您特定硬件配置的解决方案.(Reboot the Pi and attempt to ping from another machine or try to reach a web site using the browser of your choice. IceWeasel, which is Firefox is what comes preinstalled with Jessie. If everything went according to plan then you should be online and ready to continue on, if not revisit the preceding steps or google for the solution that fits your particular hardware configuration.)
安装Mosquitto(Installing Mosquitto)
安装Mosquitto非常简单,我们将配置代理以侦听端口1883进行测试,默认情况下为端口80.(Installing Mosquitto is fairly straight forward and we will be configure the broker to listen on port 1883 for testing, it defaults to port 80.) 在树莓派上打开一个终端,一次输入以下命令.(Open a terminal on the Pi and enter the following commands one at a time.)
- get(wget)4代表" Web get",是一种从网站递归下载文件和目录结构的实用程序.在这种情况下,我们将获得GPG Gnu隐私保护密钥,该密钥允许提供商使用通用密钥系统对您的数据和通信进行加密和签名.(stands for “web get” and is a utility that recursively downloads files and directory structure from a web site. In this case we’re getting a GPG, Gnu Privacy Guard Key that allows the provider to encrypt and sign your data and communication using a verstaile key system.)
- apt键(apt-key)3用于管理高级打包工具apt用来验证软件包的密钥列表.(is used to manage the list of keys used by apt, the Advanced Packaging Tool to authenticate packages.)
- 更改目录并使存储库可用于apt.(Change Directory and make the repository available to apt.)
- 获取存储库文件,该文件包含指向URL的链接以从中下载软件包.(Get the repository file, which contains a link to the URL to download the package from.)
- 使用apt-get更新apt信息(Update apt information using apt-get)5
- 最后安装Mosquitto和客户端.客户端提供mosquitto_pub和mosquitto_sub实用程序.我们将在测试阶段稍后使用它们!(Finally install Mosquitto and clients. The clients provide the mosquitto_pub and mosquitto_sub utilities. We’ll be using these later in the testing phase!) 如果一切按计划进行,则应安装Mosquitto,并且默认情况下将启动它,并且由于我们需要对其进行配置,因此需要将其停止.请参阅下一节以了解如何停止蚊子.(If all went according to plan Mosquitto should be installed and by default will be started and since we need to configure it we will need to stop it. Refer to the next section to learn how to stop Mosquitto.)
启动/停止Mosquitto(Starting/Stopping Mosquitto)
当Pi启动时,它会自动启动Mosquitto,因此我们想学习如何启动,停止和重新启动应用程序.(When the Pi boots up it automatically starts Mosquitto so we want to learn how to start, stop and restart the application.)
配置先决条件-安装mqtt_spy(Configuration Prerequisite - Install mqtt_spy)
这也是可选步骤,但我发现了mqtt_spy(This is also an optional step but I found the mqtt_spy)7该实用程序在配置/测试阶段非常宝贵. mqtt-spy是JavaFX应用程序,因此,从理论上讲,它应可在安装了适当版本的Java 8的任何操作系统上运行.(utility to be invaluable in the configuring/testing phase. mqtt-spy is a JavaFX application, so in theory should work on any operating system with an appropriate version of Java 8 installed.) 当mqtt_spy第一次启动时,会向用户显示以下窗口,其中敏锐的观察者会注意到应用程序认为我拥有的版本不是最新版本,而是吗?无论如何,显示配置的连接的部分是我们最关心的.该应用程序预加载了几种常用配置,例如localhost等.(When mqtt_spy first starts up the user is presented with the following window in which a sharp observer will notice that the application thinks that the version I have is not current, but it is? Anyway the section that shows the connections configured is what we are primarily concerned with. The app comes preloaded with several common configurations such as localhost, etc.) 要出于我们的目的配置连接,请从主菜单中选择Connections => New Connection菜单项.这将显示显示的窗口,并输入与您的配置有关的信息并以适当的名称保存.(To configure a connection for our purposes select the Connections=>New Connection menu item from the main menu. This will display the window shown and enter the information that is pertenant to your configuration and save with an appropriate name.) 建立连接后,我们便可以开始业务了.如本教程前面所述,MQTT协议是一个发布订阅模型,因此,下面显示的窗口分为两部分;顶部是发布部分,底部是订阅部分.(Once the connection is established we can get down to business. As described earlier in this tutorial the MQTT protocol is a publishsubscribe model and as such the window shown below is devided into 2 sections; the top is the publish section and the bottom the subscribe section.) 首先,通过单击"新建"按钮创建一个新的订阅,然后在弹出的对话框中输入主题信息,输入主题后,单击"订阅"按钮.(First let’s create a new subscription by clicking on the New button and enter the topic information in the dialog that pops up, when you’ve entered the topic click the subscribe button.) 接下来,我们将向刚刚配置的主题发布消息,因此在"发布"部分中输入主题,消息并单击"发布"按钮.该消息应显示在"订阅"部分的"数据"文本框中.瞧,这就是全部,不是很酷吗?(Next we are going to publish a message to the topic we just configured so in the publish section enter the topic, a message and click the Publish button. The message should be displayed in the Data textbox in the Subscription section. Voila that’s all there is to it, ain’t it cool?) 如果没有安装该工具,我将在本教程的其余部分中参考mqtt_spy实用程序,将其忽略,因为我还将提供Mosquitto客户端命令行命令来从Linux终端测试配置.配置mqtt_spy时,在主窗口中单击连接按钮以断开连接,这将导致该按钮变为红色,进行适当的更改,然后再次单击该连接按钮,如果连接成功,该按钮将变为绿色.(I will refer to the mqtt_spy utility thoughout the remainder of this tutorial if you didn’t install it ignore it as I will also supply the Mosquitto client command line commands to test the configuration from a Linux terminal. When configuring mqtt_spy click on the connection button in the main window to disconnect, this will cause the button to turn red, make the appropriate changes then click on the connection button again and if the connection is succesful the button will turn green.)
配置Mosquitto(Configuring Mosquitto)
在以下各节中,我们将讨论Mosquitto可以配置为运行的各种方式,以及用于测试配置的技术. Mosquitto启动时,它会在/etc/mosquitto/conf.d目录中查找任何文件(In the following sections we will discuss the various ways that Mosquitto may be configured to run and the techniques in which we will use to test the configuration. When Mosquitto starts it looks in the /etc/mosquitto/conf.d directory for any files)10扩展名为.conf,如果找到,则使用它们来配置Mosquitto.我们将创建一个文件/etc/mosquitto/conf.d/mosquitto.conf并输入以下代码部分中概述的数据.(with the .conf extension and if found uses them to configure Mosquitto. We will create a file /etc/mosquitto/conf.d/mosquitto.conf and enter the data outlined in the code sections that follow.)
配置Mosquitto-不安全(Configuring Mosquitto - no security)
这是没有安全性的基本配置,侦听端口80上的IP地址localhost.使用您选择的编辑器编辑/etc/mosquitto/conf.d/mosquitto.conf文件,保存并重新启动Mosquitto.(This is the basic configuration with no security, listening to IP address localhost on port 80. Edit the /etc/mosquitto/conf.d/mosquitto.conf file, using the editor of your choosing, save and restart Mosquitto.)
测试Mosquitto配置-没有安全性(Testing Mosquitto configuration - no security)
使用mqtt_spy实用工具,只需在"服务器URI"文本框中输入IP地址,然后应用并连接.然后转到pub/sub窗口并输入适当的信息.(Using the mqtt_spy utility just enter the IP address in the Server URI textbox, apply and connect. Then go to the pub/sub window and enter the appropriate information.) 从命令行进行测试时,您需要打开2个窗口.一个用于发布,另一个用于下标.输入以下内容,其中主题=端口1883上的" hello/world".(When testing from the command line you will need to open 2 windows; one for publishing and another for subscripting. Enter the following where topic = ‘hello/world’ on port 1883.)
配置Mosquitto-使用密码验证(Configuring Mosquitto - with password authentication)
为了使用密码认证,我们需要编辑mosquitto.conf文件,并在密码文件中添加pwfile路径的指令.(In order to use password authentication we need to edit the mosquitto.conf file and add the password_file directive with the path to the pwfile.) 接下来,我们需要使用mosquitto_passwd实用程序创建pwfile,方法是在终端窗口中输入以下内容.(Next we need to create the pwfile using the mosquitto_passwd utility, do so by entering the following in a terminal window.) 系统将提示您输入密码,然后输入另一个密码进行确认.(You will be prompted to enter a password and another to confirm.)
Mosquitto测试-带有密码验证(Mosquitto Testing - with password authentication)
使用mqtt_spy选择菜单选项Connections => Manage Connections,然后选择Security选项卡,User auth.标签.选中"启用用户身份验证"复选框,并输入在配置pwfile时使用的用户名密码.然后连接并配置发布/订阅信息.(Using mqtt_spy select the menu option Connections=>Manage Connections, then select the Security tab, the the User auth. tab. Check the Enable User authentication cehckbox and enter the username passwords that you used when configuring the pwfile. Then connect and configure the pub/sub infommation.) 使用pub/sub终端输入以下命令;(Using the pub/sub terminals enter the following commands;)
概要(Summary)
我发现Mosquitto易于使用,功能强大,并且有足够的文档和教程来帮助我入门和直观,可以在短时间内安装和配置它.在本教程中是否花了很多时间,这是一个瞬间,因此,我的目的是在以后的文章和家庭IoT项目中使用它,这将是本系列的最终目标.(I found Mosquitto easy to work with, powerful with plenty of documentation and tutorials enough to get me started and intuitive enough that I could install and configure it in a short period of time. Did it serveral times for this tutorial and it was a snap, therefore my intention is to use this in future articles and the home IoT project that will be the ultimate goal of this series.)
参考文献(References)
1 mosquitto.org,Mosquitto,一个开源MQTT v3.1/v3.1.1 Broker(mosquitto.org, Mosquitto, An open source MQTT v3.1/v3.1.1 Broker)
2 raspberrypi.org,Raspbian Jessie Lite(raspberry.org, Raspbian Jesse Lite)
3 switchdoc.com,教程:IOT/在Raspberry Pi上安装和测试Mosquitto MQTT(switchdoc.com, Tutorial: IOT / Installing and Testing Mosquitto MQTT on the Raspberry Pi)
4 computerhope.com,Linux和Unix wget命令(computerhope.com, Linux and Unix wget command)
5 man.he.net,apt-key参考(man.he.net, apt-key reference)
6 help.ubuntu.com,Apt-Get(help.ubuntu.com, Apt-Get)
7 github.com,mqtt_spy(github.com, mqtt_spy)
8 jeffgeerling,在Raspberry Pi上使用Raspbian Jessie Lite设置静态IP地址(jeffgeerling, Setting a Static IP address with Raspbian Jessie Lite on the Raspberry Pi)
9 mosquitto.org,文档(mosquitto.org, documentation)
10 mosquitto.org,conf文件文档(mosquitto.org, conf file documentation)
11 modmpi.com,教程-如何为Raspberry Pi提供一个静态IP地址(modmpi.com, Tutorial - How to give your Raspberry Pi a Static IP Address)
12 wi.fi,示例wpa_supplicant配置文件(wi.fi, Example wpa_supplicant configuration file)
13 wikipedia.org,wpa_supplicant(wikipedia.org, wpa_supplicant)
14 github.com,kamilfb/mqtt_spy(github.com, kamilfb/mqtt_spy)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Raspberry DevOps mqtt 新闻 翻译