Alex Glow
Published © GPL3+

SMS Button (with Twilio)

Send a text message with Python, via a Raspberry Pi set up with the Twilio SDK.

BeginnerProtip1 hour3,347
SMS Button (with Twilio)

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Adafruit USB to TTL Serial Cable
×1
Big Red Dome Button
SparkFun Big Red Dome Button
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Schematics

Button connection

This is the same on any Pi with the standard 40-pin connector.

Code

Button-to-SMS Demo

Python
Send a Twilio message when you press a button on your Raspberry Pi!
# Twilio button-to-SMS demo
# Code is a mashup of Twilio Python SMS demo and Inderpreet Singh's button demo
# Smooshed together by Alex Glow; full tutorial: http://bit.ly/sms-button

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
import RPi.GPIO as GPIO
import time

# Use the Broadcom SOC Pin numbers 
# Setup the Pin with Internal pullups enabled and PIN in reading mode. 
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'ACXXX'
auth_token = 'YOURTOKEN'
client = Client(account_sid, auth_token)

# Our function on what to do when the button is pressed
def SendMsg(channel): 
   print("Sending...")
   message = client.messages \
    .create(
         body='PyLadies Rock!',
         from_='+1TWILIONUMBER',
         to='+1YOURNUMBER'
     )

   print(message.sid)

# Add our function to execute when the button pressed event happens 
GPIO.add_event_detect(18, GPIO.FALLING, callback = SendMsg, bouncetime = 2000)

# Now wait! 
while 1:  
   time.sleep(1)

Credits

Alex Glow

Alex Glow

145 projects • 1571 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments