Industrial IoT/ML Workshop

Accessing Industrial IoT Data at the Edge

Accessing Industrial IoT Data

The EC2 instance has been prepared with an OPC-UA client called opcua-commander that allows browsing the OPC-UA data tree provided by our OPC-UA server. To keep things separated, please open a new terminal window in the Cloud9 tab:

  • Click the + symbol next to the existing terminal
  • Click “New Terminal

Launch the installed OPC UA Client Application called opcua-commander to connect to the PLC´that runs the OPC-UA server with the following command.

  • IP Address of the PLC is
  • Port of the OPC UA server is 4840
opcua-commander -e opc.tcp://

In the terminal you should see an output similar to the one below. You can use the arrows → on the keyboard to navigate to the relevant nodes: 
Objects→ PLC1→ MAIN→ Drill.

You can monitor the variables by hitting the “M” key. For instance, you can select the “DrillState” variable that indicates the state of the drill. The state where the drill is actually drilling is called “working”. For our application, we are using the data obtained while in the working state to determine if there is an anomaly in the drilling procedure.

Every monitored item will be added to the list labelled “Monitored Items” and the output for every monitored variable is shown at the bottom of the screen. In the screen above, the DrillState is currently in the state STARTUP. Repeating this step for the other variables will add the corresponding variables to the list of monitored items and lead to output in the bottom window. Add the variables MotorSpeed, SpindleSpeed and Pressure as well to see how they change.

Add the Lambda Function to the Greengrass group

We want to retrieve the data produced by the drill and published through the OPC-UA server. This requires a translation of OPC-UA to data sent via MQTT to AWS IoT Core. In our case, we will deploy a Lambda function that does this translation. To do this, open the tab with AWS Greengrass that was used in previous steps.

  • Open the AWS Greengrass Console, navigate to the Group and select “Lambdas”
  • Click on “Add Lambda”
  • On the next Screen click “Use existing Lambda”
  • Select the Lambda function which starts with “IIoTWS-OPCUA” and ends with some hex digits in the end (e.g. “IIoTWS-OPCUA-66VDLAWCTFC0”).
  • Click “Next
  • Select the Alias “latest-version”
  • Click “Finish”

The wizard will return to the AWS Greengrass console screen and show a Lambda. Prior to deploying the lambda, we have to configure it as follows:

  • The memory limit of the lambda is increased to 160MB. This makes sure that the memory monitoring of AWS Greengrass does not kill the lambda due to excessive memory usage. For production uses, this mechanism ensures that devices are not “bricked” by lambdas that have memory leaks.
  • The lambda is configured to run infinitely as the code will simply run in an endless loop to poll data from our OPC-UA server, i.e., from the drill.

Click on the Lambda Function (or as an alternative click the 3 dots on the right and select “Edit Configuration”):

  • Increase the memory allocated to the Lambda Function (e.g. 160MB)
  • Leave all other values to their defaults
  • Make sure you select “Make this function long-lived and keep it running indefinitely
  • Click “Update”  at the bottom of the screen.

Finally, the management console will display a summary screen for the lambda similar to the following:

Make sure that the Memory limit has been raised to 160MB and that the lifecycle is “Indefinite”. Click on the “Back” button at the top-right to return to the AWS Greengrass group screen.

Add Subscriptions

The code of the lambda publishes the data from the OPC-UA server to MQTT topics. We need to make sure that AWS IoT Core expects the data coming from this lambda and subscribes to the corresponding topics. We will add this subscription now:

  • Select “Subscriptions”
  • Click “Add Subscription”
  • Click “Select” on the right-hand side for the source
  • Open the Lambdas tab
  • Select the lambda we have associated with this AWS Greengrass Group (there should be only a single option to select)
  • Click “Select” on the right-hand side of the target selection
  • As Target select “IoT Cloud”
  • Click “Next”
  • Don't specify a topic filter which will result in a “#” as topic - this means all data produced by the OPCUA Lambda function is forwarded to the “IoT Cloud”. (“#” is an MQTT topic wildcard that matches all characters, including topic separators)
  • Simply click “Next
  • Finally, the summary screen will show the configurations of the subscription. Click “Finish” on this screen.

The AWS Greengrass group will now show the corresponding subscription as follows:

It is important that the subscriptions are configured as shown above, i.e., the source has to be the Lambda function, the target has to be IoT Cloud and the subscription has to be made for all topic as indicated by the “#” character. Deploy the Greengrass Group which will package the Lambda Function and deploy it to the device together with the subscription configuration.

  • Click “Actions” → “Deploy”

Test if Data from the Industrial Equipment is transferred to the Cloud

  • Open a new browser window
  • Open the AWS IoT Console (“AWS IoT Core”)
  • Click on “Test”
  • Specify “#” as a Subscription topic and click “Subscribe to topic”

You should see the OPC UA Data (by default just the DrillState is published) from the Simulated PLC similar like this:

If you do not see any data, check the following

  • have you configured enough memory (160MB should work)
  • is the Lambda function configured as long running (“Make this function long-lived and keep it running indefinitely”)

In the screenshot above, the drill has posted a message about a state change of the drill to the state “IDLE”.

Modify the OPCUA Lambda function to retrieve more information

In the next step, we will modify the lambda function so that it does not only retrieve the state changes, but also the sensor values for pressure, spindle speed and motor speed as well as the duration of the drilling process. Thus, move to the browser tab that contains Cloud9.

  • Open the index.js file and add 3 more subscriptions for the Spindle Speed, the Motor Speed and the Pressure
    For each variable, you need to change the Name and the NodeId. 

  • Ensure you have the correct NodeIds which you can get from the OPCUA Client as seen on the screenshot in the bottom right.
  • Remember to save your files once you are done editing (CTRL+S)

The Lambda function should look now like this:

Upload, Release and Change the alias version of the Lambda function

Since Greengrass deploys fixed versions of Lambda functions you need at least two steps: upload the function code to the AWS Lambda service and then publish a new version.

In oder to make it easier to update versions inside AWS Greengrass we will use an alias to the latest version called “latest-version”. Thus, we need to accomplish two steps:

  • Update the lambda with our new code and publish a new version. Since our current version is version 1, this will then be version 2.
  • Point the alias “latest-version” to version 2 of the lambda.

In the directory /home/ec2-user/environment/Lambda_Functions inside AWS Cloud9 you find a script which will do this job called

and then call the script on the terminal as following with the Lambda function name as first parameter (e.g. IIOTWS2-OPCUA-VRANKGZI9FPZ) - you can also use *OPCUA* since the shell will do the auto completion:

cd ~/environment/Lambda_Functions
./ *OPCUA*

The function should be updated to version 2 as seen in the screenshot

Now, change to the AWS Greengrass browser tab and deploy this new version of the OPCUA lambda function as described above:

  • Go to the Greengrass group
  • click “Actions” → “Deploy”