Accessing YQL from Arduino

Yahoo Query Language

While writing the tutorial on how to parse JSON in Arduino, I used a sample JSON response that I got from YQL. After writing the tutorial, it struck me that I can in fact make Arduino to directly query the YQL server to get the response. I immediately dusted off my Ethernet shield and tried doing it. After a couple of hours, my Arduino was querying YQL directly and got the data by parsing the JSON response :)

What is YQL?

First, you might ask me what is YQL. YQL stands for Yahoo Query Language. It is an expressive SQL-like language that lets you query, filter, and join data across Web services. You can read more about YQL from the Yahoo Developer network page.

YQL Query

The following lines define the YQL query that I was using for testing the code.

This query basically looks into the feed of my blog and returns the title of the latest article.

Ethernet shield

In order to make the Arduino make a web request, it first needs to connect to Internet. There is a shield called Ethernet shield that provides Ethernet capabilities to Arduino and allows it to connect to Internet directly, without using any other intermediate device.

Making the request

The Ethernet shield comes with an Arduino library, which provides lot of functionalities like DHCP, DNS etc. It has a class named EthernetClient that allows you to make web request. I was using this class to make the request to YQL.

The following are the relevant part of the code that uses the Ethernet library to query the YQL servers.

Encoding the URL

To make YQL request, you need to urlencode the url. Arduino, didn’t had a built-in way of doing it. I quickly wrote a hacky way to do it. You can find it in the URLEncode method. Update: I talk about the [logic behind the URLEncode function](http://hardwarefun.com/tutorials/url-encoding-in-arduino) in a separate blog post.

Getting the JSON string from response

I needed to get the JSON part from the response. I just searched for all content between the first ‘{‘ and the ‘}’ character. The core part of this logic is present in the following lines.

Parsing the JSON response

I used the aJson library to parse the JSON. You can also checkout my article about JSON parsing in Arduino. The actual parsing logic is present in the parseJson function.

Keeping track of free memory

Since JSON parsing is memory intensive and Arduino has very limited memory, we always need to keep an eye on the amount of free memory that is available. I used the MemoryFree Arduino library for it.

This is an interesting library as well. Will be writing a separate detailed article about it as well.

Download source code

You can download the complete source code of the sketch that I used from github. Let me know if you put this into some good use :)

It is amazing that how much you can squeeze into a small Arduino. I am going to try to see if I can add a LCD display to this. Will post about it if I succeed.

Happy hacking :)

8 thoughts on “Accessing YQL from Arduino

  1. Pingback: URL Encoding in Arduino | Hardware Fun

  2. Pingback: Arduino Blog » Blog Archive » Project feature: Accessing YQL from Arduino

  3. Pingback: Project feature: Accessing YQL from Arduino | Linux-Support.com

  4. Pingback: Using YQL to Query Webservices From Arduino « jpreardon.com

    1. Sudar Post author

      Nice to know that my article was useful to you.

      Do let me know if you manage to do something interesting with your project.

      Reply
      1. Dominik Schmidt

        Hi !

        We develope an universal Software called LogView. And at the Moment we working on a new Version which supports Linux and Data store / retrieve with a Webserver + PHP.
        What I was looking for was a solution to decode the Response from my PHP REST Service which handles the data.
        Now I have my Arduino working perfect.

        If you like to here more just give me an email : dominik att logview dott info.

        Dominik

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>