写点什么

使用 AWS Lambda 和 AWS Greengrass 在边缘站点进行协议转换

  • 2019-09-18
  • 本文字数:5808 字

    阅读完需:约 19 分钟

使用 AWS Lambda 和 AWS Greengrass 在边缘站点进行协议转换

在工业自动化领域,您会发现设备和 PLC 供应商使用了数十种协议:OPC-UA、Modbus TCP、串行协议等等。事实上,往往仅在一个工厂,就会很容易发现有不同的设备,却执行相同的功能去完成相似的工序。AWS Greengrass 通过远程部署来帮助企业减少更改协议时的运行开销。AWS Greengrass 使用 AWS Lambda,借助这种服务可以在云中对设备进行编程。例如,企业可以将 AWS Lambda 函数部署到本地设备以添加额外的传感器或更改供应商设备。通过云中编程并部署到本地,缩短了物联网应用程序的开发和维护周期。


在本博文中,我们将演示如何从常见的工业协议(例如 Modbus TCP)来转换数据的例子。其他协议的转换也可以用同样的方法。

例如:电阻温度探测器

AWS Greengrass 支持 OPC-UA(一种用于工业通信的信息交换标准)。与 Modbus TCP 类似,OPC-UA 允许您提取并处理来自工业设备的消息,然后根据您定义的规则将它们发布到 AWS Greengrass 组中或云中的设备。



在本博文使用的示例中,我们采集了安装在电机轴承外壳上的电阻温度探测器 (RTD) 的模拟温度读数。(您可以使用 AWS IoT Analytics 来分析此温度信息,虽然这不属于本博文的范畴。它可以帮助您对轴承润滑进行预防性维护,或者检测即将出现的故障,尤其是电机、发电机和其他高压设备)


下图显示了 AWS Greengrass 如何运行 Lambda 函数原生连接到使用 Modbus TCP 的设备,例如可编程逻辑控制器 (PLC)。同样的 Lambda 函数将从 Modbus 到设备读取的值发布到 AWS IoT Core 中的 MQTT 主题。


Lambda 函数可以通过本地检测到的事件触发,也可从 AWS IoT Core 触发,调用写回至 Modbus 从设备。

先决条件

要执行本博文中的步骤,您需要拥有一台安装了 python-dev、python-pip、pyModbus、pycrypto、pyasn1 和 1.3 版或更高版本 Greengrass Core 软件的 Raspberry Pi。

设置 AWS Greengrass 并安装相应的函数库

遵循以下步骤设置您的 Raspberry Pi 与 AWS Greengrass。有关更多信息,请参阅 Greengrass 开发人员指南。

创建 Greengrass 核心

创建 Greengrass 组。如果您已经拥有一个组,则可以跳过这一步。


  1. 在 AWS IoT 控制台的左侧窗格中,选择 Greengrass,然后选择组。

  2. 选择创建组按钮。

  3. 选择使用快捷创捷。

  4. 键入您的组的名称。

  5. 键入与该组关联的 Greengrass 核心的名称,或者使用默认值。

  6. 然后选择创建组和核心。

  7. 下载核心的安全性资源,这些资源将以 tar.gz 的形式提供。

  8. 遵循开发人员指南中规定的步骤以将密钥加载到您的设备并启动 Greengrass。

部署 Greengrass 核心

  1. 在 AWS IoT 控制台中,从“操作”菜单中选择“部署”。

  2. 选择“自动检测”或者手动设置核心的终端节点信息。

  3. 部署好核心后,您应会看到一条“已成功完成”的消息:

安装 PyModbus 库

现在,您需要安装 pyModbus,这是一个完整的 Modbus 协议实现。


要安装 pyModbus,请在 AWS Greengrass 设备上执行以下命令:


sudo apt-get updatesudo apt-get install python-devsudo apt-get install python-pipsudo pip install pymodbussudo pip install pycryptosudo pip install pyasn1
复制代码

创建 Lambda 函数

在这一部分,您将创建三个 Lambda 函数。如果您已经连接到真实的 Modbus 设备,则可以跳过前两个命令。


  • Modbus Slave (Server) — 此函数会模拟挤出机 PLC 设备。

  • Modbus Simulator — 此函数会定期读取您的 Raspberry Pi 的 CPU 温度,并将它写入 Modbus 从设备的寄存器以模拟电机轴承温度。

  • Modbus To AWS IoT — 此函数会定期读取 Modbus 寄存器中的轴承温度,并将该值发布到任何 MQTT 主题。

