Thursday, 30 August 2012

Side Project: Sms Messaging Service

I love going to the cinema. There's something about watching the latest blockbuster (or sometimes in my case the latest flop) on a big screen in seats you would never dream of buying for your home with a cup of fizzy that is incredibly overpriced. OK, so maybe that last part I'm not so fond of. But still, the cinema is great. It's even better in the UK if you own a mobile on the Orange network as you can get 2 for 1 tickets for use on wednesdays. Unfortunately, my primary phone is no longer on the Orange network, but to overcome this problem I have kept my old Orange PAYG sim and an old phone and can continue to reap the benefits of this offer.

The problem with this is that I either need to keep this Orange phone on my person at all times (just in case I want to go to the cinema), or I need a phone that will allow me to swap sims over for when I want a ticket. This is clumsy and not ideal. If only there was a way for me to send a text message to my Orange phone, which would then make the request for me and forward the Orange wednesday ticket onto my non-orange phone. Wait a minute...I'm a programmer. Of course there's a way.

My first task was to find out if there was a already a way available to do the hard part of the process for me...sending/receiving text messages from\to a connected phone through a computer. Luckily, there's a bunch of these solutions. The two I tried were gnokii and it's child project gammu. Both solutions involve communicating with a connected phone through AT commands which seem to be implemented in some form or another with most modern day mobile phones. This means that connecting to non-smartphones are possible,even if the solution isn't advertised anywhere. Both gnokii and gammu also offer what's known as an sms-daemon, which is a service that polls a connected phone for inbound text messages and a given directory for outbound messages. These messages are saved in text format, which means that the messages can be easily parsed and processed. This service is the thing that would be the backbone of my project for the sending/receiving of messages; and due to better documentation, platform agnostic-ness and better support, gammu was the sms-daemon solution that I ended up using. After much faffing around I managed to get text messages being sent and received through a friends Sony Ericsson w580i phone.

So I had a facility for receiving and sending text messages, now I needed to process those messages and do something with them. In comes the programming part of the project. The concept was simple. To build an application that polled the inbound directory for inbound messages and process them based on message content and/or the number the message was sent from. The processing of the message content could then lead to subsequent messages being sent. It also needed to be built to allow easy extendability in case either a) I chose a different method/sms-daemon for retrieving/sending the text messages or b) I wanted to produce future services that were driven from sms content. This lead to the following process:

As you can see I have a concept of message processors and handlers. The processor is what translates the universal message to/from the message retrieval format (in this case gammu). The handlers (in this case just the Orange Wednesday Handler) is what processes the messages and decides what to do with it. The Orange Wednesday Handler when receiving a text message goes through the following process:

This logic was important as I didn't want to send text messages to Orange unnecessarily as each text costs money. As you can see to prevent this, a few checks are made before the text message is sent. I have also built into the handler the ability to re-ask for the code just in case it's accidentally deleted.

So I had the concept of the program boiled down, but what was I going to program the application in. The first language that sprung to mind was C#. I have been using the language for over three years both leisurely and for work. But this was quickly ruled out as one of the things I wanted the application to run on was one of these.
For those that don't know, this is a Raspberry Pi. A $35 ARM based, Linux run computer that is both low powered in energy and specs. But this was perfect for my little project as I wanted the application running all of the time without a power hungry PC having to be on. So as C# was out I falled upon the other language I had a bit of experience in. C++. This proved to be a bit of a challenge as the last time I used the language was in University and turns out I've been a bit spoiled by the niceties of C#.

I finished building the application about a week ago on a Windows environment, and the last week has been trying to get the application running on another friend's Raspberry Pi. It was hard. But this was due to a mixture of my Windows compilier (MinGW) compiling things a little differently to linux's default compiler (G++) and my lack of knowledge when using Linux itself. But it works as I intended. However this is not the end as some missing features need to be added that will make using the service more useable. A task it seems for the weeks ahead.