# Predictive Maintenance workshop using AWS IOT and AI/ML services.
Predictive maintenance techniques are designed to monitor the condition of equipment in industrial or home environments such as factory equipment, pumps and compressors, oil rigs etc., and determine whether or not the equipment is in need of maintenance, and if so, when. As opposed to routine scheduled maintenance, predictive maintenance has the potential to avoid unexpected downtime arising from potential issues that go uncaught in between maintenance windows. This is often a major cost concern particularly when the equipment in question is mission critical. Predictive maintenance also has the potential of avoiding costly repairs when they are not required, and as such can inform when the next scheduled maintenance should occur.
In this workshop, you will apply Machine learning to a predictive maintenance use case. Imagine that you are in charge of running some equipment in a factory. The equipment is monitored by sensors which generate regular signals about the condition and health of the equipment. Based on the signals, you want to predict whether the equipment is in need of maintenance or not. One of the major challenges with your environment is the lack of consistent internet connectivity, so any solution you deploy, needs to function even in the absence of a connection to the cloud. Following instruction inn this workshop, you will use AWS IoT and AI-ML capabilities to arrive at a potential proof-of-concept (PoC) solution.
1. [Solution Overview](#1-solution-overview)
2. [Prerequisites](#2-prerequisites)
3. [Architecture](#3-architecture)
4. [Getting Started: Deploy your Cloud Formation template](#4-getting-started-deploy-your-cloud-formation-template)
5. [Install Greengrass, register IoT thing and connect to Greengrass](#5-install-greengrass-register-iot-thing-and-connect-to-greengrass)
5.1 [Provision the Greengrass group and core](#51-provision-the-greengrass-group-and-core)
5.2 [Register an IoT Thing with AWS IoT](#52-register-an-iot-thing-with-aws-iot)
5.3 [Register IoT Device with AWS Greengrass](#53-register-iot-device-with-aws-greengrass)
5.4 [Set up the IoT sensor](#54-set-up-the-iot-sensor)
6. [Explore data, build, train and deploy a model in Amazon SageMaker](#6-explore-data-build-train-and-deploy-a-model-in-amazon-sagemaker)
7. [Deploy the predictive-maintenance-advanced Lambda](#7-deploy-the-predictive-maintenance-advanced-lambda)
7.1 [Create Lambda function to deploy to Greengrass Core](#71-create-lambda-function-to-deploy-to-greengrass-core)
7.2 [Create a SNS topic](#72-create-a-sns-topic)
7.3 [Deploy the Lambda function locally to Greengrass Core](#73-deploy-the-lambda-function-locally-to-greengrass-core)
8. [Create Polly Lambda function](#8-create-polly-lambda)
9. [Configure Lambda function to read data from sensors](#8-configure-lambda-function-to-read-data-from-sensors)
10. [Configure Lambda function to send prediction to AWS IoT and deploy the solution](#10-configure-lambda-function-to-send-prediction-to-aws-iot-and-deploy-the-solution)
10.1 [Configure Lambda function](#101-configure-lambda-function)
10.2 [Deploy lambda function to Greengrass Core](#102-deploy-lambda-function-to-greengrass-core)
10.3 [Troubleshooting](#103-troubleshooting)
10.4 [Trigger Polly](#104-trigger-polly)
## 1. Solution overview
You will start by collecting data generated by the sensors. A local lambda function deployed on the factory floor will make API calls to the machine learning model you trained on the AWS Cloud, which is also deployed locally at the factory(more on why you want to do this later). The lambda function will send notifications to the IoT cloud whether the part is Faulty or Not. If the part is not Faulty, no further action is taken. If a faulty part is found, the Lambda function will publish a message to an Amazon SNS topic of your choice. A second lambda function, listening on this topic will automatically be triggered. This Lambda function will call the Amazon Polly API to convert the body of the notification to speech. The speech file will be asynchronously generated and saved in Amazon S3. You can download this file and play it on your factory floor to let the floor manager know that there is an issue with a part.
**The following AWS Services are leveraged:**
AWS Greengrass
AWS IoT
Amazon S3
Amazon SageMaker
Amazon SNS
Amazon Polly
AWS Cloud9
AWS CloudFormation
Amazon EC2
**Key takeaways of this workshop are the following:**
1) Casting the use case into a supervised learning problem.
1) An understanding of some of the challenges when using ML for predictive maintenance.
2) Deploy an architecture that leverages AWS ML and AI services, and learn how they interact with IoT services such as AWS GreenGrass to perform predictive maintenance on-premises.
3) Next steps towards taking this architecture to an end-to-end cloud solution.
## 2. Prerequisites
1) AWS Account
2) Laptop
3) Browser
4) Basic Linux/Python knowledge
## 3. Architecture
The architecture for this workshop comprises of two parts:
### Machine Learning:
We will assume that we have a simple S3 datalake where we will upload our training data used to train the model. The Machine learning model will be trained in Amazon SageMaker and the model artifacts will be deployed to the Greengrass core via S3.
![](images/ML-arch.png)
### IoT Architecture:
Once the model is deployed, you will build the following architecture which links the machine learning model to AWS IoT and leverages Amazon Polly to convert text to speech to generate mp3 files that can be played over the PA system in the factory floor.
![](images/IoT-arch.png)
## 4. Getting Started: Deploy your Cloud Formation template
Architecturally, this workshop comprises of two parts: your factory environment (FE) and the AWS cloud.
The FE is where your equipment and sensors live which are monitoring the condition of the equipment. In order to keep track of the sensor data, they need to send the data to the AWS Cloud. The service that accomplishes this is called IoT Greengrass. Greengrass is a software that can be installed in your local factory servers which allows you to send sensor data and messages to and from the cloud using secure MQTT messaging. For more on Greengrass service, please check out: https://aws.amazon.com/greengrass/
Here we will mimic your local FE with an EC2 instance. Launch the following Cloudformation template in your AWS account that you are using for this workshop.
Launch CloudFormation stack in **us-east-1** only: [![button](images/cloudformation-launch-stack.png)](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?stackName=predictivemaintenance&templateURL=https://iot-ml-predictive-maintenance.s3.amazonaws.com/iot-ml-predictive-maintenance.json)
**This template will create the following resources:**
1. An **S3 bucket** for use throughout this workshop. You will use this bucket to store your machine learning model, Polly notifications and any data used for training and testing.
2. **VPC + public Subnet and security groups** for an EC2 instance.
3. **Cloud9** instance where you will run your code and deploy your Greengrass core. Cloud9 is an AWS IDE where you will be able to write code and deploy scripts on your underlying EC2 instance. You will use this environment to install your Greengrass core.
4. A **SageMaker notebook environment** to build, train and deploy machine learning models.
5. An **EC2** instance which will mimic your FE. The EC2 instance comes bootstrapped with custom python libraries necessary for running this workshop.
After you have been redirected to the Quick create stack page at the AWS CloudFormation console take the following steps to launch you stack:
1. On 'Create stack', leave everything as default and click Next
2. Under 'Specifc stack details',