Now that the web interface is up and running (), it is time to add the final piece: Operating the hangars via text messaging!
Text Messaging Gateway
I'm going to use Twilio (https://www.twilio.com/) to provide a link between the SMS or text messaging world and the web-based internet world. Twilio provides a plethora of tutorials and help as well as a free phone number for your development projects. Once you have signed up and have your phone number and API credentials, come on back and follow along.
(Yet Another) Minimal Application
It seems that everyone has a sample application that shows how easy their product works, but when you try integrating it into your own project, it all falls apart. I am going to show you how HangarControl implemented text-messaging so you can see Twilio in a larger context. I have attached the application so that you can download and view in its entirety.
# Use Flask for our "web" interface. In this case "web" is going to return XML rather than HTML. import flask # Twilio provides libraries for most of the popular languages. from twilio import twiml app = flask.Flask(__name__)
Credentials, not to be shared
I'm using my Twilio credentials as set in environment variables so that they don't get published! Either in your login script, most likely .bashrc, or possibly FASTCGI init script, set a couple of environment variables that your programs will read upon execution. This allows you to share code such as this without giving away your private information. If you're never going to show someone else then you can just replace the following with variable assignments like: app.config['account_sid'] = "your SID goes here"
import os app.config['account_sid'] = os.getenv('ACCOUNT_SID', '<SID provided by Twilio>') app.config['auth_token'] = os.getenv('AUTH_TOKEN', '<Secret auth toke provided by Twilio>')
Receiving SMS Commands
# We only have a single endpoint and will parse the string sent by Twilio to determine user intent @app.route('/', methods=['GET', 'POST']) def incoming_sms(): attr = None message = flask.request.values.get('Body', '"?"') message = message[1:len(message) - 1] ary = message.split(" ") cmd = ary[0] if len(ary) > 1: attr = ary[1] if cmd in ["heat","preheat"]: rtn = preheat(attr) elif cmd == ["cancel","off"]: rtn = stop(attr) elif cmd in ["list","status"]: rtn = status(attr) else: rtn = "Ask one of 'heat <hangar>', 'stop <hangar>', 'status', '?'" r = twiml.Response() r.message('HangarControl "{1}" {2}\n{0}'.format(rtn,message,cmd==u"preheat")) return flask.Response(str(r), mimetype='text/xml')
Processing the commands
Once we have pulled apart the command and any parameters from the Twilio SMS message, the program will be dispatched to one of the following methods. I intentionally used these barebones methods to demonstrate what you would need for your own project. I will followup with actual code, but didn't want to overly complicate this presentation. Each method performs the requested action and then returns a string that will be packaged up and returned via text message to the pilot.
def preheat(hangar=None): return "Preheating {0}".format(hangar) def stop(hangar=None): return "Turn off heater in hangar {0}".format(hangar) def status(attr=None): return "Status {attr}".format(attr)
Using HangarControl with Text Messages
Getting Help
Status of Hangars
Preheat an Engine
Status While Preheating
SMS In a Nutshell
Believe it or not, that's all you need to do to implement text messages into your applications. I'd heartily recommend adding SMS capabilites to your next project!
Best of luck,
Rick