Created
March 15, 2024 15:26
-
-
Save rdlauer/82a9ba83bd59e2ac04e0c4a6804abe38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <NanoEdgeAI.h> | |
#include <Adafruit_LIS3DH.h> | |
#include <Notecard.h> | |
Adafruit_LIS3DH lis = Adafruit_LIS3DH(); | |
#define LEARNING_ITERATIONS 30 | |
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values | |
#define PRODUCT_UID "<your-product-uid>" | |
Notecard notecard; | |
static const unsigned long REFRESH_INTERVAL = 15000; // ms | |
static unsigned long lastRefreshTime = 0; | |
void fill_buffer(float input_buffer[]) | |
{ | |
for (size_t i = 0; i < 16; i++) | |
{ | |
lis.read(); // get x,y,z data at once | |
input_buffer[AXIS_NUMBER * i] = lis.x; | |
input_buffer[(AXIS_NUMBER * i) + 1] = lis.y; | |
input_buffer[(AXIS_NUMBER * i) + 2] = lis.z; | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
while (!Serial) | |
delay(10); | |
notecard.begin(); | |
J *req = notecard.newRequest("hub.set"); | |
JAddStringToObject(req, "product", PRODUCT_UID); | |
JAddStringToObject(req, "mode", "continuous"); | |
JAddNumberToObject(req, "duration", 1); | |
notecard.sendRequestWithRetry(req, 5); | |
if (!lis.begin(0x18)) | |
{ | |
Serial.println("Couldnt start"); | |
while (1) | |
yield(); | |
} | |
Serial.println("LIS3DH found!"); | |
enum neai_state error_code = neai_anomalydetection_init(); | |
if (error_code != NEAI_OK) | |
{ | |
Serial.println("Board not supported!"); | |
} | |
for (uint16_t iteration = 0; iteration < LEARNING_ITERATIONS; iteration++) | |
{ | |
fill_buffer(input_user_buffer); | |
neai_anomalydetection_learn(input_user_buffer); | |
Serial.print("Learning: "); | |
Serial.print(iteration); | |
Serial.print("/"); | |
Serial.print(LEARNING_ITERATIONS); | |
Serial.println(); | |
} | |
} | |
void loop() | |
{ | |
uint8_t similarity = 0; | |
fill_buffer(input_user_buffer); | |
neai_anomalydetection_detect(input_user_buffer, &similarity); | |
if (similarity >= 90) | |
{ | |
Serial.print("NORMAL: "); | |
} | |
else | |
{ | |
Serial.print("ANOMALY: "); | |
} | |
Serial.print(similarity); | |
Serial.print("/100"); | |
Serial.println(); | |
if (millis() - lastRefreshTime >= REFRESH_INTERVAL) | |
{ | |
J *req = notecard.newRequest("note.add"); | |
if (req != NULL) | |
{ | |
JAddStringToObject(req, "file", "nanoedge.qo"); | |
JAddBoolToObject(req, "sync", true); | |
J *body = JAddObjectToObject(req, "body"); | |
if (body) | |
{ | |
JAddNumberToObject(body, "similarity", similarity); | |
} | |
notecard.sendRequest(req); | |
} | |
lastRefreshTime += REFRESH_INTERVAL; | |
} | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment