Main Content

Perform Predictive Maintenance on Rotating Device Using ESP32 Board, ThingSpeak, and Machine Learning

This example shows how to predict and monitor the health of a rotating device using machine learning algorithm. You can use this example for predictive maintenance of any rotating device or piece of equipment so that you can fix them before they fail.

In this example, the operational state of the rotating device is divided into four operational modes of Stop, Block, Normal, and Rotor Imbalanced. This example uses a ESP32 board with lsm9ds1 sensor to detect the vibrations along the X-, Y-, and Z- directions. These vibration signals are then fed into a pre-trained machine learning model to classify into different defined operational modes.

This example also shows how to use the ThingSpeak™ platform to view the operational mode of the rotating and trigger an email alert whenever there is abnormality in the motion of the rotating device. To access all the files for this example, click Open Live Script and download the attached files.

Required Hardware

  • ESP32 board

  • LSM9DS1 IMU sensor

  • Any device that contains moving parts. This example uses a rotating fan.

  • Connecting Wires

  • Any object for blocking the normal operational mode of a device. This example uses cardboard.

  • Adhesive or sticking tape

Hardware Setup

  • Configure ESP32 hardware for USB based workflow. For more information, see Connection over Wi-Fi.

  • Connect SCL and SDA of ESP32 to SCL and SDA of LSM9DS1 IMU.

  • Connect the ground and 3.3V pin of ESP32 to ground and VDD of LSM9DS1 IMU.

  • Place the entire hardware setup in a secure location on the rotating device. Use an adhesive or a piece of tape to secure the hardware board on the top of the device. Ensure that no connecting wires create an obstacle while the device vibrates.

Prepare Data Set for Training Deep Learning Algorithm

The IMU sensor capture the acceleration data of rotating device. Follow these steps to capture and train acceleration data for any rotating device using an IMU sensor

  1. Data Acquisition Using Accelerometer

  2. Feature Extraction

  3. Develop Predictive Model to Classify Data

Data Acquisition Using Accelerometer

You can either use Stop_raw_data.mat, Block_raw_data.mat, Normal_raw_data.mat, and RotorImbalance_raw_data.mat dataset corresponding to the Stop, Normal, Block and Rotor imbalance or capture the data manually.

To use the dataset, click Open Live Script and download Stop_raw_data.mat, Block_raw_data.mat, Normal_raw_data.mat, and Rotor Imbalance_raw_data.m files.

To capture the data manually, place the setup in the secure location on the rotating device. Select iscollectData check box and start capturing the data.

The dataset in this example contains 3000 samples, as defined by the variable noOfSamples, for each of the four modes. Once the prompt '*** Collecting data for mode = ' Stop ' ***'' appears on your screen, start capturing data for the Stop mode. Continue capturing the data related to Stop mode until the prompts for Block, Normal, and Rotor Imbalanced mode appear. MATLAB stores the data related to the four modes in the Stop_raw_data.mat, Normal_raw_data.mat, Block_raw_data.mat, and RotorImbalance_raw_data.mat files.

For capturing data in the Stop mode, ensure that the device is in the power OFF state. Similarly, for capturing data in the Normal mode, power ON the device. Use a piece of cardboard or any other object to block any moving part of the device while in the ON state. For the Rotor Imbalanced mode, you can move or shake the whole setup of the device.

These images and gif shows the four operational modes of Stop, Normal, Block, and Rotor Imbalanced.

RotorImbalance.gif

% Check this if you want to manually collect the data
collectData = false;

% Create Mode list
modeList = {'Stop' , 'Normal', 'Block','RotorImbalance'};

% Collect data for all modes
noOfSamples = 30000;
if collectData
    for idx = 1:length(modeList)
        collectModeData(modeList{idx},noOfSamples);
    end
end

Feature Extraction

Select samplesPerObservation based on the granularity you desire in extracted features. For this example, set samples per observation to 50.

The extractModeFeatures function is used to extract features from the data acquired using the accelerometer on the LSM9DS1 IMU sensor. The extracted features are stored in the variable features. To use the extractModeFeatures function, click Open Live Script and download the extractModeFeatures.m file.

The modeIDList contains the IDs corresponding to the operational modes of the rotating device, where ID 1 corresponds to Stop, ID 2 to Normal, ID 3 to Block, and ID 4 corresponds to Rotor Imbalance.

% Initialize the buffer size
samplesPerObservation = 50;
features =[];
modeIDList =[];
% Extract features
for idx = 1: length(modeList)
    data = load([modeList{idx} '_raw_data.mat'],'data');
    data = data.data;
    data = data.("Acceleration");

    % Calculate the number of groups
    numOfObservations = floor(size(data,1)/samplesPerObservation);
    for i = 1:numOfObservations
        idx1 = (i-1)*samplesPerObservation +1;
        idx2 = i*samplesPerObservation;
        features = [features; extractModeFeatures(data(idx1:idx2,1),data(idx1:idx2,2),data(idx1:idx2,3))];
    end

    modeIDList = [modeIDList;ones(numOfObservations,1)*idx];
end

Develop Predictive Model to Classify Data

Use the Classification Learner (Statistics and Machine Learning Toolbox) app to train a classification model. To open the Classification Learner app, type classificationLearner at the MATLAB command line.

Alternatively, in the Apps tab click the arrow on the right on the Apps tab to open the gallery. Then, under Machine Learning and Deep Learning, click Classification Learner.

In the app, click the New Session button, and then click From Workspace.

In the Data set pane, click the arrow under Data Set Variable, and then select features. Select From Workspace under Response and select modeIDList.

In the Validation pane, under Validation Scheme, select Holdout Validation. Select percentage of data to be used as a validation set.

The app trains a model on the training set and assesses its performance with the validation set. The model used for validation is based on only a portion of the data, so Holdout Validation is recommended only for large data sets. The final model is trained with the full data set. In this example, Percent held out is set to 30. This value indicates that 70% of the data is used for training the machine learning algorithm.

Click Start Session.

Classification Learner app loads the data set and plots a scatter plot of the first two features. The X and Y fields of Predictors can be changed to different values to see the correlation between different features.

On the app toolstrip, in the Train section, click Train All and select Train Selected. When the training is complete, the Models pane displays the accuracy of the validation scheme. For this example the accuracy is 97.2%.

To see the Validation Confusion Matrix, in the Plots and Results section of the app toolstrip, click Confusion Matrix (Validation).

To export the model, on the app toolstrip, in the Export section click Export Model.

In the Export Classification Model dialog box, clear the check box to exclude the training data and export a compact model, and then click OK.

The structure trainedModel appears in the MATLAB workspace. Save the model using the following command.

save('modeTrainedModel.mat','trainedModel');

Create ThingSpeak Channel and Add Widgets to monitor the state of the device

ThingSpeak is an IoT analytics platform service that you can use to aggregate, visualize, and analyze live data streams in the cloud. For more information, see Get Started with ThingSpeak (ThingSpeak).

To monitor the device using ThingSpeak™, sign in using your MathWorks® Account credentials, or create a new account. Create a new channel in ThingSpeak to collect and analyze the data from the device. To create a new channel in ThingSpeak, on the Channels tab, click My Channels. In the My Channels window, click New Channel and enter these values to customize the channel.

  1. Name: Machine Monitoring

  2. Field 1: Mode

  3. Field 2: Acceleration Along X-axis

  4. Field 3 : Acceleration Along Y-axis

  5. Field 4 : Acceleration Along Z-axis

  6. Field 5 : Abnormality

ThingSpeak uses the Field 5 to track if any abnormality is detected that is when the machine is running in Block Mode or RotorImbalanced Mode.

Click Save Channel to save the channel settings. In the Channels window, click Add Visualizations to add graphs for monitoring the mode, acceleration along X-, Y-, and Z- axes. Click Add Widgets to add the Lamp Indicator for detecting any abnormality.

Identify API Keys in ThingSpeak

Identify the API keys of the ThingSpeak channel to read data from and write data to your channel.

To identify the API keys, on the Channels tab, click API Keys.

Predict and Write Data to ThingSpeak

Run this MATLAB code to predict the mode of operation of the rotating device and publish the data on the ThingSpeak channel. In this MATLAB code, the machine is being monitored for 180 seconds. You can set an infinite while loop to continuously monitor the rotation device.

% Create an arduino object for ESP32 board over WiFi
aObj = arduino;

% Create IMU sensor object
imuObj = lsm9ds1(aObj,"SamplesPerRead",50);

% ThingSpeak Configuration

% channel id 
channelId = 2392616;

% Write key 
writeKey = '9V0CRQ5MLJTWM3J0';


% Predict the mode
tic;
while(toc<60)
    
    % Read the sensor values and extract the features
    accelerationValues = read(imuObj);
    accelerationValues = accelerationValues.("Acceleration");
    [features,featuresX,featuresY,featuresZ]  = extractModeFeatures(accelerationValues(:,1),accelerationValues(:,2),accelerationValues(:,3));

    % Predict mode
    abnormality = 0;
    y = predictMode(features);

    disp(['Detected Mode = ' modeList{y}]);
    if(y==3 || y==4)
        abnormality = 1;
    end

    % Publishing data to the ThingSpeak
    thingSpeakWrite(channelId,[y,featuresX,featuresY,featuresZ,abnormality],'WriteKey','9V0CRQ5MLJTWM3J0','Fields',[1,2,3,4,5]);
    pause(0.5);

end
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Block
Detected Mode = Block
Detected Mode = Block
Detected Mode = Block
Detected Mode = Stop
Detected Mode = Block
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Normal
Detected Mode = Block
Detected Mode = Stop
Detected Mode = Block
Detected Mode = Block
Detected Mode = Stop
Detected Mode = Block
Detected Mode = Block
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop
Detected Mode = Stop

This image shows the channel dashboard after running the rotating device in differnt modes and executing the above code.

Set Up Email Alert

Configure ThingSpeak to alert you via email if abnormality is detected. ThingSpeak sends the email to the email address associated with your ThingSpeak account.

To set up the email alert, on the Apps tab on the ThingSpeak toolstrip, click MATLAB Analysis.

Click New to select a code template.

Select the Custom (no starter code) option and then click Create to create the template.

Set the Name to AbnormalityAlert and enter the code in the MATLAB Code window or copy the sample code from the AbnormalityAlert.m file provided with this example. To access the AbnormalityAlert.m file, click the Open Live Script button and download the file.

Specify the channel ID, Read API key, and Alerts API key corresponding to your channel in code. You can access the alerts key from your ThingSpeak profile page.

To set up an email alert for when any abnormality is detected, on the Apps tab in ThingSpeak, click Actions, and then click React. In the React window, set these parameters.

  • React Name — Enter a unique name for your alert. For this example, set it to AbnormalTrigger.

  • Condition Type — Select Numeric.

  • Test Frequency — Select On Data Insertion.

  • Condition — Select the name of your channel and the field and set the condition for your alert. In this example, select field 5 (Abnormality) and set the condition to is equal to 1.

  • Action — Select MATLAB Analysis. In the code to execute list, select the name of the MATLAB file where you stored the analysis code. For this example, select AbnormalityAlert.

  • Options — Select Run action only the first time the condition is met.

Click Save React to save your alert.

When any abnormality is detected, ThingSpeak sends an email to the registered email address. This is an image of a sample email sent by ThingSpeak.