Introduction: How to Send SMS Text Messages From Your Arduino ESP Project

About: There's always a better way, just keep recycling those brainwaves!

This instructable demonstrates how to send SMS text messages from your arduino project using an ESP8266 device and a WiFi connection.

Why use SMS?
* SMS messages are much more faster and reliable than app notification messages.
* SMS messages can also be received where mobile data connections are not available
* No 3rd party application needs to be installed on a smartphone.
* SMS is essential for message critical applications.

This project simply sends a SMS text message on pressing a button wired up to the ESP device.

For your project this could be triggered on some other trigger or event status

Step 1: Parts

For this demo, you will need:
* ESP8266 device. I chose the ESP-01 package, but any other ESP package/device should work too.
* 3.3v supply
* WiFi connection
* Kapow SMS account
* Arduino IDE
* ESP8266 Programming module.

Step 2: SMS Provider

In this project, SMS text messages are sent via an SMS gateway on the internet. For this you will need to subscribe to an SMS provider. Subscribers are normally charged on a per text message basis.

My chosen SMS service provider is KAPOW. I found that most SMS providers in the UK will only deal with Limited Companies, and not individuals. However, KAPOW does, and provides a reliable and cost effective service.

To open a Kapow SMS account, visit the link below.

www.kapow.co.uk

Step 3: Wiring It Up

Wiring diagram for this project is above

Step 4: The Code

// ESP8266 Demo Project to Send SMS via KAPOW (www.kapow.co.uk)
// <a href="https://www.instructables.com/id/How-to-Send-SMS-Text-Messages-From-Your-Arduino-ES/"> https://www.instructables.com/id/How-to-Send-SMS-...</a>

#include <Arduino.h>
#include <ESP8266WiFi.h>

char _sKapow_Host[] ="kapow.co.uk";
int  _iKapow_Port =80;

/* <<<< CHANGE THE DETAILS BELOW */

// Your WiFi details:
char _sWiFi_SSID[]      ="YourWifiPoint";   // <--- CHANGE !!!
char _sWiFi_Password[]  ="YourWifFiPassword"; // <--- CHANGE !!!

// Your Kapow User Account Details:
char _sKapow_User[]     ="YourKapowAccount";  // <--- CHANGE !!!
char _sKapow_Password[] ="YourKapowPassword";  // <--- CHANGE !!!
char _sKapow_Mobile[]   ="YourMobile";  // <--- CHANGE !!!

/* >>>> CHANGE THE DETAILS ABOVE */

// button is connected to GP0 Pin
const int gp0_Pin = 0;
const int iMaxAttempts = 10;
int gp0_State = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("\nDemo: Send SMS via KAPOW");

  // initialize the pushbutton pin as an input 
  // and pull it up high (internally)
  pinMode(gp0_Pin, INPUT_PULLUP);
 
  // establish Wifi connection
  WifiConnect();
}

void loop() {

  // read push button state
  gp0_State = digitalRead(gp0_Pin); 

  // is push button pressed low?
  if (gp0_State==0) {
    Serial.println("Button Pressed to Send SMS...");
    SendSmsKapow(_sKapow_Mobile, "This+is+a+Test+SMS+Message+sent+from+your+ESP+device");
  }

  Serial.println("Sleeping for 1 second");
  delay(1000);
}

void WifiConnect()
{
  Serial.print("\nConnecting to WiFi: ");
  Serial.println(_sWiFi_SSID);

  WiFi.begin(_sWiFi_SSID, _sWiFi_Password);

  while (WiFi.status() != WL_CONNECTED) {
    // retry after a second
    Serial.print(".");
    delay(1000); 
  }
  if (WiFi.status() == WL_CONNECTED)
    Serial.println("Connected to WiFi");
}

bool SendSmsKapow(char* sMobile, char* sMessage)
{
  WiFiClient clientSms;

  int iAttempts=0;
  Serial.print("Connecting to KAPOW host");
  while (!clientSms.connect(_sKapow_Host, _iKapow_Port)) {
    Serial.print(".");
    iAttempts++;
    if (iAttempts > iMaxAttempts) {
      Serial.println("\nFailed to Connect to KAPOW");
      return true;
    }
    delay(1000);
  }
  Serial.println("\nConnected to KAPOW");
  delay(1000);

  Serial.println("Sending HTTP request to KAPOW:");

  //An example GET request would be:
  //http://www.kapow.co.uk/scripts/sendsms.php?username=test&password=test&mobile=07777123456&sms=Test+message

  char sHttp[500]= "";
  strcat(sHttp, "GET /scripts/sendsms.php?username=");
  strcat(sHttp, _sKapow_User);
  strcat(sHttp, "&password=");
  strcat(sHttp, _sKapow_Password);
  strcat(sHttp, "&mobile=");
  strcat(sHttp, sMobile);
  strcat(sHttp, "&sms=");
  strcat(sHttp, sMessage);
  strcat(sHttp, "&returnid=TRUE\n\n");
    
  Serial.println(sHttp);
  clientSms.print(sHttp);

  Serial.println("Waiting for response (10 secs)...");
  delay(10 * 1000);
  
  char  sReply[100] = "";
  int   iPos = 0;

  while (clientSms.available()) {
    char c = clientSms.read();
    Serial.print(c);
    sReply[iPos] = c;
    iPos++;
    if (iPos == 99) break;
  }
  sReply[iPos] = '\0';

  // check if reply contains OK
  bool bResult = (strstr(sReply, "OK") != NULL);

  if (bResult)
    Serial.println("\nSMS: Succesfully sent");
  else
    Serial.println("\nSMS: Failed to Send");

  if (!clientSms.connected()) {
    Serial.println("Disconnecting from KAPOW");
    clientSms.stop();
  }

  return bResult;
}

Step 5: Flash the Code

There are many other detailed instructables showing how to flash program an ESP8266 device. So I will just give an overview of my setup.

In a nutshell, I use aUSB to Serial FTDI interface, and pull GP0 low on reset to start the bootloader flashing process.

I built my own stripboard programming device for convenience. It has
* a socket for an ESP-01 device
* a socket for the red FTDI interface to plug into PC host via USB.
* Buttons for RESET,GP0,GP2 TO GROUND

You can flash the ESP device using the 3.3v supplied by the FTDI interface. Ensure the FTDI jumper settings are also set to 3.3v.

Step 6: Press the Button !

You can test the project either connected to FTDI for serial logging output, or standalone (on breadboard with external power supply).

It is reconmended you usr an external 3.3v supply, as the FTDI supply current is erak. However, FTDI 3.3v supply also worked for me, as my wifi router was within arms reach.

Leave the ESP connected to the serial/usb port so you can monitor the running serial debug log using the Arduino IDE's Serial Monitor.

On powerup...

Press the button connected to GP0. This will send out the SMS message as below.

Check your phone for the received SMS text message.

Job done.