Thursday 12 December 2013

CommsHack

Yesterday the University of Portsmouth Computing Society held a "Comms Hack" - basically a hackathon sponsored by Twilio.

Unfortunately the Twilio guy had a problem and couldn't physically attend, but it all worked out. I could only be there for about an hour at the beginning and then another hour and half later. It took me a while to think of an idea, and eventually I started coding in python to build a simple text weather service. The idea is to text in your city (e.g. "GB Portsmouth") and then you get the latest weather. When the weather changes you also get updates.

I really wanted to use MQTT because I love it :-)

I ended building a system of about 5 concurrent processes all talking via topics:

  • One process polls openweather for weather data, and then reformats it slightly (e.g. changing Kelvin to Celsius!), and posts to a topic /weather/{country}/{city}
  • Another process listens for webhooks from Twilio and then resends as MQTT messages to /sms/
  • There is a process that listens for messages on /phone/{number} and texts those via Twilio to the phone number
  • The final process listens for subscription messages, and also keeps track of the latest weather data. When a new subscriber comes in or the weather for a city changes, it republishes it to the /phone topic.
It actually works pretty well - the only things that might be good additions are that:
a) I only get the weather from a fixed set of cities. This could be enhanced by automatically adding new cities to the subscribe list
b) You get the weather as a JSON string via text :-) 

What I liked best about this model is the loosely coupled nature of the coding. I could add and test each part independently. Similarly it would be easy to add a different type of subscription by creating a new process. Another cool aspect is how easy it is to monitor and debug just by subscribing to the various topics in MQTT. Testing is nice and easy too because you can write mosquitto_pub command-line tests.

I'll leave it running for a couple of days (or until my Twilio balance runs dry!). If you want to try it, text one of "GB Oxford", "GB Portsmouth", "GB Southampton" or "GB London" to 01143031705. I have to warn you - I haven't yet implemented an unsubscribe!

The code is (in usual hackday style messiness) here

No comments:

Post a Comment