网关接入
IoTGateway是开源开放的跨平台工业物联网网关,通过可视化配置,南向的连接到你的任何设备和系统(如PLC、扫码枪、上位机、OPC Server等)。
为让使用者快速掌握北向对接各种物联网平台的能力,本文以接入ThingsPanel为例进行扩展实操。
0. 效果展示
1. ThingsPanel 介绍
ThingsPanel
是基于Go
语言的优秀的开源物联网平台。
2. ThingsPanel 安装
- 下载
docker-compose.yml
文件https://github.com/ThingsPanel/thingspanel-docker/blob/main/docker-compose.yml - 启动容器
docker-compose up -d
访问
8080
端口,使用租户管理员登录用户名
tenant@tenant.cn
密码
123456
3. 设备接入文档与规范
http://thingspanel.io/zh-Hans/docs/device-connect/gateway
https://docs.qq.com/doc/DZXlnb25scnZRc1dK
4. 创建网关设备配置模版
5. 创建子设备配置模板
6. 创建网关设备
在设备连接tab
查看并记录MQTT Username
和MQTT ClientID
7. 创建子设备
8. 网关关联子设备
关联后,记录子设备地址
9. 添加ThingsPanel平台类型
iotgateway\IoTGateway.Model\SystemConfig.cs\IoTPlatformType
[Display(Name = "ThingsPanel")]
ThingsPanel = 9
10. 添加ThingsPanelHandler
参考其他Handler
,添加ThingsPanelHandler
iotgateway\Plugins\Plugin\PlatformHandler\ThingsPanelHandler.cs
public async Task PublishTelemetryAsync(string deviceName, Device device, Dictionary<string, List<PayLoad>> sendModel)
{
foreach (var payload in sendModel[deviceName])
{
if (payload.Values != null)
{
var telemetryData = new Dictionary<string, Dictionary<string, object>>()
{
{
"sub_device_data", new Dictionary<string, object>()
{
{ deviceName, payload.Values }
}
}
};
await MqttClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic($"gateway/telemetry")
.WithPayload(JsonConvert.SerializeObject(telemetryData)).Build());
}
}
}
11. 修改PlatformHandlerFactory
iotgateway\Plugins\Plugin\PlatformHandler\PlatformHandlerFactory.cs\PlatformHandlerFactor
case IoTPlatformType.ThingsPanel:
return new ThingsPanelHandler(mqttClient, logger, onExcRpc);
12. 添加网关侧子设备
修改后,运行iotgateway
项目,添加设备,注意设备名使用第8步的子设备地址
13. 网关侧设备通讯配置
14. 网关侧设备变量添加
15. 网关侧传输配置
输出平台选择ThingsPanel,用户名、ClientId
,使用第6步的MQTT Username
和MQTT ClientID
,密码空
16. 数据查看
17. 其他
关于IoTGatway的其他使用教程见项目首页:http://iotgateway.net/
开源地址:
关如果有其他平台接入需求可留言或加入社区交流