We recently explained how you can read out machine data with an edge device, visualize it in Azure, and prepare it for further processing. This post looks at the same question – only in AWS.
Setup and preparation
The architecture for the following use case consists of two EC2 instances, two lambda functions, a DynamoDB (database) and the QuickSight service. The following image describes the infrastructure in AWS.
The first EC2 instance is used to simulate the industrial robot. The simulation was implemented using a Python module and regularly writes data to an OPC UA server. The AWS IoT Greengrass software is installed on the second EC2 instance. It is used to read the OPC UA data and provides integration into the AWS Cloud. The software connects to the OPC UA server as required (either via a trigger or at regular, predefined intervals), retrieves the data and publishes it in a certain topic. Another function is triggered by the topic and saves the data in a database. The functions are initialized with AWS Lambda. AWS Lambda is a serverless platform on which, in our scenario, Python scripts are defined and then executed on the EC2 instance or at the edge using AWS IoT Greengrass. This service is the key element in this blog post.
The AWS IoT Greengrass software allows you to run both Lambda functions and Docker containers on connected devices. It allows the devices to capture and analyze data closer to the source of information, to respond autonomously to local events, and to communicate securely with each other in local networks. The core software enables MQTT messaging between AWS IoT functions, devices and local networks. It is also possible to read OPC UA servers with the AWS lambda functions. AWS DynamoDB, a scalable serverless database (NoSQL), is used to create the database and store all the data for the final visualization. Thanks to its very low latency, DynamoDB is well suited to this use case. The database is therefore ideal for near-real-time queries and for IoT and Industry 4.0 use cases.
Data Processing and logic in AWS IoT Greengrass
For our use case, we are using two lambda functions in AWS IoT Greengrass. Both lambdas are written in Python and programmed locally. Before you can use the AWS Greengrass IoT functions, the AWS IoT Device has to download SDK for Python. . The data from the server is retrieved every five seconds and published to the "iot/tribe" topic. The lambda is displayed briefly in the following snippet. It establishes a connection to the OPC UA server, queries the nodes and then publishes them to AWS.
client = greengrasssdk.client('iot-data')
opcuaClient = Client('opc.tcp://0.0.0.0:4840')
values = {
'disturbance - S1': opcuaClient.get_node('ns=2;i=6').get_value(),
'disturbance - S2': opcuaClient.get_node('ns=2;i=7').get_value(),
}
client.publish(
topic='iot/tribe',
queueFullPolicy="AllOrException",
payload=json.dumps(values)
)
The second lambda initializes a connection to the DynamoDB database and writes the values to the database when triggered:
dynamodb = boto3.client('dynamodb')
dynamodb.put_item(TableName='greengrass_iot', Item=values)
To enable AWS IoT Greengrass to use the lambdas, they are zipped and uploaded to AWS Lambda along with all their dependencies and libraries so that they can be integrated – and used – in AWS IoT Greengrass.
A group is created in AWS IoT Greengrass. All the required lambda functions can be added to this group to be used on the device or on the core. The functions mentioned earlier are also added. The OPC UA Server function is designed to be long-lasting and runs permanently. The second function is an "on-demand" function that is only used when something is published to the "iot/topic" topic. Similarly, any connections (source – target) must be defined in the Subscriptions tab. There are two subscriptions in our example:
- Source: opc_lambda, target: IoT Cloud, topic: iot/tribe
- Source: IoT Cloud, target: dynamo_lambda, topic: iot/tribe
This image shows the interrelated process:
Finally, the IoT group is deployed and goes live. If it is of interest, you can also define different log levels for both the core and the lambdas.
Summary
AWS offers numerous services to help you enter the world of the Internet of Things easily, effectively and without wasting time. Before long, you'll be able to read different devices, for example, migrate them into the cloud and visualize them for analysis purposes. The publish/subscribe method enables you to manage different workflows and processes asynchronously and reliably.
Do you need and want to integrate your IoT data into AWS and enter the world of the cloud? We'd be happy to help you implement the entire process.