创建 Modbus Slave Lambda 函数

  1. 在您的计算机上登录 登录 AWS 管理控制台并打开 AWS Lambda 控制台。

  2. 选择全新编写。

  3. 对于“名称”,键入 ModbusSlave。对于运行时,选择 Python 2.7。

  4. 在选择函数的 IAM 角色时,您必须提供 CloudWatch 日志权限。Greengrass-Modbus-Role 已经附加了下列策略。请务必为该角色应用恰当的权限。

  5. 下面的脚本会在 Raspberry Pi 上启动 Modbus TCP 服务器(从站)。它会在内存中分配一些 Modbus 寄存器,这些寄存器可以用于读取/写入数据。插入下面的脚本并发布一个新版本以在 AWS Greengrass 中使用。提示:为您的 AWS Lambda 函数创建一个别名并将发布的版本分配到该别名。这样可以方便您以后向函数部署更新。


#
# Copyright 2010-2017 Amazon.com, Inc. or its affiliates.保留所有权利。#
# ModbusSlaveServer.py# Runs a modbus slave device in memory for testing purposes.# This AWS Lambda function allocates registers for the modbus tcp slave and starts# the server.If an exception occurs, it will wait 5 seconds and try again.# Since the function is long-lived it will run forever when deployed to a# Greengrass core. The handler will NOT be invoked in our example since# the we are executing an infinite loop.
import timeimport loggingimport sys#import pymodbus libraries for the serverfrom pymodbus.server.sync import StartTcpServerfrom pymodbus.datastore import ModbusSequentialDataBlockfrom pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
# instantiate logger which will log any exceptions to Cloudwatch or Greengrass# local logslogger = logging.getLogger(__name__)logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
while True:try:#create registy for modbus serverstore = ModbusSlaveContext(di = ModbusSequentialDataBlock(0, [17]*100),co = ModbusSequentialDataBlock(0, [17]*100),hr = ModbusSequentialDataBlock(0, [17]*100),ir = ModbusSequentialDataBlock(0, [17]*100))context = ModbusServerContext(slaves=store, single=True)
# change default port of modbus from 502 to 5020 as it requires# root permissions below 1024StartTcpServer(context, address=("localhost", 5020))except Exception, e:logging.info("Error: {0}".format(str(e)))time.sleep(5)
# this function is never called as it will be configured as long-lived.def lambda_handler(event, context):return 'Hello'
复制代码

创建 Modbus Slave Simulator Lambda 函数

  1. 重复上文第 1-3 步以创建一个名为 ModbusSlaveSim 的函数。

  2. 下面的脚本将创建一个 Modbus 客户端(主站),连接到上一步的 Modbus TCP 服务器(从站)并且存储每隔 5 秒从 RTD 设备获取的轴承温度值。(此例中的 RTD 设备仅仅是 Raspberry Pi 的 CPU 温度。) 插入以下脚本并发布一个要在 AWS Greengrass 中使用的新版本。


## Copyright 2010-2017 Amazon.com, Inc. or its affiliates.保留所有权利。#
# ModbusSlaveSim.py# This is a simulator script that connects to a modbus slave device and# writes the CPU temperature for the raspberry pi device to a modbus register.# If an exception occurs, it will wait 5 seconds and try again.# Since the function is long-lived it will run forever when deployed to a# Greengrass core. The handler will NOT be invoked in our example since# we are executing an infinite loop.
import timeimport loggingimport sysimport os# import pymodbus libraries for the simulator clientfrom pymodbus.client.sync import ModbusTcpClient as ModbusClientfrom pymodbus.payload import BinaryPayloadDecoderfrom pymodbus.payload import BinaryPayloadBuilderfrom pymodbus.constants import Endianfrom pymodbus.compat import iteritems
# Default port for modbus slave is typically 502.Using 5020 for simulation to# avoid root permissions.client = ModbusClient('127.0.0.1', port=5020)
# instantiate logger which will log any exceptions to Cloudwatch or Greengrass# local logslogger = logging.getLogger(__name__)logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# returns the cpu temperature from the raspberry pi devicedef get_temp():f = open("/sys/class/thermal/thermal_zone0/temp")CPUTemp = f.read()f.close()return float(CPUTemp)/1000.0
# in an infinite loop, this procedure will poll the cpu temperature and write# it to a local modbus slave device.def poll_temp():while True:try:client.connect()builder = BinaryPayloadBuilder(endian=Endian.Big)builder.add_32bit_float(get_temp())payload = builder.build()address = 0rq = client.write_registers(address, payload, skip_encode=True, unit=1)
except Exception, e:logging.info("Error: {0}".format(str(3)))time.sleep(5)
# executing polling on startup.poll_temp()
# this function is never called as it will be configured as long-lived.def lambda_handler(event, context):return 'Hello'

