Andrea Ouyang
Published

TinyML: Simple DIY Burglar Alarm Project Using Wio Terminal

Use Wio Terminal + Tiny ML to make a Drawer Burglar Alarm Project easily.

BeginnerFull instructions provided86

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1

Software apps and online services

Codecraft
Seeed Studio Codecraft

Story

Read more

Schematics

diagram

Code

program

Arduino
#include"TFT_eSPI.h"
#include <project_162115_inferencing.h>
#include"LIS3DHTR.h"

TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
#define CONVERT_G_TO_MS2    9.80665f
ei_impulse_result_classification_t currentClassification[EI_CLASSIFIER_LABEL_COUNT];
const char* maxConfidenceLabel;

void runClassifier()
{
  float buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };
  for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += 3) {
    uint64_t next_tick = micros() + (EI_CLASSIFIER_INTERVAL_MS * 1000);
    lis.getAcceleration(&buffer[ix], &buffer[ix + 1], &buffer[ix + 2]);
    buffer[ix + 0] *= CONVERT_G_TO_MS2;
    buffer[ix + 1] *= CONVERT_G_TO_MS2;
    buffer[ix + 2] *= CONVERT_G_TO_MS2;
    delayMicroseconds(next_tick - micros());
  }
  signal_t signal;
  int err = numpy:: signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
  ei_impulse_result_t result = { 0 };

  err = run_classifier(&signal, &result, false);
  float maxValue = 0;
  for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
    ei_impulse_result_classification_t classification_t = result.classification[ix];
    ei_printf("    %s: %.5f\n", classification_t.label, classification_t.value);
    float value = classification_t.value;
    if (value > maxValue) {
      maxValue = value;
      maxConfidenceLabel = classification_t.label;
    }
    currentClassification[ix] = classification_t;
  }
}

void setup(){
  tft.begin();
  lis.begin(Wire1);
  lis.setOutputDataRate(LIS3DHTR_DATARATE_100HZ);
  lis.setFullScaleRange(LIS3DHTR_RANGE_4G);

  pinMode(WIO_LIGHT, INPUT);
  pinMode(WIO_BUZZER, OUTPUT);
  tft.setRotation(3);
  tft.fillScreen(0xFF7D);

}



void loop(){

  runClassifier();
  if ((maxConfidenceLabel == "idle" || ((analogRead(WIO_LIGHT) < 10)))) {
    tft.fillScreen(0x17E2);
    tft.setTextSize(3);
    tft.drawString((String)"Normal", 90, 90);
  } else {
    tft.fillScreen(0xF882);
    tft.setTextSize(3);
    tft.drawString((String)"Warning", 90, 90);
    tone(WIO_BUZZER,587);
    delay(1000);
    noTone(WIO_BUZZER);
  }

}

Credits

Andrea Ouyang
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.