Example program - sending a text message using the Twilio API

Early on in the development of the ZA6 I was letting the robot run overnight in my garage to investigate planning failures that we saw intermittently. I got tired of going out to check on the robot every few hours and thought that with a little help I could be lazy and just let the robot tell me when it ran into a problem. John Freeborg had done a little work with the Twilio text messaging API on the PathPilot CNC side and whipped up a quick example for me, so a big thanks to John for most of this code.

The example code is up on the example_robot_programs Githhub repository and can be downloaded here: example_robot_programs/text_messaging/sms_example at main · tormach/example_robot_programs · Git

There is a little bit of setup that one needs to do before you can run this code:

  1. Sign up for a Twilio account. It’s free. Twilio gives you a $10 credit towards texts (which cost $0.075 cents each for domestic numbers) when you sign up. I didn’t have to even give them a credit card.
  2. Retrieve your account SID, auth token, and then generate a phone number that will be used as the “sending” number for your text messages. This is on the very first page that you see after logging in, so all three of these are easy to find.
  3. Add the account SID, auth token, and from phone number to sms.py in the appropriate places.
  4. Add the destination phone number (the one you want your text to go to - hopefully your own :)) to the sms_example.py file.

One pitfall to watch out for - the destination phone number and the from phone number must be formatted like this: +6088498381
They must include the leading + symbol and they cannot have spaces, dashes, hyphens, parentheses, etc.

You may notice the use of a “Globals” class to keep track of the number of times a move has occured. If you aren’t a programmer, just copy that trick if you want to count other things in a robot program. If you are a programmer you’ve probably realized that the main() function is looped indefinitely by the robot interpreter and any variable defined inside that loop will be overwritten as the function loops. Creating a class and instantiating a member of that class outside the loop allows information to persist through multiple executions of that main() function.

1 Like