Playing with Azure IoT DevKit MXChip is always fun, the device has many capabilities. For the past few days, I have been working with 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 AZ3166 IoT Device. Now let’s start implementing the same. I hope you will like it.
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 do the following tasks.
- Find the Noise level using the AudioClassV2 class
- Send the Values to our IoT Hub
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.
This is our solution starting point, every project must have its own sketch file, usually this file will be containing at least the functions loop() and setup().
Before we get started, let’s include the header files we are going to use.
Now we can declare our constants and variables.
Now we can add the codes for the configuration, usually, you wouldn’t have to edit any codes in this section.
As I mentioned earlier, every INO file will have its own setup() and loop() function. We can modify our setup() function as below.
The function loop() will be called each 5 seconds, as I had set the INTERVAL as 5000 milliseconds.
Now we can edit our code of loop() function as below.
As you can see, once we get the values from the function get_prediction (), we are passing the decibels values to our setMessage() function, which we have defined in the file utility.cpp. Inside the setMessage () function, we will add the decibels value to JSON object using the function json_object_set_number ().
You should also add the files featurizer.h and featurizer.s to get it working. You can get those files from the source code repository mentioned above.
Compile and Upload to the Device
As we have already made the needed changes, it is time to compile the Device solution and upload the same to our Device. Press F1 and select Azure IoT Device Workbench: Compile Device Code. If you ever get an error as “error: utility.h: No such file or directory”, please compile the device code again. If you are facing any unexpected errors, please delete the “.build” folder and compile again.
Once you get a message as ” [Done] Finished verify sketch – Main.ino ” in your Output window, you can upload the solution to your device. To do so, press F1 again, and select ” Azure IoT Device Workbench: Upload Device Code”. Please make sure that the device is been connected to your machine. If everything goes well, you will be getting a message as ” [Done] Uploaded the sketch: Main.ino”.
Please remember to see the GitHub repository for the full code.
Device to Cloud Messages
Now your device will be sending the Decibels data to the Azure IoT Hub. Let’s see that in the D2C Monitoring window.
Wow!. Now we have learned,
- How to detect the noise level in MXChip
- How to use AudioClassV2
- How to send device data to the Azure IoT Hub
Please consider reading my IoT articles here for the continuation.
Your turn. What do you think?
Thanks a lot for reading. Did I miss anything that you may think which is needed in this article? Could you find this post as useful? Kindly do not forget to share me your feedback.