Hello,
I am creating a project on "ANN-based Water Quality Monitoring" with Arduino UNO and ESP32. As a part of this project, I am using the TensorFlowLite_ESP32 library to access my ANN model "water_quality_monitoring.h". The following code is to be uploaded into my ESP32 devboard.
#include <TensorFlowLite_ESP32.h>
#include "water_quality_model.h" // Model header file
// Micro TensorFlow Lite (TFLM) Dependencies
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/micro/all_ops_resolver.h"
#define NUM_FEATURES 4 // Temp, pH, TDS, Turbidity
#define RX2 16
#define TX2 17
// TensorFlow Lite Model Setup
constexpr int tensor_arena_size = 8 * 1024; // Memory allocation for TFLM
uint8_t tensor_arena[tensor_arena_size];
tflite::MicroErrorReporter error_reporter;
const tflite::Model* tflite_model;
tflite::MicroInterpreter* interpreter;
TfLiteTensor* input;
TfLiteTensor* output;
// Water quality parameters
float temp, ph, tds, turbidity;
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RX2, TX2); // ESP32 receives data from Arduino Uno via Serial2
// Load TensorFlow Lite Model
tflite_model = tflite::GetModel(water_quality_model);
if (tflite_model->version() != TFLITE_SCHEMA_VERSION) {
Serial.println("Model version mismatch!");
return;
}
// Set up TFLite interpreter with Micro TensorFlow Lite
static tflite::AllOpsResolver resolver;
static tflite::MicroInterpreter static_interpreter(tflite_model, resolver, tensor_arena, tensor_arena_size, &error_reporter);
interpreter = &static_interpreter;
interpreter->AllocateTensors();
input = interpreter->input(0);
output = interpreter->output(0);
Serial.println("Micro TensorFlow Lite Model Initialized.");
}
void loop() {
// Read water parameters from Arduino Uno (Assuming comma-separated values)
if (Serial2.available()) {
String data = Serial2.readStringUntil('\n');
sscanf(data.c_str(), "%f,%f,%f,%f", &temp, &ph, &tds, &turbidity);
Serial.print("Received Data: ");
Serial.println(data);
// Set input tensor values
input->data.f[0] = temp;
input->data.f[1] = ph;
input->data.f[2] = tds;
input->data.f[3] = turbidity;
// Run inference
interpreter->Invoke();
// Retrieve classification results
int usage = static_cast<int>(output->data.f[0]);
int composition = static_cast<int>(output->data.f[1]);
int source = static_cast<int>(output->data.f[2]);
// Print results to Serial Monitor
Serial.println("Classification Results:");
Serial.print("Usage: ");
switch (usage) {
case 0: Serial.println("DRINKING WATER"); break;
case 1: Serial.println("INDUSTRIAL WATER"); break;
case 2: Serial.println("AGRICULTURAL WATER"); break;
case 3: Serial.println("WASTEWATER"); break;
default: Serial.println("UNKNOWN"); break;
}
Serial.print("Composition: ");
switch (composition) {
case 0: Serial.println("FRESHWATER"); break;
case 1: Serial.println("BRACKISH WATER"); break;
case 2: Serial.println("SALINE WATER"); break;
case 3: Serial.println("HARD WATER"); break;
case 4: Serial.println("SOFT WATER"); break;
default: Serial.println("UNKNOWN"); break;
}
Serial.print("Source: ");
switch (source) {
case 0: Serial.println("SURFACE WATER"); break;
case 1: Serial.println("GROUNDWATER"); break;
case 2: Serial.println("RAINWATER"); break;
case 3: Serial.println("SEAWATER"); break;
default: Serial.println("UNKNOWN"); break;
}
Serial.println("-----------------------------------------");
}
delay(1000); // Wait before processing the next reading
}
After compiling, I get the following error(s):
In file included from e:\Arduino 2\PROGRAM FILES\libraries\TensorFlowLite_ESP32\src/third_party/flatbuffers/array.h:21,
from e:\Arduino 2\PROGRAM FILES\libraries\TensorFlowLite_ESP32\src/third_party/flatbuffers/flatbuffers.h:22,
from e:\Arduino 2\PROGRAM FILES\libraries\TensorFlowLite_ESP32\src/tensorflow/lite/micro/micro_interpreter.h:21,
from E:\Arduino 2\PROGRAM FILES\ESP32_WATER_QUALITY_WIFI2\ESP32_WATER_QUALITY_WIFI2.ino:5:
e:\Arduino 2\PROGRAM FILES\libraries\TensorFlowLite_ESP32\src/third_party/flatbuffers/stl_emulation.h: In member function 'constexpr flatbuffers::span<T, Extent>& flatbuffers::span<T, Extent>::operator=(const flatbuffers::span<T, Extent>&)':
e:\Arduino 2\PROGRAM FILES\libraries\TensorFlowLite_ESP32\src/third_party/flatbuffers/stl_emulation.h:386:12: error: assignment of read-only member 'flatbuffers::span<T, Extent>::count_'
386 | count_ = other.count_;
| ~~~~~~~^~~~~~~~~~~~~~
exit status 1
Compilation error: exit status 1
Please let me know what causes the error and how it can be solved.
Thank you.
Best regards,
Sai Mukul
