物联网云服务实践:构建实时监控与数据交互系统
1. 使用 Freeboard 构建基于 Web 的仪表板
1.1 准备工作
dweet.io 数据共享工具能让我们通过几行代码轻松将数据发布到云端。现在,我们要利用 dweet.io 和设备名称作为数据源,借助 freeboard.io 构建一个实时的基于 Web 的仪表板,以可视化传感器收集并发布到 dweet.io 的数据。
1.2 创建 Freeboard 账户
- 访问 http://freeboard.io ,点击 “Start Now”,或者直接访问 https://freeboard.io/signup 。
- 在 “Pick a Username” 中输入期望的用户名,“Enter Your Email” 中输入邮箱,“Create a Password” 中输入密码。
- 填写完所有字段后,点击 “Create My Account”。
1.3 登录 Freeboard
- 访问 http://freeboard.io ,点击 “Login”,或者访问 https://freeboard.io/login 。
- 输入用户名或邮箱和密码,点击 “Sign In”,即可看到自己的仪表板。
1.4 配置数据源
- 在 “Create New” 按钮左侧的文本框中输入 “Ambient temperature and humidity”,然后点击该按钮,会显示一个带有多个按钮的空仪表板。
- 点击 “Datasources” 下方的 “Add”,网站会打开数据源对话框。
- 在 “Type” 下拉菜单中选择 “Dweet.io”,对话框将显示定义 dweet.io 数据源所需的字段。
- 在 “Name” 中输入 “Ambient temperature and humidity”,在 “Thing Name” 中输入之前用于 dweet.io 的设备名称。若输入的名称与使用 dweet.io 时的名称不匹配,数据源将无法获取正确数据。
- 点击 “Save”,数据源将显示在 “Datasources” 下方。当板子运行发送数据的 Python 代码时,“Last Updated” 下方显示的时间将每 10 秒更新一次。若时间未更新,说明数据源配置错误或板子未运行发送数据的 Python 代码。
1.5 添加仪表组件
-
添加温度(华氏度)仪表
- 点击 “Add pane” 添加一个新的空面板,然后点击新空面板右上角的加号(+),Freeboard 将显示小部件对话框。
- 在 “Type” 下拉菜单中选择 “Gauge”,对话框将显示在仪表板面板中添加仪表小部件所需的字段。在 “Title” 中输入 “Temperature in degrees Fahrenheit”。
- 点击 “Value” 文本框右侧的 “+ Datasource”,选择 “Ambient Temperature and humidity”,然后选择 “temperature_fahrenheit”。选择后,“Value” 文本框中将显示:datasources [“Ambient temperature and humidity”] [“temperature_fahrenheit”]。
- 在 “Units” 中输入 “ºF”,“Minimum” 中输入 “-30”,“Maximum” 中输入 “130”。
- 点击 “Save”,Freeboard 将关闭对话框并将新仪表添加到之前创建的面板中。该仪表将显示板子代码最后一次发送到 dweet.io 的 JSON 数据中 “temperature_fahrenheit” 键对应的环境温度值。
-
添加湿度仪表
- 重复上述添加面板和小部件的步骤,在 “Title” 中输入 “Humidity level in percentage”。
- 点击 “Value” 文本框右侧的 “+ Datasource”,选择 “Ambient Temperature and humidity”,然后选择 “humidity_level_percentage”。选择后,“Value” 文本框中将显示:datasources [“Ambient temperature and humidity”] [“humidity_level_percentage”]。
- 在 “Units” 中输入 “%”,“Minimum” 中输入 “0”,“Maximum” 中输入 “100”。
- 点击 “Save”,将新仪表添加到面板中。该仪表将显示板子代码最后一次发送到 dweet.io 的 JSON 数据中 “humidity_level_percentage” 键对应的环境湿度值。
-
添加温度(摄氏度)仪表
- 点击显示华氏温度的面板右上角的加号(+),重复添加小部件的步骤,在 “Title” 中输入 “Temperature in degrees Celsius”。
- 点击 “Value” 文本框右侧的 “+ Datasource”,选择 “Ambient Temperature and humidity”,然后选择 “temperature_celsius”。选择后,“Value” 文本框中将显示:datasources [“Ambient temperature and humidity”] [“temperature_celsius”]。
- 在 “Units” 中输入 “ºC”,“Minimum” 中输入 “-40”,“Maximum” 中输入 “55”。
- 点击 “Save”,将新仪表添加到面板中。该面板将显示两个仪表,分别以不同单位显示温度。
1.6 配置面板标题并调整布局
- 点击显示两个温度的面板 “+” 按钮右侧的配置图标,Freeboard 将显示面板对话框。在 “Title” 中输入 “Temperature”,然后点击 “Save”。
- 点击显示湿度的面板 “+” 按钮右侧的配置图标,在 “Title” 中输入 “Humidity”,然后点击 “Save”。
- 通过拖放面板,将 “Humidity” 面板置于 “Temperature” 面板左侧。
1.7 访问仪表板
我们可以在任何设备上通过输入当前浏览器显示的 URL 访问刚刚构建的仪表板。URL 由 “https://freeboard.io/board/” 前缀和字母数字组成。例如,若 URL 为 “https://freeboard.io/board/EXAMPLE”,只需在任何连接到互联网的设备或计算机的 Web 浏览器中输入该 URL,即可查看仪表,并且当 Intel Galileo Gen 2 板子向 dweet.io 发布新数据时,仪表将自动刷新。
1.8 总结
dweet.io 作为数据源和 freeboard.io 作为基于 Web 的仪表板的结合,使我们能够使用任何带有 Web 浏览器的设备轻松监控连接到 Intel Galileo Gen 2 板子的传感器数据。这种物联网云服务的组合展示了如何轻松结合不同的服务,随着物联网云服务数量的增加,我们可以在解决方案中使用更多的服务。
2. 使用 PubNub 实现实时数据收发
2.1 选择 PubNub 的原因
之前我们开发并使用了 RESTful API 来通过 HTTP 请求控制连接到 Intel Galileo Gen 2 板子的电子组件,但要实现实时数据收发,RESTful API 并非最佳选择。因此,我们将采用基于比 HTTP 协议更轻量级的 MQTT(MQ Telemetry Transport)协议的发布/订阅模型,具体使用基于 MQTT 协议的 PubNub 服务。
2.2 MQTT 协议简介
MQTT 是一种机器对机器(M2M)和物联网连接协议,它是一种轻量级消息协议,运行在 TCP/IP 协议之上,采用发布/订阅机制。任何设备都可以订阅特定的频道(也称为主题),并接收发布到该频道的所有消息,同时设备也可以向该频道或其他频道发布消息。该协议在物联网和 M2M 项目中越来越受欢迎。
2.3 PubNub 服务介绍
PubNub 提供多种基于云的服务,其中之一允许我们基于 MQTT 协议轻松实时流式传输数据并向任何设备发送信号。PubNub 为 Python 提供了高质量的 API 文档和示例,使用 Python 调用该服务非常方便。
2.4 创建 PubNub 账户并获取密钥
- PubNub 要求我们使用有效的电子邮件和密码注册并创建账户,无需输入信用卡或支付信息。若已有 PubNub 账户,可跳过此步骤。
- 创建账户后,PubNub 会将我们重定向到管理门户,列出我们的 PubNub 应用程序。为了在网络中发送和接收消息,需要生成 PubNub 的发布和订阅密钥。
- 点击 “Temperature Control” 应用程序面板,PubNub 将显示自动为该应用程序生成的 “Demo Keyset” 面板。点击该面板,将显示发布、订阅和秘密密钥。我们需要复制并粘贴这些密钥到代码中,用于发布和订阅消息。复制秘密密钥时,需点击密钥右侧的眼睛图标,使所有字符可见。
2.5 安装 PubNub Python SDK
在之前安装了 pip 安装程序的基础上,我们可以使用以下命令在 SSH 终端中安装 PubNub Python SDK 3.7.6:
pip install pubnub
安装可能需要几分钟时间,安装完成后,输出的最后几行将显示 “pubnub” 包已成功安装,可忽略与构建 wheel 相关的错误消息和不安全平台警告。
2.6 扩展代码功能
我们将以之前读取传感器温度和湿度值、在 OLED 矩阵中打印值并旋转舵机轴以显示华氏温度的代码(文件名为 iot_python_chapter_08_03.py)为基础,添加新功能,使我们能够通过 PubNub 接收来自任何带有 Web 浏览器的设备发送到特定频道的消息,并执行以下操作:
- 旋转舵机轴以显示消息中包含的华氏温度值。
- 在 OLED 矩阵底部显示消息中包含的一行文本。
2.7 编写消息通道类
我们创建一个
MessageChannel
类来表示通信通道,配置 PubNub 订阅并声明在特定事件触发时执行的回调代码。示例代码文件为 iot_python_chapter_09_02.py。
import time
from pubnub import Pubnub
class MessageChannel:
command_key = "command"
def __init__(self, channel, temperature_servo, oled):
self.temperature_servo = temperature_servo
self.oled = oled
self.channel = channel
# 发布密钥通常以 "pub-c-" 开头
# 不要忘记将字符串替换为您的发布密钥
publish_key = "pub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# 订阅密钥通常以 "sub-c" 开头
# 不要忘记将字符串替换为您的订阅密钥
subscribe_key = "sub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key)
self.pubnub.subscribe(channels=self.channel,
callback=self.callback,
error=self.callback,
connect=self.connect,
reconnect=self.reconnect,
disconnect=self.disconnect)
def callback(self, message, channel):
if channel == self.channel:
if self.__class__.command_key in message:
if message[self.__class__.command_key] == "print_temperature_fahrenheit":
self.temperature_servo.print_temperature(message["temperature_fahrenheit"])
elif message[self.__class__.command_key] == "print_information_message":
self.oled.print_line(11, message["text"])
print("I've received the following message: {0}".format(message))
def error(self, message):
print("Error: " + str(message))
def connect(self, message):
print("Connected to the {0} channel".format(self.channel))
print(self.pubnub.publish(
channel=self.channel,
message="Listening to messages in the Intel Galileo Gen 2 board"))
def reconnect(self, message):
print("Reconnected to the {0} channel".format(self.channel))
def disconnect(self, message):
print("Disconnected from the {0} channel".format(self.channel))
2.8 新的主函数
使用之前编写的
MessageChannel
类创建一个新的
__main__
方法,该方法使用 PubNub 云接收和处理命令。新方法在环境温度变化时不会旋转舵机轴,而是在收到来自连接到 PubNub 云的任何设备的适当命令时执行此操作。示例代码如下:
if __name__ == "__main__":
temperature_and_humidity_sensor = \
TemperatureAndHumiditySensor(0)
oled = TemperatureAndHumidityOled(0)
temperature_servo = TemperatureServo(3)
message_channel = MessageChannel("temperature", temperature_servo, oled)
while True:
temperature_and_humidity_sensor.\
measure_temperature_and_humidity()
oled.print_temperature(
temperature_and_humidity_sensor.temperature_fahrenheit,
temperature_and_humidity_sensor.temperature_celsius)
oled.print_humidity(
temperature_and_humidity_sensor.humidity)
print("Ambient temperature in degrees Celsius: {0}".
format(temperature_and_humidity_sensor.temperature_celsius))
print("Ambient temperature in degrees Fahrenheit: {0}".
format(temperature_and_humidity_sensor.temperature_fahrenheit))
2.9 总结
PubNub 服务结合 MQTT 协议为我们提供了一种高效的实时数据收发解决方案,通过创建
MessageChannel
类和新的
__main__
方法,我们可以实现与连接到 PubNub 云的设备进行实时交互,控制舵机和显示信息。这种实时数据交互能力为物联网应用带来了更多的可能性。
通过以上两个部分的实践,我们展示了如何使用不同的云服务来实现物联网数据的可视化和实时交互,为物联网项目的开发提供了有价值的参考。
3. 技术要点分析
3.1 Freeboard 与 dweet.io 结合的优势
| 优势 | 说明 |
|---|---|
| 数据可视化 | Freeboard 提供了丰富的可视化组件,如仪表、图表等,能够直观地展示 dweet.io 数据源的数据。 |
| 易于使用 | 通过简单的拖放操作和配置,即可快速构建出功能强大的仪表板。 |
| 跨设备访问 | 基于 Web 的特性,使得我们可以在任何具有浏览器的设备上访问仪表板,方便实时监控。 |
3.2 PubNub 与 MQTT 协议的特性
- 轻量级 :MQTT 协议相较于 HTTP 协议更加轻量级,减少了数据传输的开销,适合物联网设备的实时数据传输。
- 发布/订阅模型 :设备可以根据需求订阅特定的频道,只接收感兴趣的数据,提高了数据处理的效率。
- 实时性 :PubNub 基于 MQTT 协议,能够实现数据的实时收发,满足物联网应用对实时性的要求。
3.3 代码结构分析
MessageChannel
类
import time
from pubnub import Pubnub
class MessageChannel:
command_key = "command"
def __init__(self, channel, temperature_servo, oled):
self.temperature_servo = temperature_servo
self.oled = oled
self.channel = channel
publish_key = "pub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
subscribe_key = "sub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key)
self.pubnub.subscribe(channels=self.channel,
callback=self.callback,
error=self.callback,
connect=self.connect,
reconnect=self.reconnect,
disconnect=self.disconnect)
def callback(self, message, channel):
if channel == self.channel:
if self.__class__.command_key in message:
if message[self.__class__.command_key] == "print_temperature_fahrenheit":
self.temperature_servo.print_temperature(message["temperature_fahrenheit"])
elif message[self.__class__.command_key] == "print_information_message":
self.oled.print_line(11, message["text"])
print("I've received the following message: {0}".format(message))
def error(self, message):
print("Error: " + str(message))
def connect(self, message):
print("Connected to the {0} channel".format(self.channel))
print(self.pubnub.publish(
channel=self.channel,
message="Listening to messages in the Intel Galileo Gen 2 board"))
def reconnect(self, message):
print("Reconnected to the {0} channel".format(self.channel))
def disconnect(self, message):
print("Disconnected from the {0} channel".format(self.channel))
-
初始化方法
__init__:接收频道名称、温度舵机和 OLED 实例作为参数,初始化 PubNub 客户端并订阅指定频道。 -
回调方法
callback:处理接收到的消息,根据消息中的命令执行相应的操作,如旋转舵机轴或显示信息。 -
错误处理方法
error:打印错误信息。 -
连接相关方法
:
connect、reconnect和disconnect分别在连接、重新连接和断开连接时打印相应的信息。
新的
__main__
方法
if __name__ == "__main__":
temperature_and_humidity_sensor = \
TemperatureAndHumiditySensor(0)
oled = TemperatureAndHumidityOled(0)
temperature_servo = TemperatureServo(3)
message_channel = MessageChannel("temperature", temperature_servo, oled)
while True:
temperature_and_humidity_sensor.\
measure_temperature_and_humidity()
oled.print_temperature(
temperature_and_humidity_sensor.temperature_fahrenheit,
temperature_and_humidity_sensor.temperature_celsius)
oled.print_humidity(
temperature_and_humidity_sensor.humidity)
print("Ambient temperature in degrees Celsius: {0}".
format(temperature_and_humidity_sensor.temperature_celsius))
print("Ambient temperature in degrees Fahrenheit: {0}".
format(temperature_and_humidity_sensor.temperature_fahrenheit))
- 初始化传感器、OLED 和舵机实例。
-
创建
MessageChannel实例,订阅 “temperature” 频道。 - 进入无限循环,不断测量温度和湿度,并在 OLED 上显示相关信息。
3.4 流程图展示
graph TD;
A[开始] --> B[初始化传感器、OLED 和舵机];
B --> C[创建 MessageChannel 实例并订阅频道];
C --> D[进入循环];
D --> E[测量温度和湿度];
E --> F[在 OLED 上显示温度和湿度];
F --> G[打印温度信息];
G --> D;
4. 实际应用场景
4.1 环境监测
在农业、气象等领域,可以使用传感器收集环境数据,如温度、湿度、光照等,通过 Freeboard 仪表板实时监测环境变化,以便及时采取相应的措施。
4.2 工业自动化
在工业生产中,通过连接各种传感器和执行器,使用 PubNub 实现设备之间的实时数据交互和控制,提高生产效率和质量。
4.3 智能家居
将智能家居设备(如智能空调、智能门锁等)连接到 PubNub 云,用户可以通过手机等设备实时控制和监控家居设备的状态。
5. 注意事项与常见问题解决
5.1 Freeboard 仪表板配置问题
- 数据源配置错误 :确保在 Freeboard 中配置的数据源名称和设备名称与 dweet.io 中的一致,否则可能无法获取到正确的数据。
- 仪表参数设置不合理 :根据实际需求合理设置仪表的单位、最小值和最大值,避免数据显示异常。
5.2 PubNub 密钥管理
- 密钥丢失或泄露 :妥善保管 PubNub 的发布、订阅和秘密密钥,避免密钥丢失或泄露导致安全问题。
- 密钥更新 :如果密钥过期或需要更换,及时在代码中更新密钥信息。
5.3 代码运行问题
- 依赖库安装失败 :在安装 PubNub Python SDK 时,可能会遇到依赖库安装失败的情况。可以尝试更新 pip 版本或手动安装依赖库。
- 回调函数执行异常 :检查回调函数中的逻辑是否正确,确保在接收到消息时能够正确处理命令。
6. 总结与展望
6.1 总结
通过使用 Freeboard 和 dweet.io 构建基于 Web 的仪表板,以及利用 PubNub 和 MQTT 协议实现实时数据收发,我们成功地实现了物联网数据的可视化和实时交互。这些云服务的结合为物联网项目的开发提供了便捷、高效的解决方案。
6.2 展望
随着物联网技术的不断发展,云服务的功能和性能也将不断提升。未来,我们可以进一步探索更多的云服务和技术,如人工智能、大数据分析等,将其应用到物联网项目中,实现更加智能化、自动化的物联网系统。同时,加强数据安全和隐私保护,也是物联网发展过程中需要重点关注的问题。
超级会员免费看

被折叠的 条评论
为什么被折叠?



