Monitoring Twitter with Netduino
This blog post will introduce you to network communication with the Netduino. To demonstrate network connectivity I’ll have my Netduino make a request to the Twitter API to get a user’s last status update. Once the particular user I’m watching (the Netduino Team, of course) posts an update, the Netduino will let me know with a little blue LED. Sounds pretty simple, right?
The problem, of course, is that the regular Netduino lacks network connectivity as it has no Ethernet shield as does the Netduino Plus. At press time, the Netduino Plus is in high demand and is on back-order. What’s a geek to do? Borrow stuff from other geeks, obviously! I’ve borrowed a colleague’s Arduino Ethernet Shield and snapped it right on top of my Netduino, as you’ll see in this photo.
Given the fact that the original Netduino lacks support for the Arduino Ethernet Shield, drivers must be written or obtained to enable communication between the Netduino and the Ethernet Shield. Luckily some nice people from SecretLabs have put together some drivers for this and other Wiznet-compatible Ethernet shields. You can download a Visual Studio 2010 project containing the code for this communication on the Netduino forums site.
The difficult part of this code (in addition to losing the original version of it) is in that the Netduino has a much more limited amount of memory than does a typical computer. So, the grabbing of XML data, parsing it, and the rest of the process can cause the device some heartburn. One must use patience, but it is possible to make it work. The meat of the code just accesses the Twitter API to get a user’s status feed. Some specific querystring parameters are passed to the Twitter API, informing it that the client only wants one status update’s worth of information. Without this, Netduino tends to have memory problems, because it’ll download lots (and lots) of XML data from Twitter, run out of memory, and crash. After some testing and tinkering I found provision of the “count” parameter (and set to 1) as well as provision of the “trim_user” variable as set to 1 as well, results in the best behavior because the resulting XML data tends to be a much smaller payload. The IE screen shot to the left shows the XML output actually received from Twitter.
To run through the code quickly to get an idea of how it works, take a look at the structure of the Program. It offers a few properties to represent the LED’s indicating action. During the construction everything is created and prepared.

Taking a look at the main execution loop (closed above) you’ll see things aren’t too complicated. The Netduino will just ping Twitter on intervals looking for updates.

You’ll find more information about this next code block on the Netduino Forums from the link above that goes into great detail regarding the Wiznet Ethernet Drivers. Nevertheless, the idea is simple; the Netduino opens up the network port and sets itself up with a static IP address. You might have to change this based on your own network topology.

Finally, the CheckTwitter method, which comprises the majority of what the software will actually be doing. The first step of the method is to make the actual call to Twitter. The code activates the green LED to indicate the check is about to take place and then calls a utility method to actually make the HTTP request (this code is in the download link below).

Once the HTTP request to Twitter is complete the green LED is turned off.
![]()
Now that the XML data has been obtained as a string from the Twitter API call it can be parsed. The ID is found from the XML. It may seem like the code is using a pretty archaic approach, but remember our memory limitations and that the XML functionality within the Micro Framework is a subset (and a small one at that) of the XML functionality found in the full .NET framework. The id value obtained from the status update XML is then compared to the last new status id the client saw. If the id’s don’t match, the program makes the assumption the status update is a new one and activates the blue LED to inform the observing user they’ve got some Twitter-reading to do.

The video below shows the Netduino Twitter Monitor in action. Please, forgive the mumbling and southern accent.
Happy Coding!