Sending SMS Messages with Twilio on ESPHome

February 1, 2021

Sending SMS messages from a microcontroller isn't new. There's a bunch of examples on the Twilio blog on how to accomplish this -- like this one or this one and many more around the internet.

Where this is different is by using ESPHome to program the esp8266 instead of micropython or the arduino IDE. As you can probably tell from the Projects section of this website, I've really enjoyed using ESPHome to quickly program devices and set up sensors using YAML.

Ultimately, I'm hoping to add this to my existing doorbell, giving it SMS capabilities -- more to come soon on that. Here's a quick breadboard prototype with a few buttons that trigger an SMS when pressed.

twilio doorbell prototype

I'm posting my code below and have commented exactly what you need to do to accomplish this yourself!

One section to highlight is the authorization section in the Header. I figured out that for Basic Authorization, you need to encode your AccountSID and API key in base64.

Take your <Account_SID>:<API_KEY> (make sure to put a colon in between the Account SID and API Key) take it and paste it on this page and put the resulting string into this form (replacing the xxxxxxxxx with the resulting output from the base64encode website): Authorization: "Basic xxxxxxxxx"


esphome:
  name: twilio_doorbell
  platform: ESP8266
  board: huzzah

wifi:
  ssid: "network_name"
  password: "network_password"

    # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Twilio Doorbell Fallback Hotspot"
    password: "random_password"

captive_portal:

# Enable logging
logger:
  # level: VERBOSE  ## not needed anymore

# Enable Home Assistant API
api:

ota:

http_request:
  useragent: esphome/device
  timeout: 10s

#### Button 1
binary_sensor:
  - platform: gpio
  ### debounce on the button press
    filters:
    - delayed_on: 10ms
    - delayed_off: 10ms
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: TRUE
    name: "twilio_doorbell_michael"
    device_class: moving
    on_click:
      - http_request.post: 
          url: https://api.twilio.com/2010-04-01/Accounts/AC_twilio_account_SID/Messages
          headers:
            authorization: "Basic QUM3MT_random_numbers_and_letters_NTVkNWZlZjVjZjE1Yg==" 
            ### I read on some random blog that Basic Auth needs to be in base64 and it worked! 
            ##### Take your <Account_SID>:<API_KEY> (make sure to put a colon in between the Account SID and API Key) take it and paste it on this page https://www.base64encode.org/  
            ####### Take the output and paste it in like this: 
            ############# Authorization: "Basic xxxxxxxxx"

            Content-Type: "application/x-www-form-urlencoded" # this is the Content-Type they used in this example:    https://www.twilio.com/blog/send-test-http-requests-twilio-sms-postman
          body:
            "To=+16505551234&From=+14155551234&Body=Ding Dong! Someone's at your front door!"
            ##### The body needs to be a string in key=value format separated by & signs: "key1=value1&key2=value2&key3=value3"
          verify_ssl: false
          #### this is a verification of SSL on the device side (not the Twilio side)

Let me know if you incorporate this into one of your projects! I can see this being useful for water detection sensors and provide urgent SMS notifications if it detects a leak!

Share: