当前位置:   article > 正文

10分钟教你在树莓派 4B 上搭建企业级 IoT 物联网平台

树莓派 iotgateway

00

准备工作

本系列物联网开发入门实战教程中硬件选取乐鑫 ESP8266 开发板,服务器采用树莓派 3B+/4B 主板,云端物联网平台基于开源EMQX社区版搭建,带你完整体验物联网场景端到端全栈开发过程。

乐鑫 ESP8266:

树莓派 4B:

01

树莓派系统烧录

树莓派(Raspberry Pi)是一款基于ARM的微型电脑主板,以SD/MicroSD卡为内存硬盘,卡片主板周围有1/2/4个USB接口和一个10/100 以太网接口,可连接键盘、鼠标和网线,同时拥有视频模拟信号的电视输出接口和HDMI高清视频输出接口,以上部件全部整合在一张仅比信用卡稍大的主板上,具备所有PC的基本功能只需接通电视机和键盘,就能执行如电子表格、文字处理、玩游戏、播放高清视频等诸多功能。

安装烧录工具

首先,打开树莓派官网,根据你的电脑系统下载对应烧录工具

https://www.raspberrypi.com/software/

4a00047320cf2b4d57c316b5fe3c8cc4.png

安装 Raspberry Pi Imager后,打开软件,选择 Raspberry Pi OS (64-bit),并选择存储 MicroSD 卡。

fb379cd5aefc577c2edcd2c25a3991c8.png

d9a4685b924abbaae415005d33de4951.png

点击右下角的设置按钮,进行配置工作。

396250fb4fd4c9d6806fa2651ca8df13.png

在高级设置页面,你需要根据实际情况配置如下信息:

配置项
参数值
主机名emqx.local
SSH服务开启
username : emqx
密码 : emqx@123
配置Wi-Fi热点名 : 按实际情况配置
密码 : 按实际情况配置

079b9d03407f2e2258fe556e8e4516d0.png

最后,点击烧录按钮,开始烧录固件,直到烧录完成。

4b314e31ad9d313640e8a3df4c42b31f.png

0f0da82eb78c72ea3135efd933f5763d.png

02

安装 EMQX 社区版

EMQX  是一款完全开源,高度可伸缩,高可用的分布式 MQTT 消息服务器,同时也支持 CoAP/LwM2M 协议,适用于 IoT、M2M 和移动应用程序。

EMQX 提供了高效可靠海量物联网设备连接,可处理千万级别的并发,实时处理消息和事件流数据,帮助您快速构建关键业务的物联网平台与应用。

  • 基于 APL 2.0 开放源码协议

  • 完整 MQTT 3.1.0、3.1.1 和 5.0 规范,支持 MQTT-SN

  • Masterless 高可用集群架构

  • 高并发、低时延、高性能

  • 可扩展的网关和插件体系

因为树莓派系统是基于 Debian 裁剪版,所以我们下载支持 Debian 操作系统的开源社区版。操作步骤如下:

2.1 SSH登陆树莓派:

ssh emqx@emqx.local

2.2 配置 EMQX Apt 源:

curl -s https://assets.emqx.com/scripts/install-emqx-deb.sh | sudo bash

2.3 安装 EMQX:

sudo apt-get install emqx

2.4 启动 EMQX:

sudo systemctl start emqx

控制台完整操作过程如下:

  1. Last login: Sat Aug 19 21:01:31 on console
  2. iot$ ssh emqx@emqx.local
  3. The authenticity of host 'emqx.local (***)' can't be established.
  4. ECDSA key fingerprint is SHA256:***/ouBrME10.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added 'emqx.local,***%en0' (ECDSA) to the list of known hosts.
  7. emqx@emqx.local's password:
  8. Linux emqx 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64
  9. The programs included with the Debian GNU/Linux system are free software;
  10. the exact distribution terms for each program are described in the
  11. individual files in /usr/share/doc/*/copyright.
  12. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
  13. permitted by applicable law.
  14. Last login: Wed May 3 04:23:52 2023
  15. emqx@emqx:~ $ curl -s https://assets.emqx.com/scripts/install-emqx-deb.sh | sudo bash
  16. Detected operating system as debian/bullseye.
  17. Checking for curl...
  18. Detected curl...
  19. Checking for gpg...
  20. Detected gpg...
  21. Running apt-get update... done.
  22. Installing debian-archive-keyring which is needed for installing
  23. apt-transport-https on many Debian systems.
  24. Installing apt-transport-https... done.
  25. Installing /etc/apt/sources.list.d/emqx_emqx.list...done.
  26. Importing gpg key... done.
  27. Running apt-get update... done.
  28. The repository is setup! You can now install packages.
  29. emqx@emqx:~/ $ sudo apt-get install emqx
  30. Reading package lists... Done
  31. Building dependency tree... Done
  32. Reading state information... Done
  33. The following package was automatically installed and is no longer required:
  34. libfuse2
  35. Use 'sudo apt autoremove' to remove it.
  36. The following NEW packages will be installed:
  37. emqx
  38. 0 upgraded, 1 newly installed, 0 to remove and 96 not upgraded.
  39. Need to get 39.9 MB of archives.
  40. After this operation, 93.8 MB of additional disk space will be used.
  41. Get:1 https://packages.emqx.com/emqx/emqx/debian bullseye/main arm64 emqx arm64 5.1.6 [39.9 MB]
  42. Fetched 39.9 MB in 16s (2,523 kB/s)
  43. Selecting previously unselected package emqx.
  44. (Reading database ... 96881 files and directories currently installed.)
  45. Preparing to unpack .../archives/emqx_5.1.6_arm64.deb ...
  46. Unpacking emqx (5.1.6) ...
  47. Setting up emqx (5.1.6) ...
  48.  emqx
  49. State: degraded
  50. Jobs: 0 queued
  51. Failed: 1 units
  52. Since: Thu 1970-01-01 01:00:04 BST; 53 years 8 months ago
  53. CGroup: /
  54. ├─user.slice
  55. │ └─user-1000.slice
  56. │ ├─user@1000.service …
  57. │ │ ├─app.slice
  58. │ │ │ ├─gvfs-goa-volume-monitor.service
  59. │ │ │ │ └─1005 /usr/libexec/gvfs-goa-volume-monitor
  60. │ │ │ ├─pulseaudio.service
  61. │ │ │ │ └─687 /usr/bin/pulseaudio --daemonize=no --log-target=journal
  62. │ │ │ ├─gvfs-daemon.service
  63. │ │ │ │ ├─ 844 /usr/libexec/gvfsd
  64. │ │ │ │ ├─ 849 /usr/libexec/gvfsd-fuse /run/user/1000/gvfs -f
  65. │ │ │ │ └─1021 /usr/libexec/gvfsd-trash --spawner :1.7 /org/gtk/gvfs/exec_spaw/0
  66. │ │ │ ├─gvfs-udisks2-volume-monitor.service
  67. │ │ │ │ └─988 /usr/libexec/gvfs-udisks2-volume-monitor
  68. │ │ │ ├─gvfs-gphoto2-volume-monitor.service
  69. │ │ │ │ └─1009 /usr/libexec/gvfs-gphoto2-volume-monitor
  70. │ │ │ ├─pipewire.service
  71. │ │ │ │ ├─686 /usr/bin/pipewire
  72. │ │ │ │ └─730 /usr/bin/pipewire-media-session
  73. │ │ │ ├─dbus.service
  74. │ │ │ │ └─715 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopid…
  75. │ │ │ ├─gvfs-mtp-volume-monitor.service
  76. │ │ │ │ └─995 /usr/libexec/gvfs-mtp-volume-monitor
  77. │ │ │ └─gvfs-afc-volume-monitor.service
  78. │ │ │ └─1013 /usr/libexec/gvfs-afc-volume-monitor
  79. │ │ └─init.scope
  80. │ │ ├─625 /lib/systemd/systemd --user
  81. │ │ └─654 (sd-pam)
  82. │ ├─session-3.scope
  83. │ │ ├─563 /bin/login -f
  84. │ │ └─708 -bash
  85. │ ├─session-4.scope
  86. │ │ ├─ 815 sshd: emqx [priv]
  87. │ │ ├─1136 sshd: emqx@pts/0
  88. │ │ ├─1137 -bash
  89. │ │ ├─2478 sudo apt-get install emqx
  90. │ │ ├─2479 apt-get install emqx
  91. │ │ ├─2520 /usr/bin/dpkg --status-fd 32 --configure --pending
  92. │ │ ├─2521 /bin/sh /var/lib/dpkg/info/emqx.postinst configure
  93. │ │ └─2535 systemctl status --no-pager
  94. │ └─session-1.scope
  95. │ ├─615 lightdm --session-child 13 16
  96. │ ├─703 /usr/bin/lxsession -s LXDE-pi -e LXDE -w openbox-lxde-pi
  97. │ ├─789 /usr/bin/ssh-agent x-session-manager
  98. │ ├─857 openbox --config-file /home/emqx/.config/openbox/lxde-pi-rc.xml
  99. │ ├─861 lxpolkit
  100. │ ├─863 lxpanel --profile LXDE-pi
  101. │ ├─865 pcmanfm --desktop --profile LXDE-pi
  102. │ ├─877 /usr/bin/ssh-agent -s
  103. │ ├─881 /usr/bin/python3 /usr/share/system-config-printer/applet.py
  104. │ ├─885 xcompmgr -aR
  105. │ └─997 /usr/lib/menu-cache/menu-cached /run/user/1000/menu-cached-:0
  106. ├─init.scope
  107. │ └─1 /sbin/init splash
  108. └─system.slice
  109. ├─packagekit.service
  110. │ └─1123 /usr/libexec/packagekitd
  111. ├─systemd-udevd.service
  112. │ └─174 /lib/systemd/systemd-udevd
  113. ├─triggerhappy.service
  114. │ └─445 /usr/sbin/thd --triggers /etc/triggerhappy/triggers.d/ --socket /run/thd…
  115. ├─cron.service
  116. │ └─352 /usr/sbin/cron -f
  117. ├─polkit.service
  118. │ └─371 /usr/libexec/polkitd --no-debug
  119. ├─rtkit-daemon.service
  120. │ └─700 /usr/libexec/rtkit-daemon
  121. ├─bluetooth.service
  122. │ └─788 /usr/libexec/bluetooth/bluetoothd
  123. ├─wpa_supplicant.service
  124. │ └─449 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
  125. ├─lightdm.service
  126. │ ├─540 /usr/sbin/lightdm
  127. │ └─562 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolist…
  128. ├─ModemManager.service
  129. │ └─493 /usr/sbin/ModemManager
  130. ├─systemd-journald.service
  131. │ └─139 /lib/systemd/systemd-journald
  132. ├─ssh.service
  133. │ └─590 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
  134. ├─hciuart.service
  135. │ └─746 /usr/bin/hciattach /dev/serial1 bcm43xx 3000000 flow - b8:27:eb:b8:c8:d0
  136. ├─rsyslog.service
  137. │ └─417 /usr/sbin/rsyslogd -n -iNONE
  138. ├─dhcpcd.service
  139. │ ├─436 /usr/sbin/dhcpcd -b -q
  140. │ └─553 wpa_supplicant -B -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0
  141. ├─rng-tools-debian.service
  142. │ └─513 /usr/sbin/rngd -r /dev/hwrng
  143. ├─cups-browsed.service
  144. │ └─592 /usr/sbin/cups-browsed
  145. ├─cups.service
  146. │ └─523 /usr/sbin/cupsd -l
  147. ├─udisks2.service
  148. │ └─447 /usr/libexec/udisks2/udisksd
  149. ├─dbus.service
  150. │ └─354 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --…
  151. ├─systemd-timesyncd.service
  152. │ └─282 /lib/systemd/systemd-timesyncd
  153. ├─avahi-daemon.service
  154. │ ├─345 avahi-daemon: running [emqx.local]
  155. │ └─363 avahi-daemon: chroot helper
  156. └─systemd-logind.service
  157. └─442 /lib/systemd/systemd-logind
  158. Created symlink /etc/systemd/system/multi-user.target.wants/emqx.service → /lib/systemd/system/emqx.service.
  159. emqx@emqx:~/ $ sudo systemctl start emqx

启动EMQX 服务后,你可以通过浏览器访问 http://emqx.local:18083/  来登录 EMQX Dashboard 管理控制台,进行设备连接与相关指标监控管理,默认用户名及密码:admin /public 。

65eac2096bfa53da08bef48b51ba3d67.png

EMQX 系统登录页面

e75d2ca58590838798e5505c34e2404e.png

EMQX 管理控制台概览

03

配置和验证 EMQX 

设备通过 MQTT 协议接入 EMQX 平台需要有合法身份认证信息,因此你需要在控制台配置客户端认证信息。

首先,接入客户端认证页面,选择Password Based认证方式,用内置数据库 Mnesia 作为数据源,配置参数采用默认方式。

3e1539831f5f78067ef157578bf4f81f.png

其次,你需要在内置数据中添加一个用户身份(username/password)。在用户管理里可以查看到已添加的用户。

d603f6a127ffd5acd080ab4ef762230f.png

MQTTX 工具

30244f880aee532679fffbc427b48763.gif

MQTTX 是由 EMQ 开发的一款开源跨平台 MQTT 5.0 桌面客户端,它兼容 macOS,Linux 以及 Windows 系统。

MQTTX 的用户界面 UI 采用聊天式设计,使得操作逻辑更加简明直观。它支持用户快速创建和保存多个 MQTT 连接,便于测试 MQTT/MQTTS 连接,以及 MQTT 消息的订阅和发布。

  • 采用聊天界面设计,使得操作更加简单明了

  • 跨平台兼容,支持在 Windows,macOS,Linux 系统上运行

  • 100% 兼容 MQTT v5.0,v3.1.1 和 v3.1 协议

  • 订阅的 MQTT 主题支持自定义颜色标签

  • 支持单向和双向 SSL 认证,同时支持 CA 和自签名证书

  • 支持通过 WebSocket 连接 MQTT 服务器

  • 支持 Hex, Base64, JSON, Plaintext 等 Payload 格式转换

  • 自定义脚本支持模拟 MQTT 发布/订阅测试

  • 提供完整的日志记录功能

  • 多语言支持:简体中文、英语、日语、土耳其语及匈牙利语

  • 自由切换 Light、Dark、Night 三种主题模式

打开 MQTTX 客户端进入创建页面,首先,你需要填写客户端的相关信息,包括clientId,MQTT服务器地址,用户名和密码。然后,点击右上角连接按钮建立MQTT连接。

426ff93751c6aa1449a58d45c7d9ce7b.png

你可以给指定的Topic发送一条消息。

585de08281b115c9759577d46b57f7bf.png

你也可以订阅指定的Topic。

29721ab96a422d774a6c3140d5bd9cdc.png

回到EMQX 控制台,你会看到当前连接的客户端Id、用户名、状态、以及 MQTT CONNECT 报文配置参数。

9b39552018842b5cd362e3669444cd78.png

进入设备详情页面,你可以查看消息发送和接收情况,当前订阅消息主题列表。

c01965ce0a6ebd816f62a999733a0d59.jpeg

至此,你已经成功的在树莓派上部署了 EMQX 服务,并通过 MQTTX 工具模拟了物联网设备和云端 MQTT 服务器通信的过程。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/673916
推荐阅读
相关标签
  

闽ICP备14008679号