Edge Computing : TPH Anomaly Detection using Multilayer Neural Network on ESP32


Edge computing is one of the actively growing technology worldwide. Developers are coming-up with new better performing ideologies to reduce the computational overhead on the centralised servers. EDGE-C provides Reduced latency, Less Overhead on network, Increase Availability, Enhanced Security and many more depending on the use cases.

In this article, we will build a machine learning model to detect the anomaly in the data read from the sensors. In this project we will read data for Temperature, Pressure and Humidity from BME280 Sensor and figure out if there is a anomaly in the data.


  1. Software Requirements:
  • Install TensorFlow library

2. Hardware Requirements

  • ESP32 Board
  • BME280 Sensor

3. Download Source Code


We’ll start the process by TensorFlow Keras Sequential model, after which we’ll be converting it into TensorFlow Lite which is specially built for microcontrollers. Then we will generate a C Array from TensorFlow Lite converter. This C Array holds the trained model which will be deployed to the edge devices.


  1. Building Labeled Dataset using Kmean++ Clustering

Firstly we will build a labeled dataset. By taking the unlabelled dataset which is recorded from the sensors and fit the KMeans clustering Model. Then we use this model data to generate a labeled dataset which contains the target variable “anomaly” and independent variables ‘humidity’, ‘pressure’ and ‘temperature’.

We will import the necessary packages and the raw dataset.

On applying WCSS to only humidity data to figure out the optimal number of clusters.

From the above figure we can find that for the Humidity data we can have three optimal clusters. On applying kmeans++ clustering to the humidity data, we can visualise the data as below and conclude that the green cluster is at the extreme case, which are supposed to detect when the sensor produces a value that fits in this cluster.

We will do the same thing for the pressure and temperature data and find the optimal number of clusterings and find the dissimilarities in the data and group them. You can find the code for the other two in the GitHub repo link below.


The WCSS graph for pressure and temperature graph is as follows.

On applying Kmean++ on both the data we can visualize the graphs for pressure and temperature as below.

We have figured out the anomalies in data and clustered them. Now with this data we will create a fresh new labeled dataset that contains only humidity, pressure, temperature and anomaly as columns.

Now the Final Labeled Dataset is Ready…

2. Building ANN Model using Tensor flow

Let’s import the necessary modules

Read the labeled dataset from the CSV file generated in the previous step.

From Keras sequential, create a neural network model with 2 layers of 3 neurons with ‘relu’ activation function, and one output layer with “sigmoid” activation function.Then train the model via ‘fit’ function by passing ‘x’ and ‘y’ values to the model. After training, store the model to a file with ‘save’ function as shown below.

Finally we have created and trained our Neural Network Model using Tensorflow Keras.

3. Converting TF Model to TF Lite Model

Now convert the saved TF model to TF Lite and save it as a file with extension ‘.tflite’. For optimization let us use ‘tf.lite.Optimize.DEFAULT’ to avoid errors.

4. Generating TF Lite Model to C Array

Use a Linux command ‘xxd’ to convert TF Lite Model to C Array. This command converts a file or data to its equivalent hexadecimal format. This is known as “hex dump”.

The ‘.tflite’ file generated in the previous step will go as input to the ‘xxd’ command and for the output, we will specify the filename with extension. Here, I will be specifying ‘.h’ as the file extension. You could use other formats like ‘.cc’.

xxd -i TF-Model/TPH_Anomaly_Predictor.tflite > TPH_Anomaly_Predictor.h

We are done with converting the tensor flow lite model to C header file.

5. Deploying to Edge Device

  • Circuit Connection
                        ESP32     BME280
5V — VCC
D21 — SDA
D22 - SCL

Use the above guide to connect ESP32 and BME280. After this write C code to include our model header file and detect anomaly in the data read by the sensor.

Import the “TPH_Anomaly_Predictor.h” header file generated in the previous step. This file should be stored in the same directory where ‘.ino’ the file is created. We should also import other required header files and define variables for reading the data from sensors.

create a TensorFlowlite library instance as shown in the below code.

Then read the data from the sensor and pass it to the model and obtain the prediction. If the prediction is greater than 0.5, then the data received from the sensor is a defective value.


This is how we can use tensor flow lite to deploy a machine learning model to a microcontroller and reduce the overhead on the servers. In this use case we can filters out the missing value and defective values and only store the clean data to the servers. Which will be ready for processing.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store