Send out SMS with the ESP32 (Twilio).

If you’re looking for various types of notices and also notifies with the ESP32, you can examine the following tutorials:

Send SMS with the ESP32 Twilio Arduino IDE Core

Twilio offers programmable communication tools for getting and making phone calls, sending out and obtaining text messages, as well as doing various other interaction functions utilizing its internet service APIs.

Insert your network credentials on the complying with lines:

Introducing Twilio

twilio logo

Insert your Twilio account information: the, and also the.

The adhering to line instantiates a new Twilio instance with the account information:

Creating a Free Trial Account

Then call the send_message() feature that approves as debates the recipient number, the sender number, the message, and a variable to hold the feedback.

We have tutorials showing exactly how to send out SMS with the ESP32 using the SIM800L as well as SIM7000G components:

Creating a Free Trial Account Twilio account

If you’re looking for various kinds of alerts and notifies with the ESP32, you can check the adhering to tutorials:

Twilio account create new

Set Up the Free Twilio Trial Account

Twilio provides programmable communication tools for making and getting phone telephone calls, sending out and also receiving message messages, and doing various other interaction functions using its web service APIs.

Verify your personal phone number

Enter your details as well as get started with a totally free test.

twilio verified personal number

Verify other recipient numbers

Start by including the twilio-esp32-client collection.

twilio add verified called ids

Get a Twilio Phone Number

Insert your network credentials on the following lines:

Insert your Twilio account details: the, and also the.

Buy twilio number

The following line instantiates a new Twilio circumstances with the account details:

Then call the send_message() function that accepts as arguments the recipient number, the sender number, the message, and a variable to hold the action.

Twilio buy US phone number

Programmable Messaging – Get Set Up

Now, you have everything set up to start creating a programmable messaging service. On your dashboard, on the left sidebar click on Messaging > Try it out > Get Set Up. Then, click on Start set up.

twilio programmable messaging

Give a name to the Messaging Service, for example, ESP32 Alerts and click on Create Messaging Service.

Twilio Create Messaging Service

Then, select the Twilio phone number you created previously and click on Add this number.

Twilio add phone number

After that, the programmable messaging service will be all set up. You’ll get access to your account information: account SID and Auth token. You’ll need them later in the ESP32 code.

twilio create messaging service

You can test if everything is working as expected by clicking on Try SMS. You’ll see a similar page as shown below. Enter the phone number you want to send the message to (it must be a verified number—), select the messaging service you created previously, and write some body text and click on Send test SMS.

Twilio try test SMS

After a few seconds, you should receive the test SMS on the selected number.

SMS from Twilio trial account

All SMS sent from a Twilio trial account will have the text: “Sent from your Twilio trial account”. This text doesn’t show up on premium accounts.

ESP32: Send SMS using Twilio

Sending SMS using Twilio is very straightforward thanks to its API. You can . You simply make HTTP requests with the ESP32 with the right parameters (accordingly to Twilio’s API) to send SMS. You can check .

Or you can use a library that takes care of all that, and you just need to insert your Twilio account details and the SMS body text. Throughout this tutorial, we’ll use a library called .

Installing the twilio-esp32-client Library

The twilio-esp32-client library can be installed through the Arduino IDE Library Manager. Go to Sketch > Include Library > Manage Libraries. Search for twilio-esp32-client and install the library.

twilio-esp32-client-library

ESP32 Send SMS using Twilio – Code

Sending code using Twilio using the twilio-esp32-client library is very straightforward. First, you need to create a Twilio instance, and then you just need to call the send_message() method and pass as arguments your Twilio account details, sender and recipient numbers, and the message body. The following code is an example from the library’s examples folder.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/send-sms-esp32-twilio/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

// Library example: https://github.com/ademuri/twilio-esp32-client

#include "twilio.hpp"

// Set these - but DON'T push them to GitHub!
static const char *ssid = "REPLACE_WITH_YOUR_SSID";
static const char *password = "REPLACE_WITH_YOUR_PASSWORD";

// Values from Twilio (find them on the dashboard)
static const char *account_sid = "REPLACE_WITH_YOUR_ACCOUNT_SID";
static const char *auth_token = "REPLACE_WITH_YOUR_ACCOUNT_AUTH_TOKEN";
// Phone number should start with "+<countrycode>"
static const char *from_number = "REPLACE_WITH_TWILIO_NUMBER";

