Enthernet Code
DIIDevHeads IoT Integration Server
•Created by Enthernet Code on 6/27/2024 in #middleware-and-os
Can i use Edge Impulse to collect data, train a model and deploy it on an Arduino Nano 33 BLE Sense
float x, y, z;
static float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };
static int feature_index = 0;
// Read data from the accelerometer
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
// Fill the feature array with the collected data
features[feature_index++] = x;
features[feature_index++] = y;
features[feature_index++] = z;
// Reset the index if it exceeds the frame size
if (feature_index >= EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) {
feature_index = 0;
}
}
// Assign the features array to the signal data
signal->total_length = EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE;
signal->get_data = [](size_t offset, size_t length, float *out_ptr) -> int {
memcpy(out_ptr, features + offset, length * sizeof(float));
return 0;
};
}
float runInference() {
ei_impulse_result_t result = { 0 };
// Run the Edge Impulse model and get the result
if (ei_run_impulse(&result) != EI_IMPULSE_OK) {
Serial.println("Failed to run Edge Impulse model");
return 0.0;
}
// Return the anomaly score from the result
return result.anomaly;
}
void loop() {
ei::signal_t signal;
// Collect data
collectData(&signal);
// Run anomaly detection
float anomalyScore = runInference();
// Check if the score is above a threshold indicating an anomaly
if (anomalyScore > THRESHOLD) {
// Handle anomaly (e.g., turn on an LED, send a notification)
Serial.println("Anomaly detected!");
digitalWrite(LED_BUILTIN, HIGH); // Turn on the built-in LED
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn off the built-in LED
}
// Small delay to prevent excessive looping
delay(1000);
}
float x, y, z;
static float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };
static int feature_index = 0;
// Read data from the accelerometer
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
// Fill the feature array with the collected data
features[feature_index++] = x;
features[feature_index++] = y;
features[feature_index++] = z;
// Reset the index if it exceeds the frame size
if (feature_index >= EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) {
feature_index = 0;
}
}
// Assign the features array to the signal data
signal->total_length = EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE;
signal->get_data = [](size_t offset, size_t length, float *out_ptr) -> int {
memcpy(out_ptr, features + offset, length * sizeof(float));
return 0;
};
}
float runInference() {
ei_impulse_result_t result = { 0 };
// Run the Edge Impulse model and get the result
if (ei_run_impulse(&result) != EI_IMPULSE_OK) {
Serial.println("Failed to run Edge Impulse model");
return 0.0;
}
// Return the anomaly score from the result
return result.anomaly;
}
void loop() {
ei::signal_t signal;
// Collect data
collectData(&signal);
// Run anomaly detection
float anomalyScore = runInference();
// Check if the score is above a threshold indicating an anomaly
if (anomalyScore > THRESHOLD) {
// Handle anomaly (e.g., turn on an LED, send a notification)
Serial.println("Anomaly detected!");
digitalWrite(LED_BUILTIN, HIGH); // Turn on the built-in LED
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn off the built-in LED
}
// Small delay to prevent excessive looping
delay(1000);
}
15 replies