Start up in 7 days series:
- Day one – The Idea
- Day two – The Setup
- Day three – The Pitch
- Day four – The Function
- Day five – The Graphics
- Day six – The Gateway
Nothing is particularly hard if you divide it into small jobs – Henry Ford
AutoCarLog was quickly becoming a fully functional system to manage vehicle mileage logbooks online. Now I needed a way for my customers to update their log books using SMS, and for this I needed an SMS gateway.
What is a gateway?
An SMS gateway is a device that takes an SMS from the mobile phone network and spits it onto the internet. There are surprisingly quite a few ways to go about this.
The easiest way is to use a third party. As previously posted, in Australia there are quite a few companies who will manage the gateway and send every incoming SMS to your website. They can also arrange to charge the mobile user a premium for this privilege (called ‘Premium SMS’). These services have a start up fee and an ongoing monthly cost, which can be as high as $1,300 a month for six digit premium ‘19’ numbers.
With my $500 budget, I couldn’t afford to go down this path for AutoCarLog so I had to come up with another cheaper way.
Using a mobile phone
The core of a SMS gateway is the GPRS module, an electronic chip which can talk to the mobile phone network and receive SMS of an attached SIM card. You can find these electronic chips everywhere – as every mobile phone has one.
So I can receive SMS on my mobile, but how do I get them to my website? Well firstly, you need to get the SMS into a computer.
Most modern mobiles come with the ability to connect to a computer via a serial or USB cable. The mobile phone companies will then install a suite of software on your computer to manage contacts, read SMS, and update SIM card details. But what you might not know is, it is possible to send “AT commands” to a connected mobile by hand using a terminal program and read the messages directly.
The good news is, this idea isn’t new. Here’s a tutorial from the Nokia website on how to connect to their mobiles, and here’s a great source about connecting mobiles to computers. Look up the AT command set for your mobile, as although it’s common, it isn’t a standard.
Sending and receiving messages from your computer by hand is all very well and good, but you’re not going to want to sit at your computer 24/7 and type AT commands every time you check for a new message, no, you need a program to do this for you.
Your program shouldn’t be too complicated, in essence it will:
- Check for new messages using AT+CMGL (Nokia),
- Read a message in using AT+CMGR (Nokia),
- Parse the message to retrieve the phone number, date and content, then
- Upload it to a website using a HTTP request.
Fortunately there are heaps of examples online of how to go about this, including completed code. You should be able to get something together with basic programming skills, and it wouldn’t cost much to outsource the development of one.
The hardware way
I didn’t use a mobile for AutoCarLog for two reasons. Firstly, believe it or not, I didn’t have a spare one lying around! And secondly, (and the real reason) I can never resist the opportunity to get out the soldering iron and build a piece of hardware. So instead of using a mobile, I built a stand-alone gateway using an AVR microprocessor and the GPRS module found in SAGEM phones. You can see my prototype below.
My device was built by combining modules from previous projects. I already had a system to send twitter updates from a pot plant (“water me please!”) so posting information to AutoCarLog – which is just a different website – should be easy.
Well it wasn’t. The twitter example used a class built by someone else to talk to the twitter servers and I had to create one from scratch for AutoCarLog. Also, I spent several hours wondering why my device crashed after 20 seconds, only to find it was because I missed a memory mapping tilde (~) in one line of code.
Eventually I got it to work. I had a system to check for new messages using the AT commands in one hour, a system to parse the message for phone and content in six hours and a system to send these messages to the AutoCarLog website in seven hours (which would have been four if it wasn’t for the tilde). I added a small LCD screen to tell me what it was doing and spent another six or so hours refining and testing the modem. My girlfriend K didn’t see me that day.
Uploading to the server
Uploading to a server is as simple as opening a website. Open up google in your browser and do a search for something. See how the URL has now changed from www.google.com to something much bigger? You have just sent google some data (your search in this case) which is contained in the URL. This is called a GET request.
For AutoCarLog the process would as such:
- Modem receives SMS message (custom hardware, but could be a standard mobile phone)
- Modem sends message to a computer (AVR MicroProcessor for me, but this could be a desktop PC)
- Function resolves phone number and message content
- Function sends an HTTP request to a website with number and message encoded in URL
- Website receives URL, decodes the message and sends to database
For example if 0413227564 sends the message “20321 b work planning meeting” to AutoCarLog, the modem would request the following URL:
/upload/?number=0413227564&message=20321%20b%20work%20planning%20meeting containing the two GET value pairs:
number = 0413227564 and
message = 20321 b work planning meeting (%20 is URL speak for a space).
My page on the server would then decode the message text and in the above example, it would add a business trip with the odometer reading 20,321 and comment “work planning meeting” to the logbook which the number 0413227564 was registered with.
It is worth noting that these methods will only work if you’re receiving SMS at a slower rate than you can read them, which is about one per second. However, if your business is receiving 60 messages a minute you could probably afford to go through a third party or invest in a commercial modem bank!
Building a modem sounds like a huge task, but when you break it down into smaller steps it becomes manageable. An SMS gateway is just a modem talking to a computer. A modem can be a simple mobile phone with a USB cable, or it can be a complex embedded hardware device. The trick is to tackle the smaller pieces first.