This project demonstrates running a trained neural network (MNIST digit classifier) on an ESP8266 microcontroller. The neural network is trained in Python, converted to C-compatible format, and deployed on the ESP8266 to perform real-time digit classification.
The project consists of three main components:
- Python-based neural network training and weight conversion
- ESP8266 firmware for inference
- Client-server communication over WiFi
- ESP8266 Generic Module
- USB cable for programming and power
- WiFi network access
convert.ipynb: Jupyter notebook for training the neural network and converting weights to C formatpredict.ipynb: Jupyter notebook for testing the model- Generated weight files:
mnist_model_weights.h: Original weight filemnist_model_weights_int.h: Quantized weights for ESP8266
NNonEsp32.ino: Main Arduino sketch implementing:- WiFi server setup
- Neural network inference
- Client communication handling
The implemented neural network has the following structure:
- Input layer: 784 neurons (28x28 MNIST image)
- Hidden layer: 32 neurons with ReLU activation
- Output layer: 10 neurons (digits 0-9) with softmax activation
Make Sure the folder name is NNonEsp32
-
Configure WiFi Settings
- Open
NNonEsp32.ino - Update the WiFi credentials:
const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD";
- Open
-
Upload the Firmware
- Open the Arduino IDE
- Select the ESP8266 board
- Upload the
NNonEsp32.inosketch
-
Connect to the Server
- The ESP8266 will create a TCP server on port 5000
- Note the IP address displayed in the Serial Monitor
The server expects data in the following format:
- 2-byte header indicating the number of pixel pairs
- For each pixel pair:
- 2 bytes: pixel index
- 1 byte: pixel value (0-255)
The server responds with a JSON object containing:
predicted_label: The classified digit (0-9)probabilities: Array of probabilities for each digit
- Weights are quantized to 8-bit integers for memory efficiency
- The model uses a scale factor of 0.01 for weight conversion
- Inference is performed in real-time on the ESP8266
- ESP8266WiFi library
- Arduino core for ESP8266
- MNIST dataset
- ESP8266 community
- Arduino platform