Log noise level on cloud using Arduino & 1Sheeld

Nowadays people started integrating stuff with the internet for their daily uses, and fortunately, it’s not hard to do so..! as there are a lot of hardware/software tools that ease the development of such applications.
One of these tools is the 1Sheeld’s Internet shield, which allows your arduino board to connect to a mobile application and then to the internet and update/retreive data over http protocol or through REST api’s.

In this blog, we will go through the process step by step:
1- Making a simple noise level sensor depending on your smartphone’s mic.
2- Choosing the best API service for our application.
3- Finally, code deployment and getting the noise level sensor connected to the internet.

 

Making noise level example

If it’s your first time to deal with 1Sheeld, please visit our Getting Started tutorial to check how to use your 1Sheeld board and upload a simple example which is ironically the same we are going to use for our application. but i am gonna simply explain what to do step by step.

1-First make sure to add the preprocessor keys to allow certain shields to be used from 1Sheeld library, preventing the library from eating your memory, especially if you have an Arduino UNO.

2-Include OneSheeld library

3-In the setup function add the line OneSheeld.begin() to initialize the serial communication between your Arduino and 1Sheeld board. And assigning pin 13 in your Arduino board to work as an ouptut calling pinMode(13,OUTPUT).

4-In the loop just use the Mic shield functions which you can find them here in 1Sheeld documentation section, and check if the noise level is more than 80 db turn on an LED.

5-That’s it, just adjust an LED to your arduino’s pin 13 and connect your 1Sheeld and open the mic shield and try to make noise to see the LED turns on and off according to the noise level.

 

Choosing your API

Before we find your best API service and getting your application working, i really recommend watching the following video,it will help you get the basic information to deal with most of the API’s available, along with simple tools like (apigee or postman).

Ok now it’s time to search for API’s. I have been through the search myself and ended up with some custom API’s, most of them have free plans, but sure they have some restrictions on data rate and other stuff. So make sure to check all the plans before implementing your application, and see if it meets your requirements.
If you are interested in API’s for certain applications like Twitter, Instagram, facebook etc. please check this link having all the new API’s available for such applications.

My small API’s List:

Log noise level on cloud using Arduino & 1SheeldLog noise level on cloud using Arduino & 1Sheeld      Log noise level on cloud using Arduino & 1Sheeld

Log noise level on cloud using Arduino & 1SheeldLog noise level on cloud using Arduino & 1Sheeld

even more out there but i have chosen google’s Firebase api for its free plan.

 

Firebase GET/PUT/POST walkthrough

Just a simple step before connecting with the internet, we need to define what the application should do exactly.
The sketch should add your smartphone’s mic noise level to Firebase’s database,so make sure that you have checked Firebase documentation section to know how to add new values to the database.

I will give you a hand if you still find it hard to get Firebase’s documentation,and will walk you through the actions of its Restfull api.

Note:

Each API implements its own way of communication and verbatic actions, for example an API has the verb PUT to add new values and POST to update them, while another flips them making these verbs not standardized, mainly it depends on the developer, so please make sure to read the provided documentation when you choose your API.

We have got these three verbs for our application (Get,Put,Post),so lets check them :).

GET method

It’s implemented to get the response of your request by several forms (like the XML-JSON etc.) but we are interested in the JSON part, so you should append the (.json) at the end of the request URL or use the setContentType method 1Sheeld library provides.

USAGE:

1-Create your Firebase account then get the url provided in the red section.Log noise level on cloud using Arduino & 1Sheeld

2-Change the rules (read&write) sections to have the true values, allowing you to read and write values to your database.Log noise level on cloud using Arduino & 1Sheeld

3-Manually add a child by clicking the ‘+’ sign name the key “Mic” and write down a value say “1234”.Log noise level on cloud using Arduino & 1Sheeld

4-Then with Postman tool place the URL in the field and append the Mic.json at the end of your url. click “Send”,then you will notice the Mic  value returned in the response body.Log noise level on cloud using Arduino & 1Sheeld

 

PUT method

Updates the values of Mic by adding them as a raw data entity.

USAGE:

1-In the put method you will have to specify which child you will be updating, otherwise you will mess the whole data.
2-With the same postman tool all you have to do is just choose PUT method, then add the raw data, lets say “9999” then click “Send”. It will respond with the updated value if the request is OK, and you can now check your updated data over your account.

Log noise level on cloud using Arduino & 1Sheeld

 

POST method

In case you would like to add new values for your sensor readings this will do the job. It will also return a response with a unique ID to access the new created child.

USAGE:

1-With the last postman settings just write down a new value in the body, and change the PUT method to POST and press “Send”.(hint: I have tried to post characters instead of numbers but it responded with a “not valid json object” error,so in case you would like to add characters make sure to create a valid json object inside the body)Log noise level on cloud using Arduino & 1Sheeld

Note:

JSON stands for “java script object notation”, which is a light weight data-interchange format, easy for human to parse and read instead of other representation like the XML. If you still want to know much about json check this link.

 

Connecting your sketch with the internet

We gonna mimic what we have done using postman, but this time with Arduino, and it’s really easy, just bear with me.

 

POSTING (CREATING) NEW DATA

With simple lines of code you will be able to connect your Arduino to the internet:

1-Add the preprocessor key for the Internet shield library included in OneSheeld library #define INCLUDE_INTERNET_SHIELD.

2-Above the setup function, create your first request object by calling HttpRequest myRequest(“YOUR_URL”).

3-Then in the setup function and with the request object created, we will send the data as raw data using the addRawData(“123”) function, and call Internet.performPost() to post the new values.

4-Now after successfully uploading the sketch, make sure you’ve switched the UART switch to the communication mode and internet shield is opened and connected to your board, then hit Arduino’s reset button and check the response in the internet shield, it should be returning a json object with the unique ID of the new created value.

 

GETTING DATA

1-With the same HttpRequest created when posting, we will just subscribe to couple of callbacks (there are several ones so please check them in the documentation).

2-Now in our sketch we are interested in the “setOnSuccess(userFunction)” & “setOnJsonResponse(userFunction)”, the setOnSuccess callback is a function which will be invoked once the request is a success, while the setOnJsonResponse callback will be invoked when your json request is processed and returned with your required json’s object value.

3-We made it easy for you to create your json objects chain to access certain keys and get their values. So, with the returned response in the setOnSuccess callback we will create the chain that would access the (Xkey) and return its value in the setOnJsonResponse callback to deal with.

4-In the setOnJsonResponse callback you have both the key and the value you already requested in the setOnSuccess callback, so you are ready now to do whatever you want with the data.

5-Here’s the code, instead of the Internet.performPost() we added Internet.performGet(), then hit Arduino’s reset button and check the data.

 

PUTTING (UPDATING) DATA

This case we will just update a certain child with a new value,but the challenge comes here by creating the json object as a char string and passing it to the addRawData function, but thanks to the backslash enlcousers where the string will be created easily, here are the steps:

1-With the same request created in the past two actions, we will just create the json object in a c char string. So, for example we would like to access the X child in the json object, so we would create the strings and pass it as the follow addRawData(“{\”GENERATED_UNIQUE_ID\”: \”2000\”}”) since all the backslashes are called string enclosures preventing the c string to be disorganized giving compiler error.

2-Hit Arduino’s reset button to call the Internet.performPut() function and check the value updated.

 

FINAL CODE&DEPLOYMENT

Check the final code that posts mic values each 5 minutes.

Pretty simple and easy ;), and with this I hope you guyz get this blog easily and make your own internet connected stuff all by yourself, and if you have any questions please leave it below in the comments.




Related Blogs

Control a fan with smarpthone gesture (Home Automation Hacks Series #2)

Welcome to our home automation hacks series #2, today I am going to show you how to hack your home plugs with Arduino and a phone gesture 😉 . This time I used Accelerometer sensor shield from 1Sheeld application. And with a simple gesture it will open\close the plug to triger the fan on or …

Control home lights with your voice (Home Automation Hacks Series #1)

It’s now the age were the word “Hack” is a relevant word we see and hear everywhere, and i don’t mean the software cyber hacks,I am talking about DIY hacks :D. We made this Home Automation Hacks series to help you add fun features to your home stuff by doing it yourself 😉 . In …

Display traffic status on an IKEA clock with Arduino and 1Sheeld

Whenever it’s time to get off the office, I always check the traffic status to choose the best time to go home. But I couldn’t find a way to do that autonomously. So then …. I was up to make a system that shows me the current traffic status with the help of a simple tool that is used …

Arduino’s SoftwareSerial vs AltSoftSerial: Which library to use with 1Sheeld+ ?

The newer version of 1Sheeld (1Sheeld+) built to support both iOS and Android devices,but not just that …! There’s another secret feature supported to solve the problem of using extra shield/board over 1Sheeld+, and this shield/board have the same way of communication used by 1Sheeld+ (The hardware UART serial pins pin0&pin1) with your Arduino. Exactly …

Top 3 NFC modules for Arduino

Top 3 NFC modules for Arduino

We can see how much RFID applications are being used in our daily life like in the elevators, many facilities entrances or even in some supermarkets but have you ever thought of how it works? RFID stands for “Radio Frequency ID” so it’s uniquely identifying items through radio frequency and it composed out of three main …

Top 5 Data logger Modules for Arduino

Top 5 Data Logger Modules for Arduino

Want to store your Arduino project’s data, measurement’s or sensor readings for future analysis?  Then you are in the right place, cause today I’m gonna list here the best 5 Arduino Data logger Modules. Data logger gives you the ability to track your system statistically and check how it worked over certain intervals . You can also …