// You choose!
// Phone number should start with "+<countrycode>"
static const char *to_number = "REPLACE_WITH_RECIPIENT_NUMBER";
static const char *message = "Hello from my ESP32 (via twilio)";

Twilio *twilio;

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to WiFi network ;");
  Serial.print(ssid);
  Serial.println("'...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting...");
    delay(500);
  }
  Serial.println("Connected!");

  twilio = new Twilio(account_sid, auth_token);

  delay(1000);
  String response;
  bool success = twilio->send_message(to_number, from_number, message, response);
  if (success) {
    Serial.println("Sent message successfully!");
  } else {
    Serial.println(response);
  }
}

void loop() {
  
}

How the Code Works

Start by including the twilio-esp32-client library.

#include "twilio.hpp"

Insert your network credentials on the following lines:

static const char *ssid = "REPLACE_WITH_YOUR_SSID";
static const char *password = "REPLACE_WITH_YOUR_PASSWORD";

Insert your Twilio account details: the , and the .

static const char *account_sid = "REPLACE_WITH_YOUR_ACCOUNT_SSID";
static const char *auth_token = "REPLACE_WITH_YOUR_ACCOUNT_AUTH_TOKEN";
// Phone number should start with "+<countrycode>"
static const char *from_number = "REPLACE_WITH_TWILIO_PHONE_NUMBER";

Insert the recipient number and the message.

// Phone number should start with "+<countrycode>"
static const char *to_number = "INSERT_RECIPIENT_NUMBER";
static const char *message = "Hello from my ESP32 (via twilio)";

If you’re using a free trial account, the recipient number must be on the .

Create a Twilio pointer variable called twilio.

Twilio *twilio;

In the setup(), initialize the Serial Monitor and connect the ESP32 to your local network so that it can get access to the internet and make the HTTP requests to send SMS.

  Serial.begin(115200);
  Serial.print("Connecting to WiFi network ;");
  Serial.print(ssid);
  Serial.println("'...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting...");
    delay(500);
  }
  Serial.println("Connected!");

The following line instantiates a new Twilio instance with the account details:

 twilio = new Twilio(account_sid, auth_token);

Then call the send_message() function that accepts as arguments the recipient number, the sender number, the message, and a variable to hold the response. This function makes an HTTP request in the background with all the necessary parameters to Twilio API to send SMS.

  String response;
  bool success = twilio->send_message(to_number, from_number, message, response);
  if (success) {
    Serial.println("Sent message successfully!");
  } else {
    Serial.println(response);
  }
}

This function will return true if the message is successfully sent or the response of the HTTP request in case it fails.

Sending an SMS is not free and it will be deducted from the credit on your Twilio account. So, we’re just sending one SMS on the setup() when the board starts. The idea is to apply this sample code to your own project.

The loop() is empty.

void loop() {
}

Demonstration

After inserting all the required details, you can upload the code to your ESP32 board. Select an ESP32 board in Tools > Board and select the COM port in Tools > Port. Then, click on the Upload button.

Arduino IDE Upload Button

After uploading, open the Serial Monitor at a baud rate of 115200. Press the ESP32 RST button to restart the board. If everything goes as expected, you should receive a similar message as shown below.

ESP32 send SMS via twilio

After a few seconds, you should receive an SMS from Twilio on your phone.

Twilio SMS received (from ESP32)

Wrapping Up

In this tutorial, you learned how to send SMS with the ESP32 using Twilio programmable messaging API. The advantage of using this method is that you don’t need to have a modem or a physical SIM card to send SMS with your board.

However, you need to buy a Twilio phone number, and you’ll need to pay a monthly subscription for the card. You’ll also need to pay for each SMS you send. You can sign up for a free trial account that gives you credit to experiment with Twilio in your projects—so, you can try their services for a while for free. If you feel their service is the right for your projects, then you can update your account later on.

You can also send SMS with the ESP32 using other methods—using modems like the SIM800L, SIM7000G, and others. We have tutorials showing how to send SMS with the ESP32 using the SIM800L and SIM7000G modules:

We hope you find this tutorial useful.

Thanks for reading.