element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] HangarControl #9, Preheat via Text Messaging
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: rhavourd
  • Date Created: 29 Aug 2016 8:09 PM Date Created
  • Views 624 views
  • Likes 3 likes
  • Comments 0 comments
  • pi-iot hangarcontrol sms text-messages flask twilio
Related
Recommended

[Pi IoT] HangarControl #9, Preheat via Text Messaging

rhavourd
rhavourd
29 Aug 2016

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

image

Status of Hangars

image

Preheat an Engine

image

Status While Preheating

image

 

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

Attachments:
twilio_app_py.txt.zip
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube