HiveMQ Edge-to-Cloud AI Pipeline
Anomaly Detection using HiveMQ Cloud, Python (PyOD), and Flask
This article explains the full data pipeline shown in the diagram starting from a PLC on the shopfloor, sending data through HiveMQ Edge and Cloud, into an AI model built with Python + PyOD, and finally sending the anomaly result back to the HiveMQ Edge for visualization.
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
3. 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)
In the example, Isolation Forest (IForest) is used. It learns what βnormal vibrationβ looks like from training data:
When new data comes, PyOD predicts:
0 β Normal
1 β Anomaly
Learn more about Pyod here: https://pyod.readthedocs.io/en/latest/
Installing Pyod and testing Anomaly detection
In our example, the Pyod has been installed in the windows computer. The following are the steps:
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.
Once, the Node-RED is installed proceed with the following steps:
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.
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.
Learn more about this code here: Anomaly detection Code Explanation
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.
Complete Workflow
β₯οΈ Work With Me
I regularly test industrial automation and IIoT devices. If youβd like me to review your product or showcase it in my courses and YouTube channel:
π§ Email: [email protected] or drop me a message on LinkedIn
Last updated
Was this helpful?








