Lucía Camacho TiembloSpiros Kotsikosmaria alifieri
Published

Who knocks my door?

Device for people with hearing problems which allows them to be aware of when somebody knocks their door.

IntermediateProtipOver 2 days357
Who knocks my door?

Things used in this project

Hardware components

Arduino Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Android device
Android device
×1

Software apps and online services

MIT App Inventor
MIT App Inventor
Arduino IDE
Arduino IDE
Edge Impulse Studio
Edge Impulse Studio

Story

Read more

Schematics

Edge Impulse Model

This is the machine learning model used for this project.

Application Project File - Code

This is the application's project file. Create an account with MIT Inventor and import it.

Application File

This is the application's file. Download it to your mobile phone, install and run the app.

Code

Arduino Code

Arduino
This is the code used to run the machine learning model and bluetooth connectivity on your Arduino Nano 33 BLE.
#include <ArduinoBLE.h>
#include <knocking_door_project_inferencing.h>
#include <PDM.h>

/** Audio buffers, pointers and selectors */
typedef struct {
    int16_t *buffer;
    uint8_t buf_ready;
    uint32_t buf_count;
    uint32_t n_samples;
} inference_t;

static inference_t inference;
static signed short sampleBuffer[2048];
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal

BLEDevice central;
BLEService yourService("cb006965-29ec-4727-92cd-5ad8bdc2d3c7");
BLEUnsignedCharCharacteristic yourClass("154c7f82-d8bf-4589-86ec-61c77d820763", BLERead | BLENotify);

  int detectedClassValue = 0;
  int detectedClass = 0;

  int conversation;
  int doorbell;
  int house_silence;
  int knocking_door;
  int tv_sound;

/**
 * @brief      Arduino setup function
 */
void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);

    Serial.println("Edge Impulse Inferencing Demo");

    // summary of inferencing settings (from model_metadata.h)
    ei_printf("Inferencing settings:\n");
    ei_printf("\tInterval: %.2f ms.\n", (float)EI_CLASSIFIER_INTERVAL_MS);
    ei_printf("\tFrame size: %d\n", EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
    ei_printf("\tSample length: %d ms.\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT / 16);
    ei_printf("\tNo. of classes: %d\n", sizeof(ei_classifier_inferencing_categories) / sizeof(ei_classifier_inferencing_categories[0]));

    if (microphone_inference_start(EI_CLASSIFIER_RAW_SAMPLE_COUNT) == false) {
        ei_printf("ERR: Failed to setup audio sampling\r\n");
        return;
    }

    if (!BLE.begin()) {
      Serial.println("starting BLE failed!");
      while (1);
    }
    
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(LEDB, OUTPUT);
    pinMode(LEDR, OUTPUT);

    BLE.setDeviceName("knocking_door"); 
    BLE.setLocalName("knocking_door");
    BLE.setAdvertisedService(yourService);
    yourService.addCharacteristic(yourClass);
    BLE.addService(yourService); 
    BLE.advertise();

    Serial.println("Bluetooth device active, waiting for connections...");

    while (1) {
      central = BLE.central();
      if (central) {
        Serial.print("Connected to central: ");
        Serial.println(central.address());
        digitalWrite(LED_BUILTIN, HIGH);
        break;
      }
    }
}

/**
 * @brief      Arduino main function. Runs the inferencing loop.
 */
void loop()
{
    ei_printf("Starting inferencing in 2 seconds...\n");

    delay(2000);

    ei_printf("Recording...\n");

    bool m = microphone_inference_record();
    if (!m) {
        ei_printf("ERR: Failed to record audio...\n");
        return;
    }

    ei_printf("Recording done\n");

    signal_t signal;
    signal.total_length = EI_CLASSIFIER_RAW_SAMPLE_COUNT;
    signal.get_data = &microphone_audio_signal_get_data;
    ei_impulse_result_t result = { 0 };

    EI_IMPULSE_ERROR r = run_classifier(&signal, &result, debug_nn);
    if (r != EI_IMPULSE_OK) {
        ei_printf("ERR: Failed to run classifier (%d)\n", r);
        return;
    }

    // print the predictions
    ei_printf("Predictions ");
    ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)",
        result.timing.dsp, result.timing.classification, result.timing.anomaly);
    ei_printf(": \n");
    for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
        ei_printf("    %s: %.5f\n", result.classification[ix].label, result.classification[ix].value);

        if (result.classification[ix].value > detectedClassValue) {
           detectedClassValue = result.classification[ix].value;
           detectedClass = ix;
        }
    }

    conversation = result.classification[0].value*100;
    doorbell = result.classification[1].value*100;
    house_silence = result.classification[2].value*100;
    knocking_door = result.classification[3].value*100;
    tv_sound = result.classification[4].value*100;

    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDR, HIGH);
    
    if (knocking_door > 30) {
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);
        delay(200);
        digitalWrite(LEDB, HIGH);
        delay(100);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);
        delay(200);
        digitalWrite(LEDB, HIGH);
        delay(100);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, HIGH);
        delay(200);
        digitalWrite(LEDB, HIGH);
        delay(100);
    }

    if (doorbell > 30) {
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, LOW);
        delay(200);
        digitalWrite(LEDG, HIGH);
        delay(100);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, LOW);
        delay(200);
        digitalWrite(LEDG, HIGH);
        delay(100);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDR, HIGH);
        digitalWrite(LEDG, LOW);
        delay(200);
        digitalWrite(LEDG, HIGH);
        delay(100);
    }
    
    if (central.connected()) yourClass.writeValue(detectedClass);
    
#if EI_CLASSIFIER_HAS_ANOMALY == 1
    ei_printf("    anomaly score: %.3f\n", result.anomaly);
#endif
}

/**
 * @brief      PDM buffer full callback
 *             Get data and call audio thread callback
 */
static void pdm_data_ready_inference_callback(void)
{
    int bytesAvailable = PDM.available();

    // read into the sample buffer
    int bytesRead = PDM.read((char *)&sampleBuffer[0], bytesAvailable);

    if (inference.buf_ready == 0) {
        for(int i = 0; i < bytesRead>>1; i++) {
            inference.buffer[inference.buf_count++] = sampleBuffer[i];

            if(inference.buf_count >= inference.n_samples) {
                inference.buf_count = 0;
                inference.buf_ready = 1;
                break;
            }
        }
    }
}

/**
 * @brief      Init inferencing struct and setup/start PDM
 *
 * @param[in]  n_samples  The n samples
 *
 * @return     { description_of_the_return_value }
 */
static bool microphone_inference_start(uint32_t n_samples)
{
    inference.buffer = (int16_t *)malloc(n_samples * sizeof(int16_t));

    if(inference.buffer == NULL) {
        return false;
    }

    inference.buf_count  = 0;
    inference.n_samples  = n_samples;
    inference.buf_ready  = 0;

    // configure the data receive callback
    PDM.onReceive(&pdm_data_ready_inference_callback);

    PDM.setBufferSize(4096);

    // initialize PDM with:
    // - one channel (mono mode)
    // - a 16 kHz sample rate
    if (!PDM.begin(1, EI_CLASSIFIER_FREQUENCY)) {
        ei_printf("Failed to start PDM!");
        microphone_inference_end();

        return false;
    }

    // set the gain, defaults to 20
    PDM.setGain(127);

    return true;
}

/**
 * @brief      Wait on new data
 *
 * @return     True when finished
 */
static bool microphone_inference_record(void)
{
    inference.buf_ready = 0;
    inference.buf_count = 0;

    while(inference.buf_ready == 0) {
        delay(10);
    }

    return true;
}

/**
 * Get raw audio signal data
 */
static int microphone_audio_signal_get_data(size_t offset, size_t length, float *out_ptr)
{
    numpy::int16_to_float(&inference.buffer[offset], out_ptr, length);

    return 0;
}

/**
 * @brief      Stop PDM and release buffers
 */
static void microphone_inference_end(void)
{
    PDM.end();
    free(inference.buffer);
}

#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_MICROPHONE
#error "Invalid model for current sensor."
#endif

Credits

Lucía Camacho Tiemblo

Lucía Camacho Tiemblo

1 project • 0 followers
Spiros Kotsikos

Spiros Kotsikos

1 project • 0 followers
maria alifieri

maria alifieri

1 project • 0 followers

Comments