Kindly refer to last article to learn how to send factory data to HiveMQ Cloud. This article continues from there to further send data to ML for anomaly detection.
System Overview
This architecture demonstrates how vibration data from a machine is collected at the OT (Operational Technology) level, transported securely to the Cloud, evaluated using an AI model, and returned to the shop-floor for further action. It consists of:- Input Layer – Reading vibration data from a PLC (via OPC UA)
- Transport Layer – Sending data from HiveMQ Edge → HiveMQ Cloud (via MQTT)
- AI Layer – ML server running Python + Flask + PyOD locally on the computer
- Output Layer – Returning anomaly alerts via MQTT → HiveMQ Edge → PLC (optional)
Step-by-Step Breakdown of the Pipeline
1. Input Layer — PLC to HiveMQ Edge (OPC UA)
- A Siemens S7-1500 PLC measures vibration (simulated) from the machine.
- This data is exposed through OPC UA.
- HiveMQ Edge connects to the PLC’s OPC UA server.
- It maps the OPC UA tag (e.g.,
vibration) to an MQTT topic:
Industrial data becomes IIoT data.
2. Transport Layer — HiveMQ Edge to HiveMQ Cloud (MQTT)
HiveMQ Edge publishes the MQTT message (machine/s71500/vib) to HiveMQ Cloud, which acts as a central cloud broker.
Benefits:
- Low latency
- Very small message size → suitable for industrial IoT
- Secure TLS transport and easy to scale
Learn more about that in our earlier posts on HiveMQ where you will learn the complete workflow: PLC to Cloud via HiveMQ (Edge + Cloud) — OPC UA → MQTT → Confluent
- AI Layer — ML server running Python + Flask + PyOD locally on the compute
This is where the anomaly detection happens. Let’s first understand what is Anomaly detection and why we need it
What is Anomaly detection?
Anomaly detection is the process of identifying data points or behavior that deviate from what’s considered normal. In simple terms, it spots when something unusual is happening. In Machine Learning, anomaly detection helps models understand patterns and flag unexpected behaviour without needing labels. In IIoT, it’s essential because machines generate huge amounts of real-time data, and even small deviations can signal issues like equipment failure, quality defects, cyber-attacks, or unsafe operating conditions.Why we need it:
- Detect problems early before they become costly
- Improve uptime and reliability
- Enhance safety and Reduce maintenance costs
PyOD — Python Outlier Detection Library
PyOD is a widely-used ML library that provides 40+ algorithms for anomaly detection, such as:- Isolation Forest (IForest)
- AutoEncoders
- One-Class SVM
- LOF (Local Outlier Factor)
- 0 → Normal
- 1 → Anomaly
Installing Pyod and testing Anomaly detection
In our example, the Pyod has been installed in the windows computer. The following are the steps:1
Install pyod on Windows PC
Make sure Python is installed in your computer. In our case, we are using Python 3. To install Python visit here: https://www.python.org/downloads/
2
Create a python file for testing:
test_pyod.py3
Add the following code in that
4
Run the Python file
5
Validate the output
‘Anomaly detected’ or ‘Normal’ based on the value sent in the code (0.45)
Anomaly detection using Pyod and Node-RED
Now, we are going to test the code using Node-RED. In this case, we setup a flask server in Node-RED so that we can execute the Python code using API. This makes it easy to send data to the Python file and get anomaly results as feedback.Make sure latest version of Node-RED and Python is installed in your system.
- To install Node-RED visit here: https://nodered.org/
- To install Python visit here: https://www.python.org/downloads/
1
Convert Your Python Code into a Simple API Server.
We will not run Python file directly from Node-RED, we will create a Python web API for data exchange.Create a new Python file named ‘server.py’ with the following code:
2
Install Flask
3
Run your Python Server
4
Validate
You should see the following:
Your anomaly detection API is now LIVE at: http://localhost:5000/predict

5
Start Node-RED and test the Anomaly detection
You can use the ‘http request node’ in Node-RED to send sample data and fetch anomaly results as shown below.
Kindly check the reference Node-RED flow below:

Testing Anomaly detection with PLC Data
Now, let’s use the above example and sends the PLC data to the Python server to get anomaly results.For the sake of demonstration, we are considering the normal values within the range and anomalies outside this range. So, in our Python code, we have created a dataset of samples in the range of to . Our model will learn from this dataset, as shown in the code below.
1
Update your Python code in the server.py file
The code trains an anomaly detection model, exposes it through a REST API, and lets you retrain the model using real machine data.This script creates a small AI-powered anomaly detection API using Flask and the PyOD Isolation Forest model.
- It generates normal vibration data (0–5 mm/s) and uses it to train an Isolation Forest model.
- It scales all vibration values using StandardScaler so the model can understand them correctly.
- It exposes a
/predictAPI endpoint where you send one vibration value, and the server returns:- Normal or Anomaly
- Anomaly score
- Scaled value
- It provides a
/retrainendpoint that allows you to send new vibration samples (from Node-RED or a PLC) and retrain the model on the fly. - Flask runs the server on port 5000, making it easy to integrate with IIoT systems.
2
Update the Node-RED flow
Now we need to subscribe the following HiveMQ Cloud topic to get the LIVE vibration data:

Every time vibration data arrives from HiveMQ Cloud → the ML server receives it, runs inference, and give the result back to Node-RED
4. Output Layer – Returning anomaly alerts via MQTT → HiveMQ Edge → PLC (optional)
Now we need to send the result back to HiveMQ Edge, visualize it on the dashboard or sends to the PLC for further action.1
Add MQTT out node in the flow
We will take another MQTT topic to filter and publish the result
The following flow can be used as a reference.

2
Bridge the HiveMQ Cloud MQTT topic with HiveMQ Edge MQTT topic
Go to MQTT bridge in HiveMQ Edge and add a remote subscription in your current bridge as shown below:

Now navigate to Broker Configuration and ‘Enable loop prevention’. This will prevent looping of data flow



3
Visualize Anomaly in HiveMQ Edge
Now that the anomaly arrives to MQTT Edge, you can visualize it with tools like Node-RED as shown below:



You can further sends the anomaly results back to PLC via OPC UA.