Personal tools
You are here: Home Documentation How-tos Read info from an RSS feed
Document Actions

Read info from an RSS feed


This How-to is intended for: Microcontroller Developers

How to get your Make Controller to check for updates from an RSS feed.

Problem

You'd like your Make Controller to let you know, or take some action, when a new RSS post is made.

Solution

We'll use the Make Controller's network interface to connect to a server and read whatever its latest RSS post is.  For this example, we'll be monitoring the Make Magazine blog - http://blog.makezine.com.  We'll keep track of the last post so we know when a new one is made.  The general strategy goes something like this:

  1. Figure out the IP address of the RSS feed
  2. Read from that address periodically and wait for a new post
  3. Blink a light when a new post has been made

Get the IP address of the RSS feed
Since the Make Controller doesn't yet have DNS (which would allow us to simply connect to the above address) we need to know the IP address of the Makezine blog.  There are a variety of ways to do this, but we'll just use the ping command. 

To do this, open up your favorite terminal.  On OS X, the built-in Terminal.app (/Applications/Utilities/Terminal) works well.  On Windows, the command prompt (Start->Run->"cmd"), and on Linux, whatever you like.  Then type
ping blog.makezine.com
and hit return.  You should then start to see some responses coming back, looking like
PING blog.makezine.com (72.34.58.9): 56 data bytes
64 bytes from 72.34.58.9: icmp_seq=0 ttl=47 time=41.691 ms
64 bytes from 72.34.58.9: icmp_seq=1 ttl=47 time=30.813 ms
That 72.34.58.9 is the IP address of the Makezine blog.  Onwards!

Read in the info from that address
Now, we're going to be writing code for the Make Controller.  I'm assuming you've already downloaded the latest version of the firmware from the downloads section, and gotten set up to build code on your computer.  Make sure you have firmware 1.3.1 or later as it fixes an important issue in the webclient code.  Now we're going to make our own version of make.c.  I'm going to strip out all the OSC stuff and all the USB stuff so we're just using the network interface.  So our setup, in the Run( ) function will look like
void Run( )
{
TaskCreate( BlinkTask, "Blink", 400, 0, 1 );
Network_SetActive( true ); // Starts the network up.
TaskCreate( ReaderTask, "Read", 1000, 0, 1 ); // start our reader task
}
 That's it.

Now we need to make our reader task.  This will sit in a loop, reading the blog every now and again checking for new posts. 

First, we need to know what we're looking for.  We're going to want to keep track of the latest date that an article was published on the Make blog.  So, we're going to cruise through the XML returned from Make's server and look for that date.  The RSS tag that tells us the last date any of the content in an RSS feed was changed is lastBuildDate, so we'll zip aong until we come across a line that looks like:
<lastBuildDate>Fri, 21 Dec 2007 10:10:19 -0800</lastBuildDate>
And if the date in there is different than what we read last time, we know there's something new.  There are a couple of slightly naive aspects of my approach on this:
  1. I'm not implementing a full-blown RSS reader (or XML parser)  on the Make Controller. I'm just looking for a single tag that I care about.
  2. I'm not checking that the date is actually later than the one I had before, I'm just checking to see if it's different and assuming no material is ever going to be posted that's older.  This wouldn't make too much sense for an RSS feed, so I think this is OK.

We're going to use the WebClient library for the Make Controller to communicate with the Make blog.  We'll send an HTTP GET request to read the contents of the RSS feed.  Then we'll go character by character through the response to find the tag we care about.  Below is a bit of pseudocode to show the general idea - check here to see the file with the full implementation.
void ReaderTask( void* p )
{
int addr = IP_ADDRESS( 72, 34, 58, 9 ); // the address we got for blog.makezine.com
int bufLength = 1000;
char responseBuffer[bufLength];
char lastDate[50];

while( 1 )
{
int getSize = WebClient_Get( addr, 80, "blog.makezine.com", "/index.xml", responseBuffer, bufLength );
while( getSize-- )
{
findTag( );
if( contentsofTag != lastDate )
{
doNotification( );
lastDate = contentsofTag;
break;
}
}
Sleep( 10 minutes );
}
}

Blink a light to alert us that a new post has been made
The only thing left to do now is to blink our light.  We'll blink all the LEDs on the Application Board, and one of the digital outputs - connect an LED or a lamp to the digital out for a better notification!

 

Discussion

Grab the files below to get the source code for this.  Copy make.c into your make.c and do the same with config.h - then you should be all set!

Naturally, you'll probably want to monitor another RSS feed, and go for another method of notification.  Please share your modifications and improvements by either creating your own how-to, or posting to the forum!

Attached files