Twilio is a web service that allows you to send and receive phone calls and text messages through the internet. I'm working on a larger Rascal-Twilio project that's still developing, but I wanted to demonstrate one element of it now, because it's cool. Using Twilio, I've set up a Rascal such that if I text the name of a color, like "blue," to a certain number, the Rascal will receive that text and change a BlinkM LED to the color sent.
Here's a quick video that shows the system in operation.
Twilio has good instructions for setting up a trial account. Here are some details:
When Twilio receives a text message to your number, one of their servers will make an HTTP POST request to an address that you specify in the Twilio dashboard in the SMS URL field. You must create and log into your account before you can get to your dashboard.
The HTTP POST request from Twilio will come with some data attached. From the full list of what data Twilio sends when you receive an SMS, I'm just interested in the "Body" field, which contains the message itself. To respond to the POST request, I'll add some Python code to server.py. The code has three main steps.
color = webcolors.name_to_rgb(request.form['Body']) pulls the body of the text message out of the POST request and uses the Python webcolors library to convert the message text to an RGB color. By default, any color from the list of named CSS3 colors can be translated by webcolors.cmd = 'blinkm set-rgb -d 9 -r ' + str(color[0]) + ' -g ' + str(color1) + ' -b ' + str(color2), takes the R, G, and B values and stuffs them into a command line that calls the Blinkm command-line tool written by Scott Ellis, which is included in the Rascal. This talks to the BlinkM LED using the Rascal's I2C bus.subprocess.Popen([cmd], shell=True) executes the Blinkm command on the Rascal.Here's the Python code.
@public.route('/sms', methods=['POST'])
def parse_sms():
import subprocess, webcolors
color = webcolors.name_to_rgb(request.form['Body'])
cmd = 'blinkm set-rgb -d 9 -r ' + str(color[0]) + ' -g ' + str(color<a href="https://www.twilio.com/user/account">1</a>) + ' -b ' + str(color<a href="http://www.twilio.com/docs/api/twiml/sms/twilio_request">2</a>)
subprocess.Popen([cmd], shell=True)
return ('color sent to Blinkm')
One other note: to run the code above, you'll need to install the webcolors library. If you can SSH into the Rascal, it's as easy as pip install webcolors. If not, it's slightly more complicated, but it will be installed by default on all Rascals in the future. Let us know if you're stuck on this.
If you find errors in this tutorial, please drop a note in the forums, and we'll fix it up.