Introduction
Playing with Azure IoT DevKit MXChip is always fun. The device has many capabilities. For the past few days, I have been examining some of the capabilities of this device, like Atmospheric pressure, Temperature, Humidity through its sensors. In this article, I will show you how you can calculate the noise level using the microphone of the AZ3166 IoT Device. Now, let’s start implementing the same. I hope you will like it. You can always read this article on my blog here.
Background
In our last article, we have already seen how to read the temperature, humidity, atmospheric pressure from the MXChip AZ3166 sensors and send those to our Azure IoT Hub. Here in this article, let’s complete the following tasks.
- Find the Noise level using the AudioClassV2 class
- Send the Values to our IoT Hub
Source Code
Please feel free to play around with this repository.
Using the Code
Once you have your own work space, we can open the solution in VSCode and start coding.
main.ino
This is our solution's starting point. Every project must have its own sketch file. Usually, this file contains at least the functions loop() and setup().
Before we get started, let’s include the header files we are going to use.
- //Header files - Start//
- #include "AZ3166WiFi.h"
- #include "AzureIotHub.h"
- #include "DevKitMQTTClient.h"
- #include "config.h"
- #include "utility.h"
- #include "SystemTickCounter.h"
- #include "RingBuffer.h"
- #include "parson.h"
- #include "EEPROMInterface.h"
- #include "http_client.h"
- #include <Arduino.h>
- #include <stdio.h>
- #include <math.h>
- #include "OledDisplay.h"
- #include "AudioClassV2.h"
- #include "stm32412g_discovery_audio.h"
- #include "RGB_LED.h"
- #include <stdint.h>
- #define MFCC_WRAPPER_DEFINED
- #include "featurizer.h"
- //Header files - End//
- //**********************//
Now, we can declare our constants and variables.
- //Constants and variables- Start//
- enum AppState
- {
- APPSTATE_Init,
- APPSTATE_Error,
- APPSTATE_Recording
- };
- static AppState appstate;
- // These numbers need to match the compiled ELL models.
- const int SAMPLE_RATE = 16000;
- const int SAMPLE_BIT_DEPTH = 16;
- const int FEATURIZER_INPUT_SIZE = 512;
- const int FRAME_RATE = 33; // assumes a "shift" of 512 and 512/16000 = 0.032ms per frame.
- const int FEATURIZER_OUTPUT_SIZE = 80;
- const int CLASSIFIER_OUTPUT_SIZE = 31;
- const float THRESHOLD = 0.9;
- static int scaled_input_buffer_pos = 0;
- static float scaled_input_buffer[FEATURIZER_INPUT_SIZE]; // raw audio converted to float
- const int MAX_FEATURE_BUFFERS = 10; // set to buffer up to 1 second of audio in circular buffer
- static float featurizer_input_buffers[MAX_FEATURE_BUFFERS][FEATURIZER_INPUT_SIZE]; // input to featurizer
- static int featurizer_input_buffer_read = -1; // next read pos
- static int featurizer_input_buffer_write = 0; // next write pos
- static int dropped_frames = 0;
- static float featurizer_output_buffer[FEATURIZER_OUTPUT_SIZE]; // 40 channels
- static float classifier_output_buffer[CLASSIFIER_OUTPUT_SIZE]; // 31 classes
- static int raw_audio_count = 0;
- static char raw_audio_buffer[AUDIO_CHUNK_SIZE];
- static int prediction_count = 0;
- static int last_prediction = 0;
- static int last_confidence = 0; // as a percentage between 0 and 100.
- static uint8_t maxGain = 0;
- static uint8_t minGain = 0;
- int decibels = 0;
- float min_level = 100;
- float max_level = 0;
- RGB_LED rgbLed;
- static bool hasWifi = false;
- static bool messageSending = true;
- static uint64_t send_interval_ms;
- int messageCount = 0; // holds ID
- bool messageReceived = false;
- char device_id[6];
- AudioClass &Audio = AudioClass::getInstance();
- //Constants and variables- End//
- //************************************//
Now, let us can add the codes for the configuration. Usually, you wouldn’t have to edit any codes in this section.
- void setup() {
- Screen.init();
- Screen.print(0, "IoT Device Demo");
- Screen.print(2, "Initializing...");
- Screen.print(3, " > Serial");
- Serial.begin(115200);
- // Initialize the WiFi module
- Screen.print(3, " > WiFi");
- hasWifi = false;
- initWifi();
- if (!hasWifi) {
- return;
- }
- Screen.print(3, " > Sensors");
- sensorInit();
- Screen.print(3, " > IoT Hub");
- DevKitMQTTClient_Init(true);
- DevKitMQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "mlIoTPlatformDevice");
- DevKitMQTTClient_SetSendConfirmationCallback(sendConfirmationCallback);
- DevKitMQTTClient_SetMessageCallback(messageCallback);
- DevKitMQTTClient_SetDeviceTwinCallback(deviceTwinCallback);
- DevKitMQTTClient_SetDeviceMethodCallback(deviceMethodCallback);
- appstate = APPSTATE_Init;
- Serial.begin(115200);
- int filter_size = mfcc_GetInputSize(0);
- if (filter_size != FEATURIZER_INPUT_SIZE) {
- Serial.printf("Featurizer input size %d is not equal to %d\n", filter_size, FEATURIZER_INPUT_SIZE);
- show_error("Featurizer Error");
- }
- if (appstate != APPSTATE_Error) {
- ::memset(featurizer_input_buffers[0], 0, FEATURIZER_INPUT_SIZE);
- // give it a whirl !!
- mfcc_Filter(nullptr, featurizer_input_buffers[0], featurizer_output_buffer);
- // check audio gain and print the result.
- uint32_t id = Audio.readRegister(nau88c10_CHIPID_ADDR);
- if (id == NAU88C10_ID) {
- Serial.printf("Found audio device: NAU88C10\r\n");
- } else {
- Serial.printf("Found audio device: 0x%x\r\n", id);
- }
- // a default gain level of 4 seems to work pretty well.
- start_recording();
- Screen.clean();
- Screen.print(0, "Listening...");
- Screen.print(1, "A = min gain");
- Screen.print(2, "B = max gain");
- minGain = 0;
- maxGain = 7;
- set_gain();
- display_gain();
- }
- send_interval_ms = SystemTickCounterRead();
- }
As I mentioned earlier, every INO file will have its own setup() and loop() function. We can modify our setup() function as below.
- void setup() {
- Screen.init();
- Screen.print(0, "IoT Device Demo");
- Screen.print(2, "Initializing...");
- Screen.print(3, " > Serial");
- Serial.begin(115200);
- // Initialize the WiFi module
- Screen.print(3, " > WiFi");
- hasWifi = false;
- initWifi();
- if (!hasWifi) {
- return;
- }
- Screen.print(3, " > Sensors");
- sensorInit();
- Screen.print(3, " > IoT Hub");
- DevKitMQTTClient_Init(true);
- DevKitMQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "mlIoTPlatformDevice");
- DevKitMQTTClient_SetSendConfirmationCallback(sendConfirmationCallback);
- DevKitMQTTClient_SetMessageCallback(messageCallback);
- DevKitMQTTClient_SetDeviceTwinCallback(deviceTwinCallback);
- DevKitMQTTClient_SetDeviceMethodCallback(deviceMethodCallback);
- appstate = APPSTATE_Init;
- Serial.begin(115200);
- int filter_size = mfcc_GetInputSize(0);
- if (filter_size != FEATURIZER_INPUT_SIZE) {
- Serial.printf("Featurizer input size %d is not equal to %d\n", filter_size, FEATURIZER_INPUT_SIZE);
- show_error("Featurizer Error");
- }
- if (appstate != APPSTATE_Error) {
- ::memset(featurizer_input_buffers[0], 0, FEATURIZER_INPUT_SIZE);
- // give it a whirl !!
- mfcc_Filter(nullptr, featurizer_input_buffers[0], featurizer_output_buffer);
- // check audio gain and print the result.
- uint32_t id = Audio.readRegister(nau88c10_CHIPID_ADDR);
- if (id == NAU88C10_ID) {
- Serial.printf("Found audio device: NAU88C10\r\n");
- } else {
- Serial.printf("Found audio device: 0x%x\r\n", id);
- }
- // a default gain level of 4 seems to work pretty well.
- start_recording();
- Screen.clean();
- Screen.print(0, "Listening...");
- Screen.print(1, "A = min gain");
- Screen.print(2, "B = max gain");
- minGain = 0;
- maxGain = 7;
- set_gain();
- display_gain();
- }
- send_interval_ms = SystemTickCounterRead();
- }

Join the conversation! Your thoughts help the community grow.