复制代码

创建 Modbus to AWS IoT 函数

  1. 要创建最后一个函数,请在 AWS Lambda 控制台中选择蓝图。在蓝图下搜索 greengrass。选择 Python 蓝图运行时并将它命名为 ModbusToAWSIoT。

  2. 2.下面的脚本会创建一个 Modbus 客户端(主站),连接到 Modbus 服务器(从站)并检索存储在 Modbus 服务器寄存器中的轴承温度。然后将该值发布到 AWS IoT 上的某个主题。插入以下脚本并发布一个要在 AWS Greengrass 中使用的新版本。



## Copyright 2010-2017 Amazon.com, Inc. or its affiliates.保留所有权利。#
# ModbusToAWSIoT.py# This is an example script that connects to a modbus slave device to read# a temperature value and publish to an MQTT Topic in AWS IoT every 5 seconds.# If an exception occurs, it will wait 5 seconds and try again.# Since the function is long-lived it will run forever when deployed to a# Greengrass core. The handler will NOT be invoked in our example since# we are executing an infinite loop.
import greengrasssdkimport platformfrom threading import Timerimport timeimport loggingimport sysimport json# import pymodbus libraries for the modbus clientfrom pymodbus.client.sync import ModbusTcpClient as ModbusClientfrom pymodbus.payload import BinaryPayloadDecoderfrom pymodbus.payload import BinaryPayloadBuilderfrom pymodbus.constants import Endianfrom pymodbus.compat import iteritems
# Instantiate the client for your modbus slave device.In this example we are# using the local IP address where a simulator exists.Change this to your# desired IP.In addition, the typical default port for Modbus TCP is 502.For# this example, 5020 was used.mbclient = ModbusClient('127.0.0.1', port=5020)
# Default port for modbus slave is typically 502.Using 5020 for simulation to# avoid root permissions.logger = logging.getLogger(__name__)logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# Creating a greengrass core sdk clientclient = greengrasssdk.client('iot-data')
# in an infinite loop, this procedure will poll the bearing temperature from a# modbus slave device (simulator) and publish the value to AWS IoT via MQTT.def poll_temp():while True:try:# connect to modbus slave devicembclient.connect()# set the address and number of bytes that will be read on the modbus deviceaddress = 0x00count = 8# read the holding register value for the temperaturerr = mbclient.read_holding_registers(address, count, unit=1)# decode results as a 32 bit floatdecoder = BinaryPayloadDecoder.fromRegisters(rr.registers, endian=Endian.Big)decoded = {'float': decoder.decode_32bit_float()}# publish results to topic in AWS IoTfor name, value in iteritems(decoded):client.publish(topic='dt/controller/extruder/plc1/rtd', payload=json.dumps({ 'Temp': value}))except Exception, e:logging.info("Error: {0}".format(str(e)))
time.sleep(5)
poll_temp()
# This is a dummy handler and will not be invoked# Instead the code above will be executed in an infinite loop for our exampledef function_handler(event, context):return
复制代码

部署 AWS Lambda 函数

现在您可以部署您的协议转换脚本。登录 AWS 管理控制台并打开 AWS IOT 控制台。

将 AWS Lambda 函数添加到 Greengrass 组

  1. 打开您之前部署的组。在左侧窗格中,选择 Lambdas,然后选择添加 Lambda。

  2. 选择使用现有 Lambda

  3. 从您之前的创建的 Lambda 函数中选择一个,然后选择下一步

  4. 选择您在定义函数时创建的别名。如果未创建别名,则选择发布的版本。提示:我们建议您创建别名,这样在需要发布 Lambda 函数的新版本时无需再次执行这一步。


5.在 AWS Lambda 函数列表中,选择添加的 Lambda 函数,然后选择编辑。 在这一页,您可以启动对设备上资源的访问权限。这可能是串口或 USB 端口,具体取决于协议驱动程序。



6. 对于 Lambda 生命周期,选择使此函数长时间生存,保持其无限期运行。对 /sys 目录的只读访问权限,请选择启用



7. 为创建的其他 Lambda 函数重复这些步骤

分配订阅

  1. 打开您之前部署的组。在左侧窗格中,选择订阅

  2. 选择添加订阅

  3. 对于订阅下的源,选择 ModbusToAWSIoT。对于目标,选择 IoT 云。不要为该主题设置筛选条件

  4. 这将在 AWS IoT 中创建一个订阅,以捕获从 AWS Greengrass 设备上的 Lamdba 函数发布的任何数据。

部署更改

  1. 从组配置页面的操作菜单,选择部署。您可以从 AWS IoT 订阅 dt/controller/extruder/plc1/rtd 以查看从 Modbus TCP 转换为 MQTT 的数据

小结

借助 AWS Greengrass,可以从多种不同工业协议的设备上采集数据,即使这些设备使用不同的本地通信协议。本博文中的示例演示了如何与 Modbus TCP 等协议通信,并安全地将数据实时传输到云中。


希望本博文对您有所帮助。欢迎在评论区中留下问题和其他反馈。


本文转载自 AWS 博客。


原文链接:


https://amazonaws-china.com/cn/blogs/china/perform-protocol-conversion-at-the-edge-with-aws-lambda-and-aws-greengrass/


2019-09-18 15:021091
用户头像

发布了 1950 篇内容, 共 164.9 次阅读, 收获喜欢 81 次。

关注

评论

发布
暂无评论
发现更多内容

ai制图软件有哪些?这5款自动生成绘画工具值得推荐!

彭宏豪95

人工智能 在线白板 办公软件 AIGC AI绘画

谈谈我对 AIGC 趋势下软件工程重塑的理解

阿里巴巴云原生

阿里云 云原生 AIGC

龙蜥社区第 22 次运营委员会圆满结束!

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

iPaaS平台能帮助企业解决什么问题?

谷云科技RestCloud

数据集成 应用集成 ipaas

使用Docker快速搭建Web服务器Nginx

霍格沃兹测试开发学社

龙蜥社区荣获 2023 年度龙芯“十佳基础软件合作伙伴”奖

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

产学研用全覆盖!信通院、中兴通讯、复旦大学等 12 家厂商共同成立龙蜥社区系统运维联盟(SOMA)

OpenAnolis小助手

操作系统 国产操作系统 龙蜥社区

59 人参会,探讨新年发展!龙蜥社区技术委员会、运营委员会会议圆满结束

OpenAnolis小助手

操作系统 国产操作系统 龙蜥社区

59 人参会,探讨新年发展!龙蜥社区技术委员会、运营委员会会议圆满结束

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

龙蜥系统运维联盟第二次会议圆满召开,深度探讨联盟发展方向

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

运维人少,如何批量管理上百个微服务、上千条流水线?

阿里云云效

阿里云 云原生 云效

走进浪潮信息,深入探讨社区发展规划交流会圆满结束 | 理事长走进系列

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

Lambda 表达式及线程安全最佳实践

伤感汤姆布利柏

走进 Intel,深度探讨合作发展规划交流会圆满结束 | 理事长走进系列

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

龙智亮相2024国际集成电路展览会暨研讨会(IIC Shanghai),分享芯片研发及管理解决方案与技术实践

龙智—DevSecOps解决方案

芯片研发

Selenium Headless模式:无头浏览器的使用与优势

霍格沃兹测试开发学社

GOPS全球运维大会2024深圳站亮点抢先看!

博睿数据

视频教程:如何集成Perforce Helix Core与S3 存储,助力无限扩展储存空间

龙智—DevSecOps解决方案

版本控制 S3 版本控制软件 储存库

龙年新目标!龙蜥安全联盟第三次月会圆满结束

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

走进龙芯中科交流会圆满结束!深入探讨未来合作规划 | 理事长走进系列

OpenAnolis小助手

操作系统 国产操作系统 龙蜥社区

龙蜥社区第四届理事大会圆满召开!中兴、英特尔、浪潮成为副理事长单位!龙蜥高级顾问团成立!

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

使用Selenium模拟鼠标滚动操作的技巧

霍格沃兹测试开发学社

详解CloudBees CI,助力Jenkins用户顺利迁移并构建高效CI/CD平台

龙智—DevSecOps解决方案

ci 持续集成 CD

运维人少,如何批量管理上百个微服务、上千条流水线?

阿里巴巴云原生

阿里云 云原生 云效

Optimism Hackathon: 加速 AI 与 Blockchain Data 发展

Footprint Analytics

大数据 gamefi #人工智能

龙蜥操作系统荣登开放原子开源基金会“2023 生态开源项目”奖项榜单

OpenAnolis小助手

开源 操作系统 国产操作系统 龙蜥社区

解决过期苹果App应用的方法

使用Selenium执行JavaScript脚本:探索Web自动化的新领域

霍格沃兹测试开发学社

使用selenium轻松实现元素拖拽

霍格沃兹测试开发学社

使用 AWS Lambda 和 AWS Greengrass 在边缘站点进行协议转换_5G/IoT_亚马逊云科技 (Amazon Web Services)_InfoQ精选文章