{"id":120879,"date":"2022-12-22T12:39:49","date_gmt":"2022-12-22T12:39:49","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=120879"},"modified":"2022-12-22T12:39:51","modified_gmt":"2022-12-22T12:39:51","slug":"send-sms-esp32-twilio","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/send-sms-esp32-twilio\/","title":{"rendered":"Send SMS with the ESP32 (Twilio)"},"content":{"rendered":"\n<p>This guide shows how to send SMS with the ESP32 using an online service called Twilio. With this service, you can send SMS with the ESP32 without needing a GSM module or a dedicated physical SIM card. Using Twilio is not free but you can sign up for a free trial for testing purposes. We&#8217;ll use a free trial account throughout the tutorial.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Send SMS with the ESP32 Twilio Arduino IDE Core\" class=\"wp-image-120951\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>You can send SMS with the ESP32 without the need to rely on third-party services if you have a GSM module and a dedicated SIM card. We have tutorials showing how to send SMS with the ESP32 using the SIM800L and SIM7000G modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-sim800l-send-text-messages-sms\/\">ESP32 SIM800L: Send Text Messages (SMS Alert) with Sensor Readings<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/lilygo-t-sim7000g-esp32-lte-gprs-gps\/\">LILYGO T-SIM7000G ESP32: Connect to the Internet, Send SMS, and Get GPS Data<\/a><\/li><\/ul>\n\n\n\n<p>If you&#8217;re looking for different types of notifications and alerts with the ESP32, you can check the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\">How to send <strong>emails <\/strong>with the ESP32?<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-messages-whatsapp\/\">Send <strong>WhatsApp <\/strong>messages with the ESP32<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/telegram-esp32-motion-detection-arduino\/\"><strong>Telegram<\/strong>: ESP32 Motion Detection with Notifications (Arduino IDE)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Twilio<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"199\" height=\"60\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-logo.png?resize=199%2C60&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio logo\" class=\"wp-image-120949\"\/><\/figure><\/div>\n\n\n<p>Twilio provides programmable communication tools for making and receiving phone calls, sending and receiving text messages, and performing other communication functions using its web service APIs. Throughout this tutorial, we&#8217;ll use their programmable messaging services.<\/p>\n\n\n\n<p>Twilio is a paid service, but you can sign-up for a free account for testing purposes. Then, if their services are useful for your specific project, you can always upgrade your account later on. We&#8217;ll use a free trial account throughout this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Free Trial Account<\/h2>\n\n\n\n<p>Go to <a href=\"https:\/\/www.twilio.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.twilio.com\/<\/a> and click on <strong>Sign up and start building<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"496\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-home-page.png?resize=750%2C496&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Creating a Free Trial Account Twilio account\" class=\"wp-image-120880\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-home-page.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-home-page.png?resize=300%2C198&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Enter your details and get started with a free trial.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"784\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-start-free-trial.png?resize=750%2C784&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio account create new\" class=\"wp-image-120881\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-start-free-trial.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-start-free-trial.png?resize=287%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 287w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Set Up the Free Twilio Trial Account<\/h2>\n\n\n\n<p>There are some steps you need to follow to set up your free Twilio trial account. We&#8217;ll describe the steps you need to follow in the present tutorial. You can also check <a href=\"https:\/\/www.twilio.com\/docs\/usage\/tutorials\/how-to-use-your-free-trial-account\" target=\"_blank\" rel=\"noopener\">Twilio&#8217;s official instructions here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Verify your personal phone number<\/h3>\n\n\n\n<p>When you sign up for your free trial account, you verified your personal phone number. It should be on the <a href=\"https:\/\/console.twilio.com\/us1\/develop\/phone-numbers\/manage\/verified\" target=\"_blank\" rel=\"noopener\">list of verified phone numbers<\/a> on your dashboard: <strong>Phone Numbers<\/strong> &gt; <strong>Manage <\/strong>&gt; <strong>Verified Caller IDs<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"563\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-personal-number.jpg?resize=750%2C563&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio verified personal number\" class=\"wp-image-120882\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-personal-number.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-personal-number.jpg?resize=300%2C225&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"verify-recipient-numbers\">Verify other recipient numbers<\/h3>\n\n\n\n<p>When using the free trial account, you must verify all numbers that you&#8217;ll want to send an SMS. When verifying the numbers, you&#8217;ll get an SMS with a verification code that you need to insert. So, you need to have physical access to those numbers. This is not needed on the paid account.<\/p>\n\n\n\n<p>On that previous menu (<strong>Phone Numbers<\/strong> &gt; <strong>Manage <\/strong>&gt; <strong>Verified Caller IDs<\/strong>), click on <strong>Add new Called ID<\/strong> to add a new verified number. You&#8217;ll receive an SMS with a verification code on that number.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"531\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-caller-ids.png?resize=750%2C531&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio add verified called ids\" class=\"wp-image-120883\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-caller-ids.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-verified-caller-ids.png?resize=300%2C212&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"twilio-phone-number\">Get a Twilio Phone Number<\/h2>\n\n\n\n<p>To send messages using Twilio, you&#8217;ll need to purchase a Twilio phone number. At the time of writing this tutorial, when I signed up for the free account, I got a $15.50 credit, which is enough to get a phone number and test sending some SMS.<\/p>\n\n\n\n<p>On your dashboard, on the left sidebar, go to <strong>Phone Numbers<\/strong> &gt; <strong>Manage <\/strong>&gt; <strong>Buy a number<\/strong>. You&#8217;ll see a list of available numbers. Make sure you select SMS on the number capabilities. You can also select the country. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"458\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/buy-twilio-number.png?resize=750%2C458&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Buy twilio number\" class=\"wp-image-120885\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/buy-twilio-number.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/buy-twilio-number.png?resize=300%2C183&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p><strong>Note: <\/strong> I tried purchasing a number from my country (Portugal), but I needed to fill out a regulatory bundle and submit some documents for review. So I ended up buying a card from the US instead, which didn&#8217;t require any of that and was ten times cheaper. This works for testing purposes, once you decide to move for a more permanent solution, double-check if that number is the most suitable for your case scenario.<\/p>\n\n\n\n<p>Once you&#8217;ve chosen a phone number click on the <strong>Buy<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"636\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-buy-us-phone-number.png?resize=608%2C636&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio buy US phone number\" class=\"wp-image-120886\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-buy-us-phone-number.png?w=608&amp;quality=100&amp;strip=all&amp;ssl=1 608w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-buy-us-phone-number.png?resize=287%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 287w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Programmable Messaging &#8211; Get Set Up<\/h2>\n\n\n\n<p>Now, you have everything set up to start creating a programmable messaging service. On your dashboard, on the left sidebar click on <strong>Messaging <\/strong>&gt; <strong>Try it out<\/strong> &gt; <strong>Get Set Up<\/strong>. Then, click on <strong>Start set up<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"492\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-programmable-messaging.png?resize=750%2C492&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio programmable messaging\" class=\"wp-image-120887\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-programmable-messaging.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-programmable-messaging.png?resize=300%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Give a name to the Messaging Service, for example, <em>ESP32 Alerts<\/em> and click on <strong>Create Messaging Service<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"268\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service-1.png?resize=456%2C268&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio Create Messaging Service\" class=\"wp-image-120888\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service-1.png?w=456&amp;quality=100&amp;strip=all&amp;ssl=1 456w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service-1.png?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/figure><\/div>\n\n\n<p>Then, select the Twilio phone number you created previously and click on <strong>Add this number<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"248\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/create-messaging-service-2.png?resize=456%2C248&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio add phone number\" class=\"wp-image-120889\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/create-messaging-service-2.png?w=456&amp;quality=100&amp;strip=all&amp;ssl=1 456w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/create-messaging-service-2.png?resize=300%2C163&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/figure><\/div>\n\n\n<p>After that, the programmable messaging service will be all set up. You&#8217;ll get access to your account information: account SID and Auth token. You&#8217;ll need them later in the ESP32 code.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\" id=\"acount-details\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"522\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service.png?resize=750%2C522&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio create messaging service\" class=\"wp-image-120898\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-create-messaging-service.png?resize=300%2C209&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You can test if everything is working as expected by clicking on <strong>Try SMS<\/strong>. You&#8217;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\u2014<a href=\"#verify-recipient-numbers\">see this previous section<\/a>), select the messaging service you created previously, and write some body text and click on <strong>Send test SMS<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"620\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-try-test-sms.png?resize=750%2C620&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio try test SMS\" class=\"wp-image-120900\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-try-test-sms.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-try-test-sms.png?resize=300%2C248&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>After a few seconds, you should receive the test SMS on the selected number.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"87\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/SMS-from-twilio-trial.png?resize=300%2C87&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"SMS from Twilio trial account\" class=\"wp-image-120901\"\/><\/figure><\/div>\n\n\n<p>All SMS sent from a Twilio trial account will have the text: &#8220;Sent from your Twilio trial account&#8221;. This text doesn&#8217;t show up on premium accounts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32: Send SMS using Twilio<\/h2>\n\n\n\n<p>Sending SMS using Twilio is very straightforward thanks to its API. You can <a href=\"https:\/\/www.twilio.com\/docs\/sms\/api\" target=\"_blank\" rel=\"noopener\">read the SMS API documentation<\/a>. You simply make HTTP requests with the ESP32 with the right parameters (accordingly to Twilio&#8217;s API) to send SMS. You can check <a href=\"https:\/\/randomnerdtutorials.com\/esp32-http-get-post-arduino\/\">this tutorial about HTTP requests with the ESP32<\/a>.<\/p>\n\n\n\n<p>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&#8217;ll use a library called <a href=\"https:\/\/github.com\/ademuri\/twilio-esp32-client\" target=\"_blank\" rel=\"noopener\">twilio-esp32-client<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the twilio-esp32-client Library<\/h3>\n\n\n\n<p>The twilio-esp32-client library can be installed through the Arduino IDE Library Manager. Go to <strong>Sketch <\/strong>&gt; <strong>Include Library <\/strong>&gt; <strong>Manage Libraries<\/strong>. Search for <strong>twilio-esp32-client<\/strong> and install the library.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-esp32-client-library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"twilio-esp32-client-library\" class=\"wp-image-120903\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-esp32-client-library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-esp32-client-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/twilio-esp32-client-library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">ESP32 Send SMS using Twilio &#8211; Code<\/h3>\n\n\n\n<p>Sending code using Twilio using the <strong>twilio-esp32-client<\/strong> library is very straightforward. First, you need to create a <span class=\"rnthl rntliteral\">Twilio<\/span> instance, and then you just need to call the <span class=\"rnthl rntliteral\">send_message()<\/span> 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&#8217;s <em>examples <\/em>folder.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/send-sms-esp32-twilio\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \r\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r\n*********\/\r\n\r\n\/\/ Library example: https:\/\/github.com\/ademuri\/twilio-esp32-client\r\n\r\n#include &quot;twilio.hpp&quot;\r\n\r\n\/\/ Set these - but DON'T push them to GitHub!\r\nstatic const char *ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\r\nstatic const char *password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\r\n\r\n\/\/ Values from Twilio (find them on the dashboard)\r\nstatic const char *account_sid = &quot;REPLACE_WITH_YOUR_ACCOUNT_SID&quot;;\r\nstatic const char *auth_token = &quot;REPLACE_WITH_YOUR_ACCOUNT_AUTH_TOKEN&quot;;\r\n\/\/ Phone number should start with &quot;+&lt;countrycode&gt;&quot;\r\nstatic const char *from_number = &quot;REPLACE_WITH_TWILIO_NUMBER&quot;;\r\n\r\n\/\/ You choose!\r\n\/\/ Phone number should start with &quot;+&lt;countrycode&gt;&quot;\r\nstatic const char *to_number = &quot;REPLACE_WITH_RECIPIENT_NUMBER&quot;;\r\nstatic const char *message = &quot;Hello from my ESP32 (via twilio)&quot;;\r\n\r\nTwilio *twilio;\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  Serial.print(&quot;Connecting to WiFi network ;&quot;);\r\n  Serial.print(ssid);\r\n  Serial.println(&quot;'...&quot;);\r\n  WiFi.begin(ssid, password);\r\n\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    Serial.println(&quot;Connecting...&quot;);\r\n    delay(500);\r\n  }\r\n  Serial.println(&quot;Connected!&quot;);\r\n\r\n  twilio = new Twilio(account_sid, auth_token);\r\n\r\n  delay(1000);\r\n  String response;\r\n  bool success = twilio-&gt;send_message(to_number, from_number, message, response);\r\n  if (success) {\r\n    Serial.println(&quot;Sent message successfully!&quot;);\r\n  } else {\r\n    Serial.println(response);\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n  \r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Send_SMS_Twilio.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Start by including the <span class=\"rnthl rntliteral\">twilio-esp32-client<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"twilio.hpp\"<\/code><\/pre>\n\n\n\n<p>Insert your network credentials on the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>static const char *ssid = \"REPLACE_WITH_YOUR_SSID\";\nstatic const char *password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>Insert your Twilio account details: the <a href=\"#acount-details\">account SID and token<\/a>, and the <a href=\"#twilio-phone-number\">Twilio phone number<\/a>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>static const char *account_sid = \"REPLACE_WITH_YOUR_ACCOUNT_SSID\";\nstatic const char *auth_token = \"REPLACE_WITH_YOUR_ACCOUNT_AUTH_TOKEN\";\n\/\/ Phone number should start with \"+&lt;countrycode&gt;\"\nstatic const char *from_number = \"REPLACE_WITH_TWILIO_PHONE_NUMBER\";<\/code><\/pre>\n\n\n\n<p>Insert the recipient number and the message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Phone number should start with \"+&lt;countrycode&gt;\"\nstatic const char *to_number = \"INSERT_RECIPIENT_NUMBER\";\nstatic const char *message = \"Hello from my ESP32 (via twilio)\";<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using a free trial account, the recipient number must be on the <a href=\"#verify-recipient-numbers\">list of the Verified caller IDs<\/a>.<\/p>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">Twilio<\/span> pointer variable called <span class=\"rnthl rntliteral\">twilio<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Twilio *twilio;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, 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.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  Serial.begin(115200);\n  Serial.print(\"Connecting to WiFi network ;\");\n  Serial.print(ssid);\n  Serial.println(\"'...\");\n  WiFi.begin(ssid, password);\n\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.println(\"Connecting...\");\n    delay(500);\n  }\n  Serial.println(\"Connected!\");<\/code><\/pre>\n\n\n\n<p>The following line instantiates a new <span class=\"rnthl rntliteral\">Twilio<\/span> instance with the account details:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code> twilio = new Twilio(account_sid, auth_token);<\/code><\/pre>\n\n\n\n<p>Then call the <span class=\"rnthl rntliteral\">send_message()<\/span> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  String response;\n  bool success = twilio-&gt;send_message(to_number, from_number, message, response);\n  if (success) {\n    Serial.println(\"Sent message successfully!\");\n  } else {\n    Serial.println(response);\n  }\n}<\/code><\/pre>\n\n\n\n<p>This function will return <span class=\"rnthl rntliteral\">true<\/span> if the message is successfully sent or the response of the HTTP request in case it fails.<\/p>\n\n\n\n<p>Sending an SMS is not free and it will be deducted from the credit on your Twilio account. So, we&#8217;re just sending one SMS on the <span class=\"rnthl rntliteral\">setup()<\/span> when the board starts. The idea is to apply this sample code to your own project.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">loop()<\/span> is empty.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After inserting all the required details, you can upload the code to your ESP32 board. Select an ESP32 board in <strong>Tools<\/strong> &gt; <strong>Board <\/strong>and select the COM port in <strong>Tools <\/strong>&gt; <strong>Port<\/strong>. Then, click on the Upload button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-2-0-upload-button.png?resize=30%2C30&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE Upload Button\" class=\"wp-image-103678\"\/><\/figure><\/div>\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"415\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-send-sms-via-twilio-Serial-Monitor-demonstration.png?resize=601%2C415&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 send SMS via twilio\" class=\"wp-image-120906\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-send-sms-via-twilio-Serial-Monitor-demonstration.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-send-sms-via-twilio-Serial-Monitor-demonstration.png?resize=300%2C207&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>After a few seconds, you should receive an SMS from Twilio on your phone.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"450\" height=\"152\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-SMS-ESP32.png?resize=450%2C152&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Twilio SMS received (from ESP32)\" class=\"wp-image-120907\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-SMS-ESP32.png?w=450&amp;quality=100&amp;strip=all&amp;ssl=1 450w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/Twilio-SMS-ESP32.png?resize=300%2C101&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>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&#8217;t need to have a modem or a physical SIM card to send SMS with your board.<\/p>\n\n\n\n<p>However, you need to buy a Twilio phone number, and you&#8217;ll need to pay a monthly subscription for the card. You&#8217;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\u2014so, 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.<\/p>\n\n\n\n<p>You can also send SMS with the ESP32 using other methods\u2014using modems like the SIM800L, SIM7000G, and others. We have tutorials showing how to send SMS with the ESP32 using the SIM800L and SIM7000G modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-sim800l-send-text-messages-sms\/\">ESP32 SIM800L: Send Text Messages (SMS Alert) with Sensor Readings<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/lilygo-t-sim7000g-esp32-lte-gprs-gps\/\">LILYGO T-SIM7000G ESP32: Connect to the Internet, Send SMS, and Get GPS Data<\/a><\/li><\/ul>\n\n\n\n<p>We hope you find this tutorial useful.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to send SMS with the ESP32 using an online service called Twilio. With this service, you can send SMS with the ESP32 without needing a GSM &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Send SMS with the ESP32 (Twilio)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/send-sms-esp32-twilio\/#more-120879\" aria-label=\"Read more about Send SMS with the ESP32 (Twilio)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":120951,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-120879","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/11\/ESP32-Send-SMS-Twilio.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/120879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=120879"}],"version-history":[{"count":13,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/120879\/revisions"}],"predecessor-version":[{"id":123937,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/120879\/revisions\/123937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/120951"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=120879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=120879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=120879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}