Front-End Web & Mobile

Predictive Maintenance with AWS IoT and Amazon Machine Learning

Predictive maintenance is one of many appealing use cases for the Internet of Things (IoT). Using sensors to predict the health of a fleet of machines in the field can prevent down time without conducting unnecessary maintenance. Further, predictive maintenance allows for maintenance to be conducted at the most cost effective time. Allowing you to shift your operations from being reactive to proactive. Predictive maintenance has applications for the automotive, aerospace, health, and smart city industries, just to name a few. Using the AWS IoT platform and Amazon Machine Learning (AML) allows you to easily connect things to the cloud, and deploy machine learning in real time to leverage predictive maintenance, preventing failures in the field.

In this blog post, we will demonstrate how to train a machine learning model on AML which we will use to generate predictions in near real time from IoT data, and use Amazon CloudWatch alarms to monitor a fleet of devices. For this blog post you will need some familiarity with AWS IoT, AWS Machine Learning, and have the AWS CLI installed. You can find an introduction to AWS IoT in the user documentation, and AML here. If you don’t already have the AWS CLI installed, installation directions can be found here. If you already have the CLI client, make sure that it is the updated to most recent version.

 

Build a model on Amazon Machine Learning

Add Data to S3

To get started, upload some data into S3 to train our model. For this blog post, we have simulated a predictive maintenance data set. You can download the training data here, and you will need the test data that we will be emitting for real time predictions. To create a bucket in S3, and copy the training data into it, use the following commands from the terminal. Be sure to swap out the text between the angle brackets to match your bucket name and path to your training data.

aws s3 mb s3://<bucket-name-you-need-to-choose>
aws s3 cp <path-to-file>/sim_pred_maint_train.csv s3://<bucket-name-you-need-to-choose>/sim_pred_maint_train.csv

The training data is the result of device simulating a thing with 7 sensors. Some of the sensors measurements are correlated to the condition of a wearing part. The target variable for our classification data set is zero, unless the thing is within 30 cycles of failure in which case the target is 1.

Create a Datasource

Now that the data is in S3, we need to use it as a data source for AML. This step can also be performed from the AWS CLI, but for our purposes we will use the web interface instead. From the Machine Learning tab of the AWS console select Datasource from the “create new…” drop down menu, and enter the location of your training data in S3 and give the datasource a name. If a pop-up asks to grant permission for AML to read your S3 bucket, grant permission to create the dataset. After clicking continue, check yes at the top of the page because our csv contains a header row with column names. Next, check the inferred schema and make sure that the variables are of the right type, the options are binary, categorical, numeric, and text. In this example all of the columns are of type numeric except for “target” which should be binary.  Your screen should now look similar to the screenshot shown below.

Description: AML screenshot.png

 After clicking continue, you will be redirected to the target page, where we will select our target for our model, check the box for the column named “target” and choose finish on the next page to accept our data source.

Train a Logistic Regression Model

From the Amazon Machine Learning console select “ML model” from the “create new…” drop down button. After you are redirected, you can choose the datasources you wish to use to train your model. Select the datasource we created earlier, and continue to the next page. Then choose “review and finish” to begin training the model. After you have finished training your model, click “Create Endpoint” under the “Enable Real Time Prediction” section, this step is necessary for us to be able to call the model for real time predictions. The model can take up to 20 minutes to train, so while you’re model is training go ahead and continue with the other steps, but when it is done make sure you go back and enable real time predictions.

Sending Test Data Over AWS IoT

Simulate Thing Data

To simulate a thing emitting data to AWS-IoT we will use a Python script which uses the AWS Python SDK (boto3) to emit lines from a test data set. You will need the pandas and NumPy Python packages. If you don’t have these packages, you can install them all at once using the following command.

pip install pandas numpy boto3

Now we have a model trained and a simulator to send data to AWS IoT, the next step will be to create an alarm which will listen to messages and generate alerts if AML predicts a thing is in need of maintenance.

Create an Amazon CloudWatch Alarm

To create an alarm based on our predictions, we first need to create an Amazon CloudWatch metric, then attach an alarm to that metric to let us know when a unit is in need of maintenance. Use these commands to create the metric from the AWS CLI and define an alarm.

