Arduino - Send SMS Message

In this tutorial we are going to learn how to use Arduino to send SMS message.

There are serveral ways to send SMS message from Arduino:

This tutorial uses IFTTT to send SMS message. The code is available for Arduino Ethernet Shield. You can modify it to make it works with other shield/board

※ NOTE THAT:

Sending SMS message via IFTTT only works on Android phone and The Android phone must be connected to Internet.

The tutorial provides the Arduino code for two cases:

Prerequisite

If you do not know about IFTTT and related concepts, We highly recommend you reading the following tutorials:

Wiring Diagram

  • Arduino - Ethernet Shield: we just need to stack the shield on Arduino Uno/Mega
Arduino - Ethernet Shield

How It Works

We need to:

  • Install IFTTT app from Google Play Store on Android phone. IFTTT service which runs on the background is also installed.
  • Create an IFTTT Applet on the IFTTT website, which connects Webhooks to SMS service.

When we create the Applet:

  • We can specify the phone number we want to send the SMS message to.
  • We can write the content of the SMS message to be sent.
  • We can include data from Arduino to the content of SMS message.

※ NOTE THAT:

The content of the SMS message is specified on the Applet, not in the Arduino code.

Workflow

  • Arduino makes an HTTP request to Webhooks of IFTTT,
  • Webhooks triggers SMS service on IFTTT
  • SMS service on IFTTT sends a command to IFTTT service on your Android phone.
  • IFTTT service on your Android phone sends an SMS from your Android device to any phone number you specify.

How To Use IFTTT with Arduino

  • Install IFTTT app from Google Play Store on Android phone
  • Create an IFTTT account and Login to IFTTT.
  • Create an Applet of Webhooks and SMS services. Please write down the event name we uses
  • After creating the applet, get Webhooks key from IFTTT.
  • Copy below Arduino code and paste in Arduino IDE.
  • Replace event name and Webhooks key in the Arduino code
  • Upload the code to Arduino

※ NOTE THAT:

  • The event name is given by you. You can give any name.
  • Webhooks key is created by IFTTT. You can not change it.

See the video tutorial at the end of this tutorial.

Arduino code

Arduino IFTTT code for Arduino Uno R4 WiFi

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-send-sms-message */ #include <WiFiS3.h> const char ssid[] = "YOUR_WIFI_SSID"; // change your network SSID (name) const char pass[] = "YOUR_WIFI_PASSWORD"; // change your network password (use for WPA, or use as key for WEP) WiFiClient client; int status = WL_IDLE_STATUS; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-sms/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key String queryString = "?value1=27"; // the supposed temperature value is 27°C void setup() { Serial.begin(9600); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true) ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // attempt to connect to WiFi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } // print your board's IP address: Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else { // if not connected: Serial.println("connection failed"); } } void loop() { }

Arduino IFTTT code for Arduino Uno/Mega with Ethernet Shield

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-send-sms-message */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-sms/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key String queryString = "?value1=27"; // the supposed temperature value is 27°C void setup() { Serial.begin(9600); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address using DHCP"); while (true); } // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else {// if not connected: Serial.println("connection failed"); } } void loop() { }

※ NOTE THAT:

This tutorial used the dynamic IP address (via DHCP). If you want to use the static IP address:

Result

In the two above codes, If succeeded,

  • The result in Serial Monitor looks like below:
COM6
Send
Connected to server HTTP/1.1 200 OK Date: Thu, 14 May 2020 07:06:40 GMT Content-Type: text/html; charset=utf-8 Content-Length: 44 Connection: close X-Top-SecreTTT: VG9vIGVhc3k/IElmIHlvdSBjYW4gcmVhZCB0a== ETag: W/"2c-4f52ca3d" Server: web_server Congratulations! You've fired the test event disconnected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • You receives an SMS message

https://arduinogetstarted.com/images/tutorial/arduino-sms-message-receive.jpg

Video Tutorial

How to Include Sensor Data to SMS Content

To include data from Arduino to SMS content, we need to do:

  • In Arduino: Include data into HTTP request (in the query string).
  • In Applet: modify the setting to extract data from the HTTP request.

You can learn what is query string in this tutorial

There are 5 variables we can use in the setting of the IFTTT Applet.

{{EventName}} Extracted from HTTP request
{{OccurredAt}} System time when event occurs
{{Value1}} Extracted from the variable value1 of query string in HTTP request
{{Value2}} Extracted from the variable value2 of query string in HTTP request
{{Value3}} Extracted from the variable value3 of query string in HTTP request

For example:

  • In Arduino: Change String queryString = "" to String queryString = "?value1=26&value2=70" in above codes.
  • In Applet: Modify the content of SMS message in the setting of the IFTTT Applet as follow:

Hi guy,


This is Arduino Uno, I would like to inform you that:

  • The outside temperature is {{Value1}} °C
  • The outside humidity is {{Value2}} %


All the above value is measured at {{OccurredAt}}


Good luck and stay healthy!

In the above message, "
" make new line when displaying.

The SMS message will be like below:

https://arduinogetstarted.com/images/tutorial/arduino-sms-temperature-humidity.jpg

You can read the temperature and humidity from sensor and put in the query string. See how to include data from sensor to query string

Challenge Yourself

Based on the above code, make the following project:

  • Send an SMS message when the button is pressed
  • Send an SMS message when the door is open
  • Send an SMS message when the temperature is too hot ...

WARNING

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

The Best Arduino Starter Kit

※ OUR MESSAGES