Archive

Archive for March, 2009

Those wiley Norweigans

March 26th, 2009

In my daily perusal of engadget yesterday, I came across this article.

Reading things like this simultaneously encourages me, and makes me uneasy.  It encourages because it proves that my little crackpot idea has merit and that there are practical implementations of SMS controlled whatevers that I haven’t even considered yet.   It makes me uneasy, because I feel like somebody else is going to beat me to the punch and cash in on this groovy idea.

Not that I had any plans to actually try to build some kind of business or anything…. just saying.

admin The SMS Gateway Project

Building an SMS gateway part 2… The API strikes back.

March 15th, 2009

It’s been a few weeks since I first did my SMS gateway experiment.  Like so many other things in my life, I reached my first plateau of success, then had to shelve the project to focus on other things.  Well, I finally got around to researching what would be necessary to build a PHP based API that would let me actually send messages from a hypothetical web application.

Obviously, the first priority was to actually have the LAMP stack running on the Ubuntu box.  I actually configured everything to do this before I ever got the notion to try the gateway.  I initially built the server to act as a both a local development / testing machine, and also act as my own subversion repository.

Anyway, with Apache, PHP, and MySQL running already, it was simply a matter of figuring out how to get PHP to call the gammu function and send the message.  As with building the gateway itself, the documentation on how to do this was somewhat lacking, so I had to dig through several examples, and read pretty much every article that came up on google when searching for the answer, before I finally pieced it all together.

What it basically boiled down to was the PHP system() command.  So basically:

system(’gammu –sendsms EMS +12223334444 -text “This is the message content”‘);

The trick that wasn’t clearly defined was that the apache level linux user that’s specified in the httpd.conf must have permission to execute anything in the /dev system folder.

Once I figured that out, the rest was just straightforward PHP programming.  I created a MySQL database with two tables.  The first was a “scheduled” table.  This table contained fields for the recipient’s mobile number, the message content, the scheduled timestamp, the sent timestamp, and a status field.  I set up a cron job to run every minute that queries that table to find any messages with a scheduled time that is less than the current time, and a status of “U” for “unsent”.   The results would be looped, and the message would be sent.  The row would then have it’s sent timestamp updated, and status updated to “S” (”sent”).

This method basically allows pre-scheduling of messages, for automated reminders or whatever.  So if I want to tell the thing to send me a reminder at 7:55 PM every Wednesday that “Lost” is about to come on, I can.

The second table was “received”.  Self explanatory… a place to store messages received.  It was the same kind of process.  I appended the cron job to also execute another PHP script  to retrieve messages, parse out the results, and store each message in the database.

Completing the bi-directional capabilities of the API got me thinking though, and I had an idea.  I further appended the retrieval process to not only parse the incoming messages and store the info, but then to also analyze the sender’s mobile number and the message content itself, and then compare it against pre-defined patterns to execute some other process. In other words, I wrote it so that if I text the phrase “change home” from my mobile phone, then a script will run that changes an image on a sample page on the web-server.

This was kind of a big breakthrough, because it represents the basic methodology that other services use when they say “Text ‘laugh’ to 11211 to receive your joke of the day”… that kind of thing.

This also got me thinking that it would be really groovy to wire up a microcontroller to the server that could then be programmed to influence something in the physical world.  For example, I could hypothetically text “turn office lights on” or something from anywhere in the world, and, well, turn my office lights on.  There are already plenty of products on the market for home automation from a web interface, but people aren’t always in front of a computer.

Anyway, I’ll continue my research and keep everyone who’s been reading this nonsense updated on my additional findings.

admin The SMS Gateway Project