top of page

Sending SMS with ESP32 via SMS Gateway: A Step-by-Step Guide

Introduction:


The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it an ideal choice for IoT projects. In this tutorial, we will learn how to send SMS messages from an ESP32 using an SMS gateway service. This allows you to integrate SMS notifications and alerts into your projects.


Materials You'll Need:

  1. ESP32 development board (e.g., ESP-WROOM-32)

  2. USB cable for power and programming

  3. Computer with the Arduino IDE installed

Step 1: Set Up the Arduino IDE for ESP32

If you haven't already, download and install the Arduino IDE from the official Arduino website (https://www.arduino.cc/en/software).

Next, set up the Arduino IDE for ESP32 development:

  • Open the Arduino IDE.

  • Go to "File" > "Preferences."

  • In the "Additional Boards Manager URLs" field, add the following URL:arduinoCopy code https://dl.espressif.com/dl/package_esp32_index.json

  • Click "OK" to close the Preferences window.

Step 2: Install the ESP32 Board Package

  • Go to "Tools" > "Board" > "Boards Manager..."

  • In the Boards Manager, type "esp32" in the search bar.

  • Click "Install" on the "esp32" by Espressif Systems package.

Step 3: Get an SMS Gateway API Key

To send SMS messages via an SMS gateway, you'll need an API key from an SMS gateway service provider. There are several providers available, such as Twilio, Nexmo (now Vonage), or Plivo. Sign up for an account with your chosen provider and obtain an API key.

Step 4: Write the ESP32 SMS Sending Sketch

Here's an example Arduino sketch to send an SMS message using the ESP32 and an SMS gateway. In this example, we'll use Twilio as the SMS gateway provider. Replace "YourSSID", "YourPassword", "YourTwilioAccountSID", "YourTwilioAuthToken", "YourTwilioPhoneNumber", and "RecipientPhoneNumber" with your specific information.



#include <WiFi.h>

#include <HTTPClient.h>


const char* ssid = "YourSSID"; // Replace with your Wi-Fi SSID

const char* password = "YourPassword"; // Replace with your Wi-Fi password

const char* twilioSID = "YourTwilioAccountSID";

const char* twilioAuthToken = "YourTwilioAuthToken";

const char* twilioPhoneNumber = "YourTwilioPhoneNumber";

const char* recipientPhoneNumber = "RecipientPhoneNumber";


void setup() {

Serial.begin(115200);


// Connect to Wi-Fi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi...");

}

Serial.println("Connected to WiFi");


// Send SMS via Twilio

sendSMS();

}


void loop() {

// Your code here

}


void sendSMS() {

HTTPClient http;


// Prepare the Twilio API URL

String url = "https://api.twilio.com/2010-04-01/Accounts/";

url += twilioSID;

url += "/Messages.json";


// Prepare the message data

String data = "To=";

data += recipientPhoneNumber;

data += "&From=";

data += twilioPhoneNumber;

data += "&Body=Hello%20from%20ESP32";


http.begin(url);

http.setAuthorization(twilioSID, twilioAuthToken);

http.addHeader("Content-Type", "application/x-www-form-urlencoded");


int httpCode = http.POST(data);

String payload = http.getString();


Serial.print("HTTP Code: ");

Serial.println(httpCode);

Serial.println("Response:");

Serial.println(payload);


http.end();

}



Step 5: Configure the Sketch

  • Replace "YourSSID" and "YourPassword" with your Wi-Fi network credentials.

  • Replace "YourTwilioAccountSID", "YourTwilioAuthToken", "YourTwilioPhoneNumber", and "RecipientPhoneNumber" with your Twilio API credentials and recipient's phone number.

Step 6: Upload and Run the Sketch

  • Connect your ESP32 to your computer using a USB cable.

  • Ensure you've selected the correct board and port (as described in previous steps).

  • Click the "Upload" button (right arrow) in the Arduino IDE to compile and upload the sketch to your ESP32.

Step 7: Monitor Serial Output

Open the Serial Monitor (Tools > Serial Monitor) to monitor the ESP32's serial output. You should see the HTTP response code and the result of the SMS sending attempt.

Conclusion:

You've successfully learned how to send SMS messages from an ESP32 using an SMS gateway service like Twilio. This capability allows you to integrate SMS notifications and alerts into your IoT projects, enhancing their communication capabilities and making them more versatile. Explore further by customizing the SMS content or integrating other IoT sensors and actuators.


364 views0 comments

Recent Posts

See All

Comments


bottom of page