基于云的物联网分析:数据处理与传感器数据收集
1. 英特尔物联网分析组件设置
在英特尔物联网分析中,我们需要对组件进行设置,具体步骤如下:
1. 打开正在使用英特尔物联网分析仪表板的网页浏览器,点击菜单图标,选择“账户”,页面将显示“我的账户”页面。
2. 点击“目录”标签,页面将显示目录中注册的组件,这些组件分为以下三类:
- 湿度
- 电源开关
- 温度
3. 确保“湿度”组件面板已展开,点击“humidity.v1.0”,页面将显示该湿度组件的定义对话框。该组件版本 1.0 表示一个以百分比表示环境湿度水平的时间序列,数据类型为数字,计量单位为百分比(%),格式为浮点型,显示方式为时间序列。
4. 点击“关闭”,确保“温度”组件面板已展开,点击“temperature.v1.0”,页面将显示该温度组件的定义对话框。该组件版本 1.0 表示一个以摄氏度表示温度的时间序列,数据类型为数字,计量单位为摄氏度,格式为浮点型,显示方式为时间序列。
5. 再次点击“关闭”,确保“温度”组件面板已展开。由于没有其他温度组件,我们需要为以华氏度表示的环境温度观测创建一个新组件。点击页面底部的“添加新目录项”,页面将显示组件定义对话框,除版本固定为 1.0 外,所有字段均为空。输入并选择以下值:
- 在“组件名称”中输入“temperaturef”
- 在“类型”中选择“传感器”
- 在“数据类型”中选择“数字”
- 在“计量单位”中输入“华氏度”
- 在“格式”中选择“浮点型”
- 在“显示方式”中选择“时间序列”
6. 最后点击“保存”,新组件定义将以“temperaturef.v.1.0”的名称添加到列表底部。
此时,我们已确保目录中包含所有所需的组件定义,接下来需要注册设备将用于创建观测的组件。我们必须为每个注册的组件提供一个名称或别名,并指定来自先前目录的组件类型和版本。以下表格总结了我们将为设备注册的组件:
| 组件名称或别名 | 组件类型 | 描述 |
| — | — | — |
| temperaturec | temperature.v1.0 | 以摄氏度(ºC)表示的环境温度观测时间序列 |
| temperaturef | temperaturef.v1.0 | 以华氏度(ºF)表示的环境温度观测时间序列 |
| humidity | humidity.v1.0 | 以百分比表示的环境湿度水平观测时间序列 |
我们可以使用以下命令注册每个组件:
iotkit-admin register component-name component-type
,只需将
component-name
替换为标识组件的名称,将
component-type
替换为目录中标识组件类型的名称(包括版本号)。
以下是在 SSH 终端中注册组件的命令及输出示例:
- 注册
temperaturec
组件:
iotkit-admin register temperaturec temperature.v1.0
输出:
2016-04-08T22:40:04.581Z - info: Starting registration ...
2016-04-08T22:40:04.711Z - info: Device has already been activated. Updating ...
2016-04-08T22:40:04.739Z - info: Updating metadata...
2016-04-08T22:40:04.920Z - info: Metadata updated.
Attributes sent
2016-04-08T22:40:10.167Z - info: Component registered name=temperaturec, type=temperature.v1.0, cid=c37cb57d-002c-4a66-866e-ce66bc3b2340, d_id=kansas-temperature-humidity-01
最后一行提供了组件 ID,即
cid=
后面和下一个逗号(,)之前的值。在上述输出中,组件 ID 为
c37cb57d-002c-4a66-866e-ce66bc3b2340
,我们必须保存每个组件 ID,因为稍后编写使用 REST API 创建观测的代码时会用到。
-
注册
temperaturef组件:
iotkit-admin register temperaturef temperaturef.v1.0
输出:
2016-04-08T22:40:20.510Z - info: Starting registration ...
2016-04-08T22:40:20.641Z - info: Device has already been activated. Updating ...
2016-04-08T22:40:20.669Z - info: Updating metadata...
2016-04-08T22:40:20.849Z - info: Metadata updated.
Attributes sent
2016-04-08T22:40:26.156Z - info: Component registered name=temperaturef, type=temperaturef.v1.0, cid=0f3b3aae-ce40-4fb4-a939-e7c705915f0c, d_id=kansas-temperature-humidity-01
组件 ID 为
0f3b3aae-ce40-4fb4-a939-e7c705915f0c
,同样需要保存。
-
注册
humidity组件:
iotkit-admin register humidity humidity.v1.0
输出:
2016-04-08T22:40:36.512Z - info: Starting registration ...
2016-04-08T22:40:36.643Z - info: Device has already been activated. Updating ...
2016-04-08T22:40:36.670Z - info: Updating metadata...
2016-04-08T22:40:36.849Z - info: Metadata updated.
Attributes sent
2016-04-08T22:40:43.003Z - info: Component registered name=humidity, type=humidity.v1.0, cid=71aba984-c485-4ced-bf19-c0f32649bcee, d_id=kansas-temperature-humidity-01
组件 ID 为
71aba984-c485-4ced-bf19-c0f32649bcee
,需保存。
注册完成后,我们可以在网页浏览器中查看注册情况。打开英特尔物联网分析仪表板,点击菜单图标,选择“设备”,页面将显示“我的设备”页面。点击设备 ID(如
kansas-temperature-humidity-01
)查看和编辑设备详细信息,点击“+组件”展开为设备注册的组件,将看到以下三个组件:
- temperaturec
- temperaturef
- humidity
如果我们丢失了组件 ID,可以通过点击组件,在“组件定义”对话框中,组件类型描述下方将显示组件 ID。
不幸的是,我们无法通过网站功能检索设备激活时生成的设备令牌。我们需要设备令牌为注册的组件创建观测。英特尔物联网分析代理将设备令牌与设备的其他配置值保存在
device.json
文件中,其默认路径为
/usr/lib/node_modules/iotkit-agent/data/device.json
。我们只需在 SSH 终端中运行以下命令即可显示该文件的文本内容,从而检索设备令牌:
cat /usr/lib/node_modules/iotkit-agent/data/device.json
输出示例如下:
{
"activation_retries": 10,
"activation_code": null,
"device_id": "kansas-temperature-humidity-01",
"device_name": false,
"device_loc": [
88.34,
64.22047,
0
],
"gateway_id": false,
"device_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjOTNmMTJhMy05MWZlLTQ3MWYtODM4OS02OGM1NDYxNDIxMDUiLCJpc3MiOiJodHRwOi8vZW5hYmxlaW90LmNvbSIsInN1YiI6ImthbnNhcy10ZW1wZXJhdHVyZS1odW1pZGl0eS0wMSIsImV4cCI6IjIwMjYtMDQtMDZUMTk6MDA6MTkuNzA0WiJ9.PH5yQas2FiQvUSR9V2pa3n3kIYZvmSe_xXY7QkFjlXUVUcyy9Sk_eVF4AL6qpZlBC9vjtd0L-VMZiULC9YXxAVl9s5Cl8ZqpQs36E1ssv_1H9CBFXKiiPArplzaWXVzvIRBVVzwfQrGrMoD_l4DcHlH2zgn5UGxhZ3RMPUvqgeneG3P-hSbPScPQL1pW85,VT2IHT3seWyW1c637I_MDpHbJJCbkytPVpJpwKBxrCiKlGhvsh5pl4eLUXYUPlQAzB9QzC_ohujG23b-ApfHZugYD7zJa-05u0lkt93EEnuCk39o5SmPmIiuBup-k_mLn_VMde5fUvbxDt_SMI0XY3_Q",
"account_id": "22612154-0f71-4f64-a68e-e116771115d5",
"sensor_list": [
{
"name": "temperaturec",
"type": "temperature.v1.0",
"cid": "c37cb57d-002c-4a66-866e-ce66bc3b2340",
"d_id": "kansas-temperature-humidity-01"
},
{
"name": "temperaturef",
"type": "temperaturef.v1.0",
"cid": "0f3b3aae-ce40-4fb4-a939-e7c705915f0c",
"d_id": "kansas-temperature-humidity-01"
},
{
"name": "humidity",
"type": "humidity.v1.0",
"cid": "71aba984-c485-4ced-bf19-c0f32649bcee",
"d_id": "kansas-temperature-humidity-01"
}
]
}
其中,
device_token
键对应的值即为设备令牌。
2. 英特尔物联网分析传感器数据收集
我们将以之前编写的读取传感器温度和湿度值的代码为基础,添加新功能,为注册的三个组件创建观测。以下是具体步骤:
首先,确保
requests
包已安装。在第 2 章中,我们确保了
pip
安装程序可用于在运行于开发板的 Yocto Linux 中安装额外的 Python 2.7.3 包。现在,我们使用
pip
安装程序确保
requests
包已安装。如果之前已经安装过,可跳过此步骤。在 SSH 终端中运行以下命令进行安装:
pip install requests
如果看到以下输出,则表示
requests
包已安装,可以继续下一步:
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python2.7/site-packages
接下来,我们创建一个
IntelIotAnalytics
类来表示与英特尔物联网分析的接口,方便为三个组件发布观测。在编写该类之前,我们需要将类的许多属性替换为适当的值:
-
account_name
:“我的账户”页面中“账户名称”字段的值,例如“温度和湿度”。
-
account_id
:“我的账户”页面中“账户 ID”字段的值,例如
22612154-0f71-4f64-a68e-e116771115d5
,也可以通过读取
device.json
文件中
account_id
键的字符串值来获取。
-
device_id
:点击“我的设备”页面中设备名称后,网站显示的“添加/编辑设备”页面中“ID”字段的值,例如
kansas-temperature-humidity-01
,也可以通过在 SSH 终端中运行
iotkit-admin device-id
命令或读取
device.json
文件中
device_id
键的字符串值来获取。
-
device_token
:设备激活时生成的设备令牌,可以通过读取
device.json
文件中
device_token
键的字符串值来获取。
-
component_id_temperature_fahrenheit
:注册
temperaturef
组件时生成的组件 ID,例如
0f3b3aae-ce40-4fb4-a939-e7c705915f0c
,也可以通过读取
device.json
文件中
name
为
temperaturef
的块中
cid
键的字符串值来获取。
-
component_id_temperature_celsius
:注册
temperaturec
组件时生成的组件 ID,例如
c37cb57d-002c-4a66-866e-ce66bc3b2340
。
-
component_id_humidity_level_percentage
:注册
humidity
组件时生成的组件 ID,例如
71aba984-c485-4ced-bf19-c0f32649bcee
。
以下是
IntelIotAnalytics
类的代码:
import time
import json
import requests
class IntelIotAnalytics:
base_url = "https://dashboard.us.enableiot.com/v1/api"
# You can retrieve the following information from the My Account page
account_name = "Temperature and humidity"
account_id = "22612154-0f71-4f64-a68e-e116771115d5"
# You can retrieve the device token with the following command:
# cat /usr/lib/node_modules/iotkit-agent/data/device.json
device_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjOTNmMTJhMy05MWZlLTQ3MWYtODM4OS02OGM1NDYxNDIxMDUiLCJpc3MiOiJodHRwOi8vZW5hYmxlaW90LmNvbSIsInN1YiI6ImthbnNhcy10ZW1wZXJhdHVyZS1odW1pZGl0eS0wMSIsImV4cCI6IjIwMjYtMDQtMDZUMTk6MDA6MTkuNzA0WiJ9.PH5yQas2FiQvUSR9V2pa3n3kIYZvmSe_xXY7QkFjlXUVUcyy9Sk_eVF4AL6qpZlBC9vjtd0L-VMZiULC9YXxAVl9s5Cl8ZqpQs36E1ssv_1H9CBFXKiiPArplzaWXVzvIRBVVzwfQrGrMoD_l4DcHlH2zgn5UGxhZ3RMPUvqgeneG3P-hSbPScPQL1pW85VT2IHT3seWyW1c637I_MDpHbJJCbkytPVpJpwKBxrCiKlGhvsh5pl4eLUXYUPlQAzB9QzC_ohujG23b-ApfHZugYD7zJa-05u0lkt93EEnuCk39o5SmPmIiuBup-k_mLn_VMde5fUvbxDt_SMI0XY3_Q"
device_id = "kansas-temperature-humidity-01"
component_id_temperature_fahrenheit = "0f3b3aae-ce40-4fb4-a939-e7c705915f0c"
component_id_temperature_celsius = "c37cb57d-002c-4a66-866e-ce66bc3b2340"
component_id_humidity_level_percentage = "71aba984-c485-4ced-bf19-c0f32649bcee"
def publish_observation(self,
temperature_fahrenheit,
temperature_celsius,
humidity_level):
url = "{0}/data/{1}".\
format(self.__class__.base_url, self.__class__.device_id)
now = int(time.time()) * 1000
body = {
"on": now,
"accountId": self.__class__.account_id,
"data": []
}
temperature_celsius_data = {
"componentId": self.__class__.component_id_temperature_celsius,
"on": now,
"value": str(temperature_celsius)
}
temperature_fahrenheit_data = {
"componentId": self.__class__.component_id_temperature_fahrenheit,
"on": now,
"value": str(temperature_fahrenheit)
}
humidity_level_percentage_data = {
"componentId": self.__class__.component_id_humidity_level_percentage,
"on": now,
"value": str(humidity_level)
}
body["data"].append(temperature_celsius_data)
body["data"].append(temperature_fahrenheit_data)
body["data"].append(humidity_level_percentage_data)
data = json.dumps(body)
headers = {
'Authorization': 'Bearer ' + self.__class__.device_token,
'content-type': 'application/json'
}
response = requests.post(url, data=data, headers=headers, proxies={}, verify=True)
if response.status_code != 201:
print "The request failed. Status code: {0}. Response text: {1}.".format(response.status_code, response.text)
该类的
publish_observation
方法接收以华氏度表示的温度、以摄氏度表示的温度和湿度水平百分比作为参数,构建 HTTP 请求,为设备和三个组件创建观测。
最后,我们使用
IntelIotAnalytics
类创建一个新的
__main__
方法,每 5 秒向英特尔物联网分析发布一次观测。以下是代码:
if __name__ == "__main__":
temperature_and_humidity_sensor = TemperatureAndHumiditySensor(0)
oled = TemperatureAndHumidityOled(0)
intel_iot_analytics = IntelIotAnalytics()
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))
print("Ambient humidity: {0}".format(temperature_and_humidity_sensor.humidity))
intel_iot_analytics.publish_observation(
temperature_and_humidity_sensor.temperature_fahrenheit,
temperature_and_humidity_sensor.temperature_celsius,
temperature_and_humidity_sensor.humidity
)
# Sleep 5 seconds (5000 milliseconds)
time.sleep(5)
运行以下命令启动示例:
python iot_python_chapter_10_01.py
运行示例后,打开空调或加热系统,改变环境温度和湿度,我们将看到每 5 秒发布的数据发生变化。在运行代码的同时,我们可以探索英特尔物联网分析的不同功能。打开英特尔物联网分析仪表板,点击菜单图标,选择“仪表板”,页面将显示“我的仪表板”页面,显示有一个活动设备,并在接收到开发板的观测数据时更新最后一小时发布的观测数量。
通过以上步骤,我们完成了英特尔物联网分析的组件设置和传感器数据收集,能够实时监测环境温度和湿度,并将数据上传到云端进行分析。
基于云的物联网分析:数据处理与传感器数据收集
3. 代码详细解析
3.1
IntelIotAnalytics
类
IntelIotAnalytics
类是与英特尔物联网分析进行交互的核心类,下面对其进行详细解析:
import time
import json
import requests
class IntelIotAnalytics:
base_url = "https://dashboard.us.enableiot.com/v1/api"
account_name = "Temperature and humidity"
account_id = "22612154-0f71-4f64-a68e-e116771115d5"
device_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjOTNmMTJhMy05MWZlLTQ3MWYtODM4OS02OGM1NDYxNDIxMDUiLCJpc3MiOiJodHRwOi8vZW5hYmxlaW90LmNvbSIsInN1YiI6ImthbnNhcy10ZW1wZXJhdHVyZS1odW1pZGl0eS0wMSIsImV4cCI6IjIwMjYtMDQtMDZUMTk6MDA6MTkuNzA0WiJ9.PH5yQas2FiQvUSR9V2pa3n3kIYZvmSe_xXY7QkFjlXUVUcyy9Sk_eVF4AL6qpZlBC9vjtd0L-VMZiULC9YXxAVl9s5Cl8ZqpQs36E1ssv_1H9CBFXKiiPArplzaWXVzvIRBVVzwfQrGrMoD_l4DcHlH2zgn5UGxhZ3RMPUvqgeneG3P-hSbPScPQL1pW85VT2IHT3seWyW1c637I_MDpHbJJCbkytPVpJpwKBxrCiKlGhvsh5pl4eLUXYUPlQAzB9QzC_ohujG23b-ApfHZugYD7zJa-05u0lkt93EEnuCk39o5SmPmIiuBup-k_mLn_VMde5fUvbxDt_SMI0XY3_Q"
device_id = "kansas-temperature-humidity-01"
component_id_temperature_fahrenheit = "0f3b3aae-ce40-4fb4-a939-e7c705915f0c"
component_id_temperature_celsius = "c37cb57d-002c-4a66-866e-ce66bc3b2340"
component_id_humidity_level_percentage = "71aba984-c485-4ced-bf19-c0f32649bcee"
def publish_observation(self,
temperature_fahrenheit,
temperature_celsius,
humidity_level):
url = "{0}/data/{1}".\
format(self.__class__.base_url, self.__class__.device_id)
now = int(time.time()) * 1000
body = {
"on": now,
"accountId": self.__class__.account_id,
"data": []
}
temperature_celsius_data = {
"componentId": self.__class__.component_id_temperature_celsius,
"on": now,
"value": str(temperature_celsius)
}
temperature_fahrenheit_data = {
"componentId": self.__class__.component_id_temperature_fahrenheit,
"on": now,
"value": str(temperature_fahrenheit)
}
humidity_level_percentage_data = {
"componentId": self.__class__.component_id_humidity_level_percentage,
"on": now,
"value": str(humidity_level)
}
body["data"].append(temperature_celsius_data)
body["data"].append(temperature_fahrenheit_data)
body["data"].append(humidity_level_percentage_data)
data = json.dumps(body)
headers = {
'Authorization': 'Bearer ' + self.__class__.device_token,
'content-type': 'application/json'
}
response = requests.post(url, data=data, headers=headers, proxies={}, verify=True)
if response.status_code != 201:
print "The request failed. Status code: {0}. Response text: {1}.".format(response.status_code, response.text)
-
类属性 :
-
base_url:访问 REST API 的基础 URL,指定了 API 的版本号,确保请求与特定版本兼容。 -
account_name:账户名称,用于标识账户。 -
account_id:账户 ID,唯一标识账户,可从“我的账户”页面获取或从device.json文件中读取。 -
device_token:设备令牌,用于身份验证,从device.json文件中获取。 -
device_id:设备 ID,唯一标识设备,可从“添加/编辑设备”页面获取或通过命令读取device.json文件。 -
component_id_temperature_fahrenheit、component_id_temperature_celsius、component_id_humidity_level_percentage:分别为三个组件的 ID,用于指定观测数据所属的组件。
-
-
publish_observation方法 :-
构建 URL
:根据
base_url和device_id构建请求的 URL。 - 获取当前时间 :将当前时间(秒)乘以 1000 作为观测时间。
-
构建请求体
:创建一个包含观测时间、账户 ID 和空数据数组的字典
body。 - 创建组件观测数据 :分别为三个组件创建包含组件 ID、观测时间和观测值的字典。
-
填充请求体
:将三个组件的观测数据添加到
body的data数组中。 -
序列化请求体
:使用
json.dumps函数将body字典序列化为 JSON 字符串。 - 构建请求头 :包含授权信息和内容类型。
-
发送请求
:使用
requests.post方法发送 POST 请求。 - 检查响应 :如果响应状态码不是 201,表示请求失败,打印错误信息。
-
构建 URL
:根据
3.2
__main__
方法
if __name__ == "__main__":
temperature_and_humidity_sensor = TemperatureAndHumiditySensor(0)
oled = TemperatureAndHumidityOled(0)
intel_iot_analytics = IntelIotAnalytics()
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))
print("Ambient humidity: {0}".format(temperature_and_humidity_sensor.humidity))
intel_iot_analytics.publish_observation(
temperature_and_humidity_sensor.temperature_fahrenheit,
temperature_and_humidity_sensor.temperature_celsius,
temperature_and_humidity_sensor.humidity
)
# Sleep 5 seconds (5000 milliseconds)
time.sleep(5)
-
初始化对象
:创建温度湿度传感器对象、OLED 显示对象和
IntelIotAnalytics类的实例。 -
循环执行
:
-
调用传感器的
measure_temperature_and_humidity方法测量温度和湿度。 - 在 OLED 上显示温度和湿度。
- 打印温度和湿度信息。
-
调用
IntelIotAnalytics类的publish_observation方法发布观测数据。 - 休眠 5 秒后再次循环。
-
调用传感器的
4. 流程图
下面是整个数据收集和发布过程的 mermaid 流程图:
graph TD;
A[开始] --> B[设置组件];
B --> C[注册组件];
C --> D[获取设备令牌];
D --> E[安装 requests 包];
E --> F[创建 IntelIotAnalytics 类];
F --> G[初始化对象];
G --> H[测量温度和湿度];
H --> I[显示温度和湿度];
I --> J[打印温度和湿度信息];
J --> K[发布观测数据];
K --> L{是否继续};
L -- 是 --> H;
L -- 否 --> M[结束];
5. 总结
通过以上步骤,我们完成了英特尔物联网分析的组件设置和传感器数据收集。具体步骤总结如下:
1.
组件设置
:在网页浏览器中进行组件的查看和创建,确保目录中包含所需的组件定义。
2.
组件注册
:使用
iotkit-admin
命令注册组件,并保存组件 ID。
3.
获取设备令牌
:从
device.json
文件中获取设备令牌。
4.
安装依赖包
:使用
pip
安装
requests
包。
5.
编写代码
:创建
IntelIotAnalytics
类和
__main__
方法,实现数据的收集和发布。
6.
运行代码
:启动 Python 脚本,实时监测环境温度和湿度,并将数据上传到云端进行分析。
通过这些步骤,我们可以利用英特尔物联网分析平台,实现对环境数据的实时监测和分析,为后续的决策提供有力支持。
超级会员免费看

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



