Back in 2011 I released a Twitter OAuth API client library for the .NET Micro Framework called MicroTweet. Here’s the original blog post about it.
A lot has changed since then: most notably, Twitter deprecated and disabled their v1.0 API and started enforcing the use of SSL connections to access API endpoints. These changes meant that the original MicroTweet library hasn’t worked for quite some time (sorry!).
After receiving my Netduino 3 Wi-Fi (which features built-in SSL support) I thought it might be interesting to revisit this library and update it to work with Twitter’s new API requirements.
In this post I’ll go over some of the changes to the library and its development process.
Building MicroTweet 2
MicroTweet 2 is a complete re-write of the original library. While it’s not intended to be a full-featured Twitter API client library, it does now have several new features that I think will be useful for NETMF projects.
I’m now using the standard HttpWebRequest class (from the System.Http assembly) instead of a custom-built HTTP client. This is partly because of the new HTTPS requirement, and partly because most modern NETMF boards have a lot more program space available than the Netduino Plus I built the original library for.
The original Netduino Plus had just 64 KB of available code space. The System.Http assembly would have required over half of that space, leaving very little room for user code. The Netduino 3 Wi-Fi has over 1,400 KB of code space and most other modern NETMF boards have at least 384 KB, so this is now much less of a concern than it used to be.
The original MicroTweet library could really only do one thing: post tweets. (It took a lot to get there, as anyone who’s implemented OAuth could attest to.) I had always intended to add more features, but the limited memory on the original Netduino Plus made parsing responses from Twitter’s API relatively difficult.
The Netduino 3 has significantly more RAM, so it’s now much easier to process responses received from Twitter.
All Twitter API responses are serialized as JSON, so for MicroTweet 2 I built a JSON parser. My implementation is fairly simple: it takes a character array and goes through it, parsing it character by character. There are other JSON parsers available for NETMF (such as this one) but I wanted to build my own to allow for greater flexibility when adding features and optimizing performance.
In building this library I found that the Netduino 3 is, to put it lightly, not exceedingly fast at parsing JSON responses. It seems to take around 4 seconds just to parse a 20KB response. This is in addition to the time it takes to receive the response in the first place, which adds at least a few more seconds to the total processing time.
I want to modify the JSON parser to accept a Stream instead of a character array, but my initial tests with this have shown it to cause a significant increase in overall response parsing time, so I will need to research this a bit further. (It might be worth the performance hit, though — this change would allow MicroTweet to handle significantly larger responses from Twitter since they wouldn’t have to be buffered into memory first before processing.)
The previous version of MicroTweet used the MicroLinq library. When I examined this dependency I found that all I was really using from it was an collection-sorting method, used to sort an array of query parameters during OAuth signature generation.
For MicroTweet 2 I’ve removed this dependency and added a simple insertion sort method to handle this need. Making MicroTweet entirely self-contained should help make the project a bit easier to maintain and keep up-to-date.
MicroTweet 2 also supports emoji! 😍🐙🐚🐬🐳🐦🐢🐛🐝🐌🐡
— Matt Isenhower (@mattisenhower) May 26, 2015
You can paste in the characters you want to use or enter them as UTF16 character pairs:
twitter.SendTweet("\uD83D\uDE80 MicroTweet lives again! http://mattisenhower.com/2015/05/26/microtweet-2/ (this tweet posted from a @Netduino 3 Wi-Fi) #netmf");
MicroTweet 2 includes a simple sample project that sends a tweet and retrieves the latest tweets from people you follow. There is no hardware-specific functionality in the included sample project — it runs equally well on any board (or even the NETMF emulator).
I thought it might be fun to make a simple hardware-based sample project, though, so here is the Twitter Follower Clock:
This project retrieves your user data and displays your current follower count on a Seven Segment Display Module. You could also, of course, configure it to retrieve the follower count for any user on Twitter. Here’s the code for this project.
Technically, it’s interactive, too: you can make my count increase by clicking this button: Follow @mattisenhower 😉
I definitely look forward to seeing what people do with this library. Who knows, maybe we’ll see another Twitter-enabled Geiger Counter! Be sure to let me know if you use it for anything cool.