aws cloudwatch put-metric-data --metric-name maintenance-status --namespace "pm-metrics" --value 0
aws cloudwatch put-metric-alarm --alarm-name maintenanceNotification --metric-name maintenance-status --namespace "pm-metrics" --statistic Maximum --period 60 --evaluation-periods 1  --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold

We chose the metric name of “maintenance-status” and created a namespace called “pm-metrics”. The second command creates an alarm named “maintenanceNotification” which is based on the metric we just created. The alarm will go off if at least one of our units is predicted to need maintenance. It is possible to configure the alarm to notify via text or email when status is set to ALARM, however, for this post we will just create the alarm.

Create an Amazon CloudWatch Metrics Rule

Next, we need to create a role with permission to write to Amazon CloudWatch metrics. This role gives the AWS IoT Rules Engine rule permission to forward our AML predictions to Amazon CloudWatch. The first step is to create a role, then a policy which is permitted to write to Amazon CloudWatch metrics, and finally, attach the policy to the role. To create the role we need to create a JSON document named cw_role.json, and save the following text to the document.

{
"Version": "2012-10-17", 
"Statement": [{
      "Sid": "",    
      "Effect": "Allow",     
      "Principal": {
            "Service": "iot.amazonaws.com"
       },
      "Action": "sts:AssumeRole"
  }]
}

After saving cw_role.json, use the AWS CLI to create a role with the following command.

aws iam create-role --role-name cw_role --assume-role-policy-document file://cw_role.json

Next, create the policy that will attach to the new role. Start by saving another JSON document named cw_policy.json ,and copy the following text to the file.

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": ["cloudwatch:PutMetricData","machinelearning:Predict","machinelearning:GetMLModel"],
    "Resource": "*"
  }
}

From the command line, create a policy based on the policy document just created.

aws iam create-policy --policy-name cw_policy --policy-document file://cw_policy.json

When this command is run, the output will include an ARN for the policy which we need for the next CLI command to attach the policy to the role. Remember, replace the X’s with your account number.

aws iam attach-role-policy --role-name cw_role --policy-arn arn:aws:iam::XXXXXXXXXXXX:policy/cw_policy

Now that the Amazon CloudWatch metric is set up, and a role is permitted to write to it, we can create a rule using the AWS IoT Rules Engine. To create a rule save the following JSON file as cw_rule.json, and then create the rule using the AWS CLI.

{
  "sql": "SELECT * FROM 'pm/topic'",
  "ruleDisabled": false,
  "actions": [{
      "cloudwatchMetric": {
          "roleArn": "arn:aws:iam::XXXXXXXXXXXX:role/cw_role",
          "metricName": "maintenance-status",
          "metricNamespace": "pm-metrics",
          "metricValue": "${machinelearning_predict('your-model-id-here', 'arn:aws:iam::XXXXXXXXXXXX:role/cw_role', *).predictedLabel}",
          "metricUnit": "None"
      }
  }]
}

The CLI command to create the rule is.

aws iot create-topic-rule --rule-name cwAlarm --topic-rule-payload file://cw_rule.json

Testing The Alarm

Now that everything is in place, we can test our ML model and Amazon CloudWatch metric/alarm by emitting our test data set to AWS IoT using the python script referenced earlier in the post.

python emit_to_AWS_IoT.py sim_pred_maint_emit.csv

Make sure to replace the file path here with the path you put the test data file in on your machine. If you didn’t download the test data earlier it can be found here. Within a few minutes of starting the script, you should be able to monitor your CloudWatch metric and see a blue line indicating the status of the metric we have created. When AML predicts a failure, the metric will move from 0 to 1 and the CloudWatch alarm will go off.

You have just completed an end-to-end walkthrough of how to use AML with AWS IoT to perform predictive maintenance on a fleet of devices, and monitor their status using Amazon CouldWatch metrics and alarms.  We hope this walkthrough helps explain predictive maintenance, and provides a good starting point for your specific use cases. If you have questions or comments, don’t hesitate to reach out.