Send SMS Message – Arduino Tutorial

3,639 views

Introduction

Sending and receiving SMS text messages using an Arduino UNO microcontroller with Arduino Ethernet Shield for Arduino and IFTTT webhooks applet is an innovative solution for remote communication using the power of microcontrollers and web technology. This method allows Arduino to send and receive SMS messages over the internet, making it possible to create complex and automated systems that can be controlled from anywhere in the world.

IFTTT Webhooks is a service offered by the popular web automation platform IFTTT (If This Then That). It allows users to create custom webhooks, which are essentially HTTP-based APIs (Application Programming Interfaces) that can be used to trigger events or actions within other applications or services. By setting up webhooks, users can automate tasks such as sending notifications, controlling smart home devices, or updating social media profiles.

Hardware Components

You will require the following hardware to Send SMS Messages with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Arduino Ethernet Shield 21
4.Ethernet Cable1
5.Power Adapter for Arduino9V1

Send SMS Message with Arduino

Step 1: Setting up the hardware

Connect the Arduino Ethernet Shield to your Arduino UNO microcontroller, following the manufacturer’s instructions. Connect the GSM module to the Ethernet Shield using the hardware serial port on the Shield. The connections are as follows:

  • TX of GSM module to RX of Ethernet Shield (pin 0)
  • RX of GSM module to TX of Ethernet Shield (pin 1)
  • GND of GSM module to GND of Ethernet Shield

Step 2: Setting up the IFTTT applet

Create an IFTTT account and set up a new applet to trigger when you receive an SMS message. In the “Then That” action, select the “Webhooks” service and set up a webhook to send a request to a URL of your choosing.

Step 3: Sending SMS messages

Here is an example of how you can send SMS messages:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // Set up the serial port for communication with the GSM module

void setup() {
  Serial.begin(9600); // Set up the serial port for debugging
  mySerial.begin(9600); // Set up the serial port for communication with the GSM module

  mySerial.println("AT+CMGF=1"); // Set the GSM module to text mode
  delay(1000);
}

void loop() {
  String message = "Hello, world!"; // The message to be sent

  mySerial.println("AT+CMGS=\"+1234567890\""); // Replace with the recipient's phone number
  delay(1000);

  mySerial.print(message);
  mySerial.write(26); // Send the Ctrl+Z character to terminate the message
  delay(5000); // Wait for the message to be sent

  Serial.println("Message sent: " + message);
}

In this example, the GSM module is set to text mode using the “AT+CMGF=1” command. The phone number of the recipient is specified in the “AT+CMGS” command. The message is then sent using the print() function and the Ctrl+Z character. The status of the message is printed on the serial monitor using the Serial.println() function.

Step 4: Receiving SMS messages

Here is an example of how you can receive SMS messages:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // Set up the serial port for communication with the GSM module

void setup() {
  Serial.begin(9600); // Set up the serial port for debugging
  mySerial.begin(9600); // Set up the serial port for communication with the GSM module

  mySerial.println("AT+CMGF=1"); // Set the GSM module to text mode
  delay(1000);

  mySerial.println("AT+CNMI=2,2,0,0,0"); // Enable SMS notifications
  delay(1000);
}

void loop() {
  while (mySerial.available()) {
    String message = mySerial.readString();
    Serial.println("Received message: " + message);
  }
}

In this example, the GSM module is set to text mode using the “AT+CMGF=1” command, and SMS notifications are enabled using the “AT+CNMI=2,2,0,0,0” command. In the loop, the mySerial.available() function is used to check if there are any new messages. If there are new messages available, they are read using the mySerial.readString() function and printed to the serial monitor using the Serial.println() function.

Step 5: Setting up the IFTTT webhook

To trigger the IFTTT applet when an SMS message is received, you need to send a webhook request to the URL specified in the applet. Here is an example of how you can do this:

#include <SoftwareSerial.h>
#include <Ethernet.h>

SoftwareSerial mySerial(2, 3); // Set up the serial port for communication with the GSM module
EthernetClient client; // Set up the Ethernet client

void setup() {
  Serial.begin(9600); // Set up the serial port for debugging
  mySerial.begin(9600); // Set up the serial port for communication with the GSM module

  mySerial.println("AT+CMGF=1"); // Set the GSM module to text mode
  delay(1000);

  mySerial.println("AT+CNMI=2,2,0,0,0"); // Enable SMS notifications
  delay(1000);
}

void loop() {
  while (mySerial.available()) {
    String message = mySerial.readString();
    Serial.println("Received message: " + message);

    // Send a webhook request to trigger the IFTTT applet
    if (client.connect("maker.ifttt.com", 80)) {
      client.print("POST /trigger/{EVENT_NAME}/with/key/{API_KEY} HTTP/1.1\r\n");
      client.print("Host: maker.ifttt.com\r\n");
      client.print("Content-Type: application/json\r\n");
      client.print("Content-Length: 0\r\n\r\n");
      client.stop();
    }
  }
}

In this example, the Ethernet.h library is included to set up the Ethernet client. The webhook request is sent to the URL specified in the IFTTT applet using the client.connect() and client.print() functions. Replace {EVENT_NAME} with the name of the event in the IFTTT applet and {API_KEY} with your IFTTT API key.

Step 6: Posting status on the serial monitor

To post the status of the webhook request on the serial monitor, you can use the client.connected() function to check if the client is connected and the Serial.println() function to print the status. Here is an example:

if (client.connect("maker.ifttt.com", 80)) {
  client.print("POST /trigger/{EVENT_NAME}/with/key/{API_KEY} HTTP/1.1\r\n");
  client.print("Host: maker.ifttt.com\r\n");
  client.print("Content-Type: application/json\r\n");
  client.print("Content-Length: 0\r\n\r\n");

  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }

  client.stop();
}

In this example, the client.connected() function is used to check if the client is connected. If it is, the status of the webhook request is printed to the serial monitor using the Serial.print() function. Once the request is complete, the client.stop() function is called to close the connection.

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Working Explanation

Sending SMS

In the setup() function, the code initializes the hardware and software serial ports at a baud rate of 9600. It then sends the “AT+CMGF=1” command to the GSM module to set it to text mode.

In the loop() function, the code sets the message variable to the desired text message. It then sends the “AT+CMGS” command followed by the recipient’s phone number to the GSM module using the mySerial.println() function.

Receiving SMS

In the setup() function, the code initializes the hardware and software serial ports at a baud rate of 9600. It then sends the “AT+CMGF=1” command to the GSM module to set it to text mode and the “AT+CNMI=2,2,0,0,0” command to enable SMS notifications.

In the loop() function, the code uses a while loop to continuously check if there are any characters available on the serial port. If there are, it reads the characters as a string using the mySerial.readString() function and assigns them to the message variable. It then prints the received message to the debugging serial port using the Serial.println() function.

Applications

  • Home Automation
  • Security Systems
  • Agriculture
  • Healthcare
  • Logistics

Conclusion.

We hope you have found this SMS Message Arduino Tutorial very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.