Reading Arduino’s Analog Pins with JSON Protocol by using WiFi Shield

Arduino has capability to measure any kind of sensors like temperature, accelerator, gyro, tiled, humid, radiation, etc. Those sensors will produce any kind variant of value from digital, analog, or pulse wide modulation. But remember for arduino, the sensor will connect to the analog pins mostly, and must have range between zero and five volts, otherwise it will burn the board.

There are six analog input pins inside the arduino uno board, those pins can be read by the sketch code by simply type “analogRead( pinIndex )” function. This function will return as numeric value with range between 0 until 1023, the numeric value produced by the sensor device.

After we read the sensor value from each of the connected pins. We will setup webserver inside the board, and try to publish the sensor data thru json protocol. On the arduino board, we will use wifi shield for tcp communication to other third party device, like computer.

Ok, lets start from the reading analog data inside the board.

[gist /]

The idea in here is how to read all those six analog pins periodically, in this example is every 2 seconds and store them into the array of analogs data.

[gist /]

In order to make “reading” module that run periodically works, i must put them into the loop function like the code above. I hated to use delay function in this situation, because it makes all the process stop as well, and the alternatif solution is to use “counter” module. With this counter module, we can run several processes almost parallely, and it has benefit for the wifi module later to build new client connection without distrubing any other processes.

[gist /]

Reading the analog data from the pin is very easy, just by using “analogRead” function. The return value of the sensor data will be stored into the array of the analogs. The “infoAnalogs” function will be used for debugging purpose, inside the debug function will performed by displaying all the array data into serial uart terminal. Like example below :


Ok, now the reading sensor works well. Let’s move into the JSON stuff.

JSON is a protocol, just like html language but using the “{” , “,” , and “}” characters instead. It focus only for data transfer, with the beauty of the simplified method. As we knew about xml before, the lack of the one was bulky and need a pairing tag each time.

For the next step, we need to convert the analog data array above into json format, therefore we need help from aJson library that you can find at This is just one of the sample json library out there, and I found with this library was pretty easy to use, and maintainable.

[gist /]

To make it clean, we built as a parser function named “jsonAnalogs”, the function that has return value as jsonObject. The purpose of this function is to convert analog array data into json format include the timestamp of the json data creation. First, we put statement to create json root object, and then we build all the child parameters like “millis” and “analogs” under the root one. Because the analog data has a array form, we need help “aJson.createIntArray” function to build “analogs” json array type.

[gist /]

Here the implementation of the “jsonAnalogs” function. On the top we put include ajson library, and inside the loop function we change the debug from “infoAnalogs” into “jsonAnalogs” function. Because the jsonAnalogs function has an object as return value, we need to convert into the string by using “aJson.print” function.


After we have done with function to display analog data with json format, we need to apply it into our tiny webserver.

[gist /]

To build tiny webserver, we can use the arduino webserver default example code. The different just at how to display the page, because json also use the http format. Therefore we need to compose the http headers, one of the header field that shall we change inside the sketch is “Content-Type: text/json” but for this example we need to display the page on the browser, so we leaved it as its original one. The last change we shall put was the response page body, simply by calling our “jsonAnalogs” function.

[gist /]

At the final code inside the initialized zone, we need to prepared the web server with open listener port 80, and put statement “server.begin()” to run. Here the final browser page look like when we do the calling thru the address of the server.


FIN :-)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s