Home Automation SMS Interface
Our Home Control System (HCS) has had the ability to send SMS messages for several years now but, these are sent using a SMS gateway (provided by http://www.bulksms.co.uk). The problem with this approach is that it requires Internet network connectivity and is thus at the mercy of your Internet connection. It is also 1-way (send only) but, can be made 2-way by subscribing to a more expensive service. We have taken steps to maintain our network connectivity during power cuts but, broadband failures and network service failures are out of our control.
The objective of this project is provide SMS send and receive capability using a USB GSM/EDGE modem, as an alternative delivery channel. This approach also extends our existing capability to become a bi-directional channel. We should be able to send commands via SMS and receive responses or confirmations back. Given that most mobile phone contracts/tariffs have huge numbers or unlimited SMS message allowances, this is also a cheaper method than using a PAYG SMS gateway service.
The thinking is that mobile phone number can be used to authenticate structured commands being sent to the Home Control System (HCS). The format of these will also be used to validate them. The kinds of commands envisaged will enable things like:
- Query status of alarm, network, etc.
- Turn alarm status on or off.
- Set house house status.
- Activate a panic alarm.
- Reset alarms.
- Request logs be emailed.
- Directly control lighting and appliances.
- Set target temperatures for rooms.
For obvious reasons, the actual messaging protocol is not going to be documented here.
The device purchased on eBay (£23 delivered) is a Sentar ST881N.
I've installed an O2 PAYG SIM in this device to provide the network connectivity. This device is compact, simple and fully powered via the USB port. It even comes with some on-board memory.
The modem comes with some software installed on the on-board memory. This installed fine under Windows 7 and installed the modem device, drivers and a simple application to manage contacts and the sending and receiving of SMS messages. We used the provided software application to test the send and receive of SMS messages.
As the device is simply a modem, it can be controlled via a USB-serial port connection in many languages. Like the rest of our Home Control System (HCS), we have written a control layer in Java, which sends AT commands to the device and receives the responses. Whilst this sounds like a simple thing to do, the reality was somewhat harder :-(
The software responds to SMS messages from a trusted set of phone numbers but we also log in-bound SMS messages from any source. This is because service providers like O2 often send information and service updates via text message.
The Java RXTX library is notoriously temperamental. The first issue to resolve was the installation of this code on our HCS server. Compilation also requires the class paths to be correct.
Even having the correct Java code is no guarantee of success. There a few other issues to be resolved. The first is getting the flow control right. The second is one of time delays. The modem is not particularly responsive. After sending each command, typically a 500mS delay is required before you can expect to receive a response.
Despite writing perfectly good code, we could not kinetically get meaningful responses from the modem. We finally resorted to the AT&F command to do a factory reset on process start-up and it magically came to life. We have also noticed some issues with the RXTX library freezing and then requiring a full reboot to recover from. The conclusion was that the USB ports were going into some kind of sleep mode after long periods of inactivity. We changed some of the advanced settings and did some stability testing, sending commands to the modem for many days at regular intervals and this appears to have fixed these issues. It has been running faultlessly for a long time now.
Modem AT Commands
We are not going to list all of the commands used here but when sending SMS messages, it is not a matter of simply sending an AT command and collecting the response.
The software architecture for our Home Control System (HCS) is based upon multiple processes and inter-process communication using socket based messaging. The SMS process waits in a loop, for messages from the HCS process, with commands to send out-bound SMS messages (these are not acknowledged). The process blocks on waiting for a message but this blocking wait times out after a few seconds, at which point the process checks for any new in-bound SMS messages. In-bound SMS messages are then sent to the HCS process via socket based inter-process communication and are also not acknowledged. This introduces a short (2-3 second) delay in receiving new messages but, this is perfectly acceptable for all our usage scenarios.
This project is now complete and in use. We can send and receive text messages reliably and independently of an Internet gateway service.
A lot of the scenarios and features described above have been implemented but with such an open and extensible capability, we can see that we will be adding new features and commands in the coming months.
One of the key things we would like to resolve is some mechanism for automatically checking the PAYG balance for the SIM used in our modem. It may be that we have to switch service provider in order to get a balance request via SMS to work.
- The main advantage of this approach to sending SMS messages is that it is two way and provides an easy way to receive SMS messages as well.
- This might not be the cheapest way to send out-bound text messages but, all of the mobile phones in our home are on an unlimited text tariff. In this application, the majority of the SMS messages are in-bound commands from these mobile phones.
- It would be better to use a tariff that supports you texting 'balance' to a number, so that you can get a text back and query your available PAYG balance automatically. Unfortunately, O2 doesn't support this on PAYG tariffs :-(
- With such an open interface, precautions need to be taken such as limiting the phone numbers (by CLI) that can send commands into the Home Control System (HCS). We also use a private format for messages with strict validation.
- For obvious reasons, you don't want to disclose the mobile phone number associated with this kind of interface!